条件规则
我如何在ply中处理一对多规则。
tokens = (
'VAR1',
'VAR2',
'TRUE',
'SINGLE_CHAR',
)
def t_VAR1(t):
r'var1'
return t
def t_VAR2(t):
r'var2'
return t
def t_TRUE(t):
r't|T'
return t
def t_SINGLE_CHAR(t):
r'[A-Za-z]'
return t
现在,如果我有两个规则。
def p_expression_variable2(p):
'''variable2 : VAR2 SINGLE_CHAR'''
print("got VAR2")
def p_expression_variable1(p):
'''variable1 : VAR1 TRUE'''
print("got VAR1")
我想制定规则,以便var1 t
与p_expression_variable1
和var2 t
与p_expression_variable 2
匹配。但是,由于t_true
是t_single_char
的子集,t
始终与t_true
始终匹配,因此给出了错误。
我是Lex和YACC的新手,想知道如何处理此类问题的规则。我知道它可以通过有条件的Lexing(定义两个状态)来处理,但是是否有一种方法可以使用单个状态来处理它?
How can I handle one to many rules in PLY.
tokens = (
'VAR1',
'VAR2',
'TRUE',
'SINGLE_CHAR',
)
def t_VAR1(t):
r'var1'
return t
def t_VAR2(t):
r'var2'
return t
def t_TRUE(t):
r't|T'
return t
def t_SINGLE_CHAR(t):
r'[A-Za-z]'
return t
Now if I have two rules.
def p_expression_variable2(p):
'''variable2 : VAR2 SINGLE_CHAR'''
print("got VAR2")
def p_expression_variable1(p):
'''variable1 : VAR1 TRUE'''
print("got VAR1")
I want to make the rules so that var1 T
matches with p_expression_variable1
and var2 T
matches with p_expression_variable2
. But as t_TRUE
is a subset of t_SINGLE_CHAR
, T
always matches with t_TRUE
, hence giving error.
I am novice in lex and yacc, want to know how to handle rules for such issue. I know that It can be handled by conditional lexing (defining two states), but is there a way to handle it with single state?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不以您想做的方式。
在YACC/LEX解析模型中,词汇分析是无需考虑解析器状态的。这就是将句法和词汇分析分为不同组成部分的重点。如果您希望他们以自己的建议方式进行互动,则必须使用
使用LEX/YACC框架,您可以使用多个词汇状态,但是这需要从解析器到词汇分析器的反馈,通常使用中规动作,ply不支持或重现词汇分析仪中手工构建的状态机中语法分析的一部分。这两种解决方案都是不高的,不可分割的且不必要的笨重。 (尽管如此,它们比人们可能希望的更普遍。)
您能做的就是使词汇分析明确:
在解析器中实施一种词汇后备:
Not in the way you want to do it.
In the yacc/lex parsing model lexical analysis is performed without regard to the parser state. That's the point of separating syntactic and lexical analysis into distinct components. If you wanted them to interact in the way you suggest, you would have to use a scannerless parser, but that involves a certain cost, both in grammar complexity and in the parsing algorithm, because the resulting grammar is unlikely to work with only a single character lookahead.
With the lex/yacc framework, you could use multiple lexical states, but that requires either feedback from the parser to the lexical analyser, generally using mid-rule actions, which Ply doesn't support, or reproducing part of the syntactic analysis inside a hand-built state machine in the lexical analyser. Both of these solutions are inelegant, unscalable, and unnecessarily bulky. (Nonetheless, they are more common than one might hope.)
What you can do is to make the lexical analysis unambiguous:
and implement a kind of lexical fallback in the parser: