使用 PHP 创建新的嵌入式语言

发布于 2024-10-09 16:43:35 字数 513 浏览 6 评论 0原文

我正在尝试开发一个管理面板,并且我有一个命令行。当用户发送如下命令时,我需要使用 PHP 来识别它。

我的目标是简化管理面板中的任务。

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

或者

 update category 90 -name 'Technology';

有两个主要内容:

  1. 动词和主语(即创建页面、更新类别)
  2. 属性(可以是 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:

  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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一人独醉 2024-10-16 16:43:35

假设此命令行是交互式的,那么您就为自己设定了一项不小的任务。本质上,您必须编写一个解释器。假设词法分析很简单,您将花费大部分时间编写解析器,以及命令集背后的代码。

如果您的命令行交互性最低,并且您的命令词汇量相对较小(大约几个命令),您可以使用正则表达式编写一个简单的识别器。鉴于您允许的参数示例,正则表达式可能会很快变得粗糙。在这种情况下,或者在你的词汇量有点大的情况下(多于一些,可能少于 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. :-)

放肆 2024-10-16 16:43:35

我认为这是很多工作...
您必须先在“-”处分割字符串。
$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

浅唱ヾ落雨殇 2024-10-16 16:43:35

为了简单起见,我会使用:

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

问候,
//t

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

妥活 2024-10-16 16:43:35

一次只接受一个变量,并根据该单个变量的验证接受或拒绝它,然后如果接受它来执行下一个任务(无论是启动一个序列还是询问),最后继续前进,这会简单得多另一个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文