Ply:定义“c”层规则时出现的问题语言

发布于 2024-10-18 03:03:20 字数 408 浏览 7 评论 0原文

我正在尝试为 C 语言编写一个解析器,它将能够处理表达式、赋值、if-else 和 while 循环。

这是我的规则:

表达式 ->表达式操作表达式
表达式->身份证号
表达式->数量
声明-> ID ASSIGN 表达式
声明-> IF 表达式 THEN 语句
声明-> WHILE 表达式 THEN 语句

大写的所有内容都是一个标记(终结符)

在解析字符串“while h<=0 then t=1”时,似乎将“h”视为一个表达式(使用规则表达式->ID) 。因此,“WHILE 表达式 THEN 语句”中的表达式变为“h”。显然,我希望它将“h<=0”视为表达式(使用规则表达式 -> 表达式 op 表达式)。我如何确保这种情况发生?

I'm trying to write a parser for the c language which will be able to take care of expressions, assignments, if-else and while loops.

here are my rules :

expression -> expression op expression
expression -> ID
expression -> NUMBER
statement -> ID ASSIGN expression
statement -> IF expression THEN statement
statement -> WHILE expression THEN statement

Everything in caps is a token(terminal symbol)

When parsing the string "while h<=0 then t=1", it seems to consider "h" as an expression (using the rule expression->ID). So, the expression in "WHILE expression THEN statement" becomes "h". Obviously, I would want it to consider "h<=0" as the expression (using the rules expression -> expression op expression). How do I make sure that this happens?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

2024-10-25 03:03:20

基于您询问 ply.lex 模块的上一篇文章,下面的代码片段似乎就像类 C 语法的部分实现。我没有太多使用 ply,但技巧之一似乎是您需要以正确的顺序定义语法规则。

tokens   = [ 'ID', 'NUMBER', 'LESSEQUAL', 'ASSIGN' ]
reserved = {
    'while' : 'WHILE',
    'then'  : 'THEN',
    'if'    : 'IF'
}
tokens += reserved.values()

t_ignore    = ' \t'
t_NUMBER    = r'\d+'
t_LESSEQUAL = r'<='
t_ASSIGN    = r'\='

def t_ID(t):
    r'[a-zA-Z_][a-zA-Z0-9_]*'
    if t.value in reserved:
        t.type = reserved[ t.value ]
    return t

def t_error(t):
    print 'Illegal character'
    t.lexer.skip(1)

def p_statement_assign(p):
    'statement : ID ASSIGN expression'
    p[0] = ('assign', p[1], p[3] )

def p_statement_if(p):
    'statement : IF expression THEN statement'
    p[0] = ('if', p[2], p[4] )

def p_statement_while(p):
    'statement : WHILE expression THEN statement'
    p[0] = ('while', p[2], p[4] )

def p_expression_simple(p):
    '''expression : ID
                  | NUMBER'''
    p[0] = p[1]

def p_expression_cmp(p):
    'expression : expression LESSEQUAL expression'
    p[0] = ( '<=', p[1], p[3] )

import ply.lex as lex
import ply.yacc as yacc
lexer = lex.lex()
parser = yacc.yacc()
inp = 'while h<=0 then t=1'
res = parser.parse(inp)
print res

代码片段的输出是:

('while', ('<=', 'h', '0'), ('assign', 't', '1'))

Building on the previous post where you were asking about the ply.lex module, the snippet below seems like a partial implementation of a c-like grammar. I haven't used ply much, but one of the tricks seems to be that you need to define the grammar rules in the correct order.

tokens   = [ 'ID', 'NUMBER', 'LESSEQUAL', 'ASSIGN' ]
reserved = {
    'while' : 'WHILE',
    'then'  : 'THEN',
    'if'    : 'IF'
}
tokens += reserved.values()

t_ignore    = ' \t'
t_NUMBER    = r'\d+'
t_LESSEQUAL = r'<='
t_ASSIGN    = r'\='

def t_ID(t):
    r'[a-zA-Z_][a-zA-Z0-9_]*'
    if t.value in reserved:
        t.type = reserved[ t.value ]
    return t

def t_error(t):
    print 'Illegal character'
    t.lexer.skip(1)

def p_statement_assign(p):
    'statement : ID ASSIGN expression'
    p[0] = ('assign', p[1], p[3] )

def p_statement_if(p):
    'statement : IF expression THEN statement'
    p[0] = ('if', p[2], p[4] )

def p_statement_while(p):
    'statement : WHILE expression THEN statement'
    p[0] = ('while', p[2], p[4] )

def p_expression_simple(p):
    '''expression : ID
                  | NUMBER'''
    p[0] = p[1]

def p_expression_cmp(p):
    'expression : expression LESSEQUAL expression'
    p[0] = ( '<=', p[1], p[3] )

import ply.lex as lex
import ply.yacc as yacc
lexer = lex.lex()
parser = yacc.yacc()
inp = 'while h<=0 then t=1'
res = parser.parse(inp)
print res

Output from the snippet is:

('while', ('<=', 'h', '0'), ('assign', 't', '1'))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文