ANTLR Section Index | Page 8
Can I do (one-pass) code generation with a tree parser? What is the normal code generation strategy?
If your language is complicated and difficult to compile (e.g., C++,
Verilog, VHDL) then the [one-pass "syntax-directed" translation]
technique is not powerful enough. It also does not work well...more
Does ANTLR always backtrack if I use a syntactic predicates?
No. ANTLR only backtracks if it cannot decide between the predicated alternative and the others in that alternative block. So if you have:
stat : (decl)=>decl
| expr
| ifstat
;
...more
How can I return a tree via the return value mechanism (instead of the automatic AST construction mechanism) and access it in an invoking rule?
If you use rule labels, you won't see the return value, you'll see what ANTLR generates for you: nothing.
assignment
: lhs:expr EQUALS rhs:expr // lhs,rhs NOT SET!!!!
;
expr returns [Tree t...more
How do I build my parser in C++?
Here is a really awesome page of information by Peter Morling on using C++ with ANTLR.
In general, you need to compile all the supplied .cpp files (in the cpp directory), and link them to your p...more
How do I handle #include files or other nested input streams?
There are a number of ways to handle include files.
At the parser level.
Detecting the directive in the grammar. I then extract the filename
and instantiate a new lexer and a new parser to par...more
How do I process or examine each token consumed by the parser?
Override Parser.consume(). Put an class member action after the Parser class definition in your grammar file:
class P extends Parser;
{
public void consume() throws IOException {
//do wha...more
How do I propagate/copy a type tree to multiple declarators of a variable declaration?
The Java grammar does this. For example, you want to propogate the INT tree node down to the rule that matches declarators like a and b in the following:
int a,b;
more
How do I recognize identifiers that can start with a number?
How about factoring...easiest way:
ID_OR_NUM : DIGITS (ID {$setType(ID);} | {$setType(NUMBER);}) ;
where ID and NUMBER are protected...
How do I track column and line information?
Take a loko at Monty's field guide entry about it at:
http://www.antlr.org/fieldguide/columns/index.html
.
I need to see what rules my parser is using, and need to know if there is a simple debugging option like the -gd option to antlr in v1.33? Or, I have no idea what is wrong with my parser/lexer; what can I do?
From ANTLR 2.2.0 you can use '-traceLexer', '-traceParser' or
-'traceTreeParser'. These call traceIn and traceOut methods you can
override for more info if you wish. I over-rode them to print t...more
I want the information from a syntactic predicate but don't want to select an alternative.
You can't do it, but you can probably work around it. I wanted to know
if a case block had any labels, if so the translation was slightly
different, so I just wanted to set a flag.
caseBlock
...more
What changes do I need to make to generate C++?
At a very simple level, you need to set the option 'language' at file
level in each of your .g files. Then run the Java antlr.Tool class as
normal.
Additionally, you need to ensure that...more
Why can't the parser tell the lexer exactly what to go get? (why can't the parser give context information to the lexer to decide what kind of token will be coming down the input stream?)
Why does ANTLR flag a warning about nondeterminism between BEGIN and
ID since BEGIN and ID always follow in that order? The reason is that
the parser applies the grammatical structure (sequence ...more
Why do lexical rules for keywords conflict with identifiers even if I specify the special case (keywords) before the more general identifier rule?
What is wrong with the following? Doesn't the lexer know by the order
in which I specify the two tokens that I want BEGIN to be a special
case of an ID?
class TestLexer extends Lexer;
...
BEGIN...more
Why doesn't my definition of string or comment does not match all characters in my input or why aren't my tokens defined by string literals matched by the lexer?
Set the char vocabulary to include all characters in your input
stream. Usually, you want charVocab='3'..'377'; in the lexer
options section.
Make sure that you have not turned off literals tes...more