我正在尝试创建一个以我的脚本作为输入的解释器。我在编写正则表达式时遇到一些问题。定义的标记之一是将所有字符串视为标记。
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]
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())
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
答案 0 :(得分:1)
请(重新)阅读有关Ply的词法分析器如何识别Ply manual中的标记的描述。请特别注意订购规则;由于模式变量的排序时间最长至最短,因此t_USER_DEFINED
模式会在任何关键字模式(例如staircase
)之前进行尝试,因此不会识别任何关键字。 (这就是为什么将t_USER_DEFINED
缩短为单个字符会改变词法行为的原因。)
有一个很好的线索,表明这是问题所在,而不是分配产生的结果:错误消息在遇到分配之前很久就在令牌staircase
处触发。通过在t.type
函数中同时打印t.value
和p_error
,您将获得另一个线索。 (或者,当然,通过在尝试解析任何内容之前测试令牌处理程序。)
如果通读我链接的Ply手册的本节末尾,您将找到有关如何使用扫描器功能和辅助关键字字典处理关键字标记的建议。我强烈建议您使用它作为模型。
还请注意,您需要在冒号之前加一个空格:
t_COLON_SYM = r' :'
但是您的样本输入中冒号前没有空格。