phply
raw JSON → 1.2.6 verified Mon Apr 27 auth: no python
A lexer and parser for PHP source code implemented using PLY (Python Lex-Yacc). Provides a tokenizer, parser, and AST representation for PHP. Current version 1.2.6, release cadence is sporadic.
pip install phply Common errors
error ModuleNotFoundError: No module named 'ply' ↓
cause PLY is a required dependency but not automatically installed.
fix
Run 'pip install ply'.
error ImportError: cannot import name 'lexer' from 'phply' ↓
cause Incorrect import path; lexer is actually in 'phplex'.
fix
Use 'from phply import phplex' instead.
error AttributeError: module 'phply' has no attribute 'phpast' ↓
cause Trying 'import phply' then 'phply.phpast', but phpast is a submodule.
fix
Use 'from phply import phpast'.
Warnings
gotcha The lexer module is called 'phplex' not 'lexer'. Importing 'from phply import lexer' will fail. ↓
fix Use 'from phply import phplex'.
gotcha The parser module is called 'phpparse' not 'parser'. Importing 'from phply import parser' will fail. ↓
fix Use 'from phply import phpparse'.
deprecated The 'make_parser()' function returns a LALR(1) parser. In some versions, parsing with case sensitivity may differ. Always pass a lexer to parse() to avoid confusion. ↓
fix Use 'parser = phpparse.make_parser()' then 'parser.parse(code, lexer=phplex.lexer)'.
Imports
- phply.phpast
from phply import phpast - phply.lexer wrong
from phply import lexercorrectfrom phply import phplex - phply.parser wrong
from phply import parsercorrectfrom phply import phpparse
Quickstart
from phply import phplex, phpparse
from phply import phpast as ast
code = '<?php echo "Hello, World!"; ?>'
tokens = list(phplex.lex(code))
print('Tokens:', tokens)
parser = phpparse.make_parser()
parsetree = parser.parse(code, lexer=phplex.lexer)
print('AST:', parsetree)