Question

I have the following ambiguous grammar where the capital letter rules are for simple lexical tokens:

program : function | function program
function : FN ID ARROW TYPE invariants DASH block
fn_call : ID L_PAREN CONSTANT R_PAREN


invariants : invariant | invariant invariants
invariant : TYPE ID COLON invariant_tests
invariant_tests : test | test COMMA invariant_tests
test : OPERATOR CONSTANT | STAR


block : expression | expression block
expression : loop | fn_call | assignment | def | constant
loop : FOR def IN CONSTANT COLON block
assignment : ID ASSIGN CONSTANT | ID ASSIGN ID
def : TYPE assignment

I receive 8 shift/reduce errors (when using Python 2.7 and PLY).

Is the ambiguity a result of how I define "one or more", eg. block : expression | expression block?

Was it helpful?

Solution

As a previous speaker said, you must differentiate between the "block usages". For example, try:

loop : FOR def IN CONSTANT COLON block END FOR

OTHER TIPS

The problem was how I defined the loop:

loop : FOR def IN CONSTANT COLON block

since the parser didn't understand if the block belonged to the loop or to the function.

Basically, another variant of the dangling else problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top