Consider the productions (shown after problem #1 has been corrected):
ArrayType:
Type
[ ]
ArrayAccess:
Name[
Expression]
PrimaryNoNewArray
[
Expression]
Now consider the partial input:
class Problem4 { Problem4() { peter[
When the parser is considering the token peter
, with one-token lookahead to
symbol [
, it cannot yet tell whether peter
will be part of a type name, as in:
peter[] team;
or part of an array access, as in:
peter[3] = 12;
Therefore, after the parser reduces peter
to the nonterminal Name, it cannot tell
with only one-token lookahead whether Name should be reduced ultimately to
Type (for an array type) or left alone (for an array access). Therefore, the productions shown above result in a grammar that is not LALR(1).
The solution is to have separate alternatives for ArrayType:
ArrayType:
PrimitiveType
[ ]
Name
[ ]
ArrayType
[ ]
This allows the parser to reduce peter
to Name and then leave it as is, delaying
the decision as to whether an array type or array access is in progress.