0

What is the fastest (smaller code) way to get grammar tree ?

I am trying to get grammar tree. I've generated C# code based on my simple grammar:

grammar MyPascal;
options
{
    language=CSharp3;
    output=AST;
}

operator: (block | ID);
block   : BEGIN operator* END;
BEGIN   :'begin';
END     :'end';
ID      :('a'..'z')+;
WS      :( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;};

When i'am using ANTLR works for simple input text like:

input.txt:
begin
  abs
  qwe
  begin
    begin
    end
  end
end

i get nice picture of grammar tree.

Now i'am wonder if there any simple way to get tree structure of my "program" from C# without writing 1000s lines of code.

Here i'am trying to get grammar tree:

class Program
{
    static void Main(string[] args)
    {
        MyPascalLexer lex = new MyPascalLexer(new ANTLRFileStream(@"M:\input.txt"));
        CommonTokenStream tokens = new CommonTokenStream(lex);
        MyPascalParser g = new MyMyPascalParser(tokens);
        MyPascalParser.myprogram_return X = g.myprogram();                                       
        Console.WriteLine(X.Tree);  // Writes: nill
        Console.WriteLine(X.Start); // Writes: [@0,0:4='begin',<4>,1:0]
        Console.WriteLine(X.Stop);  // Writes: [@35,57:57='end',<19>,12:2]
    }
}
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
Astronavigator
  • 2,021
  • 2
  • 24
  • 45

1 Answers1

1

You'll have to "tell" ANTLR to build an AST, opposed to just a flat stream of tokens (simple parse tree).

See this SO Q&A that shows how to do this in C#.

Also, you should not use:

ID : ('a'..'z')*;

i.e.: let a lexer rule match an empty string, this might (or even will?) get you in trouble (it always matches!). You'll want to let it match at least one character:

ID : ('a'..'z')+;
Community
  • 1
  • 1
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • You're right. I have cut some before asking question for compact size. I've fixed now these errors. – Astronavigator Apr 08 '11 at 16:22
  • :) I found answer to my question in this link. So i accept your answer. As in an example I've added 2 lines: 1: using Antlr.Runtime.Tree; 2: CommonTree Tree = (CommonTree)Q.Tree; And it works :) – Astronavigator Apr 08 '11 at 17:01
  • @Astronavigator, you're welcome, and good to hear you found the solution to your problem. – Bart Kiers Apr 08 '11 at 17:18