Parse function call with PyParsing

妖精的绣舞 提交于 2020-01-23 10:47:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!