Welcome!<br><br>I am new to ANTLR and trying to create a simple mini-C interpreter. I wanted to interpret all the language actions in the parser action section. I'm trying not to process the AST etc. I have managed to implement conditional statements, but still have one problem with loops. I'm trying to rewind the input stream like in the following example (I know that the loop counter is static etc. - I wanted only to test rewinding input stream):<br>
<br>(for full grammar see: miniC.g)<br><br>scope CScope {<br> String name;<br> List symbols;<br>}<br><br>@members {<br> int loopCounter = 5;<br>}<br><br>loop<br>@init {<br> int start = -1;<br>}<br>@after {<br> if (--loopCounter > 0) {<br>
System.out.println("rewinding: " + loopCounter);<br> input.rewind(start);<br> }<br>}<br> : 'for' val=ID 'in' set=ID<br> {start = input.mark(); System.out.println("start: " + start);}<br>
block<br> {if (--loopCounter > 0) {System.out.println("rewinding: " + loopCounter); input.rewind(start);}}<br> ;<br><br>block<br>scope CScope;<br>@init {<br> // initialize a scope for this code block<br>
$CScope::symbols = new ArrayList();<br> $CScope::name = "level "+$CScope.size();<br>}<br>}<br> : '{' block_content* '}' //decl* stat+ loop* cond* '}'<br> ;<br><br>block_content <br>
: decl<br> | stat<br> | loop<br> | '{' block_content* '}'<br> ;<br><br>decl: 'int' ID {if (evaluate) { $CScope::symbols.add($ID.text); System.out.println("Decalred variable: " + $ID.text);}} ';'<br>
;<br><br>stat: ID '=' INT ';'<br> {<br> if ( !isDefined($ID.text) ) {<br> System.err.println("undefined variable level "+<br> $CScope.size()+": "+$ID.text);<br>
}<br> }<br>
<br>Example file:<br>void g()<br>{<br> for a in set<br> {<br> int x;<br> x = 10;<br> }<br><br>I'll be gratefull for any suggestions.<br><br>Regards,<br>Lukasz<br>