[英]Linking CUP and jflex
I am trying to link my parser.java and yylex.java using help from 我试图使用来自的帮助链接我的parser.java和yylex.java
http://www2.cs.tum.edu/projects/cup/examples.php http://www2.cs.tum.edu/projects/cup/examples.php
http://www.cs.princeton.edu/~appel/modern/java/CUP/manual.html http://www.cs.princeton.edu/~appel/modern/java/CUP/manual.html
http://jflex.de/manual.html http://jflex.de/manual.html
But I am getting these errors. 但是我遇到了这些错误。
error: Yylex is not abstract and does not override abstract method next_token() in Scanner
错误:Yylex不是抽象的,并且不会在Scanner中覆盖抽象方法next_token()
error: next_token() in Yylex cannot implement next_token() in Scanner
错误:Yylex中的next_token()无法在扫描仪中实现next_token()
How to resolve them ? 如何解决?
My mini.flex file is: 我的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*/ }
}
My mini.cup file is: 我的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;
I compile using: 我编译使用:
java -jar java-cup-11b.jar -interface -parser Parser mini.cup
jflex mini.flex
javac -cp java-cup-11b-runtime.jar:. -Xlint *.java
The errors thrown are: 引发的错误是:
Parser.java:19: warning: [deprecation] lr_parser() in lr_parser has been deprecated public Parser() {super();}
Parser.java:19:警告:[弃用] lr_parser中的lr_parser()已被弃用public Parser(){super();}
^ Parser.java:22: warning: [deprecation] lr_parser(Scanner) in lr_parser has been deprecated public Parser(java_cup.runtime.Scanner s) {super(s);}^ Parser.java:22:警告:[不赞成使用] lr_parser中的lr_parser(Scanner)已弃用public Parser(java_cup.runtime.Scanner s){super(s);}
^ Parser.java:96: warning: [deprecation] lr_parser(Scanner) in lr_parser has been deprecated public Parser (java.io.Reader input) { super(new Yylex(input));^ Parser.java:96:警告:[不赞成使用] lr_parser中的lr_parser(Scanner)已弃用public Parser(java.io.Reader input){super(new Yylex(input)); }
}
^ Parser.java:137: warning: [cast] redundant cast to Object Object start_val = (Object)((java_cup.runtime.Symbol) CUP$Parser$stack.elementAt(CUP$Parser$top-1)).value;^ Parser.java:137:警告:[cast]强制转换为Object对象start_val =(Object)((java_cup.runtime.Symbol)CUP $ Parser $ stack.elementAt(CUP $ Parser $ top-1))。value;
^ Yylex.java:12: error: Yylex is not abstract and does not override abstract method next_token() in Scanner^ Yylex.java:12:错误:Yylex不是抽象的,并且不会在Scanner中覆盖抽象方法next_token()
public class Yylex implements java_cup.runtime.Scanner { ^公共类Yylex实现java_cup.runtime.Scanner {^
Yylex.java:474: error: next_token() in Yylex cannot implement next_token() in ScannerYylex.java:474: 错误:Yylex中的next_token()无法在扫描器中实现next_token()
public int next_token() throws java.io.IOException {public int next_token()抛出java.io.IOException {
^ return type int is not compatible with Symbol^返回类型int与Symbol不兼容
Yylex.java:631: error: incompatible typesYylex.java:631:错误:不兼容的类型
{ return new java_cup.runtime.Symbol(sym.EOF);{返回新的java_cup.runtime.Symbol(sym.EOF); }
}
^ required: int^必填:int
found: Symbol找到:符号
3 errors3个错误
4 warnings4警告
The problem is that next_token()
doesn't compile because: 问题在于
next_token()
无法编译,因为:
return type int is not compatible with Symbol
返回类型int与Symbol不兼容
and 和
Yylex.java:631: error: incompatible types
Yylex.java:631:错误:不兼容的类型
{ return new java_cup.runtime.Symbol(sym.EOF);{返回新的java_cup.runtime.Symbol(sym.EOF); }
}
^ required: int^必填:int
found: Symbol找到:符号
The generated code is 生成的代码是
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;
}
From the documentation: 从文档中:
The
%type
directive overrides settings of the%cup
switch.%type
指令将覆盖%cup
开关的设置。
Solution: Remove the %type int
directive from mini.jflex
(and use cup's java_cup.runtime.Symbol
). 解决方案:从
mini.jflex
删除%type int
指令(并使用cup的java_cup.runtime.Symbol
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.