Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR ClassCastException

Tags:

java

antlr

Why do I get this error while generating code in ANTLR?

[16:06:38] error(10):  internal error: C:\Users\user\Desktop\test.g : java.lang.ClassCastException: org.antlr.runtime.tree.CommonTree cannot be cast to org.antlr.tool.GrammarAST
org.antlr.grammar.v3.CodeGenTreeWalker.rules(CodeGenTreeWalker.java:1467)
org.antlr.grammar.v3.CodeGenTreeWalker.grammarSpec(CodeGenTreeWalker.java:1441)
org.antlr.grammar.v3.CodeGenTreeWalker.grammar_(CodeGenTreeWalker.java:509)
org.antlr.codegen.CodeGenerator.genRecognizer(CodeGenerator.java:421)
org.antlr.Tool.generateRecognizer(Tool.java:655)
org.antlr.Tool.process(Tool.java:468)
org.antlr.works.generate.CodeGenerate.generate(CodeGenerate.java:104)
org.antlr.works.generate.CodeGenerate.run(CodeGenerate.java:185)
java.lang.Thread.run(Unknown Source)

The syntax is very simple, just for testing:

grammar test;

ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
    ;

Any ideas?

like image 695
Alon Gubkin Avatar asked Aug 19 '11 13:08

Alon Gubkin


People also ask

What is ANTLR used for?

What is ANTLR? ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. Terence Parr is a tech lead at Google and until 2022 was a professor of data science / computer science at Univ.

What grammar does ANTLR use?

A language is specified using a context-free grammar expressed using Extended Backus–Naur Form (EBNF). ANTLR can generate lexers, parsers, tree parsers, and combined lexer-parsers.

Is ANTLR a programming language?

ANTLR or ANother Tool for Language Recognition is a lexer and parser generator aimed at building and walking parse trees. It makes it effortless to parse nontrivial text inputs such as a programming language syntax. In this post, we'll get into the basics of getting ANTLR up and running in a dev environment.

What is a parser in ANTLR?

ANTLR is a powerful parser generator that you can use to read, process, execute, or translate structured text or binary files. It's widely used in academia and industry to build all sorts of languages, tools, and frameworks. Twitter search uses ANTLR for query parsing, with over 2 billion queries a day.


1 Answers

You specified a mixed grammar (lexer and parser), but you did not specify a parser rule.

Either add one or use lexer grammar test;

like image 172
H-Man2 Avatar answered Sep 20 '22 01:09

H-Man2