pythonparsinggrako

Grako end closure when rule is matched


I have this grammar:

name = /[_a-zA-Z][a-zA-Z0-9]*/;
expression = name '+' name;
def_body = 'def' name:name args:{name} body:expression;

But when i try to parse, it always consume first name of expression as an part of arguments. Is there way to make it test before every name in args closure if it can match expression and end if it pass ?

Thanks in advance.

EDIT: I solved it throught semantics, but i'm still curious if it is possible through grako ebnf.


Solution

  • The lack of delimiters for the parameters requires looking further ahead in the input to decide if the closure should continue. A negative lookahead should solve the problem at the grammar level:

    def_body = 'def' name:name args:{name !'+'} body:expression;
    

    Lookahead are not "cheating". They're an integral part of the PEG definition for important reasons.