[antlr-interest] V3: Clarification parser vs tree grammer, and a question

Gyula László gyula.laszlo at profund.hu
Thu Dec 28 09:09:17 PST 2006


Hello,

On 2006.12.28., at 4:43, Mark Mandel wrote:

>
> The way I'm seeing it at the moment is that the job of the Parser is
> to build the AST tree.  It defines the tokens, explains where the root
> of the tree should be, and defines the structure of the children.
>

IMHO, the goal of separating the tree parser from the parser is the goal
to make it reusable. The AST (should) reflect the real nature of the  
language,
and not how you parse it. That said, imagine the following:

	You parse a simple Java class with your parser, that generates an AST.
	You create a tree parser skeleton, that walks the tree and does  
nothing else.

You create some tree parsers:

	the first walks the tree, and builds the symbol tables
	and grammar structure (loads imports, resolves global symbols,  
inheritance, etc).

	the second checks the typing from the previously loaded symbols.

	the third creates a wrapper bean class with StringTemplate.

	the third creates Ruby wrapper code with StringTemplate.

	...
	...

As you can see, the file only needs to be parsed once, and every  
other step can depend on
the previous one, using the same (easy to parse) AST. The rule codes  
are shorter this way, and
every aspect sits right in his/her own grammar. That's a good thing  
(tm).

The other use of AST is to use other people's WORKING parser, and  
then do your job  on the
easy to parse AST which is already created by the existing parser

>
> If my treeParser requires other Java objects to evaluate the tree it
> has been passed, is there some way to add that to the Constructor of
> the generated TreeParser, OR is the best way simply to make a
> setMyLib(myLib) function via the @members section, and call that prior
> to evaluating the tree?
>
>

IMHO the easiest way is to use a custom superclass for the parser  
(which subclasses ANTLRTreeParser)
and define everything not strictly grammar-related there. (I like to  
keep my grammars clean and use
delegates to do the job.


> Hope that was clear,
>
> Mark
>
> -- 
> E: mark.mandel at gmail.com
> W: www.compoundtheory.com

cheers,

Gyula László

email:gyula.laszlo AT profund.hu
http://profund.hu





More information about the antlr-interest mailing list