parsing - Creating shift-reduce / reduce-reduce free grammars -
i'm trying implement simple java language parser in sablecc, although i'm running shift-reduce / reduce-reduce problems when implementing if, while , block statements.
for instance, i've considered following:
stmts = stmt*;
stmt = if_stmt | block_stmt | while_stmt;
block_stmt = { stmts } | ; ;
while_stmt = while ( predicate ) { stmts } | while ( predicate ) ;
this grammar, will, instance, cause problem when have of form
while (true) ; the parser won't able know whether reduce ; (from block_stmt) or full while (true); (from while_stmt).
i've been reading everywhere reasons shift-reduce / reduce-reduce problems , think understand them. 1 thing know causes them, , different knowing how structure grammars such avoid them. i've tried implementing grammars in different ways , end problems nonetheless.
i guess instead of trying run specific ss / rr problem, there must kind of paradigm follow such avoid kinds of issues? believe way of approaching problem must totally wrong :(
any resources around on how build scratch grammar without falling these pitfalls? resources on matter tend either easy (stating obvious if else problem) or flagged grammars, kind of impenetrable.
the problem grammar specified eg semicolon can interpreted either semicolon of while_stmt or of block_stmt... no sorry not somehow grammar redundant because { stmt } appears twice on rhs. do
stmts ::= stmt | stmts stmt block_stmt ::= { stmts } stmt ::= ... | block_stmt | ; // empty while_stmt ::= while ... stmt
Comments
Post a Comment