ANTLR Section Index
What is the difference between using "a" and A in the parser if I have rule A : "a"; in the Lexer?
Imagine input:
class P extends Parser;
a : "a" A ;
class L extends Lexer;
A : "a";
ANTLR will think "a" and A are two different tokens as it doesn't know that in the Lexer you will define A...more
How do I convert my PCCTS grammar to ANTLR?
Check out the article describing conversion of the old C++ grammar to new ANTLR. This is based on Tom Nurkkala's original article.more
Is ANTLR appropriate for building a line-oriented preprocessor like the C preprocessor or m4?
Greg Lindholm points out:
ANTLR doesn't have a way of specifing start-of-line as part of a rule.
Once you have entered a rule you can use a sematic predicate to check
what column your at, but t...more
What is a tree parser and why would I want to use one?
Only simple, so-called syntax directed translations can be done with
actions within the parser. These kinds of translations can only spit
out constructs that are functions of information already ...more
What's the difference between a parse tree and an abstract syntax tree (AST)? Why doesn't ANTLR generate trees with nodes for grammar rules like JJTree does?
A parse tree is a record of the rules (and tokens) used to match some input text whereas a syntax tree records the structure of the input and is insensitive to the grammar that produced it. Note ...more
How do I build a source-to-source translator in ANTLR? What is the basic strategy?
In most general terms, the strategy for building a translator can be summarized as follows:
Parse the input file(s) in the old language, constructing a tree representing the structure of the inp...more
Why does ANTLR say that two of my lexer rules are ambiguous? Or, why am I having so much trouble with DOT or PERIOD at the left-edge of lexer rules?
Imagine that your grammar (prolog) has two types of tokens that begin with
DOT (the period symbol). Specifically the "." may appear alone as
the end token or it may appear as the start of a graph...more
Why are trees not built properly when I manually manipulate the return tree (with an action) for a rule?
Generally the problem is that you cannot use manual tree construction actions within a rule that is doing automatic tree construction--you confuse the automatic mechanism. For example, to constru...more
How can I track the character position within an input file? I want know at what index does a token start.
One way is to implement your own input stream and to override all the read() methods (much like FilterInputStream).
You can supply an instance of this stream to the lexer's constructor.
Note t...more
Can I push back the most recently read token into the input stream?
You are thinking in terms of the old one-token-of-lookahead type parser generators without backtracking capabilities. ANTLR specifically provides a mechanism to lookahead ahead a fixed amount, k,...more
Is there a C++ grammar for ANTLR? Is it even possible to parse C++ with a conventional grammar?
Update: There is a C++ grammar now for ANTLR, converted from old PCCTS: here.
The simple answer is that C++ is pretty much impossible to parse by merely writing up its grammar and running it thro...more
Why can't I get the exportVocab/importVocab directives to work with string literals across multiple files?
Christopher writes:
The literal ".accept" matches using this set of import/export rules:
class TestingParser extends Parser;
options {
exportVocab=TestingParser;
}
command : ".accept"
...more
ANTLR generates methods for rules that are too complex/large for my compiler. What can I do?
You can break up your large rule into a chain instead of one big alternative list like the following:
a : T1 | T2 | T3 | b ;
b : T4 | T5 | T6 | c ;
c : T7 ;
...
That will result in the same ...more
Testing
1-2-3
How do I parse a formula? I have a string containing a mathematical formula formula like "144/12". How do I calculate its value?
[Jo Desmet has created a forumla parser package called JMEP (or com.neemsoft.jmep). Write him at Jo_Desmet@yahoo.com to learn more. Maybe with enough input, he'll release it as open...more