本文介绍了使用PyParsing解析函数调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试解析一种简单的语言.问题来自于解析函数调用.我试图告诉它,函数调用是一个表达式,后跟左括号,参数列表和右括号.我有这样的东西:
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
问题很明显:expr
是左递归的.但是,我无法解决该问题.我对右递归样式语法(也称为PLY,Yacc等)很有经验,但仍在尝试找出左递归语法.
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.
推荐答案
Functionname = Word(alphanums + '_')
functionbody = Forward()
functionbody <<= Functionname + (Literal("(") +
Optional( delimitedList ( functionbody | Word(alphanums + '_') | "''"),'')
+ Literal(")"))
这篇关于使用PyParsing解析函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!