1

The following grammar successfully generates a parser for treating the string 'ccdunion'. But when I try with the interpreter, I have NoViableAltException error, why?

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
 : .
 ;
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
moueza
  • 69
  • 1
  • 7

1 Answers1

0

... when I try with the interpreter, I have NoViableAltException error, why?

Because the interpreter is buggy: don't use it. Use the debugger instead. The same goes for ANTLRWorks.

Bart Kiers
  • 166,582
  • 36
  • 299
  • 288