语法分析
协议
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;};
树语法
protocolWalker.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测试台
协议测试
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。另外,我已经打印了这些。
在树语法中,如何存储它们并可以对它们执行操作?
谢谢
最佳答案
我将实例化Java类(将创建Java对象),例如,树中的第一个数字将确定将创建多少个对象,然后,PAIR 5 0将创建带有2个参数(5,0)的对象,PAIR 5 1将创建带有2个参数(5,1)的第二对象,而PAIR 5 2将创建带有2个参数(5,2)的第三对象。
这是创建过渡并向其添加对的简单方法,并且只需对protocolWalker.g
进行少量更改即可。不过,首先,这是我将使用的虚拟Transitions
和Pair
类:
Transitions.java
import java.util.ArrayList;
public class Transitions {
private ArrayList<Pair> pairs = new ArrayList<Pair>();
public void addPair(Pair pair){
System.out.println(String.format("Added pair %s to transitions", pair));
pairs.add(pair);
}
@Override
public String toString() {
return "Pairs: " + pairs;
}
}
Pair.java
public class Pair {
private int a;
private int b;
public Pair(int a, int b){
this.a = a;
this.b = b;
}
@Override
public String toString() {
return String.format("(%d, %d)", a, b);
}
}
这是修改后的
protocolWalker.g
。protocolWalker.g(已修改)
tree grammar protocolWalker;
options {
language = Java;
tokenVocab = protocol;
ASTLabelType = CommonTree;
}
@header {
package com.javadude.antlr3.x.tutorial;
import java.util.List;
import java.util.ArrayList;
}
@members {
//stores all the transitions objects as they get processed
private ArrayList<Transitions> allTransitions = new ArrayList<Transitions>();
//returns all the transitions
public List<Transitions> getAllTransitions() {
return allTransitions;
}
}
transitions
@init {
//create a Transitions object when the rule is hit
Transitions transitions = new Transitions();
//store it to be accessed later.
allTransitions.add(transitions);
}
: ^(TRANSITIONS INT transitions_pair[transitions]+) //pass the object to transitions_pair for each PAIR encountered
{
System.out.println("transitions=" + $INT.text);
}
;
transitions_pair[Transitions transitions]
: ^(PAIR a=INT b=INT)
{
System.out.println("pair=" + $a.text + ", " + $b.text);
//make a call to the Transitions object that was passed to this rule.
transitions.addPair(new Pair($a.int, $b.int));
}
;
(我将
pair
重命名为transitions_pair
,因为该规则现在与构建转换有关。)transitions
规则调用transitions_pair
,同时传递一个新的Transitions
对象。 transitions_pair
将新的Pair
对象添加到接收到的Transitions
对象。可以使用
[ArgType argname,...]
方法编写树解析器和令牌解析器中的规则以接受对象。在这种情况下,可以更轻松地访问子PAIR
树。我对
Protocoltest.java
添加了一个小的更改以打印出存储的过渡: ...
//invoke the start symbol, rule parse
walker.transitions();
//get the stored transitions and print them out.
List<Transitions> transitions = walker.getAllTransitions();
System.out.println(transitions);
...
这是步行者的新输出:
pair=5, 0
Added pair (5, 0) to transitions
pair=5, 1
Added pair (5, 1) to transitions
pair=5, 2
Added pair (5, 2) to transitions
transitions=3
[Pairs: [(5, 0), (5, 1), (5, 2)]]
以下是我所做的主要更改的摘要:
添加了一种从walker存储和返回过渡的方法。
添加了在规则
Transitions
中创建transitions
对象的代码。添加了将对象传递给
transitions_pair
的代码。在测试器中添加了代码,以从步行者检索过渡并将其打印出来。
我认为一旦实现自己的
Transitions
类,您将一事无成。关于java - 如何处理树语法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13140443/