0

to solve the dangling else problem, I used the following solution:

stmt            : stmt_matched
                | stmt_unmatched
                ;
stmt_unmatched  : IF '(' exp ')' stmt
                | IF '(' exp ')' stmt_matched ELSE stmt_unmatched
                ;
stmt_matched    : IF '(' exp ')' stmt_matched ELSE stmt_matched
                | stmt_for
                | ...
                ;

For defining the rules of grammar about the for loop, I produce a conflict shift/reduce due to the same problem:

stmt_for        : FOR '(' exp ';' exp ';' exp ')' stmt
            ;

How can I solve this problem?

splash
  • 13,037
  • 1
  • 44
  • 67

1 Answers1

0

Not all for statements are matched. Consider, for example

 if (c) for (;;) if (d) ; else ;

So it is necessary to divide for statements into for_matched and for_unmatched. (And similarly with other compound statements such as while.)

rici
  • 234,347
  • 28
  • 237
  • 341
  • So I have to add the for_matched in stmt matched and for_unmatched in stmt unmatched? Accordingly stmt_for_matched : FOR '(' exp ';' exp ';' exp ')' stmt_matched ;and stmt_for_unmatched : FOR '(' exp ';' exp ';' exp ')' stmt_unmatched ; – Grazia Micele Dec 12 '16 at 10:07