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.
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.