1

I need to know how to make a grammar for expressions to create a parser and ast. I have four levels of precedence:

1. ** !

2. * / % &

3. + -  ^ |

4. <= >= < >

I have made this:

Exps -> RExp4

RExp4 -> OpEq Exps | RExp3

RExp3 -> OpAd Exps | RExp2

RExp2 -> OpMul Exps | RExp1

RExp1 -> OpExp Exps | Exp

Exp -> Val | '('Exps')'

OpExp ->  ** | !

OpMul -> * | / | % | &

OpAd -> + | - | ^

OpEq -> <= | >= | < | >

Val -> Id | Int

I'm not sure if this will work because when I make the tree in a paper I do not get the correct form for expressions like:

x*7+7

Basically because I make the sum first. My gramma have to be LL(1) and recursive by the right because my compiler will be top-down.

Thanks for help and sorry for my english

Edit

Sorry, I was wrong, I mean parser top-down.The problem I see on paper is the next. I have the next expression "7*5+5" for example and the order that my BNF follow is the next:

Take 7 with Exp in Exps, that follow to Val

Go to RExp and continue to RExp3.

Take '*' and then return to Exps.

The tree that I see on paper is the next:

    *  
  /   \  
3      +  
     /   \  
    7     5

The tree that I should have is the next:

     +
   /   \
  *    5
 / \
7   5 
Aion
  • 11
  • 3
  • You say `btdown`. I don't see how a parser can be a `bottom down parser`. I know of `top-down` and `bottom-up`. Was it one of these two? e.g. [The difference between top-down parsing and bottom-up parsing](https://qntm.org/top) – Guy Coder Apr 01 '17 at 12:31
  • 1
    Can you show more details on why you get it wrong on paper or explain where you went wrong. At a quick glance I don't see a problem with your BNF. I agree that doing them on paper first is a good way to start when first learning. – Guy Coder Apr 01 '17 at 12:36
  • Of interest: [Operator-precedence parser](https://en.wikipedia.org/wiki/Operator-precedence_parser) – Guy Coder Apr 01 '17 at 12:37
  • I added an answer to copy the tree. – Aion Apr 02 '17 at 08:08
  • It would be a lot easier, indeed trivially obvious, if you used the same non-terminal names as everybody else: `term, factor, primary, ...`. And if yo took a good look at any one of a large number of existing expression grammars, starting with that for Algo-60 ... which is now 57 years old. – user207421 Apr 02 '17 at 09:59
  • I need to use this names because its a way to talk with another part of this work. I know that is very strange, but it doesn't depends me. – Aion Apr 02 '17 at 10:31
  • The first rule `Exps -> Exp RExp4` should not have `Exp`, look at the example given in [Operator-precedence parser](https://en.wikipedia.org/wiki/Operator-precedence_parser#Precedence_climbing_method). For the example expression `7*5+5` to do the first token `7` the rules used should be `Exps -> RExp4`, `RExp4 -> RExp3`, `RExp3 -> RExp2`, `RExp2 -> RExp1`, `RExp1 -> Exp`, `Exp -> Val`, `Val -> Int`. To do this I removed `Exp` from the `Exps` rule, and for the `RExp1` rule changed `epsilon` to `Exp`. – Guy Coder Apr 02 '17 at 11:16
  • Ok, I change the rules, but I don't see the correct tree yet. – Aion Apr 02 '17 at 14:18

0 Answers0