Left recursion in conflict grammar results

In all of Bison's grammar, I use the correct recursion, and I read that residual recursion is better because it does not need to create the entire stack first.

However, when I try to switch to left recursion on any of them, I always have a lot of conflicts, and I don’t see why.

Can someone show me a general example of where using left recursion instead of right causes a conflict (when a correct recursion does not cause a conflict). Then what needs to be done when you switch to the left to correct such a conflict. I think a fundamental example will help me more than just fixing my own grammar.

Edit:
But I suppose I should include a concrete example anyway, since I understand a little less than the full one :-) Changing the list separator command to the list of command separators resolves the conflict.

State 9 conflicts: 3 shift/reduce


Grammar

    0 $accept: input $end

    1 input: error NEWLINE
    2      | input NEWLINE
    3      | input list NEWLINE
    4      | /* empty */

    5 list: command
    6     | command separator
    7     | list separator command

    8 separator: SEMI
    9          | L_OR
   10          | L_AND

   11 command: variable_assignment
   12        | external_w_redir
   13        | external_w_redir AMP
   14        | pipeline
   15        | pipeline AMP

...

state 9

    5 list: command .
    6     | command . separator

    SEMI   shift, and go to state 18
    L_AND  shift, and go to state 19
    L_OR   shift, and go to state 20

    SEMI      [reduce using rule 5 (list)]
    L_AND     [reduce using rule 5 (list)]
    L_OR      [reduce using rule 5 (list)]
    $default  reduce using rule 5 (list)

    separator  go to state 22
+3
source share
2 answers

EDIT:

I need to cancel my original answer. Your left recursive grammar doesn't seem ambiguous, as I thought. I think I was confused by the additional rule that is used to make the final delimiter optional.

Here is a simplified version of your original, correct recursive grammar:

list: COMMAND
      | COMMAND SEPARATOR
      | COMMAND SEPARATOR list
      ;

( , , , , ) C, CS, CSC, CSCS, CSCSC, CSCSCS .. SEPARATOR- , SEPARATOR .

- , / Bison:

list: COMMAND
      | COMMAND SEPARATOR
      | list SEPARATOR COMMAND
      ;

, C, CS, CSC, CSSC, CSCSC, CSSCSC .. , - . COMMANDs SEPARATOR , COMMANDS .

, / , Bison , , , , , .

, , , :

list: separatedlist
      | separatedlist SEPARATOR
      ;

separatedlist: COMMAND
               | separatedlist SEPARATOR COMMAND
               ;

, , . , .

+4

/, , . :

list: command
    | command separator
    | list separator command

:

list: command
    | command separator
    | command separator list

, . s . > . , , LAST .

, , ,

list_no_trailer: command
               | list_no_trailer separator command

list: list_no_trailer
    | list_no_trailer separator

, "" , , , . 2 , LR (2), LR (1)

+1

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


All Articles