c - Resolving ambiguous grammar without resorting to GLR-Parser -


i have grammar has 2 different possibilities when parsing 'if' expr 'then'. there's simple "assignment", such if foo bar=1; else bar=0; there's i'm calling "if_block" of code can contain 1 or more "assignments":

if foo {     bar = 1;     if xyz abc = -1; } else {     bar = 0;     if xyz     {        abc = 0;     } } 

i'm handling nested if_blocks way of dangling else matched/unmatched block.

my (very simplified) grammar basically:

program : if_blocks if_blocks : if_block | if_block if_blocks if_block : assignments assignments : assignment | assignment assignments assignment : simple_assignment | if_assignment 

so predicament assignment followed if_block. example:

foo = bar; if foo {    foo = foo + 1; } 

foo = bar; assignment that, in case, should reduced if_block. if foo { ... } if_block. whole code if_block+if_block (reduced if_blocks). after foo = bar; reduced assignment, there's not enough lookahead know whether if foo then assignment (within foo = bar; if_block) or if it's separate if_block.

i've added %glr-parser, seems resolve this, run other situations multiple branches of parsing survive , can't seem reconcile different s/r branches. what's accepted practice kind of situation, absent switching different scanner/parser (which lot of work me learn , rewrite code) or changing language (which can't do)? there easy resolution (somehow defining %dprec?) using glr or tweak grammar?

classically dangling-else problem solved insisting else attached nearest if, (conceptually) resolves ambiguity. somehow need communicate idea parser generator make ambiguity go away.

most parser generators (including yacc , bison) have way of saying when there shift-vs-reduce conflict token, prefer "shift", can used acheives effect else keyword. don't know idiom yacc or bison, should easy find in grammar description information.

(i use own glr parser, , still useful this, because gets rid of ambiguous parse in easy way).


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -