Search code examples
phpcommand-lineadministrationembedded-language

Create a new embedded language using PHP


I am trying to develop an administration panel and I have a command line. When a user send a command like below, i need to recognize it using PHP.

My aim is simplifying tasks in the admin panel.

 create page -attr1 90 -attr2 'page title';

or

 update category 90 -name 'Technology';

There are two main things:

  1. Verb and subject (ie. create page, update category)
  2. Attributes (can be both STRING and INT)

and more complex example:

create page -name EN:'Static Page' CA:'Statična Stranica' -category 3,6,12,15;

Where can I start to create this very small embedded language, or how can I do it well really?

Clever answers, please.


Solution

  • Assuming this command line is interactive, you've set yourself no small task. Essentially you will have to write an interpreter. Assuming the lexical analysis is trivial, you'll spend most of your time writing your parser, in addition to the code behind your command set.

    If your command line is minimally interactive, and your command vocabulary is relatively small (on the order of a few commands) you can get away with writing a simplistic recognizer with regular expressions. Given the examples of parameters you are allowing, the regexes would probably get gnarly fairly quickly. In that case, or in the case where your vocabulary is a bit larger (more than a few, probably less than 20 or 30--as an indefensible stab in the dark), you'd probably need to move to a simple state-based parser, pulling tokens off the command line until you have either an error or a complete command you can process.

    How out of control it gets depends entirely on how complex you make your command vocabulary and their parameters. If you get to the point of needing to write your grammar in something like ANTLR, you've gone too far and are probably building a new programming language. :-)