Questions tagged [antlr3]

Version 3 of ANTLR (ANother Tool for Language Recognition) created and written by Dr. Terrence Parr

Version 3 of ANTLR.

What is 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. ANTLR provides excellent support for tree construction, tree walking, translation, error recovery, and error reporting. ..." -- http://www.antlr.org

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.

Useful ANTLR links

Examples and/or useful posts on SO

Related links

Code at Git

Related tags

911 questions
3
votes
1 answer

Generating AST from ANTLR grammar

For the question and the grammar suggested by @BartKiers (Thank you!), I added the options block to specify the output to be options{ language=Java; output=AST; ASTLabelType=CommonTree; } However, I am not able to figure out how to access the…
name_masked
  • 9,544
  • 41
  • 118
  • 172
3
votes
4 answers

How to resolve this ambiguous grammar?

I have written this grammar: expr : multExpr ( ('+' | '-') multExpr )*; multExpr : atom ( ('*' | '/') atom )*; atom : INT | FLOAT | ID | '(' expr ')'; condition : cond ('or' cond)*; cond : c1 ('and' c1)*; c1 : ('not')? c2; c2 …
nafiseh
  • 129
  • 3
  • 11
3
votes
1 answer

Antlr Tree traversing using Java

I have a question regarding Antlr, I am building a simple parser with it but I can't traverse the tree. I have found many online tutorials and they use a getAst(); function of the Parser class. Does anyone have any experience with this? I have the…
Fokko Driesprong
  • 2,075
  • 19
  • 31
3
votes
1 answer

Antlr error 'no viable alternative at character'

I am using the Objective C grammar available here, and trying to parse this code: int main() { int k=0; } this is an objective c code and it should get parsed but it is giving me the following errors when i call the function…
3
votes
2 answers

Switching lexer state in antlr3 grammar

I'm trying to construct an antlr grammar to parse a templating language. that language can be embedded in any text and the boundaries are marked with opening/closing tags: {{ / }}. So a valid template looks like this: foo {{ someVariable }}…
pulse00
  • 1,294
  • 1
  • 16
  • 25
3
votes
1 answer

How to get line number in ANTLR3 tree-parser @init action

In ANTLR, version 3, how can the line number be obtained in the @init action of a high-level tree-parser rule? For example, in the @init action below, I'd like to push the line number along with the sentence text. sentence @init {…
Andy Thomas
  • 84,978
  • 11
  • 107
  • 151
3
votes
1 answer

Decision can match input such as "ID" using multiple alternatives: 1, 2

I am trying to define a simple functional language grammar, I am almost done with my definitions, but I can't get past the following ambiguities. [14:43:53] warning(200): mygrammar.g:14:11: Decision can match input such as "ATOM" using multiple…
user177800
3
votes
1 answer

How to get rid of the following multiple alternatives warnings in my ANTLR3 grammar?

[11:45:19] warning(200): mygrammar.g:14:57: Decision can match input such as "','" using multiple alternatives: 1, 2 As a result, alternative(s) 2 were disabled for that input [11:45:19] warning(200): C:\Users\Jarrod Roberson\mygrammar.g:14:57:…
user177800
3
votes
1 answer

NoViableAltException when parsing string literals with ANTLR

I am very new to ANTLR, trying to parse a simple PL/SQL function. My apologies if this is a silly question. function MyFunc return boolean is begin IF :USER_ID_P IS NULL THEN :USER_ID_P := 'PUBLIC'; END IF; return…
user897210
  • 109
  • 6
3
votes
1 answer

Extend ANTLR3 AST's

With ANTLR2, you could define something like this in grammar definition file: options { language = "CSharp"; namespace = "Extended.Tokens"; } tokens { TOKEN; } And then, you could create a class: public…
dcg
  • 1,144
  • 1
  • 22
  • 38
3
votes
1 answer

Antlr grammar multiplicity problem of tree in tree grammar

I have a simple grammar options { language = Java; output = AST; ASTLabelType=CommonTree; } tokens { DEF; } root : ID '=' NUM (',' ID '=' NUM)* -> ^(DEF ID NUM)+ ; and the corresponding tree grammar: options { …
csviri
  • 1,159
  • 3
  • 16
  • 31
3
votes
3 answers

Better way to map tokens to enum values?

I'm trying to have my parser rule select an enum value based on my DIR token. Is there a way I can do this without creating separate, full-fledged tokens for each direction? Or generally a cleaner approach? DIR : (NORTH|SOUTH) (EAST|WEST)? | EAST …
Brad Mace
  • 27,194
  • 17
  • 102
  • 148
3
votes
2 answers

Antlr Lexer rules

I've got a rule to match a string that looks like so: STRING : '"' ( ~( '"' | '\\' ) | '\\' . )* '"' ; I dont want the quotes to be part of the tokens text. In Antlr2 I would just put '!' after the quotes to tell Antlr not to add them to…
chollida
  • 7,834
  • 11
  • 55
  • 85
3
votes
1 answer

Listener vs visitor in this exercise

We have this exercise that we have to do in class and the professor asked us to figure out good reasons to choose the listener method over visitor method in order to implement a solution for a semantic problem. Although visitor seems more…
3
votes
1 answer

Whats the correct way to add new tokens (rewrite) to create AST nodes that are not on the input steam

I've a pretty basic math expression grammar for ANTLR here and what's of interest is handling the implied * operator between parentheses e.g. (2-3)(4+5)(6*7) should actually be (2-3)*(4+5)*(6*7). Given the input (2-3)(4+5)(6*7) I'm trying to add…
EraserheadIRL
  • 1,061
  • 2
  • 12
  • 20