All Projects → oliverklee → phpparser

oliverklee / phpparser

Licence: other
Pure Java parser for PHP programs, used by the Pixy project.

Programming Languages

java
68154 projects - #9 most used programming language
Lex
420 projects

PhpParser

PhpParser generates a pure Java parser for PHP programs. Invoking this parser yields an explicit parse tree suitable for further analysis.

This package is based upon:

The modifications to JFlex and Cup have been documented in the files inside the directory doc/modifications.

Project settings for IntelliJ IDEA

  • Project > Language Level: 1.7
  • Modules > Sources: only src/java_cup, src/project, src/jFlex

Building and cleaning the project with Ant from within Eclipse

  1. Project > Properties > Builders
  2. Deactivate the Java Builder.
  3. New ...
  4. Select "Ant builder"
  5. Name it "Ant build" or "PhpParser build" (or any other suitable name).
  6. In the Main tab, select the build.xml in the project directory as Buildfile and the project directory as Base directory.
  7. In the Targets tab for "Manual build", select "build".
  8. In the Targets tab for "During a clean", select "clean all".
  9. OK the changes for both dialogs.

You then can build the project using Project > Build Project and Clean the project using Project > Clean ...

Or you can build the project using the command line from within the project main directory:

ant build

Usage

All you need to do in order to use the generated PhpParser is to copy the directories:

build/class/at

and

build/class/java_cup

into your project and make their parent directory part of your class path.

Then add the following import statement to the Java file which shall invoke PhpParser:

import at.ac.tuwien.infosys.www.phpparser.*;

Then create the parse tree for a file given by "fileName" in the following way:

PhpLexer lexer = new PhpLexer(new FileReader(fileName));
lexer.setFileName(fileName);
PhpParser parser = new PhpParser(lexer);
ParseNode rootNode = (ParseNode) parser.parse().value;

The last two statements must be enclosed by a matching try-catch clause. The directory doc/example contains a simple usage example that parses one or more PHP files and prints the lexemes of the parse tree nodes. It can be compiled by changing into that directory and typing

javac -classpath ../../build/class Example.java

Execute it by typing

java -classpath ../../build/class:. Example test1.php test2.php

For more information, see the Javadoc inside doc/html. It is generated along with building the project.

Directory layout

  • toplevel
    • build.xml
    • README
    • build
      • class
        • generated java class files
      • java
        • generated java source files (PHP Lexer and Parser)
    • doc
      • various documentation files
    • src
      • java_cup
        • modified version of the Cup parser generator
      • jflex
        • modified version of the JFlex scanner generator
      • project
        • parse tree data structures
      • spec
        • specification (input) files for Cup and JFlex

PhpParser and Pixy

PhpParser was developed as a sub-project of the PHP security vulnerability scanner Pixy, which resides on Github.

Credits

The original author of PhpParser was Nenad Jovanovic, [email protected]. The current maintainer is Oliver Klee, [email protected].

Many thanks to Engin Kirda and Christopher Krügel for their invaluable advice and support.

Thank you for using PhpParser.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].