Skip to content
A lexer generator for PHP. It is based on JLex and requires Java to generate the lexer. Once generated, the lexer only requires PHP to run.
Java PHP
Latest commit 3b6130a Jun 20, 2013 @wez Merge pull request #2 from vonloxley/patch-1
Fix size-calculation
Failed to load latest commit information.
JLexPHP remove some crufty parts Nov 24, 2006
COPYING add copying and readme information Nov 25, 2006
Makefile tweaks to fix up lexer engine Nov 24, 2006
README remove old email Oct 23, 2010
jlex.php Fix size-calculation Jun 21, 2013
simple.lex tweaks to fix up lexer engine Nov 24, 2006
test.php ignore some bits, add test script Nov 24, 2006


JLexPHP: a Lexical Analyzer Generator for PHP, based on JLex.
For copyright and licensing information, see the COPYING file.

This is an adaptation of some Java code that generates lexers from lex style
input files.

The porting effort was pretty trivial, with the hardest part being the buffer management.

Usage is fairly typical of lexers; you'll want to create a lexer file like this:

include 'jlex.php';


D = [0-9]


D+ { echo "The number ", $this->yytext(), "\n"; }
.  { echo "Something else ", $this->yytext(), "\n"; }


Then run process this file:

  java -cp JLexPHP.jar JLexPHP.Main your.lex

(the supplied makefile will create the jar file for you, or you
can build it with:

  javac JLexPHP/
  jar cvf JLexPHP.jar JLexPHP/*.class

JLexPHP will output your.lex.php.  It will contain a class that will recognize
the input stream described in your .lex file.

Usage of that class is along the lines of:

$scanner = new Yylex(fopen("file", "r"));
while ($scanner->yylex())

A more complicated scanner will use the createToken() method to create a token
object that can then be fed into a parser, such as a lemon based parser.  You
can see an example of that in the c.lex source file.  It is designed to work in
conjunction with it's corresponding c.y file in my lemon port for php.

You can find more information on the lexer syntax in the JLex manual:



Something went wrong with that request. Please try again.