如何更新和打印“Rational”? javacc 中的令牌?

发布于 2024-08-23 05:20:35 字数 158 浏览 5 评论 0原文

我添加了新的标记 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫嚣ゝ 2024-08-30 05:20:35

您需要进一步分割 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文