Shift / decrease conflict in yacc due to foreground token restrictions?

I tried to solve the seemingly simple shift / reduction conflict to no avail. Naturally, the parser works great if I just ignore the conflict, but I would feel much safer if I reorganized my rules. Here I simplified the relatively complicated grammar for a single conflict:

statement_list
  : statement_list statement 
  | 
  ;

statement
  : lvalue '=' expression
  | function
  ;

lvalue
  : IDENTIFIER
  | '(' expression ')'
  ;

expression
  : lvalue
  | function
  ;

function
  : IDENTIFIER '(' ')'
  ;

With the verbose option in yacc, I get this output file describing the state with the specified conflict:

state 2

    lvalue  ->  IDENTIFIER .   (rule 5)
    function  ->  IDENTIFIER . '(' ')'   (rule 9)

    '('  shift, and go to state 7

    '('  [reduce using rule 5 (lvalue)]
    $default reduce using rule 5 (lvalue)

Thanks for any help.

+3
source share
1 answer

The problem is that this requires a 2 token to find out when it has reached the end of the instruction. If you have form input:

ID = ID ( ID ) = ID

, (lookahead is (), , (( ), . , ( ), .

function, expression , , - =, , .

, , . , , , .

+5

Source: https://habr.com/ru/post/1785415/


All Articles