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 can I tell ANTLR to use only the branch I specify, not other branches。

Here is my ANTLR grammar: It is divided into two section ,parameters and constraints; The parameters section consists of many row,Each rowrepresents a parameter and its values.Each parameter and its values are separated by : . Each parameter value…
1
vote
1 answer

Parsing a templating language

I'm trying to parse a templating language and I'm having trouble correctly parsing the arbitrary html that can appear between tags. So far what I have is below, any suggestions? An example of a valid input would be {foo}{#bar}blah blah…
Gaff
  • 88
  • 6
1
vote
1 answer

ANTLR4 lexer yielding 'chars used multiple times in set' error

I'm adding a rule to my ANTLR4 lexer that looks like [/a-zA-Z0-9_?+-.*\u005B-\u005E\u007B-\u007D]+ and it lexes just fine. However, when I add \u002C to it, regardless of where I add that character, or when I add a literal , to it, I get the same…
mojones101
  • 161
  • 1
  • 2
  • 12
1
vote
1 answer

When does order of alternation matter in antlr?

In the following example, the order matters in terms of precedence: grammar Precedence; root: expr EOF; expr : expr ('+'|'-') expr | expr ('*' | '/') expr | Atom ; Atom: [0-9]+; WHITESPACE: [ \t\r\n] -> skip; For example, on the…
David542
  • 104,438
  • 178
  • 489
  • 842
1
vote
2 answers

Possible to reduce lookahead on potentially ambiguous expressions

This is somewhat related to my previous question Ambiguity between tuple and parenthesized expression, but now about if there is a way to improve the number of lookaheads needed to resolve what type of expression something is. In the following…
1
vote
0 answers

How to solve the following grammar ambiguity

I am trying to parse a SQL statement that allows for both a BETWEEN expr1 AND expr2 and also expr1 AND expr2. An example would be: SELECT * FROM tbl WHERE col1 BETWEEN 1 AND 5 AND col3 = 10; What would be a good way to disambiguate this, as…
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
1
vote
1 answer

ANTLR grammar not handling my "not" operator correctly

I am trying to parse a small expression language (I didn't define the language, from a vendor) and everything is fine until I try to use the not operator, which is a tilde in this language. My grammar has been heavily influenced by these two links…
Michael
  • 2,683
  • 28
  • 30
1
vote
1 answer

Possible to format the token output in antlr4?

In antlr when I run the following test command to grab tokens: $ grun TestLexer tokens -tokens…
samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58
1
vote
0 answers

C++ Antlr Code stopped working (antlrcpp::Any)

I developed some ANTLR + LLVM parser code in spring. Since it is only a recreational project, I did not touch the code or see whether it compiles in the meantime. During that time, several system updates took place, which I assume caused my current…
1
vote
1 answer

Better to be more explicit or less explicit in a parsing grammar?

Let's say I have a SQL-like language that supports seeing if two expressions are equal only if they are the same type, and if they're not the same type it'll raise an error. Examples would be: 1 = 1 # true 1 = 1.2 # false 1 = '1' …
David542
  • 104,438
  • 178
  • 489
  • 842
1
vote
1 answer

Tracking down problems with text being ignored by ANTLR parser

I'm working on a parser that will split a string containing a person's full name into components (first, middle, last, title, suffix, ...). When I try a basic example "J. A. Doe" in ANTLRWorks, it matches the fname and lname rules, but ignores the…
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
1
vote
1 answer

Making ANTLRWorks handle whitespaces automatically

I have an ANTLR grammar like this: grammar HelloGrammar1; ID : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')* ; STATEMENT : 'hello' ID ';' ; WS : (' '|'\t'|'\r'|'\n')* ; I want it to parse the following text: hello qwerty ;. It…
Andrey Agibalov
  • 7,624
  • 8
  • 66
  • 111
1
vote
1 answer

ANTLR: Extraneous input problem

I have a grammar file Bookings.g and the file I try to parse with that grammar. Why am I getting these errors? line 1:12 extraneous input '"pcc"' expecting String line 3:0 mismatched input '}' expecting ScenarioPart1 line 7:13 mismatched input '"R"'…
Glory to Russia
  • 17,289
  • 56
  • 182
  • 325
1
vote
2 answers

Scanner (Lexing keywords with ANTLR)

I have been working on writing a scanner for my program and most of the tutorials online include a parser along with the scanner. It doesn't seem possible to write a lexer without writing a parser at the same time. I am only trying to generate…
slimbo
  • 2,699
  • 4
  • 25
  • 36
1
vote
1 answer

Antlr4 mismatched input '<' expecting '<' with (seemingly) no lexer ambiguity

I cannot seem to figure out what antlr is doing here in this grammar. I have a grammar that should match an input like: i,j : bool; setvar : set; i > 5; j < 10; But I keep getting an error telling me that "line 3:13 mismatched input '<'…