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
268
votes
5 answers

ANTLR: Is there a simple example?

I'd like to get started with ANTLR, but after spending a few hours reviewing the examples at the antlr.org site, I still can't get a clear understanding of the grammar to Java process. Is there some simple example, something like a four-operations…
Eli
  • 6,353
  • 9
  • 30
  • 25
107
votes
2 answers

What is a 'semantic predicate' in ANTLR?

What is a semantic predicate in ANTLR?
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
95
votes
5 answers

Handling errors in ANTLR4

The default behavior when the parser doesn't know what to do is to print messages to the terminal like: line 1:23 missing DECIMAL at '}' This is a good message, but in the wrong place. I'd rather receive this as an exception. I've tried using the…
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
89
votes
4 answers

How to create AST with ANTLR4?

I've been searching A LOT about this and I couldn't find anything useful that REALLY helps me build an AST. I already know that ANTLR4 doesn't build AST like ANTLR3 used to do. Everyone say: "Hey, use visitors!", but I couldn't find any example or…
Leandro
  • 1,114
  • 1
  • 12
  • 15
72
votes
14 answers

ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1

I'm getting an error in DataBindingMapperImpl.java for one specific data binding which results in the following error when building the project. ANTLR Tool version 4.5.3 used for code generation does not match the current runtime version 4.7.1.…
Casper Lindberg
  • 1,053
  • 1
  • 11
  • 16
66
votes
1 answer

If/else statements in ANTLR using listeners

I'm creating a simple programming language for a school project. I'm using ANTLR 4 to generate a lexer and a parser from my grammar. Until now, I have been using ANTLRs listener pattern to apply the actual functionality of the programming…
simonbs
  • 7,932
  • 13
  • 69
  • 115
65
votes
7 answers

antlr4-Can't load Hello as lexer or parser

I recently have to use parser to do a project. I download ANTLR4 and follow the steps described in the book The Definitive ANTLR4 Reference. The following are the steps I type in command line: 1. export…
user3162707
  • 721
  • 1
  • 7
  • 7
64
votes
5 answers

Antlr4 Listeners and Visitors - which to implement?

I'm reading "The Definitive Antlr 4 Reference" and get the idea in relation to how Listeners and Visitors work. The book explains particularly well how Listeners relate to SAX parsers and makes it obvious when methods are going to be called during…
KevinY
  • 1,229
  • 1
  • 12
  • 27
56
votes
6 answers

Webpack Error - configuration.node has an unknown property 'fs'

I have encountered an error when using the latest version of Webpack (5.1.0). It looks like the configuration is throwing an error because the validation schema is too restrictive. Here is my webpack configuration file in a gist, and the error…
redab
  • 1,551
  • 1
  • 10
  • 9
52
votes
3 answers

ANTLR 4.5 - Mismatched Input 'x' expecting 'x'

I have been starting to use ANTLR and have noticed that it is pretty fickle with its lexer rules. An extremely frustrating example is the following: grammar output; test: FILEPATH NEWLINE TITLE ; FILEPATH:…
Chiune Sugihara
  • 1,179
  • 1
  • 7
  • 14
47
votes
3 answers

How do I get the original text that an antlr4 rule matched?

Using the Java 7 grammar https://github.com/antlr/grammars-v4/blob/master/java7/Java7.g4 I want to find methods with a specific name and then just print out that method. I see that I can use the methodDeclaration rule when I match. So I subclass…
monty0
  • 1,759
  • 1
  • 14
  • 22
38
votes
2 answers

Is "Implicit token definition in parser rule" something to worry about?

I'm creating my first grammar with ANTLR and ANTLRWorks 2. I have mostly finished the grammar itself (it recognizes the code written in the described language and builds correct parse trees), but I haven't started anything beyond that. What worries…
Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
38
votes
1 answer

Once grammar is complete, what's the best way to walk an ANTLR v4 tree?

Goal I'm working on a project to create a Varscoper for Coldfusion CFscript. Basically, this means checking through source code files to ensure that developers have properly var'd their variables. After a couple of days of working with ANTLR V4 I…
Owen Allen
  • 11,348
  • 9
  • 51
  • 63
33
votes
1 answer

ANTLR 4 $channel = HIDDEN and options

I need help with my ANTLR 4 grammar after deciding to switch to v4 from v3. I am not very experienced with ANTLR so I am really sorry if my question is dumb ;) In v3 I used the following code to detect Java-style comments: COMMENT : '//'…
user2055330
  • 333
  • 1
  • 3
  • 4
1
2 3
99 100