我已经看过这个问题,即使问题标题似乎相同;它不能回答我的问题,至少不能以我能理解的任何方式回答。
解析数学
这是我正在解析的内容:
PI -> 3.14.
Number area(Number radius) -> PI * radius^2.
这就是我希望我的 AST 树的外观,减去所有无用的根节点。
它应该是什么样子 http://vertigrated.com/images/How%20I%20want%20the%20tree%20to%20look.png
以下是我希望我的语法的相关片段:
term : '(' expression ')'
| number -> ^(NUMBER number)
| (function_invocation)=> function_invocation
| ATOM
| ID
;
power : term ('^' term)* -> ^(POWER term (term)* ) ;
unary : ('+'! | '-'^)* power ;
multiply : unary ('*' unary)* -> ^(MULTIPLY unary (unary)* ) ;
divide : multiply ('/' multiply)* -> ^(DIVIDE multiply (multiply)* );
modulo : divide ('%' divide)* -> ^(MODULO divide (divide)*) ;
subtract : modulo ('-' modulo)* -> ^(SUBTRACT modulo (modulo)* ) ;
add : subtract ('+' subtract)* -> ^(ADDITION subtract (subtract)*) ;
relation : add (('=' | '!=' | '<' | '<=' | '>=' | '>') add)* ;
expression : relation (and_or relation)*
| string
| container_access
;
and_or : '&' | '|' ;
优先级
我仍然想保持precedence
如下图所示,但如果可能的话,想消除无用的节点。
来源:Number a(x) -> 0 - 1 + 2 * 3 / 4 % 5 ^ 6.
以下是我要消除的节点:
我希望优先级树看起来如何 http://vertigrated.com/images/example%202%20desired%20result.png
基本上我想消除那些没有直接在它们下面有分支的节点中的任何一个到二元期权。