Search code examples
context-free-grammarrascal

What's wrong with this Rascal grammar?


So I'm trying to get this grammar to work, however it gives me an error. It comes from the @context="num" inside the Number lexical. It tells me that it's ambiguous, can anybody tell me why?

start syntax Statement = Type Id "=" Number ";" ;   

lexical Id = @context="id" [a-z] !<< [a-z]+ !>> [a-z] \ Type;

lexical Number = @context="num" [0-9]+;

keyword Type = @context="type" ("int"|"str"|"float");

It is really weird because it works without the @context tag, but then when I add it I can suddenly not import/find the symbol Statement in a different module. Or is gives me the grammar is ambiguous error.

please help

**edit: ** If i change the keyword Type to a normal lexical type then, with or without the parentheses the problem remains.


Solution

  • I don't think your supposed to parse with the keywords lexical, as it tends to be a combination of a lot of things.

    And, the keywords have to be a normal list op productions with plain literals. It's doesn't work with arbitrary regular expressions.

    So the reason why it works without context, is I'm guessing due to the fact that you also remove the brackets, which turns it into a general list of alternatives?