I'm trying to create a Syntax Analyzer in Java (using CUP) that could recognise this piece of code:
if ¿b? then
~ a = 2;
~ if ¿b && c? then
~ ~ a = 3;
else
~ a = 4;
My productions used by the "if" statement are these that follow:
Instr ::= ...
| IF CONOP Exp:e CONCL THEN CondInstrList:l
...
;
...
CondInstrList ::= CondInstrList CondInstr
| /*empty*/
;
...
CondInstr ::= CONTROLD Instr
| CONTROLD CondInstr
;
where Instr stands for instruction/statement, CondInstrList stands for Conditional Instruction List and CONTROLD stands for Control Dash (~). (CONOP and CONCL mean Condition Open/Close)
The problem is that with that grammar, the generated AST is as follows:
if
|-condition b
|-condInstrListT
|---asig a = 2
|---if
|---condition b and c
|---condInstrListT
| |---asig a = 2
|---condInstrListF
|---asig a = 4
and so, the "else" part is associated with the inner "if".
I just don't know how to write a grammar that respects the way I want my language to be.
Any help is appreciated.
I can give more detail if needed.