[antlr-interest] BUG?: Difference between AntlrWorks and antlr grammars? - Was "how to emit separate ASTs for 'int a,b; '"
Austin Hastings
Austin_Hastings at Yahoo.com
Tue Oct 2 23:08:10 PDT 2007
I believe this is down to the differences between superclassing with
Debug and using the debugger adaptors, but I'm far from an expert.
I feed the following line into my grammar:
int a,b;
When I run the antlrworks debugger, and fast-forward to the end, I can
view the AST and see a tree that (graphically) looks like:
^(nil
^(DECL int a)
^(DECL b)
)
More graphically:
nil
/ \
DECL DECL
/ \ |
int a b
(That looks good in a fixed-width font. YMMV.)
Anyhow, the point is that the antlrworks product appears to generate an
AST that is wrong. This is incredibly frustrating, to me and apparently
to others. Hence the recent set of emails.
However, I was in the process of trying to see exactly what changes
would cause the thing to break when I inadvertently ran a "make test" on
my system, which compiled my grammar and linked it against a test jig
that runs the grammar on an input file. I opted to try that approach,
and got:
java org.antlr.Tool src/hlasm/Hlasm.g
ANTLR Parser Generator Version 3.0.1 (August 13, 2007) 1989-2007
javac -g -d classes -sourcepath src src/hlasm/Test.java
jar -cmf manifest.txt hlasm.jar -C classes .
java -jar hlasm.jar
Hello, world. Reading from input
Got lexer
Got Tokens
Got parser
Got tree
(DECL int a) (DECL int b)
Notice the interesting tree dump at the bottom? It seems that the same
grammar produces the desired AST in plain-old-antlr, at least according
to the tree-to-string code.
I don't actually know if this is a generated-tree problem, or a
displaying-it-wrong problem. Maybe the graph drawing code is skipping
nodes it has previously seen. Regardless, there appears to be a
difference. This is either a bug in the Antlr debug library, or a bug in
AntlrWorks.
The grammar is below.
=Austin
grammar Hlasm;
options { output = AST; }
tokens { DECL; INT = 'int';}
@header {
package hlasm;
import hlasm.*;
}
@lexer::header {
package hlasm;
}
compilation_unit : decl* ;
decl:
type=type_spec declarator (',' declarator)* ';'
-> ^(DECL $type declarator)+
;
type_spec : INT;
declarator: ID;
ID: 'a'..'z';
WS: (' ' | '\n') { channel=HIDDEN; };
More information about the antlr-interest
mailing list