[antlr-interest] ANTLR 3 Automatic Label Generation

Carleton Coffrin Carleton at Coffrin.com
Tue Sep 11 14:54:23 PDT 2007


In ANTLR 3 it seems local variables are generated automatically for tokens
and production return values.  For example, the follow production,

typeRef returns [ASTType ty]
    : n=identifier
    | tyg=parametrizedType
    | st='Enumeration' LBRACKET lid=listID end=RBRACKET
;

yields the following local variables,

Token st = null;
Token end = null;

ASTId n = null;
ASTTypeGeneric tyg = null;
ASTListId lid = null;


Consider a production where the return Values are of different types but
they all share a super class.  For example,

statements [ASTStatementList sList] @init {sList = new ASTStatementList();}
    : (s=assign {sList .add(s);}
    | s=print {sList .add(s);}
    | s=cond {sList .add(s);}
    | s=while {sList .add(s);}
    )+
;

In this case the assign, print, cond, and while productions return different
types but all of the types are subclasses of ASTStatement.  Because the
return values set to "s" do not have the same type a local variable for s is
not automatically generated and the user must add an explicit variable to
the @init block.  For example,

@init {sList = new ASTStatementList();}  --> @init {sList = new
ASTStatementList(); ASTStatement s;}


Is my understanding of how this works correct?

-Carleton
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070911/20799964/attachment.html 


More information about the antlr-interest mailing list