Questions tagged [antlr]

ANTLR, ANother Tool for Language Recognition, is a language tool that provides a framework for constructing recognizers, interpreters, compilers, and translators from grammatical descriptions containing actions in a variety of target languages.

What is ANTLR?

"ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files. It's widely used to build languages, tools, and frameworks. From a grammar, ANTLR generates a parser that can build and walk parse trees. From https://www.antlr.org/

Since ANTLR is not commercial software, it is not required and does not maintain backward compatibility with previous major versions and even between some minor versions.

Updated links

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 ATNLR 4.x.

ANTLR IDE

Useful ANTLR links

Examples and/or useful posts on SO

Related links

Code at Git

Related tags

Related projects

Installation

Antlr can most easily be installed through its NuGet package.

Install-Package Antlr

Books

4218 questions
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
19
votes
2 answers

LALR vs LL parser

I've been using lex/yacc and now I'm trying to switch to ANTLR. The major concern is that ANTLR is an LL(*) parser unlike yacc which is LALR. I'm used to thinking bottom-up and I don't exactly know what the advantage of LL grammars is. People say…
K J
  • 4,505
  • 6
  • 27
  • 45
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
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

ANTLR: How the behavior of this grammar which recognizes suffixes of a Java code can be explained?

A week ago I started the following project: a grammar which recognizes suffixes of a Java code. I used the official ANTLR grammar for Java (Java.g4) as a baseline and started to add some rules. However, those new rules also introduced left…
sve
  • 4,336
  • 1
  • 19
  • 30
17
votes
2 answers

Reasons for using lex/yacc alternatives?

About once a year I have to develop or at least design a grammar and a parser - that appears a constant of my working life. Every time I'm facing this task, thus about once year, I, quite a lex/yacc (flex/bison resp.) guy, consider, or reconsider,…
Solkar
  • 1,228
  • 12
  • 22
16
votes
2 answers

ANTLR not throwing errors on invalid input

I'm using ANTLR to parse logical expressions in a Java tool I'm writing, and I'm having issues because passing invalid input strings to the generated ANTLR lexer and parser doesn't cause any exceptions. Instead of throwing a RecognitionException,…
user550617
16
votes
1 answer

In ANTLR, can I look-ahead for specific tokens without actually matching them?

Basically, I need to lookahead to know if a certain token exists, but without matching it (i.e. so that the another parser rule can still match it). The exact details of the problem is an "END-ALL" clause. The language has constructs like "IF"…
bundat
  • 173
  • 1
  • 5
16
votes
1 answer

Trouble Setting Up ANTLR 4 IDE on Eclipse Luna (4.4)

I'm trying to install the ANTLR 4 IDE on Eclipse Luna (4.4). I've installed it from the Marketplace but I have no idea how to create a project that has an ANTLR 4 Lexer/Parser in it. When I go to create a new project I don't see any options for…
amura.cxg
  • 2,348
  • 3
  • 21
  • 44
16
votes
1 answer

Why does Netbeans ask for plugins which I cannot find to update?

I want to install ANTLRWorks plugin, but Netbeans says: Some plugins require plugin **Editor Settings Storage** to be installed. The plugin Editor Settings Storage is requested in implementation version 201302132200. The following plugin is…
static
  • 8,126
  • 15
  • 63
  • 89
16
votes
2 answers

ANTLR 4 tree inject/rewrite operator

In ANTLR 3 you could just do the following: andExpression : (andnotExpression -> andnotExpression) (AND? a=andnotExpression -> ^(AndNode $andExpression $a))* ; Any idea how to do it in the new version?
NeatNerd
  • 2,305
  • 3
  • 26
  • 49
16
votes
1 answer

Parsing SQL like syntax, design pattern

I am trying mock sql syntax to build a simple sql like interface to a key-value storage. The values are essentially POJOs An example would be select A.B.C from OBJ_POOL where A.B.X = 45 AND A.B.Y > '88' AND A.B.Z != 'abc'; OBJ_POOL is just a list…
jack_carver
  • 1,510
  • 2
  • 13
  • 28
15
votes
2 answers

How do I escape an escape character with ANTLR 4?

Many languages bound a string with some sort of quote, like this: "Rob Malda is smart." ANTLR 4 can match such a string with a lexer rule like this: QuotedString : '"' .*? '"'; To use certain characters within the string, they must be escaped,…
james.garriss
  • 12,959
  • 7
  • 83
  • 96
14
votes
1 answer

ANTLR ClassCastException

Why do I get this error while generating code in ANTLR? [16:06:38] error(10): internal error: C:\Users\user\Desktop\test.g : java.lang.ClassCastException: org.antlr.runtime.tree.CommonTree cannot be cast to…
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288