[antlr-interest] Who can tell me what's the mean of # in the .g file of parser?
Bryan Ewbank
ewbank at gmail.com
Tue Oct 24 13:21:49 PDT 2006
Blasted mailer defaults :-( ... ... ...
On 10/24/06, Yu Tao <yutao at td-tech.com> wrote:
> I can not find these grammer meaning in the manual doc, who can tell me which
> this means?
--- --- ---
Definition of The # Forms
--- --- ---
It means, "a tree node with....". The forms seen are:
#[ ... ] -- create a single node with the given type and text
#( ... ) -- create a tree with the first argument as root and all
following arguments as children
>>> NOTE THAT THE ARGUMENTS CAN BE LISTS <<<
#foo -- reference the (possibly created) tree associated with the
(sub)production named "foo"
## -- an alias for the (possibly created) tree associated with the
current production
--- --- ---
Some Examples
--- --- ---
> expressionList
> : expression (COMMA! expression)*
> { #expressionList = #(#[ELIST,"ELIST"], #expressionList); }
> ;
The last line means "create a tree with root ELIST/"ELIST", and use whatever
has been generated for #expressionList as the children.
--- --- ---
> expressionList
> : expression (COMMA! expression)*
> { ## = #(#[ELIST,"ELIST"], ##); }
> ;
This means exactly the same thing, but is more easily understood and less error
prone if you change the name of the production.
--- --- ---
> superClassClause!
> : ( "extends" id:identifier )?
> { ## = #(#[EXTENDS_CLAUSE,"EXTENDS_CLAUSE"],id); }
> ;
Here, ## (the tree for superClassClaus) is defined to be a simple AST with
EXTEND_CLAUSE as the root and the tree (list) returned by the production named
"id" as it's child or children.
--- --- ---
Hope this helps,
- Bryan Ewbank
More information about the antlr-interest
mailing list