0

I am writing a cup parser for a small language that is supposed to support classes with arrays and matrices as its fields. For example, if there is a class instance:

C c;

the fields are to be accessed with:

c.x;
c.y[];
c.z[][];

I am having trouble writing a production for this last part, because of the shift/reduce conflict I keep getting. This is my production:

Designator ::= IDENT
               |
               Designator DOT IDENT
               |
               Designator LSQUARE Expr RSQUARE
               |
               Designator LSQUARE Expr RSQUARE LSQUARE Expr RSQUARE
               ;

Warning : * Shift/Reduce conflict found in state #189 between Designator ::= Designator LSQUARE Expr RSQUARE () and Designator ::= Designator LSQUARE Expr RSQUARE () LSQUARE Expr RSQUARE under symbol LSQUARE Resolved in favor of shifting.

Can anyone help me solve this?

rds
  • 26,253
  • 19
  • 107
  • 134

1 Answers1

0

The last Designator line of your grammer is invalid. Designator LSQUARE Expr RSQUARE (two lines above) already recursively defines jagged array expressions of arbitrary many dimensions.

According to your grammar, the following would expression would be valid:

c.y[a].b

And the following would be invalid:

c.y[a].z[b]

Is that by intention?

It might be inspiring to look at the C# grammar which knows about jagged arrays.

Axel Kemper
  • 10,544
  • 2
  • 31
  • 54