antlr

AST with fixed nodes instead of error nodes in antlr

I have an antlr generated Java parser that uses the C target and it works quite well. The problem is I also want it to parse erroneous code and produce a meaningful AST. If I feed it a minimal Java class with one import after which a semicolon is missing it produces two "Tree Error Node" objects where the "import" token and the tokens fo...

gunit syntax for tree walker with a flat list of nodes

Here's a simple gunit test for a portion of my tree grammar which generates a flat list of nodes: objectOption walks objectOption: <<one:"value">> -> (one "value") Although you define a tree in ANTLR's rewrite syntax using a caret (i.e. ^(ROOT child...)), gunit matches trees without the caret, so the above represents a tree and i...

Exception declared on ANTLR grammar rule ignored

I have a tree parser that's doing semantic analysis on the AST generated by my parser. It has a rule declared as follows: transitionDefinition throws WorkflowStateNotFoundException: /* ... */ This compiles just fine and matches the rule syntax at the ANTLR Wiki but my exception is never declared so the Java compiler complains abou...

antlr 3 ambiguity

Hello, I try to write some simple rules and I get this ambiguity rule: field1 field2; //ambiguity between nsf1 and nsf2 even if I use lookahead k=4 field1: nsf1 | whatever1...; field2: nsf2 | whatever2...; nsf1: 'N' 'S' 'F' '1'; //meaning: no such field 1 nsf2: 'N' 'S' 'F' '2'; //meaning: no such field 2 I understand the ambiguity,...

lexers vs parsers

Are lexers and parsers really that different in theory ? It seems fashionable to hate regular expressions: coding horror, another blog post. However, popular lexing based tools: pygments, geshi, or prettify, all use regular expressions. They seem to lex anything... When is lexing enough, when do you need EBNF ? Has anyone used t...

Visualizing an AST created with ANTLR (in a .Net environment)

Hi there. For a pet project I started to fiddle with ANTLR. After following some tutorials I'm now trying to create the grammar for my very own language and to generate an AST. For now I'm messing around in ANTLRWorks mostly, but now that I have validated that the parse tree seems to be fine I'd like to (iteratively, because I'm still ...

ANTLR IDE does not find the grammar files that need to be imported in another grammar file(in the import package)

I have set up my ANTLR project to build using maven just like it is mentioned here http://www.antlr.org/antlr3-maven-plugin/index.html. I also have the ANTLR IDE plugin for eclipse. Maven builds my project fine and finds all the file it needs to build. However, when I try to use the ANTLR IDE in my eclipse for development, it does not fi...

Antlr Lexer Quoted String Predicate

I'm trying to build a lexer to tokenize lone words and quoted strings. I got the following: STRING: QUOTE (options {greedy=false;} : . )* QUOTE ; WS : SPACE+ { $channel = HIDDEN; } ; WORD : ~(QUOTE|SPACE)+ ; For the corner cases, it needs to parse: "string" word1" word2 As three tokens: "string" as STRING and word1" an...

Parsing some particular statements with antlr3 in C target

Hello all! I have some questions about antlr3 with tree grammar in C target. I have almost done my interpretor (functions, variables, boolean and math expressions ok) and i have kept the most difficult statements for the end (like if, switch, etc.) 1) I would like interpreting a simple loop statement: repeat: ^(REPEAT DIGIT stmt); ...

What happened to setASTNodeClass in Antlr 3?

Hi, What happened to setASTNodeClass in Antlr3? What needs to be used in Antlr3 instead? Thanks. ...

Java static source analysis/parsing (possibly with antlr), what is a good tool to do this?

I need to perform static source analysis on Java code. Ideally, I want the system to work out of the box without much modification from me. For example, I have used Antlr in the past, but I spent a lot of time building grammar files and still didn't get what I wanted. I want to be able to parse a java file and have return the charact...

Removing Left Recursion in ANTLR

As is explained in http://stackoverflow.com/questions/2652060/removing-left-recursion , there are two ways to remove the left recursion. Modify the original grammar to remove the left recursion using some procedure Write the grammar originally not to have the left recursion What people normally use for removing (not having) the left ...

Why is Rhino unhappy with this javascript?

I have been successfully using my code with the javascript library in the ANTLR javascript target in a few browsers, but now I want to use Rhino on the server and I am having some trouble. I have some simple java code that references the Rhino 1.7R2 release's js-14.jar file. Context context = Context.enter(); Scriptable scope = context...

ANTLR, Dynamic variables

Hello, I have an ANTLR grammar that can parse and evaluate simple expressions like 1+2*4, etc. What I would like to do is to evaluate expressions like 2+$a-$b/4 where the $ variables are dynamic variables, that come from an external source and are continuously updated. Is there any design pattern on how to do this using ANTLR, best pra...

Getting tree construction with ANTLR

As asked and answered in http://stackoverflow.com/questions/2999755/removing-left-recursion-in-antlr , I could remove the left recursion E -> E + T|T T -> T * F|F F -> INT | ( E ) After left recursion removal, I get the following one E -> TE' E' -> null | + TE' T -> FT' T' -> null | * FT' Then, how to make the tree construction ...

Is there a way to specify java annotations in antlr grammar files?

I'm looking for a way to include a few additional strings in output .java files generated from antlr. Is there a comprehensive listing of available directives? For example, given parser output like this: package com.foo.bar; //<-- this can be generated with @header { .... } //antlr generated import org.antlr.runtime.*; ... //<-- is t...

ANTLR: simple example from ANTLRWorks wizard doesn't work

Grammar: grammar test; WS : ( ' ' | '\t' | '\r' | '\n' ) {$channel=HIDDEN;} ; STRING : '"' ( ESC_SEQ | ~('\\'|'"') )* '"' ; fragment HEX_DIGIT : ('0'..'9'|'a'..'f'|'A'..'F') ; fragment ESC_SEQ : '\\' ('b'|'t'|'n'|'f'|'r'|'\"'|'\''|'\\') | UNICODE_ESC | OCTAL_ESC ;...

Netbeans and EOF

Hey there, Java, ANTLR and Netbeans newbie here. I have installed a jdk and netbeans. I started a new project on netbeans 6.8 and i have added the antlr-3.2.jar as a library. I also created a lexer and parser class using AntlrWorks. These classes are named ExprParser.java and ExprLexer.java. I copied them into a directory named p...

Which is the best counterpart to ANTLR to create parsers in ruby?

Hi there, I've used antlr and javacc/freecc for a while. Now I need to write a bunch of parsers using antlr grammars but such parsers need to be written in ruby lang. I googled but nothing found. Is there any ruby parser generator that takes antlr grammars and create a parser? If there are many, which is the best one in your opinion? T...

how to check an ANTLR token is only used once or less in the parser

In Antlr, if I have a rule for example: someRule : TOKENA TOKENB; it would accept : "tokena tokenb" if I would like TOKENA to be optional, I can say, someRule : TOKENA* TOKENB; then I can have : "tokena tokenb" or "tokenb" or "tokena tokena tokenb" but this also means it can be repeated more that once. Is there anyway I can say t...