2

I'm having trouble making my CUP parser parse the EOF token. I read in the documentation that using the %cup flag in my Jflex code implies that there is something present like this:

%eofval{
  return new java_cup.runtime.Symbol(<CUPSYM>.EOF);
%eofval}
%eofclose

This is all good and well, but when I try the following first rule in my grammar (CUP file):

program                 ::=   program declaration EOF
                          | /* Empty */
                          ;

I get the error message that EOF is not declared by CUP.

Error : java_cup.runtime.Symbol "EOF" has not been declared

Allrighty, so I add the following to my CUP file:

terminal EOF;

No, because then CUP tells me:

Fatal : JavaCUP Internal Error Detected: Duplicate terminal (EOF) created
enter code here

However, without defining the terminal EOF, and I have a look in the sym.java class that Cup generates I do find:

  public static final int EOF = 0;

So I'm pretty lost on how to fix this. The documentation for both is really vague on the issue.

Edit: The main reason I want to parse the EOF token is because I want to print out my symbol table in the end and other stuff for debugging purposes.

rds
  • 26,253
  • 19
  • 107
  • 134
Christophe De Troyer
  • 2,852
  • 3
  • 30
  • 47

3 Answers3

2

Okay, so instead of explicitly parsing the EOF token to execute some code I added the following "wrapper" to my grammar, which allows me to do exactly what I wanted to do.

Instead of:

program                 ::=   program declaration
                          | /* Empty */
                          ;

I now have:

initial                 ::=   program   {: /* Code for EOF here */ :}
                          ;
program                 ::=   program declaration EOF
                          | /* Empty */
                          ;

This allows me to execute code at the end of every succesful parse.

Christophe De Troyer
  • 2,852
  • 3
  • 30
  • 47
0
program                 ::=   program declaration EOF
                          | /* Empty */
                          ;

You don't need EOF in this production. It's implicit.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • So do you have an idea on how I could insert a codeblock that is executed after my EOF is parsed? I would like to print out my symboltable and such for debugging purposes. – Christophe De Troyer Apr 01 '14 at 11:04
0

Make sure you didn´t forgot a ";" in any of your grammar productions corresponding to your "file.cup".