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
17
votes
2 answers

ANTLR4: Matching all input alternatives exaclty once

How can I make a rule to match all of its alternatives only once in any order, in ANTLR? i.e. rule: ('example\r\n' | 'example2\r\n') nextRule I would like 'example' and 'example2' to match only once before moving onto the next rule. Should match…
Har
  • 3,727
  • 10
  • 41
  • 75
16
votes
3 answers

IntelliJ IDEA Gradle project not recognizing/locating Antlr generated sources

I'm using Antlr in a simple Kotlin/Gradle project, and while my Gradle build is generating Antlr sources, they are not available for importing into the project. As you can see (on the left), the classes (Lexer/Parser, etc.) are being generated. I…
Craig Otis
  • 31,257
  • 32
  • 136
  • 234
16
votes
2 answers

Antlr4 - Is there a simple example of using the ParseTree Walker?

Antlr4 has a new class ParseTreeWalker. But how do I use it? I am looking for a minimal working example. My grammar file is 'gram.g4' and I want to parse a file 'program.txt' Here is my code so far. (This assumes ANTLR has run my grammar file and…
john k
  • 6,268
  • 4
  • 55
  • 59
16
votes
1 answer

Trouble Setting Up ANTLR 4 IDE on Eclipse Luna (4.4)

I'm trying to install the ANTLR 4 IDE on Eclipse Luna (4.4). I've installed it from the Marketplace but I have no idea how to create a project that has an ANTLR 4 Lexer/Parser in it. When I go to create a new project I don't see any options for…
amura.cxg
  • 2,348
  • 3
  • 21
  • 44
16
votes
3 answers

Is it possible to throw an exception if the input isn't valid?

I have a simple ANLTR grammar and accompanying Visitor. Everything works great, unless the input is invalid. If the input is invalid, the errors get swallowed and my calculator comes out with the wrong output. I've tried implementing an error…
RubberDuck
  • 11,933
  • 4
  • 50
  • 95
16
votes
2 answers

How can I access alternate labels in ANTLR4 while generically traversing a parse tree?

How can I access alternate labels in ANTLR4 while generically traversing a parse tree? Or alternatively, is there any way of replicating the functionality of the ^ operator of ANTLR3, as that would do the trick. I'm trying to write an AST pretty…
Chucky Ellison
  • 470
  • 6
  • 14
16
votes
2 answers

Processing a String with ANTLR4

I'm trying to convert my grammar from v3 to v4 and having some trouble finding all the right pieces. In v3 to process a String, I used: public static DataExtractor create(String dataspec) { CharStream stream = new ANTLRStringStream(dataspec); …
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
16
votes
2 answers

ANTLR 4 tree inject/rewrite operator

In ANTLR 3 you could just do the following: andExpression : (andnotExpression -> andnotExpression) (AND? a=andnotExpression -> ^(AndNode $andExpression $a))* ; Any idea how to do it in the new version?
NeatNerd
  • 2,305
  • 3
  • 26
  • 49
15
votes
2 answers

How do I escape an escape character with ANTLR 4?

Many languages bound a string with some sort of quote, like this: "Rob Malda is smart." ANTLR 4 can match such a string with a lexer rule like this: QuotedString : '"' .*? '"'; To use certain characters within the string, they must be escaped,…
james.garriss
  • 12,959
  • 7
  • 83
  • 96
15
votes
7 answers

Intellij will not recognize antlr generated source code

I am having trouble getting Intellij to recognize the generated source code from antlr4. Any reference to the generated code appears as errors, code completion doesn't work, etc. I am using maven and the antlr4-maven-plugin to generate the code. My…
oillio
  • 4,748
  • 5
  • 31
  • 37
15
votes
1 answer

Using ANTLR Parser and Lexer Separatly

I used ANTLR version 4 for creating compiler.First Phase was the Lexer part. I created "CompilerLexer.g4" file and putted lexer rules in it.It works fine. CompilerLexer.g4: lexer grammar CompilerLexer; INT : 'int' ; //1 FLOAT :…
user2998131
  • 205
  • 1
  • 2
  • 6
15
votes
2 answers

Get rid of token recognition error

How can I get rid of the default ANTLR recognition error? I want to write another message using my own error class instead of ANTLR's error. I mean is there any possibility that some ANTLR error classes can be extended in order to display my own…
Hakan Özler
  • 968
  • 1
  • 10
  • 22
15
votes
1 answer

how to use antlr4 visitor

I am a beginner of antlr. I was trying to use visitor in my code and following the instruction on the net. However, I found out that the visitor was not entering the method I create at all. May anyone tell me what I did wrong? This is my…
Sherwood Lee
  • 237
  • 2
  • 4
  • 12
15
votes
6 answers

How do I get help on the antlr4-maven-plugin

The antlr4-maven-plugin does not appear to be document on the Antlr4 website.
lexicalscope
  • 7,158
  • 6
  • 37
  • 57
14
votes
3 answers

Gradle can't find Antlr token file

I created a file MyLexer.g4 inside myproject/src/main/antlr/com/mypackage like: lexer grammar MyLexer; DIGIT : '0' .. '9' ; ... WS : [ \t\r\n]+ -> skip ; and then trying to write parser in MyParser.g4 in the same directory: grammar…
Dims
  • 47,675
  • 117
  • 331
  • 600