解析CUP中的EOF令牌 [英] Parse EOF token in CUP
问题描述
我无法让我的CUP解析器解析EOF令牌.我在文档中读到,在我的Jflex代码中使用%cup
标志意味着存在类似这样的内容:
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
这一切都很好,但是当我尝试语法(CUP文件)中的以下第一条规则时:
This is all good and well, but when I try the following first rule in my grammar (CUP file):
program ::= program declaration EOF
| /* Empty */
;
我收到错误消息,即CUP未声明EOF.
I get the error message that EOF is not declared by CUP.
Error : java_cup.runtime.Symbol "EOF" has not been declared
好的,所以我将以下内容添加到我的CUP文件中:
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
但是,在未定义终端EOF的情况下,我在Cup生成的sym.java
类中进行了查看,我发现:
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.
我想解析EOF令牌的主要原因是因为我想最后打印出我的符号表和其他内容以进行调试.
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.
推荐答案
好的,因此,与其显式解析EOF令牌以执行一些代码,我在语法中添加了以下包装器",这使我可以准确地执行我的操作想做.
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.
代替:
program ::= program declaration
| /* Empty */
;
我现在有:
initial ::= program {: /* Code for EOF here */ :}
;
program ::= program declaration EOF
| /* Empty */
;
这使我能够在每次成功解析的末尾执行代码.
This allows me to execute code at the end of every succesful parse.
这篇关于解析CUP中的EOF令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!