编译器设计:如何区分“op_Minus number”和“op_Minus number”和“负数”?
如何区分标记序列“op_Minus”“number”和简单的负数?
How do you distinguish between the token sequence "op_Minus" "number" and simple a negative number?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为它依赖于上下文,并且依赖于解析源代码时遇到的操作数的数量。
根据您选择/遇到的语法类型,在处理减法时,您要么会遇到这样的情况:
或者如果您的语言使用前缀表示法,您最终会得到这样的结果:
在这两种情况下,您都应该能够如果您正在处理减法或负数,则可以从前一个标记(在中缀表示法的情况下)或从前瞻到下一个标记/接下来的两个标记)进行推导,因为后者只有一个操作数。
I would think it's context dependent and dependent on the number of operands you encounter while parsing the source code.
Depending on the type of syntax you choose/encounter, you either have something like this when you're dealing with subtraction:
or if your language uses prefix notation, you'll end up with something like this:
In both cases you should be able to either deduce from the previous token (in the case of infix notation) or from the lookahead to the next token/next two tokens) if you're dealing with a subtraction or a negative number, given that the latter would only have a single operand.