[antlr-interest] is there any way make loop staments without a tree?

Randall R Schulz rschulz at sonic.net
Mon Jun 16 08:17:44 PDT 2008


On Monday 16 June 2008 07:46, Christian Meléndez wrote:
> hi there, i just want to know if ther is possible to make a loop
> statement (for and while) without a tree, if it is a way, how??,
> almost i already finish the grammar only needs this. Or tell me a way
> how to do a simple for or while instruccion, i need to do something
> like this
>
> if you put
>
> x = 0;
> while (x < 5){
>  print(x);
>  x++;
> }
>
> the output is
> 01234
>
> i just need how to make the loop, how to visit de condition and the
> insturccions again


It may be my imagination, but it seems that we're seeing this sort of 
question a lot lately, and I think it betrays a misunderstanding of 
what a tool like ANTLR can or should be able to do.

ANTLR is a tool for writing _recognizers_ for formally specified 
grammars, not a complete tool for writing compilers, translators or 
interpreters.

In other words, trying to encode the procedural semantics of your 
language using only ANTLR and furthermore doing this in a single pass 
without producing an intermediate representation of the program that 
was decoded during the parsing phase is really not a good idea, to the 
extent it's even possible.

For sufficiently simple translation or transliteration tasks, a 
single-pass ANTLR (or yacc or Javacc, etc.) parser that builds no 
intermediate representation of the input might be feasible. But to 
actually implement some kind of execution model or anything that 
requires information to propagate from one part of the input to another 
(e.g., a language with declarations) really is not something that can 
be done without building an internal representation of the input.

Rather than try to avoid building an AST or some other representation, 
you should just acknowledge that this extra step will make the rest of 
your job a lot easier.


Randall Schulz


More information about the antlr-interest mailing list