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
11
votes
1 answer

ANTLR4: TokenStreamRewriter output doesn't have proper format (removes whitespaces)

I am using Antlr4 and java7 grammar (source) for modifying an input Java Source file. More specifically, I am using the TokenStreamRewriter class to modify some tokens. The following code is a sample that shows how the tokens are modified: …
Mike B
  • 1,522
  • 1
  • 14
  • 24
11
votes
3 answers

Antlr4 C# targets and output path of generated files

I have a C# solution with an Antlr3 grammar file, and I'm trying to upgrade to Anltr4. It turns out the grammar was the easy part (it became better, and one third the size!). Generating the parser turned out to be the tricky part. In the old…
Anders Forsgren
  • 10,827
  • 4
  • 40
  • 77
11
votes
1 answer

How can non-associative operators like "<" be specified in ANTLR4 grammars?

In a rule expr : expr '<' expr | ...; the ANTLR parser will accept expressions like 1 < 2 < 3 (and construct left-associative trees corrsponding to brackets (1 < 2) < 3. You can tell ANTLR to treat operators as right associative, e.g. expr : expr…
user2818521
  • 111
  • 3
11
votes
1 answer

antlr4: ATN version 2 expected 3

When trying to use a generated grammar and lexer I get: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 2 (expected 3). What's wrong?
11
votes
1 answer

ANTLRv4: How to read double quote escaped double quotes in string?

In ANTLR v4, how do we parse this kind of string with double quote escaped double quotes like in VBA? for text: "some string with ""john doe"" in it" the goal would be to identify the string: some string with "john doe" in it And is it possible to…
JayDee
  • 949
  • 1
  • 13
  • 20
11
votes
2 answers

ANTLR 4 and AST visitors

I'm trying to use ASTs with ANTLR4, with this files: Builder.java import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.TokenStream; public…
GlinesMome
  • 1,549
  • 1
  • 22
  • 35
10
votes
3 answers

Convert Sql like custom dsl queries to ElasticSearch?

We are building our own query language similar to Mysql using antlr4. Except we only use where clause, in other words user does not enter select/from statements. I was able to create grammar for it and generate lexers/parsers/listeners in…
omurbek
  • 742
  • 1
  • 7
  • 23
10
votes
2 answers

Is there an existing ANTLR or IRONY grammar for R?

Does anyone know if there is an existing existing ANTLR or IRONY grammar for R? Many thanks.
Jonno
  • 789
  • 9
  • 26
10
votes
1 answer

ANTLR4: zero or one times

I'm working on defining a grammar using ANTLR4 and Java. For Integers, I want a number that mat be preceeded by a minus sign. I know it is possible to do it like this: integer: '-' (DIGIT)* | DIGIT* ; But I was wondering if there is a symbol…
Elias
  • 432
  • 6
  • 17
10
votes
1 answer

Maven only generates Antlr-sources in default package

I'll start with my pom.xml:
Meik Vtune
  • 450
  • 8
  • 25
10
votes
1 answer

Antlr4 Javascript Visitor

I'm currently trying to develope a JavaScript Compiler with the help of an Antlr4 Visitor. I've got this already implemented with Java but cannot figure out how to do this in JavaScript. Probably somebody can answer me a few questions? 1: In Java…
Bruno
  • 894
  • 11
  • 32
10
votes
1 answer

ANTLR 4 - How to access hidden comment channel from custom listener?

Writing a pretty-printer for legacy code in an older language. The plan is for me to learn parsing and unparsing before I write a translator to output C++. I kind of got thrown into the deep end with Java and ANTLR back in June, so I definitely have…
Shelby S.
  • 157
  • 1
  • 5
  • 16
10
votes
1 answer

Can a walker be stopped?

I have a ParseTree listener implementation that I'm using to fetch global-scope declarations in standard VBA modules: public class DeclarationSectionListener : DeclarationListener { private bool _insideProcedure; public override void…
Mathieu Guindon
  • 69,817
  • 8
  • 107
  • 235
10
votes
1 answer

How to implement error handling in ANTLR4

I have the following grammar for parsing first-order logic formulas applied over graphs: grammar Graph; /*------------------------------------------------------------------ * PARSER…
Wosh
  • 1,565
  • 2
  • 17
  • 30
10
votes
6 answers

What is the format for specifying a package in the Antlr4 maven plugin?

What is the format for specifying a package in the Antlr4 maven plugin antlr4-maven-plugin? I feel like I should be able to do the following: com.tunnelvisionlabs antlr4-maven-plugin
Brian Kent
  • 3,754
  • 1
  • 26
  • 31