Change Object type in Java CUP

819 Views Asked by At

I am creating a parser using CUP together with JFLex to create the scanner.

I was following this link Parse tree generation with Java CUP but I got stuck because I do not know how to change the type to Node because by default the type is Object.

I want the CUP case statements to look like the following

case 16: 
{
  Node RESULT =null; ... }

and not:

case 16: 
{
  Object RESULT =null; ...}
2

There are 2 best solutions below

0
On BEST ANSWER

You can specify the type of the terminal / non terminal at their respective sections:

terminal [TYPE] TERM [, TERM]*;
non terminal [TYPE] NONTERM [, NONTERM]*;

The generated code of:

terminal Foo A,B,C;
non terminal BinaryExpression E0,E1;
non terminal BinaryOperator OP0,OP1;
/*etc*/

will be something like this:

 /*. . . . . . . . . . . . . . . . . . . .*/
case 63: // E0 ::= E0 OP0 E1 
{
  BinaryExpression RESULT =null;
  BinaryExpression e0 = (BinaryExpression)((java_cup.runtime.Symbol) CUP$/* ... */.value;
  BinaryOperator op0 =  (BinaryOperator)((java_cup.runtime.Symbol) CUP$C/* ... */.value;
  BinaryExpression e1 = (BinaryExpression)((java_cup.runtime.Symbol) CUP$/* ... */.value;
  RESULT = new BinaryExpression(e0,op0,e2); 
  CUP$/*...*/$result = /*...*/
}
return CUP$ConstructorAST$result;
1
On

Don't you just need to cast to Node? as below?

case 16: 
{
  Node RESULT = (Node) null; ... }