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

how print parse-tree using python2 runtime with antlr4

I'm trying to use antlr4 version 4.4 and the python2 runtime. The grammar is from the antlr4 book, page 6, file: Hello.g4: grammar Hello; r : 'hello' ID ; ID : [a-z]+ ; WS : [ \t\r\n]+ -> skip ; and I generate lexer and parser with…
lgwest
  • 1,347
  • 5
  • 16
  • 26
6
votes
2 answers

'a-zA-Z' came as a complete surprise to me while matching alternative

I have problem generating my grammar defintion with antlr v4: grammar TagExpression; expr : not expr | expr and expr | expr or expr | '(' expr ')' | tag ; tag : [a-zA-Z]+ ; and : '&' ; or : '|' ; not : '!' ; WS : [ \t\n\r]+ -> skip ; The…
user3749218
  • 61
  • 1
  • 2
6
votes
1 answer

Understanding the context data structure in Antlr4

I'm trying to write a code translator in Java with the help of Antlr4 and had great success with the grammar part so far. However I'm now banging my head against a wall wrapping my mind around the parse tree data structure that I need to work on…
user2323596
  • 523
  • 1
  • 6
  • 11
6
votes
1 answer

antlr 4.2.2 output to console warning (157)

I downloaded latest release of ANTLR - 4.2.2 (antlr-4.2.2-complete.jar) When I use it to generate parsers for grammar file Java.g4 it prints me some warnings like: "Java.g4:525:16: rule 'expression' contains an 'assoc' terminal option in an…
6
votes
2 answers

How to avoid mutual left-recursion in ANTLR 4

I am writing a grammar to handle scalar and vector expressions. The grammar below is simplified to show the problem I have where a scalar expression can be derived from a vector and a vector can be derived from a scalar. For example, a vector could…
Rangi Keen
  • 935
  • 9
  • 29
6
votes
1 answer

Translation of PL/SQL code to Java using Antlr 4 and stringtemplate 4

I am trying to construct a translator that could convert PL/SQL code to Java using Antlr 4 and StringTemplate 4. I have the grammar of PL/SQl and have already build a parser for PL/SQL but i have no idea how to approach the problem further. I found…
ayush narula
  • 11,275
  • 2
  • 15
  • 18
6
votes
2 answers

Is it feasible to use Antlr for source code completion?

I don't know, if this question is valid since i'm not very familiar with source code parsing. My goal is to write a source code completion function for one existing programming language (Language "X") for learning purposes. Is Antlr(v4) suitable for…
Dante
  • 10,722
  • 16
  • 51
  • 63
6
votes
1 answer

Good CSS grammar in Antlr v4

Are there any good CSS grammars out there for antlr4? I know there are some grammars for antlr3, but it turns out CSS is not trivial to parse without "lexer modes", which were added in v4. Why? Consider the following CSS selectors: .hello.world { /*…
gzak
  • 3,908
  • 6
  • 33
  • 56
6
votes
4 answers

Antlr4 "Test Rig" and maven

I am struggling a little bit to figure out how I can use the antlr4 maven plug in to run the TestRig class? I've read though the output of mvn antlr4:help -Ddetail=true, which is the only documentation I have been able to find, but this doesn't…
JustDanyul
  • 13,813
  • 7
  • 53
  • 71
6
votes
1 answer

How to collect errors during run time given by a parser in Antlr4

I have upgraded from Antlr 3 to Antlr 4. I was using this code to catch exceptions using this code. But this is not working for Antlr 4. partial class XParser { public override void ReportError(RecognitionException e) { …
diyoda_
  • 5,274
  • 8
  • 57
  • 89
6
votes
2 answers

ANTLR4 Lexer Matching Start of Line End Of Line

How to achieve Perl regular expression ^ and $ in the ANLTR4 lexer? ie. to match the start of a line and end of a line without consuming any character. I am trying to use ANTLR4 lexer to match a # character at the start of a line but not in the…
JavaMan
  • 4,954
  • 4
  • 41
  • 69
6
votes
1 answer

antlr4: Ignore superflous tokens when creating ParseTrees

I am developing a compiler for the real-time language PEARL with ANTLR4. With ANTLR4 my ParseTree is populated with superflous tokens like e.g. semicolons for ending a grammatical unit. Is there a way to tell ANTLR to ignore these kinds of token?
6
votes
1 answer

ANTLR4: Obtaining list of tokens for a specific rule in Listener

I am extending a Listener in ANTLR4 and I want to get all of the tokens which are associated with a particular rule in the parser, is there a method in doing that? i.e. myConfiguration: CONFIG EQUALS parameters ; parameters: ALPHANUMERIC+ CONFIG:…
Har
  • 3,727
  • 10
  • 41
  • 75
6
votes
2 answers

antlr 4 Can't load as lexer or parser

I'm using antlr 4 to write my grammar. I would like to see the gui three generated by my grammar. When I try to run the example on the antlr site (http://www.antlr.org/wiki/display/ANTLR4/Getting+Started+with+ANTLR+v4) it works, but if I try with…
mary
  • 305
  • 3
  • 12
5
votes
1 answer

Dynamically create lexer rule

Here is a simple rule: NAME : 'name1' | 'name2' | 'name3'; Is it possible to provide alternatives for such rule dynamically using an array that contains strings?
Villa F.
  • 55
  • 1
  • 5