[antlr-interest] Re: parsing single java expressions

jsrs701 jsrs701 at yahoo.com
Thu Jun 13 10:03:14 PDT 2002

I'll do a little expanding on it, since I use this trick with one of 
my languages, too.

Basically, what you want is a second major entry point into your 
language parser.  In ANTLR, an entry point is simply a parser rule 
that is NOT referenced by any other rule.  That's it.

So, like Sinan showed earlier, just make an "orphan rule" that calls 
your main expression parser, and then have your native code call that 

In terms of the example Java parser on antlr.org, the main entry 
point is the compilationUnit rule.  The top rule for the expression 
parser is "expression", so you would just add:

    :    expression

and call "expression_entry" in your native code.  You must add 
another rule, because "expression" is referenced elsewhere.

Make sense?

--- In antlr-interest at y..., Sinan <sinan.karasu at b...> wrote:
> Vette98Man wrote:
> > 
> I have done this with other languages and antlr and it works.
> What you have to do is , lets say you have a grammar
> -------------
> module:
>       preamble
>      ( statement )+
>       end
>       EOF;
> statement: ....;
> ---------------
> Now you add an orphan rule
> single_statement:
>      statement EOF;
> now you can use the orphan rule ( once you attach the string stream 
> your parser/lexer )
> to return to you an AST.
> You can even create a separate parser, as long as you create a 
> outside of the original parser, so that you can keep track of your
> variables
> and pass it to the constructor of the parser.
> If this is too cryptic, I can expand on it next week. Right now I 
> have the time....
> sinan


Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 

More information about the antlr-interest mailing list