【发布时间】:2017-04-13 13:18:55
【问题描述】:
我正在尝试编写一个非常简单的解析器。我将 JFlex 与 Java 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 处的语法错误
但显然我们为“|”定义了一个适当的推导。
为什么会这样?
编辑:
- “从我的规则开始;”没用
【问题讨论】:
-
输入文件中会不会有BOM? msdn.microsoft.com/en-us/library/windows/desktop/…
-
据我所知,没有。
-
我使用了
file -,得到了ASCII text(我使用的是Ubuntu)
标签: java parsing compilation jflex cup