0

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?

Georgi Hristozov
  • 3,899
  • 2
  • 17
  • 23
Julio Gomez
  • 33
  • 1
  • 5

1 Answers1

0

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.)

rici
  • 234,347
  • 28
  • 237
  • 341
  • Thanks now it works, one more question, its posible to debug cup to see the list of the production thats follow ? – Julio Gomez Dec 14 '18 at 16:04
  • @ivan: i don't know much about CUP's debugging features, sorry. But in any case, that's a completely different question which should be asked as such. – rici Dec 14 '18 at 16:57