解析器语法协议.g
grammar protocol;
options {
language = Java;
output = AST;
ASTLabelType=CommonTree;
}
tokens{
TRANSITIONS;
PAIR;
}
@header {
package com.javadude.antlr3.x.tutorial;
}
@lexer::header {
package com.javadude.antlr3.x.tutorial;
}
parse
: transitions EOF!
{
CommonTree root = $transitions.tree;
int count = root.getChildCount();
Tree child1 = root.getChild(0);
Tree child2 = root.getChild(1);
Tree child3 = root.getChild(2);
Tree child4 = root.getChild(3);
System.out.println("root=" + root.getToken().getText() + " has " + count + " child nodes:");
System.out.println(" - child1=" + child1.toStringTree());
System.out.println(" - child2=" + child2.toStringTree());
System.out.println(" - child3=" + child3.toStringTree());
System.out.println(" - child4=" + child4.toStringTree());
}
;
transitions
: 'transitions' '=' INT pair+ ';' -> ^(TRANSITIONS INT pair+)
;
pair
: '(' INT ',' INT ')' -> ^(PAIR INT INT)
;
INT
: ('0'..'9')+;
WHITESPACE
: ('\t' | ' ' | '\r' | '\n' | '\u000C')+ {$channel = HIDDEN;};
树语法协议Walker.g
tree grammar protocolWalker;
options {
language = Java;
tokenVocab = protocol;
ASTLabelType = CommonTree;
}
@header {
package com.javadude.antlr3.x.tutorial;
}
transitions
: ^(TRANSITIONS INT pair+)
{
System.out.println("transitions=" + $INT.text);
}
;
pair
: ^(PAIR a=INT b=INT)
{
System.out.println("pair=" + $a.text + ", " + $b.text);
}
;
JAVA TEST RIG
协议test.java
package com.javadude.antlr3.x.tutorial;
import org.antlr.runtime.*;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.CommonTreeNodeStream;
public class Protocoltest {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
//create input stream from standard input
ANTLRInputStream input = new ANTLRInputStream(System.in);
//create a lexer attached to that input stream
protocolLexer lexer = new protocolLexer(input);
//create a stream of tokens pulled from the lexer
CommonTokenStream tokens = new CommonTokenStream(lexer);
//create a parser attached to teh token stream
protocolParser parser = new protocolParser(tokens);
//invoke the program rule in get return value
protocolParser.parse_return r =parser.parse();
CommonTree t = (CommonTree)r.getTree();
//output the extracted tree to the console
System.out.println("\nAST is: " + t.toStringTree());
//walk resulting tree; create treenode stream first
CommonTreeNodeStream nodes = new CommonTreeNodeStream(t);
//AST nodes have payloads that point into token stream
nodes.setTokenStream(tokens);
//create a tree walker attached to the nodes stream
protocolWalker walker = new protocolWalker(nodes);
//invoke the start symbol, rule parse
walker.transitions();
}
}
输入
transitions = 3(5,0) (5,1) (5,2);
输出
root=TRANSITIONS has 4 child nodes:
- child1=3
- child2=(PAIR 5 0)
- child3=(PAIR 5 1)
- child4=(PAIR 5 2)
AST is: (TRANSITIONS 3 (PAIR 5 0) (PAIR 5 1) (PAIR 5 2))
pair=5, 0
pair=5, 1
pair=5, 2
transitions=3
问题
您可以在上面看到,在解析器语法(protocol.g)中,我可以将转换根的所有孩子存储为 child1、child2、child3 和 child4。另外,我已经打印了这些。在树语法中,我如何存储这些并可以对它们执行操作?谢谢