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

How to build a JavaScript ANTLR visitor

I actually have a problem with the implementation of an antlr-visitor in JavaScript. I have already created a grammar. However, I found no documentation, example or tutorial. Somehow only multi-dimensional arrays are returned. A similar problem…
Simon
  • 354
  • 1
  • 5
  • 15
8
votes
2 answers

ANTLR v4, JavaLexer and JavaParser returning null as parse tree

I am using antlr v4 for extracting parse tree of java programs for other purposes. I have started from this sample: ANTLR v4 visitor sample And I have tested the steps on given link to check if it works and everything gone right: java Run a = 1+2 b…
8
votes
2 answers

How to split an ANTLR grammar file into multiple ones

I have a large grammar file, and plan to split it into multiple ones, so that I can reuse some of those smaller files in another grammar file. I have tried doing it but failed. Can you please tell if such a feature is available, and if so, please…
Nishanth Reddy
  • 589
  • 2
  • 14
  • 27
8
votes
1 answer

Integrating antlr4 with LLVM

I am developing a compiler using ANTLR and LLVM. I have already implemented a lexer and a parser using ANTLR 4's Eclipse IDE. I want to implement a semantic analyzer and a code generator using LLVM. For this I want to know how to integrate the…
NewGirl
  • 101
  • 1
  • 4
8
votes
1 answer

ANTLR4: Any difference between import and tokenVocab?

The import statement or the tokenVocab option can be put in a parser grammar to reuse a lexer grammar. Sam Harwell advises to always use tokenVocab rather than import [1]. Is there any difference between import and tokenVocab? If there's no…
Roger Costello
  • 3,007
  • 1
  • 22
  • 43
8
votes
2 answers

Is there any REST API query standard / DSL to express complex filters in GET URL?

I am currently researching for an elegant way, for my REST API under development, to express filters for GET requests returning collections. I'd like to express queries as "http://[...]?filter=expressions", where I am going to evaluate the…
Marius Schmidt
  • 633
  • 6
  • 18
8
votes
3 answers

How to preserve whitespace when we use text attribute in Antlr4

I want to keep white space when I call text attribute of token, is there any way to do it? Here is the situation: We have the following code IF L > 40 THEN; ELSE IF A = 20 THEN PUT "HELLO"; In this case, I want to transform it into: if…
Hai Pham
  • 173
  • 2
  • 9
8
votes
1 answer

Antlr Extraneous Input

I have a grammar file BoardFile.g4 that has (relevant parts only): grammar Board; //Tokens GADGET : 'squareBumper' | 'circleBumper' | 'triangleBumper' | 'leftFlipper' | 'rightFlipper' | 'absorber' | 'portal' ; NAME : [A-Za-z_][A-Za-z_0-9]* ; INT :…
tripatheea
  • 311
  • 1
  • 3
  • 12
8
votes
5 answers

ANTLR4 Parser, Visitor not created

I'm new to ANTLR and trying to write grammar in ANTLR4 without any prior brush with the previous version. I'm following the book 'The Definitive ANTLR 4 Reference'. I use Eclipse and installed ANTLR4 IDE as given in here. I wrote the following…
Pacu
  • 163
  • 1
  • 2
  • 8
8
votes
1 answer

How do I pretty-print productions and line numbers, using ANTLR4?

I'm trying to write a piece of code that will take an ANTLR4 parser and use it to generate ASTs for inputs similar to the ones given by the -tree option on grun (misc.TestRig). However, I'd additionally like for the output to include all the line…
Chucky Ellison
  • 470
  • 6
  • 14
8
votes
4 answers

ANTLR and Eclipse (or any decent IDE)

I have been using ANTLR with Eclipse for some time using the ANTLRv3IDE plugin. While it is not perfect, and a bit outdated, it does its job reasonably well. Now I am looking to switch to ANTLRv4 for another DSL that I am creating. However, Eclipse…
Krumelur
  • 31,081
  • 7
  • 77
  • 119
8
votes
1 answer

How can I build an AST using ANTLR4?

I have an ANTLR3 grammar that builds an abstract syntax tree. I'm looking into upgrading to ANTLR4. However, it appears that ANTLR4 only builds parse trees and not abstract syntax trees. For example, the output=AST option is no longer recognized.…
Peter Chapin
  • 330
  • 3
  • 9
8
votes
1 answer

antlr4 parser re-use and warm up

In my use case I have to parse several thousand small and independent expressions into a tree representation using a Visitor on the generated parse trees. Currently new streams, lexer and parser instances are created for each parse operation. I…
FreeJack
  • 778
  • 6
  • 7
7
votes
0 answers

Using ANTLR with Visual Studio 2022

I can't find any documentation that shows how to get ANTLR support in Visual Studio 2022. It seems like the ANTLR extension developers haven't kept up with the changes made to the Visual Studio API object model: ANTLR VSIX is only compatible with…
MikeB
  • 1,452
  • 14
  • 28
7
votes
1 answer

How to implement the visitor pattern for nested function

I am a newbie to Antlr and I wanted the below implementation to be done using Antlr4. I am having the below-written functions. 1. FUNCTION.add(Integer a,Integer b) 2. FUNCTION.concat(String a,String b) 3. FUNCTION.mul(Integer a,Integer b) And I am…
ashok
  • 1,078
  • 3
  • 20
  • 63