1

I've got a grammar which I believe is ambiguous. I don't know how to eliminate the ambiguity and convert it to an unambiguous grammar.

expr    ::= num | lvalue | incrop expr | expr incrop | expr binop expr | (expr)

lvalue  ::= $expr

incrop  ::= ++ | --

binop   ::= + | - |

num ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

And This is what I came up with:

expr ::=    num expr' | lvalue expr'| incrop expr expr' | (expr) expr'

expr' ::= incrop expr' | binop expr expr' | empty

lvalue  ::= $expr

incrop  ::= ++ | --

binop   ::= + | - |

num ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

is it correct?

BugHunterUK
  • 8,346
  • 16
  • 65
  • 121
pooria
  • 343
  • 2
  • 14
  • I think as written, the grammar could only be satisfied by an infinite sequence of paired plus or minus characters, since the first token of an `S` must be `++` or `--`, and it must be followed by an `S` (which is in turn a `++` or `--` followed by an `S`, ad infinitum). – supercat Feb 10 '19 at 20:13
  • @supercat It is not the whole grammar. This is the part which makes it ambiguous. – pooria Feb 10 '19 at 20:20
  • That part does not in and of itself make it ambiguous since `S` has no finite expansion and thus the only relevant expansion is `AS`. What is necessary to make it unambiguous would depend upon what else exists that makes it ambiguous. – supercat Feb 10 '19 at 20:22
  • @supercat I just modified the grammar. This is the actual grammar – pooria Feb 10 '19 at 20:25
  • 2
    Please take a look at [this answer](https://stackoverflow.com/a/54558463/1566221), which tries to explain how to write a grammar which doesn't have these ambiguities. – rici Feb 10 '19 at 20:41

0 Answers0