如何在我的语法中优先考虑操作(+ * - /)? [英] How to do priority of operations (+ * - /) in my grammars?
本文介绍了如何在我的语法中优先考虑操作(+ * - /)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 antlr 4 定义我自己的语法我想要构建树 true 根据操作优先级(+ * - /)....
我在执行操作优先级(* +)工作正常...
我尝试编辑它到添加操作优先级( - /)但我失败了:(
语法 for 运营优先级(+ *) :
println:PRINTLN表达式SEMICOLON {System。 out .println($ expression。 value );};
表达式返回[对象 值]:
t1 = factor {$ value =( int )$t1. value ;}
(PLUS t2 = factor {$ value =( int )$ value +( int )$ t2。 value ;})*;
系数返回[对象 值]:t1 = term {$ value = ( int )$ t1。 value ;}
(MULT t2 = term {$ value = ( int )$ value *( int )$ t2。值跨度>;})*;
术语返回[对象 值]:
NUMBER {$ value = Integer.parseInt($ NUMBER.text);}
| ID {$ value = symbolTable。 get ($ value = $ ID.text);}
| PAR_OPEN表达式{$ value = $ expression。 value ;} PAR_CLOSE
;
MULT:' *';
PLUS:' +';
减:' - ';
DIV:' /';
如何向他们添加操作优先级( - /)?
我尝试了什么:
我所有的尝试都失败了:(也许我有错误的玩家
解决方案
expression。 value );};
表达式返回[对象 值]:
t1 = factor {
value =( int )
t1。值;}
(加上t2 =因子{
I define my own grammars using antlr 4 and I want to build tree true According to Priority of Operations (+ * - /) ....
I find sample on do Priority of Operations (* +) it work fine ...
I try to edit it to add the Priority of Operations (- /) but I failed :(
the grammars for Priority of Operations (+ *) is :
println:PRINTLN expression SEMICOLON {System.out.println($expression.value);};
expression returns [Object value]:
t1=factor {$value=(int)$t1.value;}
(PLUS t2=factor{$value=(int)$value+(int)$t2.value;})*;
factor returns [Object value]: t1=term {$value=(int)$t1.value;}
(MULT t2=term{$value=(int)$value*(int)$t2.value;})*;
term returns [Object value]:
NUMBER {$value=Integer.parseInt($NUMBER.text);}
| ID {$value=symbolTable.get($value=$ID.text);}
| PAR_OPEN expression {$value=$expression.value;} PAR_CLOSE
;
MULT :'*';
PLUS :'+';
MINUS:'-';
DIV:'/' ;
How I can add to them the Priority of Operations (- /) ?
What I have tried:
all my try failed :( maybe I have missunderstund the greammers
解决方案
expression.value);}; expression returns [Object value]: t1=factor {
value=(int)
t1.value;} (PLUS t2=factor{
这篇关于如何在我的语法中优先考虑操作(+ * - /)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文