What's new? | Help | Directory | Sign in
Google
yaxx
YACC eXtension to XML
  
  
  
  
    
Search
for
Updated Dec 16, 2006 by wcandillon
Labels: Phase-Implementation, Phase-Deploy, Featured
phpParseTree  
The Parse_Tree extension generates an XML parse tree from a php code.

Introduction

The Parse_Tree extension generates an XML parse tree from a php source code. For example:

<?php
if (!extension_loaded('parse_tree')) {
$prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
dl($prefix . 'parse_tree.' . PHP_SHLIB_SUFFIX);
}
echo generate_parse_tree(__FILE__);
?>

Will output something very ugly like:

<yaxx:T_IF id="4">if</yaxx:T_IF>
<yaxx:CHAR40 id="6">(</yaxx:CHAR40>
<yaxx:expr id="28">
<yaxx:expr_without_variable id="27">
<yaxx:CHAR33 id="8">!</yaxx:CHAR33>
<yaxx:expr id="24">
<yaxx:r_variable id="23">
<yaxx:variable id="20">
<yaxx:base_variable_with_function_calls id="1d">
<yaxx:function_call id="1c">
<yaxx:T_STRING id="a">extension_loaded</yaxx:T_STRING>
<yaxx:CHAR40 id="c">(</yaxx:CHAR40>
etc...

This extension also provides an XSLT stylesheet able to output a php source code from an XML parse tree.

define('XML_OPTIONS', LIBXML_DTDLOAD | LIBXML_NOENT | LIBXML_DTDATTR | LIBXML_NOCDATA);
 
$source = parse_tree_from_file('source/order.php');
/*
Tree modifications...
*/
$xml = new DOMDocument;
$xml->loadXML($source, XML_OPTIONS);
 
$xsl = new DOMDocument;
$xsl->load('toWrite.xsl', XML_OPTIONS);
 
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
file_put_contents('target/order.php', $proc->transformToXML($xml));

So now you just have to create your own XSLT stylesheet to perform any operations you want (code optimization, code obfuscation, whatever...).

The parse tree generator

If you want, you can try this extension without installing it: http://phpaspect.org/ast

Contacts

You can join the googlegroups about this pecl extension: php_parse_tree@googlegroups.com, see the archives.

If you have any specific question, you can e-mail me directly: wcandillon@elv.telecom-lille1.eu.


Comment by damien.seguy, Dec 13, 2007

With newer versions of parse_tre, this example should use

parse_tree_from_file

instead of

generate_parse_tree

(tested on PHP 5.3.0-dev, with parse_tree-0.1.4).

Comment by a...@kollmorgen.net, Feb 11, 2008

the demo at http://phpaspect.org/ast doesn't work:

"The parse generator is currently not working, I need to put this application somewhere else, please come back soon."

then the link here might be disabled as well.


Sign in to add a comment