PHP Classes
elePHPant
Icontem

FSM Parser: Generic parser using finite state machine

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files View files (6)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2006-01-31 (9 years ago) RSS 2.0 feedNot enough user ratingsTotal: 685 This week: 1All time: 4,433 This week: 1,104Up
Version License Categories  
fsmparser 1.0.0Freely DistributableText processing
Description Author  

This class can be used to parse or preprocess any markup or programming language using a finite state machine.

First it is necessary to define each state transition by specifying a regular expression to determine the format of the text to expect, the code to execute when the expected text is found, the next states of the transaction to when the expected text is found or is not found.
FSM program can be loaded from file or string.

Then the state machine parse function starts parsing a given text string by executing the previsouly defined states and transitions.

A simple XML parser is supplied as an example.

Picture of Dmitry A. Kirilin
Name: Dmitry A. Kirilin is available for providing paid consulting. Contact Dmitry A. Kirilin .
Classes: 5 packages by
Country: Russian Federation Russian Federation
Age: 36
All time rank: 79126 in Russian Federation Russian Federation
Week rank: 426 Up17 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 1x

Details provided by the author  
FSM Parser
==========

What is it?
~~~~~~~~~~~
A PHP class for creating parsers and preprocessors using Finite State Machine.

How to use?
~~~~~~~~~~~
First, include the class file.
Then you must define language constructs (as regular expressions), actions
to take when each of them are found, and optional machine states: the default
target state (can be changed inside action; can be NULL if no change) and
required state (NULL if no state check is needed).

Example: xmlparser.php - a very simple XML parser.

It is also possible to load FSM definition from file or another source using
LoadFSM() and LoadFSMFile() methods.

Example: xmlparser_loadfsm.php - the same XML parser loaded from .fsm file.

FSM definition syntax.
~~~~~~~~~~~~~~~~~~~~~~
It is just like Makefile syntax:

Line beginning with "#" is a comment.
Empty line is an end of state definiton (it is required even at EOF).
Line starting with an alphanumeric symbol is a state definition:
<Required state or *> <regular expression> [Default state]
Any following line starting with tab character is an action string.


Method description.
~~~~~~~~~~~~~~~~~~~

void FSM( string Expect, string Do [, string Target [, string Require] ] )
 Add a state definition.
  Expect: regex to match closest to current position.
  Do: PHP code to execute on best match. Inside this code, $STRING is that portion of parsed text that matches "Expect", $STATE is current state.
  Target: state to take if "Do" code did not specify it explicitly.
  Require: search for "Expect" only if macine is in this state.

 "Do" code may return:
   string: state to take.
   array("STOP"=> stop ,"NEWSTATE"=> state):
    if "stop" is nonzero, the FSM will stop with FSMSTOP_STOP return code.
    if "state" is nonempty string, the machine will take this state.

void LoadFSM( string data )
 Load state definitions from string in syntax described above.

void LoadFSMFile( string filename )
 Load state definitions from specified file (wrapper for LoadFSM() ).

int Parse( string data , string StartState)
 Runs a FSM on specified data.
 Return codes:
  FSMSTOP_OK		Stopped at data end.
  FSMSTOP_STOP		Stopped by action handler.
  FSMSTOP_UNHANDLED	No matches found.

int ParseFile( string filename , string StartState )
 Runs a FSM on specified file (wrapper for Parse() ).
  Files folder image Files  
File Role Description
Accessible without login Plain text file example.xml Data Example XML file for parser testing
Plain text file fsmparserclass.inc.php Class Class source
Accessible without login Plain text file README Doc. Some documentation
Accessible without login Plain text file xmlparser.php Example Simple XML parser based on this class
Accessible without login Plain text file xmlparser_loadfsm.fsm Data FSM program for XML parser
Accessible without login Plain text file xmlparser_loadfsm.php Example XML parser with external FSM program

 Version Control Unique User Downloads Download Rankings  
 0%Total:685All time:4,433
 This week:1This week:1,104Up