在 Java 中从 cup 创建 AST 时需要帮助

发布于 2024-10-22 20:47:09 字数 5729 浏览 2 评论 0原文

我需要在我的代码中创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小巷里的女流氓 2024-10-29 20:47:09
/* 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); 
        :} 
/* Non-terminals */
non terminal        extension;
non terminal Integer    expr;

Here Integer is the type of expr, you should change it to Node;

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

This is were you can build the syntax tree:

    expr:e1 AND expr:e2 
      {: 
        RESULT = new Node("", "AND", e1, e2); 
        :} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文