Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing Perl 5 and examining the syntax tree

I've read and understood that it's not possible to parse Perl 5 code without a Perl interpreter because of the BEGIN blocks.

I know there are standalone parsers like PPI (used by Perl::Critic) that approximately parses the initial document.

But I couldn't find any reference to the procedure for outputting some kind of abstract syntax tree, or whatever internal structure resembling a tree that Perl uses for the execution phase.

It would be interesting to compare the internal representation for the parse and execution phases to see the code expand and change.

like image 882
Alejandro Pulver Avatar asked Mar 13 '18 17:03

Alejandro Pulver


1 Answers

So basically there are a few options:

  1. For static analysis, use PPI, which has the downside of not processing Perl code at BEGIN blocks. This would be analogous to "parsing".

  2. For dynamic analysis, use Use B::Concise to get the internal opcodes used for execution. This would be analogous to "compiling" (to the Perl VM).

  3. A combination of both would be using B::Deparse to get Perl code of the internal representation (instead of VM opcodes), and then send it to PPI for building the tree.

EDIT: clarified point 3 based on comment. Still don't know a command that would expand imports and output a single big file (so that syntax extensions are resolved).

like image 61
Alejandro Pulver Avatar answered Oct 20 '22 18:10

Alejandro Pulver