开发者

Create a new embedded language using PHP

开发者 https://www.devze.com 2023-02-01 18:00 出处:网络
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开发者_JS百科 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开发者_JS百科 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.


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. :-)


I think its a lot of work... You have to split the string first at the "-". $Split_string = explode("-", $cmd_line_input); then you should check the first string on the verbs you want to include in your command line, and then make different cases: Verb_array = explode(" ", $Split_string[0]); switch($Verb_array[1])

Case create: Create page here and discover the attributes as you discover the verbs to? Im on my ipod so i cant write it out all, but i think you can stArt with this


To keep it simple, I'd use:

$args = explode(' ',$yourstring);
$cmd = array_shift($args);
//use function or class now upon requested cmd.
//pass the args, just as any bash...

regards, //t


It would be a lot simpler to just take one var in at a time, and accept or deny it based on validation of that single var and then finally move onwards if it is accepted to do the next task (be it start a sequence or ask the user for another var).

http://en.wikipedia.org/wiki/Finite-state_machine

This approach will cut out a large amount of parsing and would be quite easy to maintain.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号