Lemon Parser-Generator:非终结符不被评估吗?

发布于 2024-09-28 10:10:25 字数 1248 浏览 4 评论 0原文

我尝试学习解析器。因为我的 C 技能相当低,所以我在 google 上搜索了 PHP Lemon 了解解析器生成器。不管怎样,这里的代码对于普通的柠檬朋友来说应该也是可读的。

与往常一样,在处理解析问题时,我首先尝试生成一个简单的计算器。

所以我的第一步很简单:

start(A)   ::= expr(B). {echo "======RESULT:".A.":".B.":=========".PHP_EOL;}

解析第一个测试:

include "mysimple.php"; //include the generated Parser
$P = new ParseParser(); //create a Parser object
$P->Parse(ParseParser::VALUE,"13"); // here is the simple test, just understand the Number 13, pls
$P->Parse(0,0); //input is finished, parse!
echo "finished. yeah!".PHP_EOL;

...结果:

======RESULT:13:=========
finished. yeah!

所以,一切都按预期进行。现在我们尝试准备一个最终允许我们处理操作的步骤,即表达式

start ::= expr(B).  {echo "======RESULT:".B.":=========".PHP_EOL;}
expr  ::= VALUE(B). {echo "got a value:".B.PHP_EOL;}

当我现在运行相同的测试时,我期望看到相同的输出,加上一行表示got值:13。但我只是明白:

got a value:13
======RESULT::=========
finished. yeah!

嗯,发生了什么事?为什么结果行是空的?显然,expr 的计算结果为 VALUE '13'。 Lemon不关心评价吗?我必须自己做吗?但是,如果我在这一点上什么也没得到呢?

I try to learn parsers. Because my C skills are pretty low, I googled a PHP Lemon to learn sth about Parser Generators. Anyway, the code here should be readable for normal lemon friends, too.

As always when handling parsing questions, I start with trying to produce a simple calculator first.

So my first step is simply this:

start(A)   ::= expr(B). {echo "======RESULT:".A.":".B.":=========".PHP_EOL;}

what parses the first test:

include "mysimple.php"; //include the generated Parser
$P = new ParseParser(); //create a Parser object
$P->Parse(ParseParser::VALUE,"13"); // here is the simple test, just understand the Number 13, pls
$P->Parse(0,0); //input is finished, parse!
echo "finished. yeah!".PHP_EOL;

...to the result of:

======RESULT:13:=========
finished. yeah!

So, everything as expected. Now we try to prepare a step that finally will allow us to handle operations, the expression:

start ::= expr(B).  {echo "======RESULT:".B.":=========".PHP_EOL;}
expr  ::= VALUE(B). {echo "got a value:".B.PHP_EOL;}

When I run the same test now, I expect to see the same output, plus one line saying got a value: 13. But I just get this:

got a value:13
======RESULT::=========
finished. yeah!

Well, what happened? Why is the result line empty? Obviously expr evaluates to a VALUE of `13'. Does Lemon not care about evaluating? Do I have to do that myself somehow? But how, if I get nothing in this point?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

风月客 2024-10-05 10:10:25

你不想要这样的东西吗:

expr(A) ::= VALUE(B). {A = B; echo "got a value:".B.PHP_EOL;}

Do you not want something like:

expr(A) ::= VALUE(B). {A = B; echo "got a value:".B.PHP_EOL;}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文