使用 PHP 创建新的嵌入式语言
我正在尝试开发一个管理面板,并且我有一个命令行。当用户发送如下命令时,我需要使用 PHP 来识别它。
我的目标是简化管理面板中的任务。
create page -attr1 90 -attr2 'page title';
或者
update category 90 -name 'Technology';
有两个主要内容:
- 动词和主语(即创建页面、更新类别)
- 属性(可以是 STRING 和 INT)
和更复杂的示例:
create page -name EN:'Static Page' CA:'Statična Stranica' -category 3,6,12,15;
我可以从哪里开始创建这个非常小的嵌入式语言,或者我该怎么做真的好吗?
请聪明的回答。
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:
- Verb and subject (ie. create page, update category)
- 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设此命令行是交互式的,那么您就为自己设定了一项不小的任务。本质上,您必须编写一个解释器。假设词法分析很简单,您将花费大部分时间编写解析器,以及命令集背后的代码。
如果您的命令行交互性最低,并且您的命令词汇量相对较小(大约几个命令),您可以使用正则表达式编写一个简单的识别器。鉴于您允许的参数示例,正则表达式可能会很快变得粗糙。在这种情况下,或者在你的词汇量有点大的情况下(多于一些,可能少于 20 或 30——作为在黑暗中站不住脚的刺),你可能需要转移到一个简单的状态 -基于解析器,从命令行中提取令牌,直到出现错误或可以处理的完整命令。
它失控的程度完全取决于你的命令词汇及其参数的复杂程度。如果您需要用 ANTLR 之类的东西编写语法,那么您已经走得太远了,并且可能正在构建一种新的编程语言。 :-)
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. :-)
我认为这是很多工作...
您必须先在“-”处分割字符串。
$Split_string=explode("-", $cmd_line_input);
那么您应该检查要包含在命令行中的动词的第一个字符串,然后做出不同的情况:
Verb_array =explode(" ", $Split_string[0]);
switch($Verb_array[1])
案例创建:
在这里创建页面并像发现动词 to? 一样发现属性。
我在我的 iPod 上,所以我无法全部写出来,但我想你可以从这个开始
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
为了简单起见,我会使用:
问候,
//t
To keep it simple, I'd use:
regards,
//t
一次只接受一个变量,并根据该单个变量的验证接受或拒绝它,然后如果接受它来执行下一个任务(无论是启动一个序列还是询问),最后继续前进,这会简单得多另一个 var 的用户)。
http://en.wikipedia.org/wiki/Finite-state_machine
这种方法会减少进行大量的解析并且很容易维护。
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.