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

ANTLR-4.7.1:The type ANTLRInputStream is deprecated

import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.tree.*; public class Test{ public static void main(String[] args) throws Exception{ ANTLRInputStream input = new ANTLRInputStream(System.in); ArrayInitLexer lexer = new…
Daxer
  • 61
  • 1
  • 4
6
votes
0 answers

Antlr4 - generate grammar source for more language in gradle

In my project I have to generate the grammar sources for more than one language (Java, Javascript and Python) using gradle. I'm using the antlr plugin, so I have the following rows in my build.gradle file: apply plugin:…
user6180
  • 61
  • 2
6
votes
2 answers

Retrieving Base class from antlrcpp::Any

I'm currently porting over some code from the C# version of ANTLR4 to the C++ target and I'm currently running into some issues. The way I built my AST in C# was to create a base class (let's call it Base) and derived classes (let's called it…
Thatguypat
  • 163
  • 2
  • 6
6
votes
1 answer

What's the difference between ANTLR4's errorListener and errorHandler?

I want to get the specific error message of ANTLR4's parser. And I found that there are two way to handle error: errorListener and errorHandler. // set error handler parser.removeErrorListeners(); parser.addErrorListener(new…
lulijun
  • 415
  • 3
  • 22
6
votes
2 answers

Antlr not working with VS2017

I am trying to set up a simple project with Antlr in .net core 1.0 project using VS2017. Following https://github.com/sharwell/antlr4cs, added .g4 file to the project. The project file looks like this,
Dhanuka777
  • 8,331
  • 7
  • 70
  • 126
6
votes
1 answer

Antlr4 how to build a grammar allowed keywords as identifier

This is a demo code label: var id let id = 10 goto label If allowed keyword as identifier will be let: var var let var = 10 goto let This is totally legal code. But it seems very hard to do this in antlr. AFAIK, If antlr match a token let, will…
wener
  • 7,191
  • 6
  • 54
  • 78
6
votes
3 answers

ANTLR4 parse rule to match open/close brackets

I'm parsing a language that has a statement 'code' followed by '{', followed by a bunch of code that I have no interest in parsing, followed by '}'. I'd ideally like to have a rule like: skip_code: 'code' '{' ~['}']* '}' ..which would simply skip…
Stan
  • 1,227
  • 12
  • 26
6
votes
1 answer

ANTLR doesn't give correct output tokens for Scala Grammar

I am new to Scala and I am trying to parse Scala files with the use of Scala Grammar and ANTLR. Below is the code for Scala Grammar which I got from the git hub link: https://github.com/antlr/grammars-v4/tree/master/scala There are chances of repo…
shiva
  • 730
  • 8
  • 25
6
votes
1 answer

Generate source code from AST with Antlr4 and StringTemplates

If I have an AST and modify it, can I use StringTemplates to generate the source code for the modified AST? I have successfully implemented my grammar for Antlr4. It generates the AST of a source code and I use the Visitor Class to perform the…
50k4
  • 241
  • 2
  • 11
6
votes
2 answers

In Antlr4, C#, Error Handling isn't getting fired as expected

In Antlr 4, I have a grammar which takes strings of format x*Y, x+y, etc I want to catch an error when an invalid operator is used, such as x&y. If I add code like: parser.RemoveErrorListeners(); parser.AddErrorListener(new MyErrorListener()); And…
shuri17
  • 341
  • 1
  • 2
  • 10
6
votes
3 answers

Implement Language Auto-Completion based on ANTLR4 Grammar

I am wondering if are there any examples (googling I haven't found any) of TAB auto-complete solutions for Command Line Interface (console), that use ANTLR4 grammars for predicting the next term (like in a REPL model). I've written a PL/SQL grammar…
Antonello
  • 1,326
  • 2
  • 16
  • 23
6
votes
1 answer

Controlling false IntelliJ code editor error in Scala plugin

I have Java code generated from ANTLR4. Scala is using the Java code by extending some of the methods. The issue is that IntelliJ's scala plugin does not seem to know the relationship between Java base class and Scala subclass to show a false…
prosseek
  • 182,215
  • 215
  • 566
  • 871
6
votes
2 answers

Treat invalid chars as a single token in ANTLR4 lexer

I'm using the JSON grammar from the antlr4 grammar repository to parse JSON files for an editor plugin. It works, but reports invalid chars one by one. The following snippet results in 18 lexer errors: { sometext-without-quotes : 42 } I want to…
msteiger
  • 2,024
  • 16
  • 22
6
votes
1 answer

How can I import an ANTLR lexer grammar into another grammar using Gradle 2.10?

I've been learning about ANTLR 4 with Terence Parr's The Definitive ANTLR 4 Reference, which I've been following so far using Gradle 2.10 and its built-in ANTLR plugin. However I'm having some trouble getting some code which I adapted from Chapter…
jbduncan
  • 425
  • 9
  • 17
6
votes
1 answer

ANTLR 4 RuntimeMetaData and Vocabulary

I am using the ANTLR4 IntelliJ plug-in, and I am using a the Antlr-4.5.1 Jar. When I Generate Antlr Recognizer in Lexer and Parser can't resolve symbols RuntimeMetaData and Vocabulary. If I use the command line to compile and run my .g4 file it…
DonnellyOverflow
  • 3,981
  • 6
  • 27
  • 39