为什么我无法使用 javacc 工具解析简单的表达式
Why i am not able to parse a simple Expression using javacc tool
我是使用工具的初学者,我制作了一个 class Expression.jj
,其中我使用了以下语法
expr->expr+term|term
term->term*fact|fact
fact->(expr)|num
这是源代码
PARSER_BEGIN(Expression)
public class Expression
{
public static void main(String s[])
{
try
{
new Expression(new java.io.StringReader(args[0])).StartSymbol();
System.out.println("Syntex checking successfull");
}
catch(Throwable e)
{
System.out.println("Syntex checking failed");
e.getMessage();
}
}
}
PARSER_END(Expression)
SKIP:{"" | "\t" | "\n" | "\r" }
TOKEN:{"(" | ")" | "+" | "*" |<NUM: (["0"-"9"])+>}
void StartSymbol():
{
(Expr())*
<EOF>
}
void Expr():
{
Term() ("+" Term())*
}
void Term():
{
Factor() ("*" Factor())*
}
void Factor():
{
<NUM> | "(" Expr() ")"
}
cmd 中显示的输出
C:\>java -cp C:\javacc5.0\javacc-5.0\bin\lib\javacc.jar javacc
C:\compilerprog\Expression.jj Java Compiler Compiler Version 5.0 (Parser Generator)
(type "javacc" with no arguments for help)
Reading from file C:\compilerprog\Expression.jj . . .
Error: Could not open file Expression.java for writing.
Exception in thread "main" java.lang.Error
at org.javacc.parser.ParseGen.start(Unknown Source)
at org.javacc.parser.Main.mainProgram(Unknown Source)
at org.javacc.parser.Main.main(Unknown Source)
at javacc.main(Unknown Source)
@编辑
当我使用这个命令时是 运行:
C:\javaccprog>java -cp C:\javacc5.0\javacc-5.0\bin\lib\javacc.jar javacc Expression.jj
而不是:
C:\java -cp C:\javacc5.0\javacc-5.0\bin\lib\javacc.jar javacc C:\compilerprog\Expression.jj
Error: Could not open file Expression.java for writing.
表示无法写入'Expression.java',请更换工作目录。
我是使用工具的初学者,我制作了一个 class Expression.jj
,其中我使用了以下语法
expr->expr+term|term
term->term*fact|fact
fact->(expr)|num
这是源代码
PARSER_BEGIN(Expression)
public class Expression
{
public static void main(String s[])
{
try
{
new Expression(new java.io.StringReader(args[0])).StartSymbol();
System.out.println("Syntex checking successfull");
}
catch(Throwable e)
{
System.out.println("Syntex checking failed");
e.getMessage();
}
}
}
PARSER_END(Expression)
SKIP:{"" | "\t" | "\n" | "\r" }
TOKEN:{"(" | ")" | "+" | "*" |<NUM: (["0"-"9"])+>}
void StartSymbol():
{
(Expr())*
<EOF>
}
void Expr():
{
Term() ("+" Term())*
}
void Term():
{
Factor() ("*" Factor())*
}
void Factor():
{
<NUM> | "(" Expr() ")"
}
cmd 中显示的输出
C:\>java -cp C:\javacc5.0\javacc-5.0\bin\lib\javacc.jar javacc
C:\compilerprog\Expression.jj Java Compiler Compiler Version 5.0 (Parser Generator)
(type "javacc" with no arguments for help)
Reading from file C:\compilerprog\Expression.jj . . .
Error: Could not open file Expression.java for writing.
Exception in thread "main" java.lang.Error
at org.javacc.parser.ParseGen.start(Unknown Source)
at org.javacc.parser.Main.mainProgram(Unknown Source)
at org.javacc.parser.Main.main(Unknown Source)
at javacc.main(Unknown Source)
@编辑
当我使用这个命令时是 运行:
C:\javaccprog>java -cp C:\javacc5.0\javacc-5.0\bin\lib\javacc.jar javacc Expression.jj
而不是:
C:\java -cp C:\javacc5.0\javacc-5.0\bin\lib\javacc.jar javacc C:\compilerprog\Expression.jj
Error: Could not open file Expression.java for writing.
表示无法写入'Expression.java',请更换工作目录。