Python PLY做赋值操作失败

Python PLY fail to do assignment operation

我正在尝试创建一个将我的脚本作为输入的解释器。我在编写正则表达式时遇到了一些问题。定义的标记之一是将所有字符串视为标记。

import ply.lex as lex
import ply.yacc as yacc

tokens = (
    'STAIRCASE',
    'STAIRCASE_END',
    'STAIR',
    'STAIR_END',
    'TAG',
    'COLON_SYM',
    'LINE_START_SYM',
    'NONE',
    'USER_DEFINED',
    'ARRAY',
    'IS',
)

assignments = {}

t_STAIRCASE                 = r'staircase'
t_TAG                       = r'\(([a-zA-Z0-9\ ])*\)'
t_COLON_SYM                 = r' :'
t_LINE_START_SYM            = r'-'
t_STAIRCASE_END             = 'EOSC'
t_ignore                    = ' \t\n'
t_STAIR                     = 'stair'
t_STAIR_END                 = 'EOS'
t_NONE                      = 'EOP'

这是正则表达式的问题

t_USER_DEFINED              = r'[a-zA-Z0-9]+'

代码继续

t_IS                        = 'is'

def t_error(t):
    print 'Illegal character "%s"' % t.value[0]
    t.lexer.skip(1)
lex.lex()

NONE, STAIRCASE, TAG, STAIRCASE_DESCRIPTION = range(4)
states = ['NONE', 'STAIRCASE','STAIRCASE_DESCRIPTION']
current_state = NONE

def x():
    print "Hi How you doing"

def p_staircase_def(t):
    """STAIRCASE_DEF         : STAIRCASE TAG COLON_SYM STAIRCASE_DESCRIPTION
                             """
    print t[0:]
    help(t)

def p_staircase_description(t):
    """STAIRCASE_DESCRIPTION : LINE_START_SYM DICTONARY STAIRCASE_DESCRIPTION
                             | STAIRCASE_END STAIR_DEF
                             """
    print t[0:]

def p_dictonary(t):
    """
    DICTONARY                : USER_DEFINED IS USER_DEFINED
                             """

这是我的赋值操作,实际上它创建了一个变量字典

    temp = { t[1] : t[2] }
    print assignments.update( temp )
def p_stair_def(t):
    """STAIR_DEF             : STAIR TAG COLON_SYM STAIR_DESCRIPTION
                             """
    print t[0:]

def p_stair_description(t):
    """STAIR_DESCRIPTION     : LINE_START_SYM DICTONARY STAIR_DESCRIPTION
                             | STAIR_END STAIR_DEF
                             | STAIR_END
                             """
    print t[0:]

def p_error(t):
    print 'Syntax error at "%s"' % t.value if t else 'NULL'
    global current_state
    current_state = NONE

yacc.yacc()

file_input = open("x.staircase","r")
yacc.parse(file_input.read())

这是我的解释器需要接受的示例输入“x.staircase”

staircase(XXXX XXX XXX):
- abc is 23183       # which need to {'abc' : '23183'}
- bcf is fda
- deh is szsC
EOSC
stair(XXXX XXX XXX):
- lkm is 35
- raa is 233
EOS
stair(XXXX XXX XXX):
- faa is zxhfb
- faa is 1
EOS

我收到错误

Syntax error at "staircase"
[Finished in 0.1s]

BELOW 代码有效,但输入文件不符合预期。

import ply.lex as lex
import ply.yacc as yacc

tokens = (
    'STAIRCASE',
    'STAIRCASE_END',
    'STAIR',
    'STAIR_END',
    'TAG',
    'COLON_SYM',
    'LINE_START_SYM',
    'NONE',
    'USER_DEFINED',
    'ARRAY',
    'IS',
)

assignments = {}

t_STAIRCASE                 = r'staircase'
t_TAG                       = r'\(([a-zA-Z0-9\ ])*\)'
t_COLON_SYM                 = r' :'
t_LINE_START_SYM            = r'-'
t_STAIRCASE_END             = 'EOSC'
t_ignore                    = ' \t\n'
t_STAIR                     = 'stair'
t_STAIR_END                 = 'EOS'
t_NONE                      = 'EOP'

##########################################
Here is the issue with this regular exprission

It worked, If I Use this
t_USER_DEFINED              = r'a'

Instead of this
#t_USER_DEFINED              = r'[a-zA-Z0-9]+'

But, when it comes to my input file it only accept one variable called 'a'
##########################################
Code continues

t_IS                        = 'is'

def t_error(t):
    print 'Illegal character "%s"' % t.value[0]
    t.lexer.skip(1)
lex.lex()

NONE, STAIRCASE, TAG, STAIRCASE_DESCRIPTION = range(4)
states = ['NONE', 'STAIRCASE','STAIRCASE_DESCRIPTION']
current_state = NONE

def x():
    print "Hi How you doing"

def p_staircase_def(t):
    """STAIRCASE_DEF         : STAIRCASE TAG COLON_SYM STAIRCASE_DESCRIPTION
                             """
    print t[0:]
    help(t)

def p_staircase_description(t):
    """STAIRCASE_DESCRIPTION : LINE_START_SYM DICTONARY STAIRCASE_DESCRIPTION
                             | STAIRCASE_END STAIR_DEF
                             """
    print t[0:]

def p_dictonary(t):
    """
    DICTONARY                : USER_DEFINED IS USER_DEFINED
                             """

HERE is my assignment operation, actually it create a dictionary of variables

    temp = { t[1] : t[2] }
    print assignments.update( temp )

def p_stair_def(t):
    """STAIR_DEF             : STAIR TAG COLON_SYM STAIR_DESCRIPTION
                             """
    print t[0:]

def p_stair_description(t):
    """STAIR_DESCRIPTION     : LINE_START_SYM DICTONARY STAIR_DESCRIPTION
                             | STAIR_END STAIR_DEF
                             | STAIR_END
                             """
    print t[0:]

def p_error(t):
    print 'Syntax error at "%s"' % t.value if t else 'NULL'
    global current_state
    current_state = NONE

yacc.yacc()

file_input = open("x.staircase","r")
yacc.parse(file_input.read())

下面给出了一个示例输入(“x.staircase”)的作品。但如您所见,所有 USER_DEFINED 始终是

staircase(XXXX XXX XXX):
- a is a       # which need to {'abc' : '23183'}
- a is a
- a is a
EOSC
stair(XXXX XXX XXX):
- a is a
- a is a
EOS
stair(XXXX XXX XXX):
- a is a
- a is a 1
EOS

请(重新)阅读 Ply manual 中关于 Ply 的词法分析器如何识别标记的描述。特别注意订购规则;由于模式变量从长到短排序,t_USER_DEFINED 模式在任何关键字模式(例如 staircase)之前被尝试,所以 none 的关键字将被识别。 (这就是为什么将 t_USER_DEFINED 缩短为单个字符会改变词汇行为的原因。)

有一个很好的线索表明这是问题所在,而不是作业生成:错误消息在遇到作业之前很久就在令牌 staircase 处触发。通过在 p_error 函数中打印 t.typet.value,您会得到另一条线索。 (或者,当然,在尝试解析任何内容之前测试标记器。)

如果您通读到我链接的 Ply 手册部分的末尾,您将找到有关如何使用扫描仪功能和辅助关键字词典处理关键字标记的建议。我强烈建议您使用它作为模型。

另请注意,冒号前需要一个 space 字符:

t_COLON_SYM                 = r' :'

但是您的示例输入在冒号前没有 spaces。