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

Why does Antlr default to certain alternatives over others?

I am using the antlr4 plugin for IntelliJ to create a grammar for a project I'm working on. In the process of learning how to use antlr, I took a look at Github user shmatov's simple antlr4 calculator shown below. grammar Calculator; INT :…
Bankst
  • 97
  • 1
  • 1
  • 4
1
vote
1 answer

ANTLR4 g4 grammar to read key/value pair in different blocks

I'm new to antlr, i'm trying to make simple grammar but i can't succeeded. I would like to parse this kind of file: BEGIN HEADER CharacterSet "CP1252" END HEADER BEGIN DSJOB test "val" END DSJOB BEGIN DSJOB test "val2" END DS JOB I'm…
Damien F
  • 415
  • 1
  • 5
  • 8
1
vote
1 answer

ANTLR4 generated code not compiling: " cannot override reset()"

using ANTLR4 with maven, Java8, I cannot compile generated sources. I tried several ANTLR versions (started with 4.7.1, but got the same Problem down to…
Walter Kuhn
  • 479
  • 1
  • 6
  • 19
1
vote
1 answer

Reuse Antlr objects for a new input string (C++ runtime)?

I've built a basic parser using the C++ runtime demo and it works fine. However I typically parse a lot of input strings, can the code be modified to reuse existing objects for repeated calls? If so, does anyone have an example of this?
Jason
  • 531
  • 6
  • 19
1
vote
1 answer

Antlr4 grammar unexpected error (C++ target)

I'm using the C++ target of Antlr 4.7.1 and certain input strings trigger an unexpected error. The string that doesn't parse with the grammar file as-is: keys abc-def; Error given: Line(1:5) Error(mismatched input 'abc-def' expecting…
Jason
  • 531
  • 6
  • 19
1
vote
1 answer

Handling blank lines when White Space is important in ANTLR4

This may be a newbee question, since I don't have a lot of ANTLR experience, but I've done a lot of research and troubleshooting and have not found a solution so resorting to asking. I am trying to write a parser for a very odd format file (PCGEN…
Joe Bryant
  • 21
  • 4
1
vote
2 answers

Am I making a context free grammar context sensitive and does it matter?

If I have a language with basic things such as a = expression if expression then ... while expression do ... then I might have a grammar that looks like this: (pseudo code) assignment: identifier Equals expression ; if : If expression Then…
David
  • 5,991
  • 5
  • 33
  • 39
1
vote
1 answer

How do I use ANTLR to check for valid variable names?

We all know variable names cannot start with a number. (eg. foo1 is valid, 1foo is not). I am trying to write a grammar file to allow only valid variable names, and they must be followed by a colon. (This is part of a much larger grammar - I'm…
john k
  • 6,268
  • 4
  • 55
  • 59
1
vote
3 answers

ANTLR "no viable alternative at input" Error for parsing SAS code If Then Else

I am new to ANTLR and working on a parser to parse SAS code which mainly comprises of if then else if statements. I have created the following grammar to parse the code but I am getting error in Intellij when I tried to run using sample…
Ajay Sant
  • 665
  • 1
  • 11
  • 21
1
vote
1 answer

How to return a value from ANTLR action?

I'd like to know how: return a runtime value from a rule action access that value from the parent rule's action. With ANTLR I want to build a custom tree using actions (not listeners/visitors which are too complex). And I'd like to have a parser…
Pavel Vlasov
  • 4,206
  • 6
  • 41
  • 54
1
vote
1 answer

can antlr semantic predicates access grammar symbols

The antlr book has the following sample code to resolve grammar ambiguities using semantic predicates: // predicates/PredCppStat.g4 @parser::members { Set types = new HashSet() {{add("T");}}; boolean istype() { return…
Riaz
  • 69
  • 5
1
vote
2 answers

Antlr4 parsing issue

When I try to work my message.expr file with Zmes.g4 grammar file via antlr-4.7.1-complete only first line works and there is no reaction for second one. Grammar is grammar Zmes; prog : stat+; stat : (message|define); message : 'MSG' …
Tekin Güllü
  • 363
  • 1
  • 3
  • 18
1
vote
1 answer

Antlr - Why it expect FunctionCall but PrintCommand gave

my Antlr-grammar expect a FunctionCall but in my example-code for the compiler built by antlr, i wrote a print-command. Does someone know why and how to fix that? The print-command is named: RetroBox.show(); The print-command should be recognised…
1
vote
2 answers

Antlr parsing rule to parse string with matching braces

I have a parser rule which looks like below nested_query: ~(LPARAN | RPARAN)+? LPARAN nested_query RPARAN ~(LPARAN | RPARAN)+? | nested_query nested_query_op LPARAN nested_query RPARAN | ~(LPARAN | RPARAN)+? ; nested_query_op: binary_in…
tuk
  • 5,941
  • 14
  • 79
  • 162
1
vote
2 answers

ANTLR4: How to parse a WKT polygon string?

I'm using ANLTR4 in java and I could parse a WKT polygon string like this polygon((20 30, 30 40, 50 60, 20 30)) with this Lexer: POLYGON: ('polygon'|'POLYGON')'(('[0-9:,-.eTZ" ]+'))'; because the numbers inside polygon(( )) can be datetime or…
Bằng Rikimaru
  • 1,512
  • 2
  • 24
  • 50
1 2 3
99
100