Andrew Potozniak wrote in perl.qa :
>       I was wondering if there was anything built in Perl (a Module) that
> will take in a Perl file and parse that into an abstract or concrete syntax
> tree.  I searched around cpan for a bit and couldn't find what I was looking
> for.  If anyone is wondering what I'm talking about there is a nice Java
> package that Eclipse uses to create a tree for Java compilation that I can
> point you toward.  Thanks for your time.

Hm -- as the task of parsing perl involves some complex interplay
between compile-time and run-time -- think about the function
prototypes, for example -- perl can't be described by a non-ambiguous
grammar, and can't be parsed accurately by tools that don't include
a full-fledged perl interpreter already.

However, during parsing, perl builds internally an abstract syntax tree
for its own usage ; the syntax tree is then walked during execution.
It's possible to stop the perl interpreter after the compilation of the
main program and to have it print the current contents of the parse
tree. This is achieved by the O and B::* modules, that come with perl.
You may want something not too far from this :

    $ perl -MO=Concise -e 'print $a + $b'
    8  <@> leave[1 ref] vKP/REFC ->(end)
    1     <0> enter ->2
    2     <;> nextstate(main 1 -e:1) v ->3
    7     <@> print vK ->8
    3        <0> pushmark s ->4
    6        <2> add[t1] sK/2 ->7
    -           <1> ex-rv2sv sK/1 ->5
    4              <$> gvsv(*a) s ->5
    -           <1> ex-rv2sv sK/1 ->6
    5              <$> gvsv(*b) s ->6
    -e syntax OK

See the docs for O, B and B::Concise for more information.

Reply via email to