在 Python 中使用 PLY 的两个单词标记
我正在编写一个编译器作为实验室练习的一部分,并选择使用 PLY 在 Python 中完成它。我花了一些时间试图解决这个特殊问题,但和我的实验室助手一样,也陷入了死胡同。
在我必须编写的语言中,声明符用两个单词“was a”指定。例如:
x 是一个数字,x 变成了 5。
等于
int x; x = 5;
当用 PLY 解析时,我将 'was a' 作为保留字
reserved = {
...
'was a' : 'DECLARATOR',
...
}
但是当我用 PLY 词法分析器解析时,它将 'was' 和 'a' 视为单独的标记
我如何解析 < code> 是 a 作为“DECLARATOR”类型的标记,而没有 PLY 词法分析器将其拆分?
如果有任何不清楚的地方,请告诉我,我会尽力回答任何问题,
谢谢,
皮特
I am writing a compiler as part of a lab excercise and have chosen to do it in Python using PLY. I have spent some time trying to work this particular problem out and have reached a dead end as have my lab helpers.
In the language I have to write, declarators are specified with two words "was a". For example:
x was a number and x became 5.
is equal to
int x; x = 5;
When parsing with PLY, I have put 'was a' as a reserved word
reserved = {
...
'was a' : 'DECLARATOR',
...
}
But when I parse with the PLY lexer, it treats 'was' and 'a' as separate tokens
How can I parse was a
as a token of type 'DECLARATOR' without the PLY lexer splitting it up?
If any of that is unclear let me know and I will try and answer any questions as best I can
Thanks,
Pete
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您永远不必在令牌中使用两个单词。相反,将它们分成两个单独的标记,并确保您的语言强制一个标记始终跟在另一个标记后面。
例如,实现
'was' : 'DECLARATOR_WAS'
令牌和'a' : 'DECLARATOR_A'
令牌。You should never have to use two words in a token. Instead split them into two seperate tokens and ensure your language enforces that one is always followed by the other.
e.g. implement a
'was' : 'DECLARATOR_WAS'
token and an'a' : 'DECLARATOR_A'
token.