javaabstract-syntax-treecup

cup_v10k AST does not recognize Boolean


I am writing a compiler for a simple educational Java-like language. I am using cup_v10k.jar to generate the parser. I am using the AST CUP extension, to generate the abstract syntax tree.

Along with other terminal symbols, in my parser.cup file i have

terminal Integer INT_VALUE;
terminal Character CHAR_VALUE;
terminal Boolean BOOL_VALUE;

I also have a non terminal symbol Constant

The production for Constant looks like this:

Constant ::= INT_VALUE
             |
             CHAR_VALUE
             |
             BOOL_VALUE
             ;

CUP generates the production in the parser_astbuild.cup file like this:

Constant ::= INT_VALUE:I1 {: RESULT=new ConstantDerived1(I1); RESULT.setLine(I1left); :}
             |
             CHAR_VALUE:C1 {: RESULT=new ConstantDerived2(C1); RESULT.setLine(C1left); :}
             |
             BOOL_VALUE:B1 {: RESULT=new ConstantDerived3(B1); RESULT.setLine(B1left); :}
             ;

The generated classes for INT_VALUE and CHAR_VALUE look OK, but the problem lies in the BOOL_VALUE class - CUP somehow does not recognize that Boolean is a built-in type, and assumes it is a SyntaxNode. The generated class looks like this:

public class ConstantDerived3 extends Constant {

    private Boolean B1;

    public ConstantDerived3 (Boolean B1) {
        this.B1=B1;
        if(B1!=null) B1.setParent(this);
    }

    public Boolean getB1() {
        return B1;
    }

    public void setB1(Boolean B1) {
        this.B1=B1;
    }

    public void accept(Visitor visitor) {
        visitor.visit(this);
    }

    public void childrenAccept(Visitor visitor) {
        if(B1!=null) B1.accept(visitor);
    }

    public void traverseTopDown(Visitor visitor) {
        accept(visitor);
        if(B1!=null) B1.traverseTopDown(visitor);
    }

    public void traverseBottomUp(Visitor visitor) {
        if(B1!=null) B1.traverseBottomUp(visitor);
        accept(visitor);
    }

    public String toString(String tab) {
        StringBuffer buffer=new StringBuffer();
        buffer.append(tab);
        buffer.append("ConstantDerived3(\n");

        if(B1!=null)
            buffer.append(B1.toString("  "+tab));
        else
            buffer.append(tab+"  null");
        buffer.append("\n");

        buffer.append(tab);
        buffer.append(") [ConstantDerived3]");
        return buffer.toString();
    }
}

This does not compile because Boolean is not a SyntaxNode, and does not have setParent and other SyntaxNode methods.

My question is, am I doing something wrong, or is this a bug in the AST CUP extension?


Solution

  • This is a bug in the AST CUP extension. The extension is running this code to check if the type is a basic type, or a SyntaxNode:

    public static boolean isBasicType(String stack_type) {
        if (stack_type.equals("Object")) {
            return true;
        } else if (stack_type.equals("String")) {
            return true;
        } else if (stack_type.equals("Integer")) {
            return true;
        } else if (stack_type.equals("Character")) {
            return true;
        } else if (stack_type.equals("int")) {
            return true;
        } else {
            return stack_type.equals("char");
        }
    }
    

    This does not recognize Boolean, and then assumes that Boolean is a SyntaxNode type.