PLY Lex和Yacc问题

时间:2014-09-07 16:20:18

标签: python yacc lex ply

我在使用PLY时遇到了麻烦。我一直在阅读documentation并决定试用这些例子。 lexing的例子工作得很好但解析我无法工作。我也看着文档,不明白你如何将lex和yacc连接起来创建一个合适的编译器。解析器只包含词法分析器可能的令牌,据我所知,没有别的。

我添加了一些颜色(Colorama模块)和略有不同的消息,但除此之外,这与示例相同:

#!/usr/bin/env python

### LEXICAL ANALYSIS ###


import ply.lex as lex

import colorama
colorama.init()

tokens = (
    "NUMBER",
    "PLUS",
    "MINUS",
    "MULTIPLY",
    "DIVIDE",
    "LBRACKET",
    "RBRACKET"
)


t_PLUS = r"\+"
t_MINUS = r"-"
t_MULTIPLY = r"\*"
t_DIVIDE = r"/"
t_LBRACKET = r"\("
t_RBRACKET = r"\)"

t_ignore = "\t\r "

def t_NUMBER(t):
    r"\d+"
    t.value = int(t.value)
    return t

def t_newline(t):
    r"\n+"
    t.lexer.lineno += len(t.value)

def t_COMMENT(t):
    r"\#.*"
    print "Comment:", t.value

def t_error(t):
    print colorama.Fore.RED + "\n\nLEXICAL ERROR: line", t.lexer.lineno, "and position", t.lexer.lexpos, "invalid token:", t.value.split("\n")[0] + colorama.Fore.RESET
    t.lexer.skip(len(t.value))


def mylex(inp):
    lexer = lex.lex()

    lexer.input(inp)


    for token in lexer:
        print "Token:", token

工作正常,但解析器不会:

#!/usr/bin/env python


import ply.yacc as yacc

from langlex import tokens

def p_expression_plus(p):
    "expression : expression PLUS term"
    p[0] = p[1] + p[3]

def p_expression_minus(p):
    "expression : expression MINUS term"
    p[0] = p[1] - p[3]

def p_expression_term(p):
    "expression : term"
    p[0] = p[1]

def p_term_times(p):
    "term : term MULTIPLY factor"
    p[0] = p[1] * p[3]

def p_term_div(p):
    "term : term DIVIDE factor"
    p[0] = p[1] / p[3]

def p_term_factor(p):
    "term : factor"
    p[0] = p[1]

def p_factor_num(p):
    "factor : NUMBER"
    p[0] = p[1]

def p_factor_expr(p):
    "factor : LBRACKET expression RBRACKET"
    p[0] = p[2]

def p_error(p):
    print "Syntax error!"

parser = yacc.yacc()

while True:
    s = raw_input("calc > ")
    if not(s):
        continue
    result = parser.parse(s)
    print result

尝试运行时出现以下错误:

calc > 5 + 10
Traceback (most recent call last):
  File "C:\Users\Max\Desktop\lang\langyacc.py", line 49, in <module>
    result = parser.parse(s)
  File "C:\Python27\lib\site-packages\ply\yacc.py", line 265, in parse
    return self.parseopt_notrack(input,lexer,debug,tracking,tokenfunc)
  File "C:\Python27\lib\site-packages\ply\yacc.py", line 881, in parseopt_notrack
    lexer = lex.lexer
AttributeError: 'module' object has no attribute 'lexer'

我是lex,yacc和编译器开发的初学者,并且不知道为什么会这样。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

您还没有在词法分析器文件中构建词法分析器。你在函数mylex()中有它,但它实际上并没有构建。

将其拉出功能。

lexer = lex.lex()

def mylex(inp):
    lexer.input(inp)
    # etc.