antlr

How do I manage optional whitespace in ANTLR?

I am trying to parse a data file in ANTLR - it has optional whitespace exemplified by 3 6 97 12 15 18 The following shows where the line starts and ends are. There is a newline at the end and there are no tabs. ^ 3 6$ ^ 97 12$ ^ 15 18$ ^ My grammar is: lines : line+; line : ws1 {System.out.println("WSOPT :"+$ws1.text...

How to specify a target package for ANTLR?

If I call: java org.antlr.Tool -o outdir sources/com/example/Java5.g ...with antlr-3.1.3 the parser and lexer code will be generated in the directory outdir/sources/com/example. But the generated classes don't have any package statement. I need them to life in the package com.example. Is there a way to specify the target package? ...

ANTLR: "missing attribute access on rule scope" problem

I am trying to build an ANTLR grammar that parses tagged sentences such as: DT The NP cat VB ate DT a NP rat and have the grammar: fragment TOKEN : (('A'..'Z') | ('a'..'z'))+; fragment WS : (' ' | '\t')+; WSX : WS; DTTOK : ('DT' WS TOKEN); NPTOK : ('NP' WS TOKEN); nounPhrase: (DTTOK WSX NPTOK); chunker : nounPhrase {System.out....

Getting started with ANTLR and avoiding common mistakes.

I have started to learn ANTLR and have both the 2007 book "The Definitive ANTLR Reference" and ANTLRWorks (an interactive tool for creating grammars). And, being that sort of person, I started at Chapter 3. ("A quick tour for the impatient"). It's a fairly painful process especially as some errors are rather impenetrable (e.g. http://s...

What's the matter with this Grammar?

grammar Test; IDHEAD: ('a'..'z' | 'A'..'Z' | '_'); IDTAIL: (IDHEAD | '0'..'9'); ID: (IDHEAD IDTAIL*); fragment TYPE: ('text' | 'number' | 'bool'); define: 'define' ID 'as' TYPE; The problem is that the define rule matches the tokens define, ID, as, but wont match TYPE. I'm yielding a MissingTokenException. If I inline the TYP...

How to generate introductory recognizer using ANTLR3C?

The Definitive ANTLR Guide starts with a simple recognizer. Using grammar verbatim to target C-runtime fails because '%s' means something to ANTLR: $ cat T.g grammar T; options { language = C; } @parser::includes { #include <stdio.h> } /** Match things like "call foo;" */ r : 'call' ID ';' {printf("invoke %s\n", $ID.t...

JavaCC Problem - Generated code doesn't find all parse errors

Just started with JavaCC. But I have a strange behaviour with it. I want to verify input int the form of tokens (letters and numbers) wich are concatenated with signs (+, -, /) and wich can contain parenthesis. I hope that was understandable :) In the main method is a string, which should produce an error, because it has one opening but...

Tolerating malformed statements with ANTLR (e.g., for code-completion)

I have an ANTLR grammar for a simple DSL, and everything works swimmingly when there are no syntax errors. Now, however, I need to support an auto-completion mechanism, where I need to get possible completions from my tree grammars that perform basic type-checking on attributes, functions, etc. The problem is, ANTLR isn't reporting syn...

Problems with implicit "and" in query grammar build using Antlr

I have been building a google-like query syntax parser in ANTLR (C#). I am finished except for one thing, which I have struggled with for a long time and failed to solve. If the user enters "word1 word2 word3", I want the parser to treat this the same as "word1 and word2 and word3". So in effect the "and" is optional/implicit between t...

How to use ANTLR to parse xml document

can anybody tell how to use ANTLR tool(in java) to create our own grammar for xml documents and how to parse those documents using ANTLR tool(in java)? ...

How to serialise an antlr3 AST

Hiya, I have just started using antlr3 and am trying to serialize the AST output of a .g grammar. Thanks, Lezan ...

Will ANTLR Help? Different Suggestion?

Before I dive into ANTLR (because it is apparently not for the faint of heart), I just want to make sure I have made the right decision regarding its usage. I want to create a grammar that will parse in a text file with predefined tags so that I can populate values within my application. (The text file is generated by another applicati...

What's JavaCC's ADVANTAGE versus ANTLR

Too many people have told me about the disadvantages, but what is its advantage if any? ...

Getting java source method name list given an ANTLR java grammar?

Given an ANTLR Java Grammar - what java source code would I write to get a list of method names in a java source listing? eg for the following public class HelloWorld { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new HelloWorld().printHelloWorld(); ...

Understanding trees in ANTLR

I'm trying to use Antlr for some text IDE-like functions -- specifically parsing a file to identify the points for code folding, and for applying syntax highlighting. First question - is Antlr suitable for this requirement, or is it overkill? This could be achieved using regex and/or a hand-rolled parser ... but it seems that Antlr is ...

ANTLR parser hanging at proxy.handshake call

I am attempting to get a basic ECMAScript parser working, and found a complete ANTLR grammar for ECMAScript 3, which appears to compile ok and produces the appropriate Lexer/Parser/Walker Java files. (Running inside ANTLR IDE plugin for Eclipse 3.5) However, when actually trying to use it with some simple test code (following guide on A...

What are the disadvantages of using Antlr?

I've worked on Flex, Bison few years ago during my undergraduate studies. However, I don't remember much about it now. Recently, I have come to hear about Antlr. Would you recommend that I learn Antlr or better to brush up Flex/Bison. Does Antlr have more/less features than Flex/Bison ? Thanks, ...

Can ANTLR grammar files be modularised?

I am building ANTLR3 applications where some of the same grammar rules are likely to be used in different applications. There will be many *.g files but they may, for example, all wish to reference the same version of FLOAT. Are there "import" facilities whereby a *.g can be assembled from other *.g files and if so what are the precedenc...

Is there a list of reserved words in ANTLR grammars?

I recently created an ANTLR3 parser rule options : foo bar; which didn't compile and it took me some time to discover that options was a reserved word (AntlrWorks indicated an error but not why). Is there a list of reserved words in ANTLR and are there best practices in naming rules (which might help avoid this)? ...

Automatic Documentation - Best method for creating a quick parser

I have a large script that end-users need to edit so it requires somewhat redundant commenting. I use a layout for my files similar to this //******************** // // FileName // This script contains: // - Function X - does something // - Function Y - does something else // //******************** //******************** // Fu...