0

I am using jflex and byaccj to build an AST. I am unable to resolve the error and I have used type casting but the error persists

for the following rule in the grammar:

program : CLASS Program '{' field_decl '}'      {
                program1 $$ = new program1($1.sval, $2.sval, $4.obj);
        }
        ;

and i have declared this in my .java file

abstract class program extends ASTnode{
}

class program1 extends program {
  private classexp ce = null;
  private String id="Program";
  private ArrayList<field_decl> fdecl = null;
  public program1(classexp ce,String id, ArrayList<field_decl> fdecl) {
    super();
    this.ce = ce;           
    this.id = id;
    this.fdecl = fdecl;
  }
}

The error is:

{program1 yyval= new program1(val_peek(4).sval,val_peek(3).sval,val_peek(1).obj);}
               ^
required: classexp,String,ArrayList<field_decl>
found: String,String,Object
reason: actual argument String cannot be converted to classexp by method invocation conversion
1 error
rici
  • 234,347
  • 28
  • 237
  • 341
user1683894
  • 405
  • 1
  • 6
  • 20
  • Please use the Markdown facilities for formatting code. To see what I did, edit the question and take a look. (Most important: indent code snippets by 4 spaces.) I'm not sure if I got the `^` in the error message in the correct spot, although the error is clear enough without the caret. – rici Sep 29 '14 at 21:19

1 Answers1

1

Your declaration of program1 says that the constructor is:

public program1(classexp ce,String id, ArrayList<field_decl> fdecl)

which takes a classexp, a String and an ArrayList<field_decl> (as indicated in the required: line in the error message). You are providing it with:

new program1(val_peek(4).sval,val_peek(3).sval,val_peek(1).obj)

which is to say, a String, another String, and an Object (as indicated in the found: line in the error message). In order to make the provided arguments fit the required parameters, it would be necessary to convert the first String to a classexp, and this is not possible (as indicated by the reason: line in the error message). It will also be necessary to convert the third argument, an Object into an ArrayList<field_decl>, which is probably also not possible, but one error in a constructor is sufficient to reject the call.

rici
  • 234,347
  • 28
  • 237
  • 341