[antlr-interest] Every grammar an output grammar

Gerald Rosenberg gerald at certiv.net
Wed Apr 2 16:31:51 PDT 2008


Nice!  Couple of possible issues, though:

First, the proposed annotation syntax appears to conflict (at least 
in syntax presentation) with the heterogeneous AST production 
syntax.  Even though the example shown only uses lhs annotation, 
annotations in the rewrite production would likely be as or more 
useful.  On the rhs, the conflict with heteroAST would be direct.

Second, any chance to not further overload the "<" and ">" 
symbols?  I realize there is a certain parallel to the template 
definition syntax, but these are really grammar-based selectors and 
values, so bringing in template definition syntax just feels a bit 
odd.  Perhaps a simplified anchor/parameter syntax?

rule#ruleref : (DOC_COMMENT)? (RULE_REF#name | TOKEN_REF#name) ;

sends "name" values to "ruleref" template, skipping any output for 
DOC_COMMENT -- leaving no attribute produced by default might be more 
desirable if only go keep the grammar as lean and literate as possible.

or

rule#ruleref : (DOC_COMMENT)? (ref=RULE_REF | ref=TOKEN_REF)  -> 
^(RULE<RuleNode> $ref#name ^(SRC<SourceNode> $rule#content)) ;

My $0.02 ...

Best,
Gerald

At 02:50 PM 4/2/2008, Loring Craymer wrote:
>One of the problems that we hoped that ANTLR 3 would solve was that 
>of text output--generating text from ANTLR 2 was an unpleasant 
>experience.  StringTemplate helps considerably, and one of the 
>inchoate ideas that Ter was grappling with at the ANTLR 3 cabal was 
>that of an "output grammar" that would provide a mapping between an 
>ANTLR grammar and a template group.  This idea never quite gelled; 
>the ANTLR 3 "template" output options was a first attempt to go in 
>this direction.
>
>Last week's "template generation" discussion came just as I was 
>finishing a pretty printer for Yggdrasil and started me thinking 
>about the problem again.  I finally came up an approach that seems 
>to provide a solution and have implemented that in Yggdrasil.  After 
>trying other syntaxes, I ended up with an annotation that "mirrors" 
>the template syntax:  a <foo> suffix assigns a token's text to a 
>"foo" attribute, while <<bar>> references a bar template (fills a 
>slot that holds slots; i. e:  a template).  This approach could 
>easily be incorporated into the baseline ANTLR 3; the whole idea of 
>output grammars seems to be a big step forward.



More information about the antlr-interest mailing list