gpt4 book ai didi

java - 连接 CUP 和 jflex

转载 作者:行者123 更新时间:2023-12-02 04:53:49 53 4
gpt4 key购买 nike

我正在尝试使用
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

但是我遇到了这些错误。

error: Yylex is not abstract and does not override abstract method next_token() in Scanner

error: next_token() in Yylex cannot implement next_token() in Scanner

如何解决?

我的 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: warning: [deprecation] lr_parser() in lr_parser has been deprecated 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:96: warning: [deprecation] lr_parser(Scanner) in lr_parser has been deprecated 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;
^ Yylex.java:12: error: Yylex is not abstract and does not override abstract method next_token() in Scanner
public class Yylex implements java_cup.runtime.Scanner { ^
Yylex.java:474: error: next_token() in Yylex cannot implement next_token() in Scanner
public int next_token() throws java.io.IOException {
^ return type int is not compatible with Symbol
Yylex.java:631: error: incompatible types
{ return new java_cup.runtime.Symbol(sym.EOF); }
^ required: int
found: Symbol
3 errors
4 warnings

最佳答案

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

return type int is not compatible with Symbol

Yylex.java:631: error: incompatible types
{ return new java_cup.runtime.Symbol(sym.EOF); }
^ required: int
found: Symbol

生成的代码是

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;
}

来自文档:

The %type directive overrides settings of the %cup switch.

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

关于java - 连接 CUP 和 jflex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28972993/

53 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com