[antlr-interest] Default behavior for string templates when no template is defined for a given production?

Stevenson, Todd (GE Healthcare, consultant) ToddStevenson at ge.com
Thu Mar 19 10:06:32 PDT 2009


What is the default behavior when for string templates when no template
rule is defined for a given production?
 
I have the following grammar and when I run this grammar I get no
StringTemplate back from the parse (parser_return.st == null).  However,
when I tried this grammar with 'rewrite=true' and the associated java
changes, it worked just fine.
 
 
grammar Expr;
 
options {
output=template;
}
 
prog 
    : stat+
    ;
 
stat 
    : expr ';' 
    | ID '=' expr ';' -> template(x={$ID.text},y={$expr.text}) "<x> :=
<y>;"
    | ';'
    ;
 
expr 
    : multExpr (( '+' | '-' ) multExpr)*
    ;
 
 
multExpr
    : atom ( '*' atom)*
    ;
 
atom
    : INT
    | ID
    | '(' expr ')'
    ;
 
    ID : ('a'..'z'|'A'..'Z')+ ;

INT : '0'..'9'+ ;

SEMI : ';' ;

WS : (' ' | '\t' | '\n' | '\r' )+ {$channel=HIDDEN;} ;

 

The associated java code is:

ANTLRInputStream input = new ANTLRInputStream(System.in);
ExprLexer lexer = new ExprLexer(input);
CommonTokenStream tokens = new CommonTokenStream(lexer);
ExprParser parser = new ExprParser(tokens);
ExprParser.prog_return r = parser.prog();
System.out.println(r.st.toString());
 
 

The java code I used with the 'rewrite=true' option:

ANTLRInputStream input = new ANTLRInputStream(System.in);
ExprLexer lexer = new ExprLexer(input);
TokenRewriteStream tokens = new TokenRewriteStream(lexer);
ExprParser parser = new ExprParser(tokens);
ExprParser.prog_return r = parser.prog();
System.out.println(tokens.toString());

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090319/0c9470f9/attachment.html 


More information about the antlr-interest mailing list