[antlr-interest] C# Just starting with Parsers, Trees

Gavin Lambert antlr at mirality.co.nz
Thu May 22 13:06:16 PDT 2008


At 03:12 23/05/2008, Doc In Nuke wrote:
 >One of the rule i added for exemple is the following one:
 >globalfuncdec    :    'function' ^ funcname funcbody ;
 >
 >so for every global function (declaration not starting with 
local)
 >i get a subtree with the funcname and funcbody block.
 >Now what would really be nice is a way to get the funcname as 1
 >token and not more.
 >funcname is the following rule:
 > funcname : NAME ('.' NAME)* (':' NAME)? ;

There's a few ways you can go here.  One is to convert funcname 
into a lexer rule (but this may introduce problems into your 
grammar, if it's not sufficiently unique).  Another is to keep it 
as separate tokens but put it into a subtree, like this:

funcname : n=NAME (n+='.' n+=NAME)* (n+=':' n+=NAME)? -> 
^(FUNCNAME $n+);

I'm not completely sure about this last option (I've never used it 
myself), but I think you can also construct an imaginary token 
around it after the fact.  Something like this:

globalfuncdec : 'function' funcname funcbody -> ^('function' 
FUNCNAME[$funcname.text] funcbody);

I think doing this causes it to lose the line/column information 
though; there is a way to preserve that but I'm not sure exactly 
how in this case.



More information about the antlr-interest mailing list