我正在尝试编写一种基本的编程语言。我正在使用RPLY进行词法分析。在为变量赋值时,我已经写了4个令牌:
self.lexer.add('INT', r'd+')
self.lexer.add('STRING', r'"[^"]*"')
self.lexer.add('ASSIGN', r'\=')
self.lexer.add('IDENT', r'[a-zA-Z_][a-zA-Z\d_]*')
我的解析器生成器接受这些令牌
self.pg = ParserGenerator(
# A list of all token names accepted by the parser.
['NUMBER', 'PRINT', 'OPEN_PAREN', 'CLOSE_PAREN',
'SEMI_COLON', 'SUM', 'SUB', 'MUL', 'INT', 'STRING', 'IDENT', 'ASSIGN'],
precedence=[
('left', ['SUM','SUB']),
('left', ['STRING', 'IDENT', 'ASSIGN'])
]
)
这是创建变量的结果
@self.pg.production('identifier : STRING IDENT ASSIGN expression')
@self.pg.production('identifier : INT IDENT ASSIGN expression')
def identifier(p):
return p
当我尝试运行它时,出现此错误:
Traceback (most recent call last):
File "main.py", line 21, in <module>
parser.parse(tokens).eval()
File "/xxx/xxx/anaconda3/lib/python3.7/site-packages/rply/parser.py", line 60, in parse
self.error_handler(lookahead)
File "/home/xxx/xxx/Project/parser.py", line 53, in error_handle
raise ValueError(token)
ValueError: Token('IDENT', 'string')
输入文件:
string oki = "ssds"
print(oki);
它将字符串解析为IDENT,而应将字符串解析为STRING令牌。设置了优先级,但也许无法识别?