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

Jim Idle jimi at temporal-wave.com
Fri May 23 09:36:04 PDT 2008


On Fri, 2008-05-23 at 17:29 +0300, Doc In Nuke wrote:

> Hi,
> 
> Thanks for the information, however i am truly a beginner, what do I 
> need to get this to work in ANTLR:
> 
> funcname : n=NAME (n+='.' n+=NAME)* (n+=':' n+=NAME)? -> ^(FUNCNAME $n+);
> 
> I added a token at the start of the grammar named FUNCNAME but i still 
> have grammar errors :(
> Do I need to declare n in a special way ?
> 
> (if you have a link to a documentation on the site I ll gladly take it)


Your variables can only accumulate the same type, so you can't
accumulate ':' and '.' and NAME. However, it is usually more useful to
create nodes from the elements like ':' and '.' as this gives you a tree
that helps resolve them (with symbol tables and so on. Of course if you
are just going to 'print' them separately then you might not need that,
in which case you probably don't need the n+= anyway.

If you want a flat token sequence in the tree, but within a FUNC node
then do this:


funcname : ns=nameSequence -> ^(FUNCNAME $ns) ;

nameSequence : NAME ('.' NAME)* (':' NAME)?  ;

If you want a tree (I usually do this, but as i say it is more useful
when you are resolving name space sequences and so on, then use:

nameSequence : NAME ('.'^ NAME)* (':'^ NAME)?  ;

And construct the tree grammar rules appropriately

funcname: ^(FUNCNAME names) ;

names : ^(':' nameSeq NAME)
      | nameSeq
      ;

nameSeq : ^('.' nameSeq nameSeq)
        | NAME
        ;

(Check that, I just typed it straight in to the email ;-).

Jim
  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080523/a009678d/attachment.html 


More information about the antlr-interest mailing list