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

Abort on parse error with useful message

I've got an ANTLR 4 grammar and built a lexer and parser from that. Now I'm trying to instantiate that parser in such a way that it will parse until it encounters an error. If it encounters an error, it should not continue parsing, but it should…
MvG
  • 57,380
  • 22
  • 148
  • 276
12
votes
2 answers

How do LL(*) parsers work?

I cannot find any complete description about LL(*) parser, such as ANTLR, on Internet. I'm wondering what is the difference between an LL(k) parser and an LL(*) one and why they can't support left-recusrive grammars despite their flexibility.
Fabio Strocco
  • 300
  • 4
  • 14
12
votes
3 answers

ANTLR Parse tree modification

I'm using ANTLR4 to create a parse tree for my grammar, what I want to do is modify certain nodes in the tree. This will include removing certain nodes and inserting new ones. The purpose behind this is optimization for the language I am writing. I…
Marc Deleuran
  • 344
  • 2
  • 6
12
votes
2 answers

Extending simple ANTLR grammar to support input variables

I'm still on my quest for a really simple language and I know now that there are none. So I'm writing one myself using ANTLR3. I found a really great example in this answer: Exp.g: grammar Exp; eval returns [double value] : exp=additionExp…
arturh
  • 6,056
  • 4
  • 39
  • 48
12
votes
1 answer

Unparse AST < O(exp(n))?

Abstract problem description: The way I see it, unparsing means to create a token stream from an AST, which when parsed again produces an equal AST. So parse(unparse(AST)) = AST holds. This is the equal to finding a valid parse tree which would…
11
votes
2 answers

Generating an Abstract Syntax Tree for java source code using ANTLR

How Can I Generate an AST from java src code Using ANTLR? any help?
Aboelnour
  • 1,423
  • 3
  • 17
  • 39
11
votes
1 answer

Parse comment line

Given the following basic grammar I want to understand how I can handle comment lines. Missing is the handling of the which usually terminates the comment line - the only exception is a last comment line before the EOF, e. g.: #…
ANTLRStarter
  • 309
  • 1
  • 4
  • 16
11
votes
4 answers

Writing a code formatting tool for a programming language

I'm looking into the feasibility of writing a code formatting tool for the Apex language, a Salesforce.com variation on Java, and perhams VisualForce, its tag based markup language. I have no idea on where to start this, apart from feeling/knowing…
Steven Herod
  • 764
  • 8
  • 20
11
votes
2 answers

Does logical AND and NOT exists in ANTLR?

Is there NOT logic in ANTLR? Im basically trying to negate a rule that i have and was wondering if its possible, also is there AND logic?
Victor
  • 1,305
  • 2
  • 11
  • 15
11
votes
5 answers

antlr4/java: pretty print parse tree to stdout

Beginners question: how do I print a readable version of the parse tree to stdout? CharStream input = CharStreams.fromFileName("testdata/test.txt"); MyLexer lexer = new MyLexer(input); CommonTokenStream tokens = new…
JPT
  • 410
  • 2
  • 7
  • 18
11
votes
1 answer

Antlr rule priorities

Firstly I know this grammar doesn't make sense but it was created to test out the ANTLR rule priority behaviour grammar test; options { output=AST; backtrack=true; memoize=true; } rule_list_in_order : ( first_rule | second_rule …
probably at the beach
  • 14,489
  • 16
  • 75
  • 116
11
votes
1 answer

Antlr parser operator priority

Consider the following grammar. I have issues with the operator priority, for instance: res=2*a+b has a similar parse tree as res=2*(a+b). I know where the problem is, but no "beautiful" solution without mutual left recursion comes to my mind. Can…
Martin B
  • 141
  • 1
  • 5
11
votes
1 answer

How does the ANTLR lexer disambiguate its rules (or why does my parser produce "mismatched input" errors)?

Note: This is a self-answered question that aims to provide a reference about one of the most common mistakes made by ANTLR users. When I test this very simple grammar: grammar KeyValues; keyValueList: keyValue*; keyValue: key=IDENTIFIER '='…
Lucas Trzesniewski
  • 50,214
  • 11
  • 107
  • 158
11
votes
1 answer

Using @header in ANTLR

I'm having trouble getting the "@header" or any other @ rule to work in ANTLR. With a very basic grammer, like this: grammar test; options { language = CSharp2; } @header { using System.Collections.Generic; } tokens { …
Moxen
  • 233
  • 3
  • 10
11
votes
3 answers

ANTLR (or alternative): decoupling parsing from evaluation

I have a relatively simple DSL that I would like to handle more robustly than a bunch of manually-coded java.util.regex.Pattern statements + parsing logic. The most-quoted tool seems to be ANTLR. I'm not familiar with it and am willing to give it a…
Jason S
  • 184,598
  • 164
  • 608
  • 970