News Feed
Jobs Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Michael Nitschinger's Blog:
Writing a simple lexer in PHP
May 10, 2012 @ 12:57:00

In this new post to his blog Michael Nitschinger shows you how to create a simple lexer to parse incoming content (like custom configuration files or anything that uses its own domain-specific language).

A lot of developers avoid writing parsers because they think it's pretty hard to do so. Writing an efficient parser for a general purpose language (like PHP, Ruby, Java,...) is hard, but fortunately, most of the time we don't need that much complexity. Typically we just want to parse input coming from config files or from a specific problem domain (expressed through DSLs). DSLs (Domain Specific Languages) are pretty cool, because they allow you to express logic and flow in a very specific and convenient way for a limited set of tasks.

He illustrates with an example based on the Lithium framework's routing engine and how it could parse a text file that relates a route to a controller/action combination. He creates a "Lexer" class that defines a few regular expressions to parse the incoming text strings for matches on things like whitespace, URLs and identifiers (words) and return each in the lexer's output.

0 comments voice your opinion now!
lexer parse configuration regularexpression tutorial


blog comments powered by Disqus

Similar Posts

Developer.com: PHP 5 OOP - Delegation and Custom Exceptions

Thilanka Kaushalya's Blog: How to use Mysql Transactions with PHP

WebReference.com: XML and PHP Simplified - XML,PHP and the Database

Felix Geisendorfer's Blog: Setting up Xdebug on Mac OS X (or Win32 / Linux)

SitePoint.com: Introduction to Unit Testing in PHP with PHPUnit


Community Events











Don't see your event here?
Let us know!


hhvm release composer security introduction series package unittest application hack podcast symfony2 framework overview facebook component install dependency language opinion

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework