Lemon Parser-Generator:非终结符不被评估吗?
我尝试学习解析器。因为我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你不想要这样的东西吗:
Do you not want something like: