Rejecting lexemes

Skip to first unread message

Thomas Weigert

Aug 11, 2018, 1:08:45 AM8/11/18
to marpa parser
Sorry for the following beginner's question....

I have an ambiguous grammar which I would like to resolve during parsing.

I arranged for an event to be triggered when I have seen enough so that I can make a decision whether the current production will be the right one (by considering context). I would like to in the event handler either accept this lexeme (when I determine that this production is the correct one) or reject this lexeme (when I determine that this production is the wrong one). If I reject this lexeme, I hope that marpa will go on to try the other production which will now be the correct one.

Is this something reasonable to want to accomplish? If so, is there any example I could emulate for this behavior. It is easy to accept the lexeme, but I am having a hard time figuring out how to reject it and continue trying.

Thanks for any suggestions.

Ron Savage

Aug 11, 2018, 3:07:35 AM8/11/18
to marpa parser
Hi Thomas

See I wrote those 2 modules, and they each have a method called _validate_event(), which I suggest may be relevant to your situation.

The point is that with ambiguity Marpa will generate multiple events at the same point in the input stream, so there is no need to rewind Marpa to rescan. Simply study the list of events generated and select one.

Jeffrey Kegler

Aug 11, 2018, 9:40:13 AM8/11/18
to Marpa Parser Mailing LIst
What you want to do is "external lexing".  When the event triggers you can write custom code to deliver only the lexeme you want the parser to set, then resume the Marpa parser.

Sometimes you can accomplish the same thing more easily using lexeme priorities, however: 

Hope this helps, jeffrey

You received this message because you are subscribed to the Google Groups "marpa parser" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
For more options, visit

Thomas Weigert

Aug 11, 2018, 9:46:37 AM8/11/18
to marpa parser
Thanks, Ron, for the inspiration.

After scanning your code I realized that this situation is even easier. All I need to do is when I hit the point where I want to tell these apart (both productions have the same token at this point), that I can recognize anything I want with lexeme_read, so I just need to decide whether I want to recognize this parse or the other. I don't even have to get into resolving ambiguous parses.

And I found that I can put the "no output parentheses" even around non-explicit lexical elements, so my little check above does not even end up in the parse tree.
Reply all
Reply to author
0 new messages