[antlr-interest] Default behavior for string templates when no template is defined for a given production?
Terence Parr
parrt at cs.usfca.edu
Thu Mar 19 10:30:34 PDT 2009
On Mar 19, 2009, at 10:06 AM, Stevenson, Todd (GE Healthcare,
consultant) wrote:
> What is the default behavior when for string templates when no
> template rule is defined for a given production?
You currently get no output/template for that production.
rewrite=true means that it will alter the associated input as you give
the templates rather than build up a big one and return it from the
start symbol.
Ter
>
> 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());
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
More information about the antlr-interest
mailing list