Search code examples
programming-languagesgrammarambiguous-grammar

Resolving ambiguity of the grammar


I have a grammar rules like that;

S -> S and S
S -> S or S
S -> (S)
S -> true | false

-- and , or , ( , ) , true ,false are terminals -- 

I can find out that this grammar is ambiguous, but how can I modify this grammar to solve the ambiguity?


Solution

  • The standard approach is to give them precedence/associativity:

    S -> S or  A | A
    A -> A and P | P
    P -> (S)
    P -> true | false