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开发者_StackOverflow社区 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?
Do you not want something like:
expr(A) ::= VALUE(B). {A = B; echo "got a value:".B.PHP_EOL;}
精彩评论