[antlr-interest] Implement a for loop in the tree grammar

Marius Meier mariusmeier at hispeed.ch
Wed Sep 21 12:38:45 PDT 2011


Hello everybody

 

I am trying to implement a for-loop in my Math parser but it doesn't really
work.

I have some problem with seeking the input to the right destination in the
tree which

was generated by the parser. Following tree grammar describes the for-loop:

 

 

enterForLoop

                @init { int mark = 0; }

                :               ^('for' assignStat c=condExpr incStat[true]
block) {

 
input.Seek($c.start.TokenStartIndex);

                                                object condResult =
condExpr();

                                                

                                                /*ForLoopSymbol symbol = new
ForLoopSymbol("forLoop", _CurrentScope);

 
_CurrentScope.DefineSymbol(symbol);

                                                _CurrentScope = symbol;

                                                incStat(true); // Doesn't
work this way

                                                input.Release(mark);*/

                                }

                ;

                                

exitForLoop

                :               /*'for' {

                                                _CurrentScope =
_CurrentScope.EnclosingScope;

                                }*/

                ;

 

                

condExpr returns [bool value]

                :               ^('<' ID INT) { 

                                                Symbol symbol =
globalTable.ResolveSymbol($ID.text);

                                                

                                                if(symbol != null) {

                                                                if(symbol is
VariableSymbol) {

 
if(((VariableSymbol)symbol).SymType.Equals("int")) {

 
$value=(int)((VariableSymbol)symbol).Value < int.Parse($INT.text); 

 
} else if(((VariableSymbol)symbol).SymType.Equals("float")) {

 
$value=(float)((VariableSymbol)symbol).Value < int.Parse($INT.text); 

 
}

                                                                }

                                                } else {

 
Console.WriteLine("Undefined variable: " + $ID.text + " (" + $ID.line + ":"
+ $ID.pos + ")");

                                        } 

                                }

                |              ^('>' ID INT) {

                                                Symbol symbol =
globalTable.ResolveSymbol($ID.text);

                                                

                                                if(symbol != null) {

                                                                if(symbol is
VariableSymbol) {

 
if(((VariableSymbol)symbol).SymType.Equals("int")) {

 
$value=(int)((VariableSymbol)symbol).Value > int.Parse($INT.text); 

 
} else if(((VariableSymbol)symbol).SymType.Equals("float")) {

 
$value=(float)((VariableSymbol)symbol).Value > int.Parse($INT.text); 

 
} 

                                                                }

                                                } else {

 
Console.WriteLine("Undefined variable: " + $ID.text + " (" + $ID.line + ":"
+ $ID.pos + ")");

                                        } 

                                }

                ;

 

I tried to jump to the conditional expression of the for loop. But I don't
get there in

the tree node stream.

Can anybody help me to get it right? I tried a few times with different
options but

I don't get anywhere. I'm just beginning to learn ANTLR.

 

Greetings



More information about the antlr-interest mailing list