2

I'm writing some kind of calculator using Jflex and CUP, I was able to make my grammar for infix, postfix and prefix notation as seen below, they works well if I just use each grammar and don't combine them.

The problem now is how can I combine them without getting a reduce/shift error, I've been doing this a few days now and I can't really figure it out. The main problem is when F --> NUMBER and E --> NUMBER they will have a reduce/shift error, I also tried adding more rules to try to mitigate the reduce/shift error but I'm not getting it correctly. Take note also that I was able to combine any combination of two but not three of them.

One approach I did is to try to merge my prefix and postfix to my infix by changing E to T and creating a new Rule T --> E|Number, but I still

It would be very helpful if someone could give me an explanation on how I can try to combine them.

Here is my Grammar

 /* INFIX */
   E --> E - T | E + T| T
   T --> T * F | T / F | F
   F --> NUMBER


/*PRE fix */
   E --> + E E | - E E | / E E | * E E | NUMBER


/*POST fix*/
   E --> E E + | E E -| E E * |E E / |NUMBER
  • Just to confirm, you're using java CUP from here, correct? http://www2.cs.tum.edu/projects/cup/ – kabb Mar 25 '14 at 20:06
  • 1
    yes that's what I'm using, I'm just showing my grammar, instead of the cup file, and I was able to get the correct grammar now, how can I answer my own question :) – rey_padilla Mar 26 '14 at 01:40
  • You can just post an answer like you would to any other question, and then immediately accept it as correct. – kabb Mar 26 '14 at 14:53

1 Answers1

2

Here's the grammar

program -> infix | prefix | postfix

infix -> E

E -> E - T | E + T | T | T * F | T / F | F

F -> (E) | n

prefix -> Epre

Epre -> + Fpre Fpre | - Fpre Fpre | * Fpre Fpre | / Fpre Fpre

Fpre -> Epre | n

postfix -> Epost

Epost -> Fpost Fpost + | Fpost Fpost - | Fpost Fpost * | Fpost Fpost /

Fpost -> Epost | n