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

ANTLR4 Python parsing big files

I am trying to write parsers for juniper/srx router access control lists. Below is the grammar I am using: grammar SRXBackend; acl: 'security' '{' 'policies' '{' COMMENT* replaceStmt '{' policy* '}' '}' '}' applications …
prthrokz
  • 1,120
  • 8
  • 16
27
votes
2 answers

ANTLR4 visitor pattern on simple arithmetic example

I am a complete ANTLR4 newbie, so please forgive my ignorance. I ran into this presentation where a very simple arithmetic expression grammar is defined. It looks like: grammar Expressions; start : expr ; expr : left=expr op=('*'|'/') right=expr…
Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94
27
votes
2 answers

ANTLR4: Whitespace handling

I have seen many ANTLR grammars that use whitespace handling like this: WS: [ \n\t\r]+ -> skip; // or WS: [ \n\t\r]+ -> channel(HIDDEN); So the whitespaces are thrown away respectively send to the hidden channel. With a grammar like this: grammar…
flux
  • 275
  • 1
  • 3
  • 5
24
votes
2 answers

How to configure antlr4 plugin for Intellij IDEA

I looked all over the place for how to configure the antlr4 plugin for IntelliJ IDEA. But I can't find anything. I was only able to install the plugin. If I add .g4 files manually for a empty project I get the "Generate ANTLR Recognizer" option in…
Morpheus
  • 1,722
  • 5
  • 27
  • 38
21
votes
1 answer

Negating inside lexer- and parser rules

How can the negation meta-character, ~, be used in ANTLR's lexer- and parser rules?
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
20
votes
4 answers

ANTLR 4: Avoid error printing to console

With ANTLR I parse a grammar. The ANTLR errors are indicated in a custom editor for the grammar. However I would like to disable the printing of error messages to a Java console. I've implemented my own BaseErrorListener and removed the default as…
Marcel
  • 502
  • 3
  • 11
19
votes
2 answers

Get all Antlr parsing errors as list of string

How can I get all parsing errors of Antlr in a list of strings? I use antlr as follows: ANTLRInputStream input = new ANTLRInputStream(System.in); grLexer lexer = new grLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); grParser…
HamedFathi
  • 3,667
  • 5
  • 32
  • 72
19
votes
4 answers

Antlr4 C# Application Tutorial/Example

I want to use Antlr4 to parse some files in my C# application. I have been able to generate the parser and lexer files so far given my grammer. Now I would like to use read in the files and apply the parser and lexer to them. I have been…
Neil Pittman
  • 359
  • 1
  • 3
  • 12
18
votes
7 answers

What is minimal sample Gradle project for ANTLR4 (with antlr plugin)?

I have created new Gradle project, added apply plugin: 'antlr' and dependencies { antlr "org.antlr:antlr4:4.5.3" to build.gradle. Created src/main/antlr/test.g4 file with the following content grammar test; r : 'hello' ID; ID : [a-z]+ ; WS…
Dims
  • 47,675
  • 117
  • 331
  • 600
18
votes
2 answers

Nested Boolean Expression Parser using ANTLR

I'm trying to parse a Nested Boolean Expression and get the individual conditions within the expression separately. For e.g., if the input string is: (A = a OR B = b OR C = c AND ((D = d AND E = e) OR (F = f AND G = g))) I would like to get the…
Sud
  • 183
  • 1
  • 1
  • 7
18
votes
5 answers

Generating ANTLR4 grammar files with package declaration in gradle

I try to use the gradle antlr plugin, but run into several problems. I have a grammar file called wls.g4: grammar WlsScript; @header { package hu.pmi.wls.antlr.ws; } program : 'statementList'? EOF ; // Several more grammar and lexer…
Balage1551
  • 1,037
  • 1
  • 10
  • 28
18
votes
1 answer

How to define tokens that can appear in multiple lexical modes in ANTLR4?

I am learning ANTLR4 and was trying to play with lexical modes. How can I have the same token appear in multiple lexical modes? As a very simple example, let's say my grammar has two modes, and I want to match white space and end-of-lines in both of…
medhat
  • 303
  • 2
  • 4
18
votes
4 answers

antlr4 sql grammar

Does ANTLR4 have a sql grammar available? If so, where can I find it? There is a link from the ANTLR wiki, but the link is broken: grammar list
Chris Snow
  • 23,813
  • 35
  • 144
  • 309
17
votes
1 answer

ANTLR4 grammar token recognition error after import

I am using a parser grammar and a lexer grammar for antlr4 from GitHub to parse PHP in Python3. When I use these grammars directly my PoC code works: antlr-test.py from antlr4 import * # from PHPParentLexer import PHPParentLexer # from…
Diarmaid
  • 2,706
  • 2
  • 16
  • 19
17
votes
1 answer

Is there an ANTLR4 grammar for YAML?

Since YAML has a rather complicated syntax, is it possible to write a parser for YAML mainly using ANTLR4 ? I was looking for examples, that implement the YAML like indentation parsing and the detection of data types.
JE42
  • 4,881
  • 6
  • 41
  • 51