再会。刚开始用PEG。我熟悉wp:peg 和其他一些理论,但仍然无法理解它在内部是如何工作的。
我的任务是解析表达式
if($a=='qwerty' && $b>2 || $c<=5)
print 'something';
endif;
is 语句的主体只包含打印操作符,没有其他内容,但条件复杂。
我的想法:
/*!* Calculator
Int: /[0-9]+/
Var: /\$[a-z]+/
tThen: /then{1}/
tIf: /if{1}/
tElse: /else{1}/
tEndif: /endif{1}/
block: /.+/
condEq: Var '==' ( Int | Var ) *
condStatement: '(' condEq ')'
function condEq( &$result, $sub ) {
if( eval($sub['text'].';') ) {
$result['text'] = 'true';
}
else {
$result['text'] = 'false';
}
}
ifStatement: tIf condStatement
Expr: ifStatement
*/
但我相信这个任务有更好的解决方案:) 你能帮我吗?谢谢。