Click here to Skip to main content
15,891,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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 :


C#
 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
Posted
Comments
Tomas Takac 14-Sep-16 10:20am    
Show what you have tried. Otherwise there is nothing to talk about.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900