条件规则

发布于 2025-02-07 00:39:14 字数 854 浏览 1 评论 0原文

我如何在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 tp_expression_variable1var2 tp_expression_variable 2匹配。但是,由于t_truet_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ま昔日黯然 2025-02-14 00:39:14

不以您想做的方式。

在YACC/LEX解析模型中,词汇分析是无需考虑解析器状态的。这就是将句法和词汇分析分为不同组成部分的重点。如果您希望他们以自己的建议方式进行互动,则必须使用

使用LEX/YACC框架,您可以使用多个词汇状态,但是这需要从解析器到词汇分析器的反馈,通常使用中规动作,ply不支持或重现词汇分析仪中手工构建的状态机中语法分析的一部分。这两种解决方案都是不高的,不可分割的且不必要的笨重。 (尽管如此,它们比人们可能希望的更普遍。)

您能做的就是使词汇分析明确:

def t_TRUE(t):
    r'[tT]'
    return t

def t_SINGLE_CHAR(t):
    r'[A-SU-Za-su-z]'
    return t

在解析器中实施一种词汇后备:

def p_any_char(p):
    '''any_char : SINGLE_CHAR
                | TRUE
    '''
    p[0] = p[1]

def p_expression_variable2(p):
    '''variable2 : VAR2 any_char'''
    print("got VAR2")

def p_expression_variable1(p):
    '''variable1 : VAR1 TRUE'''
    print("got VAR1")

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:

def t_TRUE(t):
    r'[tT]'
    return t

def t_SINGLE_CHAR(t):
    r'[A-SU-Za-su-z]'
    return t

and implement a kind of lexical fallback in the parser:

def p_any_char(p):
    '''any_char : SINGLE_CHAR
                | TRUE
    '''
    p[0] = p[1]

def p_expression_variable2(p):
    '''variable2 : VAR2 any_char'''
    print("got VAR2")

def p_expression_variable1(p):
    '''variable1 : VAR1 TRUE'''
    print("got VAR1")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文