Token JavaCC令牌不匹配
我正试图为简单语言编写一个解析器,但我不知道如何处理这个问题。这是我的.jj文件Token JavaCC令牌不匹配,token,javacc,Token,Javacc,我正试图为简单语言编写一个解析器,但我不知道如何处理这个问题。这是我的.jj文件 options { STATIC = false; LOOKAHEAD=2; //DEBUG_LOOKAHEAD = true; DEBUG_TOKEN_MANAGER=true; FORCE_LA_CHECK = true; DEBUG_PARSER = true; JDK_VERSION = "1.7"; } PARSER_BEGIN(Parser) import java.io.
options
{
STATIC = false;
LOOKAHEAD=2;
//DEBUG_LOOKAHEAD = true;
DEBUG_TOKEN_MANAGER=true;
FORCE_LA_CHECK = true;
DEBUG_PARSER = true;
JDK_VERSION = "1.7";
}
PARSER_BEGIN(Parser)
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Parser{
private static BufferedWriter bufferFileWriter;
private static FileWriter fWriter;
public static void main(String args []) throws ParseException, IOException
{
Parser parser = new Parser(System.in);
fWriter = new FileWriter("result", true);
bufferFileWriter = new BufferedWriter(fWriter);
parser.program();
// TO DO
}
}
PARSER_END(Parser)
SKIP :
{
" "
| "\r"
| "\t"
| "\n"
}
TOKEN : /* OPERATORS */
{
< PLUS : "+" >
| < MINUS : "-" >
| < MULTIPLY : "*" >
| < DIVIDE : "/" >
| < MODULO : "%" >
| < ASSIG : ":=" >
| < EQUAL : "==" >
| < DIFF : "!=" >
| < SMALLER : "<" >
| < GRATER : ">" >
| < S_OR_EQU: "<=" >
| < G_OR_EQU: "=>" >
}
TOKEN : /*KEY WORDS FROM LANGUAGE */
{
< VAR: "VAR">
| < BEGIN : "BEGIN" >
| < END : "END" >
| < IF : "IF" >
| < ELSE : "ELSE" >
| < THEN : "THEN" >
| < WHILE: "WHILE" >
| < DO : "DO" >
| < READ : "READ" >
| < WRITE : "WRITE" >
| < SEMICOL : ";" >
}
TOKEN :
{
< VALUE : < ID > | < NUMBER > >
| < NUMBER : (< DIGIT >)+ >
| < #DIGIT : [ "0"-"9" ] >
| < ID : (["a"-"z"])+ >
}
void program():
{}
{
varDeclarations()< BEGIN > commands() < END >
}
void varDeclarations():
{}
{
< VAR >
{
System.out.println("past VAR token");
}
(< ID >
)+
}
void commands():
{}
{
(LOOKAHEAD(3)
command())+
}
void command():
{
Token t;
}
{
assign()
|< IF >condition()< THEN >commands()< ELSE >commands()< END >
|< WHILE >condition()< DO >commands()< END >
|< READ >
t=< ID >
{
try
{
fWriter.append("LOAD "+t.image);
System.out.println("LOAD "+t.image);
}
catch(IOException e)
{
};
}
< SEMICOL >
|< WRITE >
t = < VALUE >< SEMICOL >
}
void assign():
{
Token t;
}
{
t=< ID >
{
}
< ASSIG >expression(t)< SEMICOL >
}
void condition():
{}
{
< VALUE > condOperator() < VALUE >
}
void condOperator():
{}
{
< EQUAL > | < DIFF > | < SMALLER > | < S_OR_EQU > | < GRATER > | < G_OR_EQU >
}
Token operator():
{
Token tok;
}
{
tok=< PLUS >
{
System.out.println(tok.image);
return tok;
}
|tok=< MINUS >
{
System.out.println(tok.image);
return tok;
}
|tok=< MULTIPLY >
{
System.out.println(tok.image);
return tok;
}
|tok=< DIVIDE >
{
System.out.println(tok.image);
return tok;
}
|tok=< MODULO >
{
System.out.println(tok.image);
return tok;
}
}
void expression(Token writeTo):
{
Symbol s;
Token t1, t2, t3;
}
{
t1 = < VALUE >
t2 = operator()
t3 = < VALUE >
< SEMICOL >
{
if(t2.image.equals("+"))
{
try
{
fWriter.append("ADD "+t1.image+" "+t2.image);
System.out.println("ADD "+t1.image+" "+t2.image);
}catch(IOException e)
{
}
}
}
}
正如您所看到的,解析器进入了varDeclaration方法,但为什么他不能将token与单词VAR匹配呢?如果有任何帮助,我将不胜感激 @西奥多,我按照你的建议做了,但没用。也许我编译和执行程序的方式不对? 这是我的控制台的副本:
$javacc Parser.jj
Java Compiler Compiler Version 5.0 (Parser Generator)
(type "javacc" with no arguments for help)
Reading from file Parser.jj . . .
File "TokenMgrError.java" is being rebuilt.
File "ParseException.java" is being rebuilt.
File "Token.java" is being rebuilt.
File "SimpleCharStream.java" is being rebuilt.
Parser generated successfully.
$ javac *.java
$ java Parser VAR a
Call: program
Call: varDeclarations
让解析器识别“VAR”关键字没有问题。问题是“a”被标记为“VALUE”标记,而解析器希望在“VAR”关键字之后有一个“ID”标记。(请参见下面的输入和输出。)
VALUE'的规则优先于
ID`的规则,因为它是第一个。(请参见常见问题解答中的问题3.3。)
您可能应该做的是用以下规则替换您现在对VALUE
的规则
void Value() : {} { <ID> | <NUMBER> }
输出:
Call: program
Call: varDeclarations
Current character : V (86) at line 1 column 1
Possible string literal matches : { "VAR" }
Current character : A (65) at line 1 column 2
Possible string literal matches : { "VAR" }
Current character : R (82) at line 1 column 3
No more string literal token matches are possible.
Currently matched the first 3 characters as a "VAR" token.
****** FOUND A "VAR" MATCH (VAR) ******
Consumed token: <"VAR" at line 1 column 1>
past VAR token
Skipping character : \n (10)
Current character : a (97) at line 2 column 1
No string literal matches possible.
Starting NFA to match one of : { <VALUE> }
Current character : a (97) at line 2 column 1
Currently matched the first 1 characters as a <VALUE> token.
Possible kinds of longer matches : { <VALUE>, <ID> }
Current character : \n (10) at line 2 column 2
Currently matched the first 1 characters as a <VALUE> token.
Putting back 1 characters into the input stream.
****** FOUND A <VALUE> MATCH (a) ******
Return: varDeclarations
Return: program
Exception in thread "main" tokenNotMatched.ParseException: Encountered " <VALUE> "a "" at line 2, column 1.
Was expecting:
<ID> ...
调用:程序
电话:VAR
当前字符:第1行第1列的V(86)
可能的字符串文字匹配:{“VAR”}
当前字符:第1行第2列A(65)
可能的字符串文字匹配:{“VAR”}
当前字符:第1行第3列的R(82)
不能再进行字符串文字标记匹配。
当前匹配前3个字符作为“VAR”标记。
******找到一个“VAR”匹配项(VAR)******
已消费代币:
过去VAR令牌
正在跳过字符:\n(10)
当前字符:第2行第1列的a(97)
不可能有字符串文字匹配。
正在启动NFA以匹配下列之一:{}
当前字符:第2行第1列的a(97)
当前匹配前1个字符作为标记。
可能的长匹配类型:{,}
当前字符:\n(10)在第2行第2列
当前匹配前1个字符作为标记。
将1个字符放回输入流。
******找到匹配项(A)******
返回:VAR声明
返回:程序
线程“main”tokenNotMatched中出现异常。ParseException:在第2行第1列遇到“a”。
他期望:
...
使用选项DEBUG\u TOKEN\u MANAGER=true并显示结果。另外,如果你可以发布一个链接到你完整的jj文件,那会有帮助。嗨,我已经编辑了我的问题并粘贴了我的.jj文件。谢谢你的关注。很抱歉没有在你的答案下发表评论,但是迷你降价编辑让我很难受。我发现编辑我的问题更容易,结果也更容易阅读。没问题。在我看来,您正在将输入(
VAR a
)放在命令行上。试着通过java解析器运行它,我觉得即使是双面掌也不够。你是对的。早些时候,我用$java Parser testFile运行它,而我应该用$java Parser运行它,我们都有这样的时刻。
void Value() : {} { <ID> | <NUMBER> }
VAR
a
Call: program
Call: varDeclarations
Current character : V (86) at line 1 column 1
Possible string literal matches : { "VAR" }
Current character : A (65) at line 1 column 2
Possible string literal matches : { "VAR" }
Current character : R (82) at line 1 column 3
No more string literal token matches are possible.
Currently matched the first 3 characters as a "VAR" token.
****** FOUND A "VAR" MATCH (VAR) ******
Consumed token: <"VAR" at line 1 column 1>
past VAR token
Skipping character : \n (10)
Current character : a (97) at line 2 column 1
No string literal matches possible.
Starting NFA to match one of : { <VALUE> }
Current character : a (97) at line 2 column 1
Currently matched the first 1 characters as a <VALUE> token.
Possible kinds of longer matches : { <VALUE>, <ID> }
Current character : \n (10) at line 2 column 2
Currently matched the first 1 characters as a <VALUE> token.
Putting back 1 characters into the input stream.
****** FOUND A <VALUE> MATCH (a) ******
Return: varDeclarations
Return: program
Exception in thread "main" tokenNotMatched.ParseException: Encountered " <VALUE> "a "" at line 2, column 1.
Was expecting:
<ID> ...