2
我正在尝试编写一个非常简单的解析器。我在Java CUP中使用JFlex。这里是我的代码:CUP解析器为有效输入返回语法错误
LEX文件:
import java_cup.runtime.*;
%%
%class Lexer
%line
%column
%cup
%{
/*********************************************************************************/
/* Create a new java_cup.runtime.Symbol with information about the current token */
/*********************************************************************************/
private Symbol symbol(int type) {return new Symbol(type, yyline, yycolumn);}
private Symbol symbol(int type, Object value) {return new Symbol(type, yyline, yycolumn, value);}
%}
%%
<YYINITIAL> {
<<EOF>> { return symbol(sym.EOF); }
"|" { return symbol(sym.PIPE); }
}
CUP文件:
import java_cup.runtime.*;
terminal PIPE;
non terminal myrule;
myrule ::= PIPE {: RESULT = 42; :};
Main.java
import java.io.FileReader;
public class Main {
public static void main(String[] args) throws Exception {
CUP_FILECup parser = new CUP_FILECup(new Lexer(new FileReader(args[0])));
parser.debug_parse();
}
}
正如你所看到的,我试图使它简单因为我仍然可以,只有包含一个字符的输入文件出现以下错误:“|”。
语法错误在输入
但显然我们定义了一个合适的推导的字符0 “|”。
为什么会发生?
编辑:
- “从myrule开始;”没有帮助
输入文件中是否有BOM? https://msdn.microsoft.com/en-us/library/windows/desktop/dd374101(v=vs.85).aspx –
据我所知,没有。 – Plex
我用'file -'获得了'ASCII文本'(我使用的是Ubuntu) – Plex