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

Where is tree grammars in ANTLR4?

I'm going to migrate from ANTLR3 to ANTLR4, I appreciated auto-left recursion resolving, but..I did not found tree grammars, -> instructions supported. Could anyone explain what is the reason of removing of this feature and what would I do? If I…
Jussi P.
  • 71
  • 4
7
votes
1 answer

Visitor/Listener code for a while loop in ANTLR 4

So far i have searched the whole "The Definitive ANTLR 4 Reference" book as well as many sites for an answer to my question and i still can't find anything on the topic. I am using ANTLR 4 to create a sub-set of the C language that does some basic…
Greg
  • 403
  • 2
  • 6
  • 16
7
votes
1 answer

antlr4: how to know which alternative is chosen given a context

Assume there is a rule about 'type'. It is either a predefined type (referred by IDENTIFIER) or a typeDescriptor. type : IDENTIFIER | typeDescriptor ; In my program, I have got an instance of typeContext 'ctx'. How do I know if the path…
qinsoon
  • 1,433
  • 2
  • 15
  • 34
7
votes
1 answer

Switching Antlr lexer modes from parser

How to control switching Antlr lexer modes from parser actions? I extended the generated Parser and Lexer so I can call pushMode and popMode from parser witch in turn call suitable pushMode and popMode of lexer but with no effect. Is there any thing…
Angie
  • 319
  • 3
  • 12
7
votes
1 answer

Checked exceptions in visitors

I am learning ANTLR4 and I have no previous experience with parser generators. When I define my own visitor implementation, I have to override methods of the BaseVisitor (I am looking for instance at the EvalVisitor class at page 40 of the book).…
7
votes
2 answers

'´' came as a complete surprise to me

I'm having trouble setting up ANTLR v4 for C#, anyone keen to help me? I'm using the build target referred from the official site here https://github.com/sharwell/antlr4cs I added a simple grammar Foo.g grammar Foo; r : 'hello' ID ; //…
ains
  • 1,436
  • 4
  • 18
  • 33
7
votes
1 answer

Defining rule that accepts arguments in any order

I have a rule that reads as interfaceCommands : descriptionCmd ipAddressCmd otherCmd ; Though the expected order of Cmds is as stated in the grammar, I also should be able to accept the input when the order of these Cmds…
callmeDP
  • 93
  • 7
7
votes
3 answers

How to match any symbol in ANTLR parser (not lexer)?

How to match any symbol in ANTLR parser (not lexer)? Where is the complete language description for ANTLR4 parsers? UPDATE Is the answer is "impossible"?
Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
7
votes
1 answer

ANTLR 4: Dynamic Token

Given: grammar Hbs; var: START_DELIM ID END_DELIM; START_DELIM: '{{'; END_DELIM: '}}'; I'd like to know how to change START_DELIM and END_DELIM at runtime to be for example <% and %>. Does anyone know how to do this in ANTLR 4? Thanks.
Edgar Espina
  • 507
  • 4
  • 10
7
votes
1 answer

ANTLR4: Tree construction

I am extending the baseClass Listener and am attempting to read in some values, however there doesnt seem to be any hierrarchy in the order. A cut down version of my grammar is as follows: start: config_options+ config_options: (KEY) EQUALS^ (PATH |…
Har
  • 3,727
  • 10
  • 41
  • 75
7
votes
3 answers

Using Antlr for parsing data from never-ending stream

Is Antlr suitable for parsing data from streams that don't have EOF right after the text to parse? According to my observation, the lexer does not emit the current token until the first character of next token is received. On top of that - the…
AndreyP
  • 91
  • 1
  • 4
7
votes
1 answer

Is ANTLR 4 faster than ANTLR 3?

What should I expect from ANTLR 4? Is it faster ANTLR 3? I mean the parsing speed. Note code generation speed would be interesting too. For design reasons?
Aftershock
  • 5,205
  • 4
  • 51
  • 64
6
votes
1 answer

Repeat evaluation of the same visitor in Antlr 4

I built an Antlr 4 filter using a grammar (not important in the context here) and the filters look something like "age > 30 AND height < 6.1". However, the question is, I'll build this filter once, and use it to evaluate maybe a thousand docs. Each…
Eddie Xie
  • 875
  • 1
  • 8
  • 20
6
votes
2 answers

Is there any example of language grammar that possible for Yacc to express but impossible for Antlr4?

I try to learn about language parser recently and always seen the review about difference in Yacc and Antlr (about LALR and LL). It was always some concluded wording like "LALR is more powerful". But I can't understand what its really means So could…
Thaina Yu
  • 1,372
  • 2
  • 16
  • 27
6
votes
2 answers

ANTLR4 : ordering problem of parser rules for a keyword used in several rules (AND, BETWEEN AND)

I am having a problem while parsing some SQL typed string with ANTLR4. The parsed string is : WHERE a <> 17106 AND b BETWEEN c AND d AND e BTW(f, g) Here is a snippet of my grammar : where_clause : WHERE element ; element : element…
Kronos
  • 174
  • 11