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

Doc In Nuke docinnuke at gmail.com
Thu May 22 08:42:56 PDT 2008


Hi,

Here is what I did:
Took the lua grammar
generated a parser and lexer with it in csharp. I also added the 
output=AST option.
This without modifying the rules. This generated 2 classes

used the following code:
            ANTLRFileStream afs = new ANTLRFileStream("LuaFile/queue.lua");
            LuaLexer lexer = new LuaLexer(afs);
            CommonTokenStream cts = new CommonTokenStream();

            cts.TokenSource = lexer;

            LuaParser parser = new LuaParser(cts);

            LuaParser.chunk_return r = parser.chunk();

            CommonTree t = (CommonTree)r.Tree;

            CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);

now,t is flat if I only do that. This is why I started trying stuff with 
the tree syntax. And since I am not english I do not really understand 
all the documentation available :(

So the more general question would be, how do I get a real tree from 
that, either by modifying the grammar, or do I need to create some code 
too ?

I would love to know how I get the $funcname.Text stuff in my code :)

Best Regards,


Haralambi Haralambiev wrote:
> Hello,
>
> I was wondering - what is the purpose of collecting all the nodes into 
> one?
> What difficulties do you encounter in the current state of the tree - 
> maybe this could be solved in an eaiser way?
>
> For example, if you just need the name of the function, you could 
> always use $funcname.text and this will get everything in one place.
>
> Best Regards,
> Hari
>
> On 5/22/08, *Doc In Nuke* <docinnuke at gmail.com 
> <mailto:docinnuke at gmail.com>> wrote:
>
>     Hi @ all
>
>     First of all let me apologize for the english mistakes you will
>     surely find in this message.
>
>     So, I just started working with ANTLR a few days ago.
>
>     I was able to generate a parser and lexer for the lua grammar
>     provided on the site.
>     Now I also was able to create nodes / sub trees using the ^
>     parameter in the grammar.
>     I also modified the grammar a bit to try to generate what i really
>     wanted in the end.
>
>     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)? ;
>
>     in the same way having just a childnode named funcbody with all
>     the stuff related to funcbody under it would be great.
>
>     So i would have something like that:
>                                   function
>                                  /            \
>               funcname/value         funcbody
>                                                /           \
>                                            stuff_1  ... stuff_n
>
>     So I was wondering if one can do such a thing, and if yes, how ?
>
>     Best Regards,
>
>



More information about the antlr-interest mailing list