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

Why Antrl4 ErrorListener is not firing SyntaxError method?

I have a grammar on Antlr4. I am trying to implement a custom error listeners (to grab all syntax errors to a UI control). I have followed the guidelines to implement: class ReaderErrorListener : IAntlrErrorListener { public void…
NirMH
  • 4,769
  • 3
  • 44
  • 69
1
vote
2 answers

How to detect an expression result is unused in an interpreted programming language?

I'm working on a simple procedural interpreted scripting language, written in Java using ANTLR4. Just a hobby project. I have written a few DSLs using ANTLR4 and the lexer and parser presented no real problems. I got quite a bit of the language…
san-ho-zay
  • 170
  • 1
  • 7
1
vote
1 answer

ANTLR4: have parser rule match token wildcards

I am working on a grammar that allows for keywords as identifiers, and the current suggestion seems to be to do something like: id : 'if'|'call'|'then'|ID; My language has a lot of keywords, so I've been doing: id: ~(PLUS | MINUS); Basically any…
1
vote
1 answer

ANTLR4 JavaScript parser: how to catch an error in parsing

I have a grammar in ANTLR4 around which I am writing an application. A snippet of the pertinent grammar is shown below: grammar SomeGrammar; // ... a bunch of other parse rules operand : id | literal ; id : ID ; literal : LITERAL ; // A…
Sonny
  • 2,103
  • 1
  • 26
  • 34
1
vote
1 answer

ANTLR correct grammar for if statement

I have created a simple grammar for a language I have decided to call SWL: grammar swl; program : 'begin' statement+ 'end'; statement : assign | add | sub | mul | div | print | whilecond | ifcond ; condition : expr ; expr : '(' expr ')' |…
Emma Rossignoli
  • 935
  • 7
  • 25
1
vote
1 answer

Is it possible to disallow self-recursion?

I have the rule: expression : //... | expression (relative_operator expression)+ | //... ; Ideally, when I put in 1=1=1, it will produce the expression(1, =, 1, =, 1) tree. However, in reality it produces expression(1, =, expression(1,…
Unlocked
  • 648
  • 6
  • 19
1
vote
1 answer

Exception in thread "main": –gui -tokens -tree

I'm using antlr4 on mac os, I installed it following the getting started documentation on github https://github.com/antlr/antlr4/blob/master/doc/getting-started.md. The testing works fine, but when enter any of these commands: $ grun grammar rule…
fullon
  • 11
  • 1
  • 5
1
vote
2 answers

Match most specific rule

In my grammar, I want to have both "variable identifiers" and "function identifiers". Essentially, I want to be less restrictive on the characters allowed in function identifiers. However, I am running in to the issue that all variable identifiers…
Panda
  • 877
  • 9
  • 21
1
vote
1 answer

ANTLR proper ordering of grammar rules

I am trying to write a grammar that will recognize <> as a special token but treat as just a regular literal. Here is my grammar: grammar test; doc: item+ ; item: func | atom ; func: '<<' WORD '>>' ; atom: PUNCT+ …
skrilmps
  • 625
  • 2
  • 10
  • 29
1
vote
1 answer

No way to implement a q quoted string with custom delimiters in Antlr4

I'm trying to implement a lexer rule for an oracle Q quoted string mechanism where we have something like q'$some string$' Here you can have any character in place of $ other than whitespace, (, {, [, <, but the string must start and end with the…
user3770822
  • 63
  • 1
  • 5
1
vote
1 answer

How can I detect whitespace in my parse tree in Antlr4?

When a rule matches in antlr4, and you get the text of that rule, the whitespace is commonly stripped out by the lexer with WS: [ \n\t\r]+ -> skip; Is it possible to ask in a parse tree visitor "Did this rule skip over any whitespace?" E.g. WS: […
izb
  • 50,101
  • 39
  • 117
  • 168
1
vote
2 answers

Antlr Skip text outside tag

Im trying to skip/ignore the text outside a custom tag: This text is a unique token to skip < ?compo \5+5\ ?> also this < ?compo \1+1\ ?> I tried with the follow lexer: TAG_OPEN : ' pushMode(COMPOSER); mode COMPOSER; TAG_CLOSE …
Massimo
  • 43
  • 7
1
vote
1 answer

ANTLR4 repeating ANDs

With the following rule: expr: '(' expr ')' #exprExpr | expr ( AND expr )+ #exprAnd | expr ( OR expr )+ #exprOr | atom #exprAtom | ID #exprId ; atom: '[' ID RELOP INT ']' ; I would like to allow statements like this: [a<3] and…
Mike75
  • 504
  • 3
  • 18
1
vote
1 answer

Syntax Error in mysql grammar file for ANTLR v4

I am using Lexer and Parser from ANTLR v4 repo here for parsing mysql in java. But I am getting an error in file MySqlLexer.g4 available here at the following set of lines: lexer grammar MySqlLexer; channels { MYSQLCOMMENT, ERRORCHANNEL } //…
KCK
  • 2,015
  • 2
  • 17
  • 35
1
vote
2 answers

Is there any alternate way to preview the ANTLR generated parse tree?

I am using IntelliJ Idea Plugin for ANTLR v4 which previews the parse tree. But the preview is quite difficult to interpret when a large binary tree is generated in case of large code. Is there any alternate way to view the same or generate the…