Search code examples
context-free-grammarlemon

Why are there 3 parsing conflicts in my tiny grammar?


//complete
start ::= template.

//template
template ::= template_elements.
template ::= template template_elements.
template ::= .

//template elements
template_elements(res) ::= COMMENT. 
template_elements(res) ::= tag(t). 

//tag
tag(res) ::= LDEL exp(e) RDEL. 

//exp
exp(res) ::= value(v). 
exp(res) ::= exp(e1) OP(o) exp(e2).

//value
value(res) ::= variable(v). 

//variable
variable(res) ::= DOLLAR ID(i).

Anyone knows where the conflicts locate?

UPDATE

If I delete exp(res) ::= exp(e1) OP(o) exp(e2).,there'll be only two conflicts,but I don't know why this is causing conflict...

UPDATE2

Why it's OK here:

template ::= template_elements.
template ::= template template_elements.
template ::= .

Solution

  • To fix your exp ambiguity, set the precedence and associativity -- see the documentation under Precedence Rules.

    Lemon can handle left recursion, but your template rule should be

    template ::= template template_elements.
    template ::= .
    

    Because template can be empty, you don't need the template ::= template_elements case.

    What errors do you get after making those changes?