Questions tagged [antlr4]

Version 4 of ANother Tool for Language Recognition (ANTLR), a flexible lexer/parser generator. ANTLR4 features an enhanced adaptive LL(*) parsing algorithm, that improves on the simpler LL(*) algorithm used in ANTLR3.

ANTLR stands for ANother Tool for Language Recognition, a powerful parser generator for reading, processing, executing, or translating structured text or binary files. At its core, ANTLR uses a grammar, with syntax loosely based on Backus–Naur_Form, to generate a parser. That parser produces easily traversable parse trees, which can be processed further by the user. ANTLR's simplistic and powerful design has allowed it to be used in many projects, from the expression evaluator in Apple's Numbers application1, to IntelliJ's IDEA IDE2.

The main improvement between ANTLR4 and ANTLR3 is a change in the parsing algorithm. This new variation of the LL(*) parsing algorithm, coined adaptive LL(*), pushes all of the grammar analysis effort to runtime, making ANTLR able to handle left recursive rules. This new resilience lead to the name "Honey Badger", on which Terence Parr had this to say:

ANTLR v4 is called the honey badger release after the fearless hero of the YouTube sensation, "The Crazy Nastyass Honey Badger". To quote the honey badger, ANTLR v4 just doesn't give a damn. It's pretty bad ass. It'll take just about any grammar you give it at parse correctly. And, without backtracking!*

-- Terence Parr

(To read more, check out the full conversation!)

If you are interested in learning to use ANTLR4, a good place to start would be the official documentation, which provides an excellent introduction to the library itself.

Further Reading:

1 Sourced from a paper written by Terrence Parr himself.

2 Sourced from Jetbrain's official list of third party software in IDEA.

3 On January 24th 2013, the www.antlr.org address was changed from pointing at site for ANTLR version 3 (www.antlr3.org) to ANTLR version 4 (www.antlr4.org). So questions and answers that used www.antlr.org were correct for ANTLR 3.x before this date. The links should be updated to www.antlr3.org for ANTLR 3.x or www.antlr4.org for ANTLR 4.x.

3877 questions
14
votes
2 answers

How to automatically generate lexer+parser with ANTLR4 and Maven?

I'am new to ANTLR4, and it seems that there is no Eclipse-Plug-In for v4. So it would nice to build automatically the Java sources from the .g4 grammars. I have a simple, empty Maven-project with src/main/java, src/test/java. Where to place the .g4…
Vertex
  • 2,682
  • 3
  • 29
  • 43
14
votes
2 answers

In antlr4 lexer, How to have a rule that catches all remaining "words" as Unknown token?

I have an antlr4 lexer grammar. It has many rules for words, but I also want it to create an Unknown token for any word that it can not match by other rules. I have something like this: Whitespace : [ \t\n\r]+ -> skip; Punctuation : [.,:;?!]; //…
mdakin
  • 1,310
  • 11
  • 17
14
votes
1 answer

label x assigned to a block which is not a set

trying out upgrading antlr4, I have 2 lines in the grammar that produce the error message: label tok assigned to a block which is not a set Specifically for a grammar line that looks like this: contextRadius: tok=('radius'…
joehitt
  • 310
  • 3
  • 10
14
votes
1 answer

Semantic predicates in ANTLR4?

How would you translate this portion of code written in ANTLR 3 into ANTLR 4? expr: (Identifier '.')=> (refIdentifier) | (Identifier '!')=> (refIdentifier) | (Identifier '=>')=> (lambdaExpression); I mean this kind of semantic predicate does…
Aftershock
  • 5,205
  • 4
  • 51
  • 64
13
votes
2 answers

How can i see the live parse tree using Antlr4 Ide in Eclipse?

I'm new using Antlr4 but I know that exist a plugin for Eclipse. I have a simple question...After I created the g4 file how can I visualize the live parse tree in order to see the tree of an input expression? Thanks
userSimo
  • 165
  • 3
  • 12
13
votes
1 answer

Troubles with returns declaration on the first parser rule in an ANTLR4 grammar

I am using returns for my parser rules which works for all parser rules except the first one. If the first parser rule in my grammer uses the returns declaration ANTLR4 complains as follows: expecting ARG_ACTION while matching a rule If I add…
iterator
  • 280
  • 1
  • 7
13
votes
1 answer

Syntax of semantic predicates in Antlr4

In What is a 'semantic predicate' in ANTLR3? Bart Kiers gives a very well overview about the different semantic predicates in Antlr3. Too bad the syntax/semantics were seemingly changed in Antlr4, so this does not compile: end_of_statement : …
Kijewski
  • 25,517
  • 12
  • 101
  • 143
12
votes
2 answers

Abort on parse error with useful message

I've got an ANTLR 4 grammar and built a lexer and parser from that. Now I'm trying to instantiate that parser in such a way that it will parse until it encounters an error. If it encounters an error, it should not continue parsing, but it should…
MvG
  • 57,380
  • 22
  • 148
  • 276
11
votes
5 answers

antlr4/java: pretty print parse tree to stdout

Beginners question: how do I print a readable version of the parse tree to stdout? CharStream input = CharStreams.fromFileName("testdata/test.txt"); MyLexer lexer = new MyLexer(input); CommonTokenStream tokens = new…
JPT
  • 410
  • 2
  • 7
  • 18
11
votes
1 answer

Antlr parser operator priority

Consider the following grammar. I have issues with the operator priority, for instance: res=2*a+b has a similar parse tree as res=2*(a+b). I know where the problem is, but no "beautiful" solution without mutual left recursion comes to my mind. Can…
Martin B
  • 141
  • 1
  • 5
11
votes
1 answer

How does the ANTLR lexer disambiguate its rules (or why does my parser produce "mismatched input" errors)?

Note: This is a self-answered question that aims to provide a reference about one of the most common mistakes made by ANTLR users. When I test this very simple grammar: grammar KeyValues; keyValueList: keyValue*; keyValue: key=IDENTIFIER '='…
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
11
votes
1 answer

Why is there such a difference between parse tree visualization and my visitor/listener traversal?

I've created this sample grammar using the ANTLR4 plugin in IntelliJ and when I use its tool chain to generate a visual representation for some invalid content (in this case an empty string), this representation seems to differ from what I'm able to…
predi
  • 5,528
  • 32
  • 60
11
votes
1 answer

Error: Could not find or load main class org.antlr.v4.Tool Antrl4

I want to use Antlr to create parser and i followed instruction from Here after following all instruction while I am running following command antlr4 -visitor D:\T\arithmetic.g4 I am getting this error msg: Error: Could not find or load main class…
yashpal
  • 326
  • 1
  • 3
  • 16
11
votes
4 answers

ANTLR4 and the Python target

I'm having issues getting going with a Python target in ANTLR4. There seems to be very few examples available and going to the corresponding Java code doesn't seem relevant. I'm using the standard Hello.g4 grammar: // Define a grammar called…
Dustin Oprea
  • 9,673
  • 13
  • 65
  • 105
11
votes
4 answers

How to profile an Antlr grammar

I have an Antlr grammar that is currently about 1200 lines. It parses the language that I want, but for at least one construct it is prohibitively slow even for smaller input files. The execution time seems to be growing exponentially for each added…
oyse
  • 1,023
  • 9
  • 18