[antlr-interest] CommonTree & Tree grammar versus DIY
Terence Parr
parrt at cs.usfca.edu
Fri Aug 15 12:17:16 PDT 2008
On Aug 15, 2008, at 10:13 AM, Andy Tripp wrote:
> There are an awful lot of parser/lexer tools out there besides ANTLR.
> I would guess that of all the people doing AST manipulation, few of
> them
> use ANTLR treewalkers to do it.
I don't recommend rewriting asts unless you stay in same tree structure.
>>> The "manipulating an AST" work of building a translator is quite a
>>> different
>>> job than the "build a lexer/parser" part of it.
>> Yep. i'm mostly a fan of walking not rewriting ASts by the
>> way...that gets WAY too hard.
>
> Maybe I'm misunderstanding you, but walking vs. rewriting is like
> apples vs. oranges (or "the drive to and from work" vs. "going to
> work all day"). Walking is what you do when there's (almost) nothing
> to do.
Except the entire work of the semantic analyzer of a compiler
etc... ;) Compute symb tables, flow analysis, well, just about
everything right? ;)
> Further, Andy has a nice approach of rewriting.
>> Expect ANTLRMorph, by Leon Su, this Fall!
>
> To pick an AST manipulation rule at random...consider that when
> translating
> C to Java, you can assign a zero value to any C struct. The C struct
> will become
> a Java class, and the zero needs to change to "null". Where in the AST
> do we need to check? Here are a few ways to "assign 0" in C:
>
> * simple assignment: a = NULL; (where we have #define NULL 0
> somewhere)
> * function call arg: f(0); (where f's first arg is some
> struct type)
> * ternary operator: a = b ? 0 : 0; * function return: a =
> f(); (where f() returns struct type)
You mean pointer to struct not struct right?
> So if we have a checkForZeroAssignment() method, calls to it will
> have to be inserted
> in at least four distinct places in our Ctree.g file. And
> checkForZero() will
> have to "know" where it's being called from anyway (for example, in
> the
> simple assignment case, it may have to search deep into nested curly
> braces
> for array assignment).
>
> So which way is best to slice the problem? We can have a
> ZeroAssignmentRule class (along with a couple hundred others to do
> other things)
> that searches the AST for these four cases. Or
> we can have a Ctree.g that, at the ASSIGN node calls
> checkForZeroAssignment()
> and a bunch (20? 50?) of other checks to do other things.
>
> I'll be pretty impressed if ANTLRMorph makes this kind of work any
> easier,
> as none of the other AST-rewrite systems seem to.
We don't do AST rewrite. We go text-to-text. You would list those
rules above as you've specified.
assignment:
"x = NULL;" -> "<x> = null"
expr:
"f(0)" -> "<f>(null)"
the ternary could be handled a few ways...but it's worse, right? You
have a type conflict from int to ptr. You have to decide what to do
in that case in general.
ANTLRMorph rocks!
Ter
More information about the antlr-interest
mailing list