2011-03-16 97 views
1

我需要在我的代码中创建AST。我创建了类Node和AST像助手类。需要帮助从Java中创建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?任何人都可以帮忙吗?

回答

0
/* Non-terminals */ 
non terminal  extension; 
non terminal Integer expr; 

这里Integer是expr的类型,您应该将其更改为Node;

expr:e1 AND expr:e2 
     {: 
     //System.out.println("AND"); 
     RESULT = 1; 
     :} 

这是你可以建立语法树:

expr:e1 AND expr:e2 
     {: 
     RESULT = new Node("", "AND", e1, e2); 
     :}