问题
I'm trying to parse a simple language. The trouble comes with parsing function calls. I'm trying to tell it that a function call is an expression followed by left parenthesis, argument list, and right parenthesis. I have something like this:
expr = Forward()
iden = Word(alphas+'_', alphanums+'_')
integer = Word(nums)
binop = operatorPrecedence(expr, ...) # irrevelant
call = expr + Literal('(') + delimitedList(expr) + Literal(')')
expr << call | integer | iden
The problem is obvious: expr
is left-recursive. However, I can't figure what to do to solve this. I'm experienced with right-recursive-style grammars(a.k.a. PLY, Yacc, etc.) but am still trying to figure out left-recursive grammars.
回答1:
Functionname = Word(alphanums + '_')
functionbody = Forward()
functionbody <<= Functionname + (Literal("(") +
Optional( delimitedList ( functionbody | Word(alphanums + '_') | "''"),'')
+ Literal(")"))
来源:https://stackoverflow.com/questions/24899845/parse-function-call-with-pyparsing