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
1
vote
1 answer

How to append two lexer expressions- ANTLR4

I need lexer to parse two different character expressions as one expression. So I've something like this, rootPath : 'A' rootType SEP childPath; //my output should be AB:2 or AC:4 childPath : RESERVED_NUMBERS; rootType : ONE_LETTER; SEP:…
1
vote
1 answer

Is Antlr4 Visitor really a visitor?

I've been learning how to make an AST with Antlr4's visitor and after reading Terrance Parr's book, and multiple forums on the topic of AST generation specifically with Antlr visitors, it seems that the standard approach for doing this involves…
1
vote
1 answer

Use source files generated by ANTLR

I use ANTLR with the maven plugin to generate the source files. When creating them with mvn package they get correctly compiled and put together into target/generated-sources/antlr4, but from there I cannot access them in from my project. My…
0xff
  • 181
  • 2
  • 11
1
vote
1 answer

Is it possible to count the number of times an element appears when parsing?

I am trying to find a way to keep track of the number of times an element appears when building a parse tree. Let's say my grammar looks something like this: grammar sample; @members { private int xCount= 0; private int yCount= 0; …
jj1999
  • 11
  • 4
1
vote
2 answers

How to solve ANTLR error "Attribute references not allowed in lexer actions"

After reading Chapter 10 of "The Definitive ANTLR 4 Reference", I tried to write a simple analyzer to get lexical attributes, but I got an error. How can I get the lexical attributes? lexer grammar TestLexer; SPACE: [ \t\r\n]+…
Poison
  • 389
  • 2
  • 14
1
vote
1 answer

ANTLR antlrWorks error messages are not displayed to the output console

When enter the following input with an error at the third line: SELECT entity_one, entity_two FROM myTable; first_table, extra_table as estable, tineda as cam; asteroid tenga, tenta as myName, new_eNoal as coble I debugged it with antlrWorks and…
Manny
  • 11
  • 1
1
vote
1 answer

ANTLR how can I check all the permutations of a key in ANTLR grammar?

To manage all the permutations of this key: mess a possible solution can be: key : 'mess' | 'MESS'| 'meSs'| 'mEss'| 'Mess'| 'mESs'| 'MeSs'| 'MEss'; or maybe something like: key: MESS; MESS: M E S S; fragment M: [mM]; fragment E:…
1
vote
2 answers

How to make parser decide on which alternative to use, based on the rule in the previous step

I'm using ANTLR 4 to parse a protocol's messages, let's name it 'X'. Before extracting a message's information , I have to check if it complies with X's rules. Suppose we have to parse X's 'FOO' message that follows the following rules: Message…
Jos
  • 11
  • 5
1
vote
1 answer

Why isn't ANTLR 4 recognising Unicode characters as valid tokens?

I have been struggling to get ANTLR 4 to recognise Unicode characters in the input. I reduced my grammar to a simpler test I found on this answer to a related question, but all I've done is change the characters it's supposed to recognise, and it…
Hakanai
  • 12,010
  • 10
  • 62
  • 132
1
vote
2 answers

Simplify chained expression in ANTLR listener

I have an ANTLR listener for C++ where I want to get the name of a member declarator. Currently, I'm using this approach: def enterMemberDeclarator(self, ctx: CPP14Parser.MemberDeclaratorContext): id =…
Schottky
  • 1,549
  • 1
  • 4
  • 19
1
vote
2 answers

Disambiguating a left-recursive ANTL4 rule

Let's consider this simple ANTL4 language grammar. Lexer: lexer grammar BiaLexer; Lt : '<' ; Gt : '>' ; Identifier : [a-zA-Z] ([a-zA-Z1-9] | ':')* ; LeftParen : '(' ; RightParen …
cubuspl42
  • 7,833
  • 4
  • 41
  • 65
1
vote
0 answers

How to setTreeAdaptor in Antlr4 parser?

I am writing a program that utilizes an javaparser generated by antlr4 (4.7). In this programm i need to change the TreeAdaptor the parser is using. Everything i found in the internet tells me to use the setTreeAdaptor() function of the parser but…
1
vote
1 answer

parsing nested function calls

Im trying to parse some C++ code using grammars from the github repository https://github.com/antlr/grammars-v4 . The problem I have is for parsing nested function calls, for example, int main() { foo(bar(test())); return 0; } How can I…
Alex
  • 49
  • 6
1
vote
1 answer

ANTLR4: How to hide a specific character?

I am new to ANTLR4 and have a trouble. I want to have the code accept integers with _ between digits, but the output would not include _ characters. For example: it would accept 12_34_5 and the expected token would be 12345. Is there a way to do…
Hoang
  • 33
  • 3
1
vote
1 answer

Antlr grun error - no viable alternative input at

I'm trying to write a grammar for Prolog interpreter. When I run grun from command line on input like "father(john,mary).", I get a message saying "no viable input at 'father(john,'" and I don't know why. I've tried rearranging rules in my grammar,…
rikki
  • 13
  • 3
1 2 3
99
100