|
| 1 | +JLexPHP: a Lexical Analyzer Generator for PHP, based on JLex. |
| 2 | +For copyright and licensing information, see the COPYING file. |
| 3 | + |
| 4 | +This is an adaptation of some Java code that generates lexers from lex style |
| 5 | +input files. |
| 6 | + |
| 7 | +The porting effort was pretty trivial, with the hardest part being the buffer management. |
| 8 | + |
| 9 | +Usage is fairly typical of lexers; you'll want to create a lexer file like this: |
| 10 | + |
| 11 | +----8<------ |
| 12 | +<?php |
| 13 | +include 'jlex.php'; |
| 14 | + |
| 15 | +%% |
| 16 | + |
| 17 | +D = [0-9] |
| 18 | + |
| 19 | +%% |
| 20 | + |
| 21 | +D+ { echo "The number ", $this->yytext(), "\n"; } |
| 22 | +. { echo "Something else ", $this->yytext(), "\n"; } |
| 23 | + |
| 24 | +----8<------ |
| 25 | + |
| 26 | +Then run process this file: |
| 27 | + |
| 28 | + java -cp JLexPHP.jar JLexPHP.Main your.lex |
| 29 | + |
| 30 | +(the supplied makefile will create the jar file for you, or you |
| 31 | +can build it with: |
| 32 | + |
| 33 | + javac JLexPHP/Main.java |
| 34 | + jar cvf JLexPHP.jar JLexPHP/*.class |
| 35 | +) |
| 36 | + |
| 37 | +JLexPHP will output your.lex.php. It will contain a class that will recognize |
| 38 | +the input stream described in your .lex file. |
| 39 | + |
| 40 | +Usage of that class is along the lines of: |
| 41 | + |
| 42 | +$scanner = new Yylex(fopen("file", "r")); |
| 43 | +while ($scanner->yylex()) |
| 44 | + ; |
| 45 | + |
| 46 | +A more complicated scanner will use the createToken() method to create a token |
| 47 | +object that can then be fed into a parser, such as a lemon based parser. You |
| 48 | +can see an example of that in the c.lex source file. It is designed to work in |
| 49 | +conjunction with it's corresponding c.y file in my lemon port for php. |
| 50 | + |
| 51 | + |
| 52 | +You can find more information on the lexer syntax in the JLex manual: |
| 53 | +http://www.cs.princeton.edu/~appel/modern/java/JLex/current/manual.html |
| 54 | + |
| 55 | + |
| 56 | +Enjoy! |
| 57 | + |
| 58 | +--Wez. |
| 59 | + |
| 60 | + |
0 commit comments