Preserving parens (precedence override) in AST

Clint Olsen <clint@0lsen.net>
14 Mar 2003 17:35:05 -0500

          From comp.compilers

Related articles
Preserving parens (precedence override) in AST clint@0lsen.net (Clint Olsen) (2003-03-14)
| List of all articles for this month |
From: Clint Olsen <clint@0lsen.net>
Newsgroups: comp.compilers
Date: 14 Mar 2003 17:35:05 -0500
Organization: AT&T Broadband
Keywords: parse, question
Posted-Date: 14 Mar 2003 17:35:05 EST

So, what is generally the best way to preserve grouping (parens) operators
in the AST? Many of the compiler treatments seem to toss this information
out:


E: ( E )
E: - E
E: E B E
B: + | - | / | *


So, in the first rule, they just assign $$ = $2 and call it done. Some say
to rely on the different "appearance" of the tree due to the use of the
parentheses, but I don't know if this is very reliable. For example:


a + b + c + d


would yield:


            +
          / \
        + d
      / \
    + c
  / \
a b


whereas:


(a + b) + (c + d)


would yield:


          +
      / \
    + +
  / \ / \
a b c d


but I don't know if there is an "attractive" way to check for these
situations when walking the tree for your target language. You also have
to consider that the target language won't necessarily have the same
precedence rules, either.


Thanks,


-Clint
[I'd either define a flag bit in my operator nodes to say that this was
a parenthesized expression so don't mess with it, or else define a unary
() operator so your last example would turn into this:


          +
      / \
    () ()
    | |
    + +
  / \ / \
a b c d


-John]





Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.