| Liste des Groupes | Revenir à cl c |
Bart <bc@freeuk.com> wrote:I seem to remember one problem was with minus, for example original expr is:>You probably mean some more complicated example. This one is
Personally I don't have much use for CSTs for a normal compiler, but
they might be useful for source-to-source translators, or programs that
do source refactoring, where you want to preserve extras such as
parentheses even if they're not strictly needed.
>
(Injecting the right parentheses for examples like `(a + b) * c' which
would have an AST like '(* (+ a b) c)' is surpringly tricky. Easier to
just follow the original source!
easy:
(10) -> parse("(a + b) * c")
(10) (* (+ a b) c)
(11) -> unparse(parse("(a + b) * c"))
(11) "(a+b)*c"
(12) -> parse("a + b * c")
(12) (+ a (* b c))
(13) -> unparse(parse("a + b * c"))
(13) "a+b*c"
You just need to track priorities of subexpressions to produce the
above: '+' has lower priority than '*' so subexpression needs
parentheses, '*' has higher priority, so there is no need for
parentheses.
Les messages affichés proviennent d'usenet.