如何更新和打印“Rational”? javacc 中的令牌?
我添加了新的标记 RATIONAL,它可以在我的 JavaCC 解析器上识别有理数。如何更新程序的输出部分以打印有理数的数值?
例如,('2/5') 值 = 0.4,('8/2') 值 = 4.0,('4/0') 值 = 无穷大。
如果有人能帮助我,我将不胜感激。谢谢。
I have added the new token RATIONAL that recognises rational numbers on my JavaCC parser. How can I update the output part of the program to print the numeric value of the rational number?
For example ('2/5') value = 0.4, ('8/2') value = 4.0, ('4/0') value = infinity.
I will be grateful if anyone could help me. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要进一步分割 RATIONAL 代币的图像,以便两个数字都可用于计算。
从您的问题中不清楚括号和引号是否是定义的语法的一部分。如果是,您可以轻松地将它们定义为有理表达式的分隔符,并定义数字和斜杠的标记。如果与数字可能出现的其他位置产生歧义,请记住词汇状态是您的朋友。
如果有理表达式只是由两个用斜杠分隔的数字组成,那么我建议构建一个 AST 并在解析完成后对其进行评估。您也可以在解析过程中对其进行评估,但这的难度取决于语法的其余部分。
You need to further split the image of your RATIONAL token in order to have both numbers available for computation.
It's not clear from your question whether the parentheses and quotes are part of the defined syntax. If they are, you can easily define them as delimiters for a rational expression and define tokens for the numbers and the slash. In case of ambiguity with other places where numbers can appear, remember that lexical states are your friends.
If a rational expression is simply made of two numbers separated by a slash, then I'd recommend building an AST and evaluate it after the parsing is complete. You can alternatively evaluate it during the parsing, but the difficulty of this depends on the rest of your grammar.