0
votes

Based on this question Where are the shift/reduce conflicts in this Bison code coming from? I'm trying to create a grammar for a syntax like this:

-10 + 3 - 2 or +4 - 2 + 1. The first integer obviously has a sign.

This is what I already have:

%token INT
...
Term: INT;
TermOps: "+" | "-";
SignedTerm: TermOps Term | Term;
reminder: /* EPS */ | TermOps TermList;
TermList: SignedTerm reminder;

I know that the rule for SignedTerm is obviously wrong because it creates ambiguity, but how do I resolve that?

1

1 Answers

1
votes

Bison generates LALR parsers, so there is no need to warp the grammar into a version parseable with an LL algorithm.

The grammar simply needs to reflect the structure of the language. In this simple case, the language specifies that a term is an integer, possibly with a sign preceding it, and an expression is grouped left-to-right. (That's my assumption, but it is the usual convention.)

That can be written simply:

term: INT
    | '+' term
    | '-' term

expr: term
    | expr '+' term
    | expr '-' term

These definitions come directly from the description.