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

Is "Implicit token definition in parser rule" something to worry about?

I'm creating my first grammar with ANTLR and ANTLRWorks 2. I have mostly finished the grammar itself (it recognizes the code written in the described language and builds correct parse trees), but I haven't started anything beyond that. What worries…
Matěj Zábský
  • 16,909
  • 15
  • 69
  • 114
38
votes
1 answer

Once grammar is complete, what's the best way to walk an ANTLR v4 tree?

Goal I'm working on a project to create a Varscoper for Coldfusion CFscript. Basically, this means checking through source code files to ensure that developers have properly var'd their variables. After a couple of days of working with ANTLR V4 I…
Owen Allen
  • 11,348
  • 9
  • 51
  • 63
33
votes
1 answer

ANTLR 4 $channel = HIDDEN and options

I need help with my ANTLR 4 grammar after deciding to switch to v4 from v3. I am not very experienced with ANTLR so I am really sorry if my question is dumb ;) In v3 I used the following code to detect Java-style comments: COMMENT : '//'…
user2055330
  • 333
  • 1
  • 3
  • 4
32
votes
1 answer

How to output the AST built using ANTLR?

I'm making a static analyzer for C. I have done the lexer and parser using ANTLR in which generates Java code. Does ANTLR build the AST for us automatically by options {output=AST;}? Or do I have to make the tree myself? If it does, then how to…
Raphael
  • 395
  • 1
  • 5
  • 7
31
votes
4 answers

When do we use ANTLR

Can anyone please guide me the purpose of using ANTLR dlls in a ASP.Net, C# project. I just noticed antlr.runtime, Antlr3.Runtime, Antlr3.Utlility assemblies being referenced in some project. Will be great if can cite some real world examples or any…
inutan
  • 10,558
  • 27
  • 84
  • 126
31
votes
2 answers

Practical difference between parser rules and lexer rules in ANTLR?

I understand the theory behind separating parser rules and lexer rules in theory, but what are the practical differences between these two statements in ANTLR: my_rule: ... ; MY_RULE: ... ; Do they result in different AST trees? Different…
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
29
votes
4 answers

Running/Interpreting C on top of the JVM?

Is there a way to run plain c code on top of the JVM? Not connect via JNI, running, like you can run ruby code via JRuby, or javascript via Rhino. If there is no current solution, what would you recommend I should do? Obviously I want to use as many…
amitkaz
  • 2,732
  • 1
  • 20
  • 18
28
votes
3 answers

Getting the line number in the ParserVisitor?

I'm trying to get line numbers for more specific error messages in my ParserVisitor (visits the parse tree generated by antlr). However, all I have in this class is the context ctx, and I can do things like ctx.getText() but not getLine(). Is there…
27
votes
2 answers

ANTLR4 Python parsing big files

I am trying to write parsers for juniper/srx router access control lists. Below is the grammar I am using: grammar SRXBackend; acl: 'security' '{' 'policies' '{' COMMENT* replaceStmt '{' policy* '}' '}' '}' applications …
prthrokz
  • 1,120
  • 8
  • 16
27
votes
2 answers

ANTLR4 visitor pattern on simple arithmetic example

I am a complete ANTLR4 newbie, so please forgive my ignorance. I ran into this presentation where a very simple arithmetic expression grammar is defined. It looks like: grammar Expressions; start : expr ; expr : left=expr op=('*'|'/') right=expr…
Giovanni Botta
  • 9,626
  • 5
  • 51
  • 94
27
votes
4 answers

Where can I find C# 3.0 grammar?

I'm planning to write a C# 3.0 compiler in C#. Where can I get the grammar for parser generation? Preferably one that works with ANTLR v3 without modification.
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
27
votes
2 answers

ANTLR4: Whitespace handling

I have seen many ANTLR grammars that use whitespace handling like this: WS: [ \n\t\r]+ -> skip; // or WS: [ \n\t\r]+ -> channel(HIDDEN); So the whitespaces are thrown away respectively send to the hidden channel. With a grammar like this: grammar…
flux
  • 275
  • 1
  • 3
  • 5
24
votes
2 answers

How to configure antlr4 plugin for Intellij IDEA

I looked all over the place for how to configure the antlr4 plugin for IntelliJ IDEA. But I can't find anything. I was only able to install the plugin. If I add .g4 files manually for a empty project I get the "Generate ANTLR Recognizer" option in…
Morpheus
  • 1,722
  • 5
  • 27
  • 38
23
votes
4 answers

ANTLR What is simpliest way to realize python like indent-depending grammar?

I am trying realize python like indent-depending grammar. Source example: ABC QWE CDE EFG EFG CDE ABC QWE ZXC As i see, what i need is to realize two tokens INDENT and DEDENT, so i could write something like: grammar mygrammar; text: (ID…
Astronavigator
  • 2,021
  • 2
  • 24
  • 45