为什么我无法使用javacc工具解析一个简单的表达式

时间:2015-04-08 07:19:26

标签: java compiler-construction javacc

我是使用工具的初学者,我创建了一个类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中的OutputShown

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)

@EDIT

当我使用此命令时它正在运行:

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

1 个答案:

答案 0 :(得分:1)

Error: Could not open file Expression.java for writing.表示您无法写入'Expression.java',请更改您的工作目录。