链接CUP和jflex

时间:2015-03-10 19:58:31

标签: java flex-lexer jflex cup

我正在尝试使用来自http://www2.cs.tum.edu/projects/cup/examples.php的帮助链接我的parser.java和yylex.java http://www.cs.princeton.edu/~appel/modern/java/CUP/manual.html
http://jflex.de/manual.html

但我收到了这些错误。

  

错误:Yylex不是抽象的,不会覆盖抽象方法   扫描仪中的next_token()

     

错误:Yylex中的next_token()无法在Scanner中实现next_token()

如何解决它们?

我的mini.flex文件是:

import java.io.*; 

%%

%public
%cup 
%line
%column
%type int

identifier = {letter} ( {letter} | {decimal_digit} )*
letter        = [a-z] | "_"
decimal_digit = [0-9]

LineTerminator = \r|\n|\r\n
WhiteSpace     = {LineTerminator} | [ \t\f]
Comment = {GeneralComment} | {LineComment}
GeneralComment   = "/*"([^*]|\*+[^*/])*\*+"/"
LineComment     = "//" {LineTerminator}?

%%

<YYINITIAL> { 
"break" { return sym.breakd; }
"default" { return sym.defaultd; }
}

<YYINITIAL> {
  {identifier} { return sym.identifier; }
}

<YYINITIAL> { 
"+" {return sym.plus_op;}
"&" {return sym.amp_op;}
}

<YYINITIAL> { 
  {Comment}                      { /* ignore */ }
  \n                             { }
  {WhiteSpace}                   { /* ignore */ }
  [^]                            { /*Illegeal Character*/ }
}

我的mini.cup文件是:

// import java-cup-11b-runtime.*;

import java_cup.runtime.*;

parser code {: public Parser (java.io.Reader input) { super(new Yylex(input)); } :};

/* Preliminaries to set up and use the scanner.  */

/*init with {: scanner.init();              :};
scan with {: return scanner.next_token(); :};*/

terminal identifier;
terminal breakd, defaultd, plus_op, amp_op;

non terminal SourceFile, UnaryExpr, Expression, Statement, SimpleStmt, EmptyStmt, ExpressionStmt, unary_op;

    precedence left plus_op;

Expression ::= UnaryExpr | Expression plus_op UnaryExpr;
UnaryExpr  ::=  unary_op identifier;
unary_op   ::= plus_op | amp_op;

Statement ::= SimpleStmt;
SimpleStmt ::= EmptyStmt | ExpressionStmt;
EmptyStmt ::= ; // should be Empty
ExpressionStmt ::= Expression;

SourceFile ::= Statement;

我使用编译:

java -jar java-cup-11b.jar -interface -parser Parser mini.cup
jflex mini.flex
javac -cp java-cup-11b-runtime.jar:. -Xlint *.java

抛出的错误是:

  

Parser.java:19:警告:lr_parser中的[deprecation] lr_parser()有   已弃用public Parser(){super();}
                     ^ Parser.java:22:警告:lr_parser中的[deprecation] lr_parser(Scanner)已被弃用   解析器(java_cup.runtime.Scanner s){super(s);}
                                               ^ Parser.java:96:警告:lr_parser中的[deprecation] lr_parser(扫描程序)已经   deprecated public Parser(java.io.Reader input){super(new   函数yylex(输入)); }                                           ^ Parser.java:137:警告:[cast]冗余强制转换为Object Object start_val =   (对象)((java_cup.runtime.Symbol)   。CUP $ $分析器stack.elementAt(CUP $ $分析器顶部-1))值;
                           ^ Yylex.java:12:错误:Yylex不是抽象的,并且不会覆盖扫描器中的抽象方法next_token()
public   Yylex类实现了java_cup.runtime.Scanner {          ^
  Yylex.java:474:Yylex中的错误:next_token()无法在扫描程序中实现next_token()
public int next_token()throws   java.io.IOException {
               ^返回类型int与符号不兼容.Yylex.java:631:错误:不兼容的类型
{return new   java_cup.runtime.Symbol(sym.EOF); }                          ^ required:int
found:符号
3个错误
4个警告

1 个答案:

答案 0 :(得分:1)

问题是next_token()无法编译,因为:

  

返回类型int与Symbol

不兼容

  

Yylex.java:631:错误:不兼容的类型
  {return new java_cup.runtime.Symbol(sym.EOF); }
               ^必需:int
  发现:符号

生成的代码是

public int next_token() throws java.io.IOException {
...
if (c) {
  // sometimes returns a Symbol
  return new java_cup.runtime.Symbol(sym.EOF);
} else {
  // sometimes returns an int
  return sym.identifier;
}

来自文档:

  

%type指令会覆盖%cup开关的设置。

解决方案:从%type int移除mini.jflex指令(并使用杯子的java_cup.runtime.Symbol)。