- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 PLY 编写自己的解析器。我想分别封装lex和yacc
这是代码
对于 Lex 类:
class Lex:
tokens = (
'NAME', 'NUMBER',
)
literals = ['=', '+', '-', '*', '/', '(', ')']
# Tokens
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
...
class Parser:
# Parsing rules
tokens = Lex.tokens
def p_statement_assign(self, p):
'statement : NAME "=" expression'
self.names[p[1]] = p[3]
...
def run(self):
print self.tokens
lex.lex(module=Lex) # ----what should I do here?-----
parser = yacc.yacc(module=self)
parser.parse("1+2")
module=Lex
对于 lex,就像
yacc.yacc(module=self)
,但它没有用,任何人都可以告诉解决方案。
最佳答案
这是我第一次尝试构建解释器。我真的不喜欢现代 Python 的 PLY。使用文档字符串来存放正则表达式感觉很奇怪。 PLY 样本似乎也已经过时了。但是,嘿,PLY 的第一个版本于 2001 年发布,16 年后它仍然存在,因此感谢作者/社区维护它。
话虽如此,这就是我如何进行一些封装工作:
class MyLexer(object):
tokens = (
'NUMBER',
'PLUS', 'MINUS', 'TIMES', 'DIVIDE',
'LPAREN', 'RPAREN',
)
t_PLUS = r'\+'
t_MINUS = r'-'
t_TIMES = r'\*'
t_DIVIDE = r'/'
t_LPAREN = r'\('
t_RPAREN = r'\)'
...
def __init__(self):
# Build the lexer
self.lexer = lex.lex(module=self)
class MyParser(object):
tokens = MyLexer.tokens
...
def p_expression_binop(self, t):
'''
expression : expression PLUS expression
| expression MINUS expression
| expression TIMES expression
| expression DIVIDE expression
'''
left_hand_side = t[1]
right_hand_side = t[3]
operator = t[2]
if operator == '+':
value = left_hand_side + right_hand_side
elif operator == '-':
value = left_hand_side - right_hand_side
elif operator == '*':
value = left_hand_side * right_hand_side
elif operator == '/':
value = left_hand_side / right_hand_side
else:
raise AssertionError('Unknown operator: {}'.format(operator))
t[0] = value
...
def __init__(self):
self.lexer = MyLexer()
self.parser = yacc.yacc(module=self)
关于python-2.7 - 如何将 PLAY 的 lex 和 yacc 封装在两个单独的类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38262552/
如何使用 yyparse() 编译、链接和调用不同的 YACC 语法在一个程序中? 最佳答案 对每个单独的 yacc 语法生成使用 -p 选项 -p prefix Use pr
我正在为类项目工作,我们必须在其中构建解析器。我们目前正处于在 yacc 中构建解析器的阶段。目前让我感到困惑的是,我读到您需要为每个非终结符分配一个类型。在某些情况下,虽然我会有类似的东西: ...
我有一个关于 yacc 编译器的问题。我不编译简单的 yacc 语法。这是代码部分: /*anbn_0.y */ %token A B %% start: anbn '\n' {printf(" is
我正在使用 Yacc 和 lex 来解析 C 类型语言, 我已经使用 C++ 构建了数据结构。 一切正常,但我无法在 main.cpp 中使用 yyin 读取输入文件。 以下是代码: 请帮忙 ! #i
每当找到一些正则表达式时,我都会使用 lex 来执行一些代码, Yacc 还能做更多的事情吗?如果是,那是什么? 最佳答案 lex 和 yacc 通常一起使用。这就是您通常使用以下两种方式构建应用程序
我是 lex 和 yacc 以及编译器设计的新手。我想知道符号表是在哪个阶段(词汇、句法或任何其他阶段)以及如何生成的? 我可以简单描述一下 y.output 文件,该文件是通过向 yacc 提供 -
我正在 yacc 中处理一个项目并且遇到了 shift/reduce 错误,但我不知道为什么我会得到它。我一直在查看 y.output 文件,但不太确定如何阅读它。我的 y.output 文件超出了
我是 yacc/lex 的新手,我正在开发一个由其他人编写的解析器。我注意到,当找到未定义的标记时,解析器返回错误并停止。有没有一种简单的方法可以让它完全忽略它无法解析的行并继续进行下一个? 最佳答案
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
嗨,我正在学习 Lex 和 yacc。我创建了以下 lex 程序。 %{ #include %} %% [0123456789]+ printf("NUMBER\n"); [a-
莱克斯部分: %% [0-9]+ { yyval = atoi (yytext); return num; } %% 雅克部分: %token num %% exp:num '+' num ; {$$
问题 我正在尝试使用 Python Lex-Yacc (PLY) 实现容错解析器,但在输入字符串末尾使用错误恢复规则时遇到问题。 如何从意外的输入结束中恢复? 示例 此示例语法生成以下形式的字符串 A
我有两个带有函数 xxlex() 和 yylex() 的 lex 文件,还有一个 yacc语法文件。我希望语法规则始终调用 xxlex()。但是 xlex() 只被调用一次,我希望它总是被调用。我该怎
我正在尝试使用 PLY (python implementation of yacc) 编写一个相当简单的语法,并且在我想要的时候让 yacc 减少标记字符串时遇到困难。 我想解释一系列采用不同类型参
notype_declarator: notype_declarator '(' parmlist_or_identifiers %prec '.' { $$ = bui
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭12 年前。 Improve th
拜托,我面临一个简单的问题..这就是问题所在,在我的 lex 文件中,我有类似的内容: char *ptr_String; "name = " { BEGIN sName; } .+ {
关闭。这个问题是off-topic .它目前不接受答案。 想改善这个问题吗? Update the question所以它是 on-topic对于堆栈溢出。 9年前关闭。 Improve this q
嗨,我正在尝试运行 R. Levine 所著的 lex 和 yacc 书中的 John 代码,我已经使用命令在 Linux 中编译了 lex 和 yacc 程序 lex example.l yacc
我想传递 token 的实际字符串。如果我有一个名为ID的 token ,那么我希望我的yacc文件实际知道称为什么ID。我必须将使用yylval的字符串从flex文件传递到yacc文件。我怎么做?
我是一名优秀的程序员,十分优秀!