[antlr-interest] ANTLR 3 output=AST for tree parser?

Mark Bednarczyk mark at slytechs.com
Mon Feb 26 06:14:19 PST 2007


Ok, we're on the same page. I also said that for tree grammars you can only
have "template" output type and of course no tree rewrites with ANTLR
syntax. 

On the other hand, you do have complete access to the AST in action section
for each rule and I have rewritten trees that way but by accessing AST nodes
from the matched AST input. I pull out a symtable, I'm currently working on
"typecasting" walker that rewrites and AST and inserts specific instructions
to change one type to another, etc...

Example from my tree walker:

modifiers returns [List<String> modifiers]
@init { $modifiers = new ArrayList<String>(); }
	:	^(MODIFIERS (m=modifier {$modifiers.add($m.ret);} )* ) 
	;

The rewrite syntax, currently only support "template" output type, so you
can only put template statements after the "->" symbol in tree grammars:

header
	:	^(HEADER_DEF m=modifiers i=id headerBody) {
			System.out.println("We got a header " + $i.id);
			$cu::header.setName($i.id);
			$cu::header.getModifiers().addAll($m.modifiers);
			
			SymHeader h = $cu::sym.getHeader($i.id,
$m.modifiers);
			h.setCu($cu::header);
		}
		->	template(a={$cu::header.toString()}) " HEADER(<a>)"
	;

So in essence, I rely on tree walker rules to do the matching and get me in
the right place of the AST, the action code to do any processing and
rewrites and then rewrite "->" to produce the templates in the last phase of
the processing.

Cheers,
mark...  

> -----Original Message-----
> From: Kay Roepke [mailto:kroepke at classdump.org] 
> Sent: Monday, February 26, 2007 6:38 AM
> To: mark at slytechs.com
> Cc: 'Werner Lehmann'; 'ANTLR Interest'; FranklinChen at cmu.edu
> Subject: Re: [antlr-interest] ANTLR 3 output=AST for tree parser?
> 
> 
> On Feb 26, 2007, at 12:27 PM, Mark Bednarczyk wrote:
> 
> > Hi Kay,
> > 	I'm jumping in here a bit late, but tree rewritting is 
> implemented in 
> > b6. I'm using it in my grammar heavily.
> 
> Yes, for parsers spitting out ASTs rewriting (from a token 
> stream to a tree) is supported.
> But it is not supported for *tree* parsers, i.e. having a 
> tree parser that modifies trees by using ANTLR's rewrite syntax.
> That is what I meant, sorry if I wasn't clear enough in that.
> 
> > I also have the 1.5 grammar compiled and working as well.
> 
> Cool, then it shouldn't be a problem for OP to get what he wants :)
> 
> cheers,
> 
> -k
> --
> Kay Röpke
> http://classdump.org/
> 
> 
> 
> 




More information about the antlr-interest mailing list