I create this code:
import java_cup.runtime.*;
terminal MAS,MENOS,POR,DIV,AP,CP,MINUS;
terminal String NUMERO,IDENT;
non terminal A;
precedence left MAS,MENOS;
precedence left POR,DIV;
precedence left AP,CP;
precedence left MINUS;
A ::= A:a1 MAS {:System.out.print("+ ");:} A:a2 {:System.out.print(a1+""+a2+" ");RESULT = "";:}
| A:a1 MENOS {:System.out.print("- ");:} A:a2 {:System.out.print(a1+""+a2+" ");RESULT = "";:}
| A:a1 POR {:System.out.print("* ");:} A:a2 {:System.out.print(a1+""+a2+" ");RESULT = "";:}
| A:a1 DIV {:System.out.print("/ ");:} A:a2 {:System.out.print(a1+""+a2+" ");RESULT = "";:}
| AP {:System.out.print("");:} A:a1 CP {:System.out.print("");RESULT = "";:}
| NUMERO:n1 {:RESULT = n1+" ";:}
| IDENT:i1 {:RESULT = i1+" ";:}
| MENOS A:a1
%prec MINUS;
The solution to this expression: alfa + beta * gamma +77 is + + alfa * beta gamma 77 but the program doesnt show the correct solution,anyone can help me?
You are printing the intermediate value of operators when you should be returning it as RESULT
as you do with identifiers and numbers. You should only print the string when you have finished parsing the entire expression. (You might use a unit production for that.)