简体   繁体   中英

How to match the empty case in CUP parser grammar

I am using CUP to generate a parser, and I want an empty file to be an acceptable program. I have tried add the empty case to my start symbol, based off the response to a similar question here .

start with prog;

/* The grammar rules */
prog    ::= class_block:cb   
        |   class_block:cb stmts:sb
        |   stmts:sb
        |   // desired empty case
        ;

Including the desired empty case gives me the following error:

parser.java:516: error: incompatible types: Object cannot be converted to Symbol
CUP$parser$result = parser.getSymbolFactory().newSymbol("prog",0, ((java_cup.runtime.Symbol)CUP$parser$stack.peek()), RESULT);

How can I modify my grammar so that the parser accepts an empty file? I am using Jflex as my lexer, and ComplexSymbolFactory as the type of the symbols.

EDIT: I've confirmed that the grammar above is the correct way to include empty. However, ComplexSymbolFactory is having problems converting the empty object to a symbol. I get this error even when runningthis example from the official CUP website.

I downloaded the .jars from a friend github project and problem solved.

You can see it's a BUG here: https://github.com/jflex-de/jflex/issues/384

Try to downgrade the version.

I don't know about any bugs, but I know you can rewrite your grammar to make the empty case work. for example:

Prog ::= 
   class_block:cb statement:s
   | /* the empty production */
;

I have tried this, and it works for empty productions. Having a production where it is a block and then a statement, or a statement, or an empty, conflicts. there isn't a way to tell during the generation for the parser at the program grammar rules which is a statement and what is an empty. Changing it only being a block and a statement, or an empty clarifies that.

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