开发者

Generating simple AST in ANTLR

I'm playing a bit around开发者_运维问答 with ANTLR, and wish to create a function like this:

MOVE x y z pitch roll

That produces the following AST:

 MOVE
   |---x
   |---y
   |---z
   |---pitch
   |---roll

So far I've tried without luck, and I keep getting the AST to have the parameters as siblings, rather than children.

Code so far:

C#:

class Program
{
    const string CRLF = "\r\n";

    static void Main(string[] args)
    {
        string filename = "Script.txt";

        var reader = new StreamReader(filename);
        var input = new ANTLRReaderStream(reader);
        var lexer = new ScorBotScriptLexer(input);
        var tokens = new CommonTokenStream(lexer);
        var parser = new ScorBotScriptParser(tokens);

        var result = parser.program();
        var tree = result.Tree as CommonTree;
        Print(tree, "");

        Console.Read();
    }

    static void Print(CommonTree tree, string indent)
    {
        Console.WriteLine(indent + tree.ToString());

        if (tree.Children != null)
        {
            indent += "\t";

            foreach (var child in tree.Children)
            {
                var childTree = child as CommonTree;

                if (childTree.Text != CRLF)
                {
                    Print(childTree, indent);
                }
            }    
        }            
    }

ANTLR:

grammar ScorBotScript;

options
{
    language     = 'CSharp2';
    output       = AST;
    ASTLabelType = CommonTree;
    backtrack    = true;
    memoize      = true;
}

@parser::namespace { RSD.Scripting }
@lexer::namespace { RSD.Scripting }

program
    : (robotInstruction CRLF)*
    ;

robotInstruction
    : moveCoordinatesInstruction
    ;

/**
 * MOVE X Y Z PITCH ROLL
 */
moveCoordinatesInstruction
    : 'MOVE' x=INT y=INT z=INT pitch=INT roll=INT
    ;

INT : '-'? ( '0'..'9' )*
    ;

COMMENT
    :   '//' ~( CR | LF )* CR? LF { $channel = HIDDEN; }
    ;

WS
    :   ( ' ' | TAB | CR | LF ) { $channel = HIDDEN; }
    ;

ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

STRING
    :  '"' ( ESC_SEQ | ~('\\'|'"') )* '"'
    ;

fragment
ESC_SEQ
    :   '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\')
    ;

fragment TAB 
    : '\t' 
    ;

fragment CR 
    : '\r' 
    ;

fragment LF 
    : '\n' 
    ;

CRLF
    : (CR ? LF) => CR ? LF
    | CR
    ;

parse
    : ID
    | INT
    | COMMENT
    | STRING
    | WS
    ;


I'm a beginner with ANTLR myself, this confused me too.

I think if you want to create a tree from your grammar that has structure, you augment your grammar with hints using the ^ and ! characters. This examples page shows how.

From the linked page:

By default ANTLR creates trees as "sibling lists".

The grammar must be annotated to with tree commands to produce a parser that creates trees in the correct shape (that is, operators at the root, which operands as children). A somewhat more complicated expression parser can be seen here and downloaded in tar form here. Note that grammar terminals which should be at the root of a sub-tree are annotated with ^.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜