Unary and binary minus in the analysis tree

I am creating a parsing tree that will contain expressions similar to

3 - 4 * 8

or

8 * -5

or

-(10 * 1)

I need a way to distinguish between unary and binary minus. The way my grammar goes is now called a binary minus, but I'm thinking of changing this and adding a flag variable that contains the last variable.

Ex: if it is 5 - 6

The flag holds 5, and if it sees a minus, and the flag is a number, skip unary and go to binary.

However, I don’t know exactly how to implement this in C ++

Any help would be greatly appreciated.

thank

+5
source share
1 answer

- Recursive Descent. , , , :

 E -->  | E "+" E
        | E "-" E
        | "-" E
        | E "*" E
        | E "/" E
        | E "^" E
        | "(" E ")"
        | v
+6

All Articles