在 Java 中从 cup 创建 AST 时需要帮助
我需要在我的代码中创建 AST。我创建了 Node 类和 AST 类,就像辅助类一样。
public class Node {
private String value;
private String type;
private Boolean visited;
private Node leftChild, rightChild;
public Node(){
value="";
type="";
visited=false;
}
public Node(String value, String type, Boolean visited, Node leftChild, Node rightChild){
this.value=value;
this.type=type;
this.visited=visited;
this.leftChild=leftChild;
this.rightChild=rightChild;
}
public Node(String value, String type, Boolean visited){
this.value=value;
this.type=type;
this.visited=visited;
}
public Node(String value, String type,Boolean visited, Node leftChild){
this.value=value;
this.type=type;
this.visited=visited;
this.leftChild=leftChild;
this.rightChild=null;
}
public Node(String value, String type, Node leftChild){
this.value=value;
this.type=type;
this.visited=false;
this.leftChild=leftChild;
this.rightChild=null;
}
public void SetValue(String value){this.value=value;}
public String GetValue(){return value;}
public void SetType(String type){this.type=type;}
public String GetType(){return type;}
public void SetVisited(Boolean visited){this.visited=visited;}
public Boolean GetVisited(){return visited;}
public Node GetLeftChild(){return leftChild;}
public void SetLeftChild(Node leftChild){this.leftChild=leftChild;}
public void SetRightChild(Node rightChild){this.rightChild=rightChild;}
public Node GetRightChild(){return rightChild;}
public String EvaluateToString(){
String temp="";
if(leftChild!=null)
temp+=leftChild.EvaluateToString();
temp+=value;
if(rightChild!=null)
temp+=rightChild.EvaluateToString();
return temp;
}
}
public class AST {
private Node root=null;
private Stack<Node> stack=null;
public AST(){
root=null;
stack=new Stack<Node>();
}
public Node GetRoot(){return root;}
public void SetRoot(Node root){this.root=root;}
public String GetExpression(){
return root.EvaluateToString();
}
}
这是我的杯子
import java_cup.runtime.*;
parser code {:
public boolean result = true;
/***************************************************************************
* following are redefined methods for error reporting on message text change
/***************************************************************************
public void report_fatal_error(String message, Object info) throws java.lang.Exception {
done_parsing();
System.out.println("report_fatal_error");
report_error();
}
public void syntax_error(Symbol cur_token) {
System.out.println("syntax_error");
report_error();
}
public void unrecovered_syntax_error(Symbol cur_token) throws java.lang.Exception {
System.out.println("unrecovered_syntax_error");
report_fatal_error("Fatalna greska, parsiranje se ne moze nastaviti", cur_token);
}
public void report_error(){
System.out.println("report_error");
result = false;
}
:}
init with {: result = true; :};
/* Terminals (tokens returned by the scanner). */
terminal AND, OR, NOT;
terminal LPAREN, RPAREN;
terminal ITEM;
terminal OPEN, CLOSE, MON, MOFF, TIMEOUT, ESERR, BAE, I, O, BUS, EXT, PUSHB;
terminal VAL, OK, BUS_BR_L, BUS_BR_R, SH_CRT_L, SH_CRT_R, BUS_ALL, EXT_ALL, NO_TIMEOUT, NO_ES_ERR, IBUS_OK, CFG_OK, SYNTAX;
terminal OUT;
/* Non-terminals */
non terminal extension;
non terminal Integer expr;
/* Precedences */
precedence left AND, OR;
/* The grammar */
expr ::=
|
expr:e1 AND expr:e2
{:
//System.out.println("AND");
RESULT = 1;
:}
|
expr:e1 OR expr:e2
{:
//System.out.println("OR");
RESULT = 2;
:}
|
NOT expr:e1
{:
//System.out.println("NOT");
RESULT = 3;
:}
|
LPAREN expr:e RPAREN
{:
//System.out.println("()");
RESULT = 4;
:}
|
ITEM extension:e1
{:
//System.out.println("ITEM.");
RESULT = 5;
:}
|
error
{:
System.out.println("error");
parser.report_error();
RESULT = 0;
:}
;
extension ::=
OPEN
|
MON
|
CLOSE
|
MOFF
|
TIMEOUT
|
ESERR
|
BAE
|
I
|
O
|
BUS
|
EXT
|
PUSHB
|
VAL
|
OK
|
BUS_BR_L
|
BUS_BR_R
|
SH_CRT_L
|
SH_CRT_R
|
BUS_ALL
|
EXT_ALL
|
NO_TIMEOUT
|
NO_ES_ERR
|
IBUS_OK
|
CFG_OK
|
SYNTAX
|
OUT
;
需要对杯子进行哪些更改才能获得 AST?有人可以帮忙吗?
I need to create AST in my code. I created classes Node and AST like helper class.
public class Node {
private String value;
private String type;
private Boolean visited;
private Node leftChild, rightChild;
public Node(){
value="";
type="";
visited=false;
}
public Node(String value, String type, Boolean visited, Node leftChild, Node rightChild){
this.value=value;
this.type=type;
this.visited=visited;
this.leftChild=leftChild;
this.rightChild=rightChild;
}
public Node(String value, String type, Boolean visited){
this.value=value;
this.type=type;
this.visited=visited;
}
public Node(String value, String type,Boolean visited, Node leftChild){
this.value=value;
this.type=type;
this.visited=visited;
this.leftChild=leftChild;
this.rightChild=null;
}
public Node(String value, String type, Node leftChild){
this.value=value;
this.type=type;
this.visited=false;
this.leftChild=leftChild;
this.rightChild=null;
}
public void SetValue(String value){this.value=value;}
public String GetValue(){return value;}
public void SetType(String type){this.type=type;}
public String GetType(){return type;}
public void SetVisited(Boolean visited){this.visited=visited;}
public Boolean GetVisited(){return visited;}
public Node GetLeftChild(){return leftChild;}
public void SetLeftChild(Node leftChild){this.leftChild=leftChild;}
public void SetRightChild(Node rightChild){this.rightChild=rightChild;}
public Node GetRightChild(){return rightChild;}
public String EvaluateToString(){
String temp="";
if(leftChild!=null)
temp+=leftChild.EvaluateToString();
temp+=value;
if(rightChild!=null)
temp+=rightChild.EvaluateToString();
return temp;
}
}
public class AST {
private Node root=null;
private Stack<Node> stack=null;
public AST(){
root=null;
stack=new Stack<Node>();
}
public Node GetRoot(){return root;}
public void SetRoot(Node root){this.root=root;}
public String GetExpression(){
return root.EvaluateToString();
}
}
This is my cup
import java_cup.runtime.*;
parser code {:
public boolean result = true;
/***************************************************************************
* following are redefined methods for error reporting on message text change
/***************************************************************************
public void report_fatal_error(String message, Object info) throws java.lang.Exception {
done_parsing();
System.out.println("report_fatal_error");
report_error();
}
public void syntax_error(Symbol cur_token) {
System.out.println("syntax_error");
report_error();
}
public void unrecovered_syntax_error(Symbol cur_token) throws java.lang.Exception {
System.out.println("unrecovered_syntax_error");
report_fatal_error("Fatalna greska, parsiranje se ne moze nastaviti", cur_token);
}
public void report_error(){
System.out.println("report_error");
result = false;
}
:}
init with {: result = true; :};
/* Terminals (tokens returned by the scanner). */
terminal AND, OR, NOT;
terminal LPAREN, RPAREN;
terminal ITEM;
terminal OPEN, CLOSE, MON, MOFF, TIMEOUT, ESERR, BAE, I, O, BUS, EXT, PUSHB;
terminal VAL, OK, BUS_BR_L, BUS_BR_R, SH_CRT_L, SH_CRT_R, BUS_ALL, EXT_ALL, NO_TIMEOUT, NO_ES_ERR, IBUS_OK, CFG_OK, SYNTAX;
terminal OUT;
/* Non-terminals */
non terminal extension;
non terminal Integer expr;
/* Precedences */
precedence left AND, OR;
/* The grammar */
expr ::=
|
expr:e1 AND expr:e2
{:
//System.out.println("AND");
RESULT = 1;
:}
|
expr:e1 OR expr:e2
{:
//System.out.println("OR");
RESULT = 2;
:}
|
NOT expr:e1
{:
//System.out.println("NOT");
RESULT = 3;
:}
|
LPAREN expr:e RPAREN
{:
//System.out.println("()");
RESULT = 4;
:}
|
ITEM extension:e1
{:
//System.out.println("ITEM.");
RESULT = 5;
:}
|
error
{:
System.out.println("error");
parser.report_error();
RESULT = 0;
:}
;
extension ::=
OPEN
|
MON
|
CLOSE
|
MOFF
|
TIMEOUT
|
ESERR
|
BAE
|
I
|
O
|
BUS
|
EXT
|
PUSHB
|
VAL
|
OK
|
BUS_BR_L
|
BUS_BR_R
|
SH_CRT_L
|
SH_CRT_R
|
BUS_ALL
|
EXT_ALL
|
NO_TIMEOUT
|
NO_ES_ERR
|
IBUS_OK
|
CFG_OK
|
SYNTAX
|
OUT
;
What to change in cup to get AST ? Can anybody help please ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里Integer是expr的类型,应该改为Node;
这是您可以构建语法树的地方:
Here Integer is the type of expr, you should change it to Node;
This is were you can build the syntax tree: