コードで 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を取得するためにカップで何を変更しますか? 誰でも助けてくれますか?