0

When creating the grammar for parsing a list (something like “ITEM*”) with a LALR(1) parser, this basically can be done in two ways:

list
    : list ITEM
    |
    ;

or

list
    : ITEM list
    |
    ;

What are the pros and cons of these two possibilities? In general, can an advice be given which one to choose, or does this depend on the complete grammar?

What about the case “ITEM+”, i.e.

list
    : list ITEM
    | ITEM
    ;

and

list
    : ITEM list
    | ITEM
    ;

Do the same pros and cons apply here, or others?

3

Here is a nice explanation. In short:

For LALR(1) the opposite is true: left recursion is good, right recursion is bad.

This has to do both with distinguishing the ITEM elements from the separators, and with run-time efficiency.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.