Eclipse Antlr Interpreter失败

时间:2012-08-21 14:43:28

标签: antlr interpreter antlrv3ide

以下语法成功生成用于处理字符串'ccdunion'的解析器。但是当我尝试使用解释器时,我有NoViableAltException错误,为什么?

grammar SimpleCalc;

options {
  k = 2;
  output = AST;
  backtrack = true;
}

tokens {
  UNION = 'union';
}


@header {
  package myPack;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.PrintWriter;
}

@lexer::header {
  package myPack;
}

@members {
  public static void main(String[] args) throws Exception {
    File fileOut = new File("src/ARFF.arff");

    FileWriter fw = new FileWriter(fileOut);
    PrintWriter pw = new PrintWriter(fw);

    pw.print("a ");
    pw.println("b");
    pw.println(12);

    fw.close();
    SimpleCalcLexer lex = new SimpleCalcLexer(new ANTLRFileStream(args[0]));
    CommonTokenStream tokens = new CommonTokenStream(lex);

    SimpleCalcParser parser = new SimpleCalcParser(tokens);

    try {
      parser.expr();
    } catch (RecognitionException e) {
      e.printStackTrace();
    }
  }
}

expr
 : filsPiquets EOF 
   {
     System.out.println("base mono: ");
   }
 ;

filsPiquets
 : chars UNION
   {
     System.out.println("TXTunionII"+$chars.text);
   }
 ;

chars
 : CHAR
 | . chars
 ;

CHAR
 : .
 ;

1 个答案:

答案 0 :(得分:0)

  

...当我尝试使用解释器时,我有NoViableAltException错误,为什么?

因为翻译是错误的:不要使用它。请改用调试器。 ANTLRWorks也是如此。