parsing - Converting given ambiguous arithmetic expression grammar to unambiguous LL(1) -


in term, have course on compilers , studying syntax - different grammars , types of parsers. came across problem can't figure out, or @ least can't make sure i'm doing correctly. did 2 attempts , counterexamples found. given ambiguous grammar arithmetic expressions: e → e+e | e-e | e*e | e/e | e^e | -e | (e)| id | num , ^ stands power.

i figured out priorities should be. highest priority parenthesis, followed power, followed unary minus, followed multiplication , division, , there addition , substraction. asked convert equivalent ll(1) grammar. wrote this:

  • e → e+a | e-a | a
  • a → a*b | a/b | b
  • b → -c | c
  • c → d^c | d
  • d → (e) | id | num

what seems problem not equivalent grammar first one, although it's non-ambiguous. example: given grammar can recognize input: --5 while grammar can't. how can make sure i'm covering cases? how should modify grammar equivalent given one? in advance.

edit: also, of course elimination of left recursion , left factoring make ll(1), first need figure out main part asked above.

here's 1 should work case

e = e+a | e-a | a = a*c | a/c | c c = c^b | b b = -b | d d = (e) | id | num 

as sidenote: pay attention requirements of task since some applications might assign higher priority unary minus operator respect power binary operator.


Comments