简体   繁体   中英

Cast a non-terminal to java_cup.runtime.Symbol for empty case in grammar

I am writing a compiler using JFlex and CUP. I am trying to parse the empty state in my .cup file. To simplify things, consider this grammar:

terminal    Integer            INT_LIT;
nonterminal List<Statement>    stmtlist;
nonterminal Statement          stmt;

start with stmtlist;

stmtlist    ::= stmtlist:sl stmt:s      {: sl.add(s); RESULT = sl; :}
            |                           {: RESULT = new LinkedList<Statement>(); :}
            ;

stmt        ::= INT_LIT:i               {: RESULT = Statement.stub(); :}

Here, Statement.stub() returns a stubbed Statement instance for testing purposes.

I get this error:

parser.java:653: error: incompatible types: List<Statement> cannot be converted to Symbol
          CUP$parser$result = parser.getSymbolFactory().newSymbol("stmtlist",2, ((java_cup.runtime.Symbol)CUP$parser$stack.peek()), RESULT);

I'm confused by this because the call to newSymbol provides the arguments:

parser.getSymbolFactory().newSymbol(String name, int id, Symbol left, Symbol right) 

which is different than all of the other calls to newSymbol() in my parser. All other calls to newSymbol() are of the form:

parser.getSymbolFactory().newSymbol(String name, int id, Symbol left, Symbol right, Object value)

effectively casting RESULT to an Object instead of a Symbol.

So. What am I doing wrong here? Why is the empty case generating such a strange parser? And what is the solution?

This issue was solved by upgrading my Mac OS from 10.13 to 10.14, and installing CUP in /usr/bin. Unfortunately I'm not sure why this worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM