开发者

ANTLR: What is the fastest way to get grammar tree?

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 wa开发者_开发技巧y 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]
    }
}


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')+;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜