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
11
votes
4 answers

C# ANTLR grammar?

I'm looking for turn-key ANTLR grammar for C# that generates a usable Abstract Syntax Tree (AST) and is either back-end language agnostic or targets C#, C, C++ or D. It doesn't need to support error reporting. P.S. I'm not willing to do hardly any…
BCS
  • 75,627
  • 68
  • 187
  • 294
11
votes
4 answers

ANTLR4 and the Python target

I'm having issues getting going with a Python target in ANTLR4. There seems to be very few examples available and going to the corresponding Java code doesn't seem relevant. I'm using the standard Hello.g4 grammar: // Define a grammar called…
Dustin Oprea
  • 9,673
  • 13
  • 65
  • 105
11
votes
4 answers

How to profile an Antlr grammar

I have an Antlr grammar that is currently about 1200 lines. It parses the language that I want, but for at least one construct it is prohibitively slow even for smaller input files. The execution time seems to be growing exponentially for each added…
oyse
  • 1,023
  • 9
  • 18
11
votes
1 answer

ANTLR4: TokenStreamRewriter output doesn't have proper format (removes whitespaces)

I am using Antlr4 and java7 grammar (source) for modifying an input Java Source file. More specifically, I am using the TokenStreamRewriter class to modify some tokens. The following code is a sample that shows how the tokens are modified: …
Mike B
  • 1,522
  • 1
  • 14
  • 24
11
votes
1 answer

antlr4: ATN version 2 expected 3

When trying to use a generated grammar and lexer I get: org.antlr.v4.runtime.atn.ATN; Could not deserialize ATN with version 2 (expected 3). What's wrong?
11
votes
7 answers

ANTLR error(99) grammar has no rules

I previously posted about my first attempt at using ANTLR when I was having issues with left recursion. Now that I have resolved those issues, I am getting the following error when I try to use org.antlr.v4.Tool to generate the code: error(99):…
Felix Bembrick
  • 233
  • 2
  • 7
11
votes
2 answers

ANTLR 4 and AST visitors

I'm trying to use ASTs with ANTLR4, with this files: Builder.java import org.antlr.v4.runtime.ANTLRInputStream; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.TokenStream; public…
GlinesMome
  • 1,549
  • 1
  • 22
  • 35
11
votes
1 answer

C# Dynamic Method - IL vs Expression Trees

I'm playing and learning little with ANTLR building a simple DSL for .NET, transforming the script in string into Dynamic Method. My first idea was translate to IL opcodes, but now I am reading about expression trees for DM creation. It seems that I…
Ricardo
  • 1,549
  • 2
  • 12
  • 11
11
votes
2 answers

ANTLR Operator Precedence

How is operator precedence implemented in ANTLR? I'm using the XText/Antlr package at the moment. Edit: I did what sepp2k suggested, and operator precedence works now, but stuff like 3 +* also work now. The operators are basically "falling…
jameszhao00
  • 7,213
  • 15
  • 62
  • 112
10
votes
1 answer

Convert simple Antlr grammar to Xtext

I want to convert a very simple Antlr grammar to Xtext, so no syntactic predicates, no fancy features of Antlr not provided by Xtext. Consider this grammar grammar simple; // Antlr3 foo: number+; number: NUMBER; NUMBER: '0'..'9'+; and its Xtext…
Adam Schmideg
  • 10,590
  • 10
  • 53
  • 83
10
votes
2 answers

Is there an existing ANTLR or IRONY grammar for R?

Does anyone know if there is an existing existing ANTLR or IRONY grammar for R? Many thanks.
Jonno
  • 789
  • 9
  • 26
10
votes
3 answers

Is ANTLR an appropriate tool to serialize/deserialize a binary data format?

I need to read and write octet streams to send over various networks to communicate with smart electric meters. There is an ANSI standard, ANSI C12.19, that describes the binary data format. While the data format is not overly complex the standard…
Dan Finucane
  • 1,547
  • 2
  • 18
  • 27
10
votes
1 answer

ANTLR4: zero or one times

I'm working on defining a grammar using ANTLR4 and Java. For Integers, I want a number that mat be preceeded by a minus sign. I know it is possible to do it like this: integer: '-' (DIGIT)* | DIGIT* ; But I was wondering if there is a symbol…
Elias
  • 432
  • 6
  • 17
10
votes
2 answers

Visualizing an AST created with ANTLR (in a .Net environment)

For a pet project I started to fiddle with ANTLR. After following some tutorials I'm now trying to create the grammar for my very own language and to generate an AST. For now I'm messing around in ANTLRWorks mostly, but now that I have validated…
Benjamin Podszun
  • 9,679
  • 3
  • 34
  • 45
10
votes
4 answers

ANTLR JavaScript Target

I have been using ANTLR to generate a parser + tree grammar for a mark up language with Java target which works fine. Now I am trying to get the target in JavaScript to use it in my web browser. However, I have not been able to locate any good…
Gunner4Life
  • 736
  • 1
  • 8
  • 18