BNF 学说有什么用?

发布于 2024-08-22 18:58:51 字数 258 浏览 2 评论 0原文

看起来很乱,如何作为参考?

http://www.doctrine- project.org/documentation/manual/1_1/en/dql-doctrine-query-language%3Abnf

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

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

发布评论

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

评论(2

我们只是彼此的过ke 2024-08-29 18:58:51

实际上,我认为它没有被任何人用作参考;但如果有人想使用一些理解 BNF 的自动工具,它可能会很有用;例如,某种代码生成器。

BNF 的优点在于它是描述语言的一种正式方式——当你是一个程序时,它比英语更容易理解。

供参考:


在评论后编辑: 这是一个关于 DQL / 对象内容的简单示例:

让我们考虑这部分代码,它使用面向对象的 API 编写查询、执行查询并获取结果(水合为数组 - 打印出来调试时只有数据,这样)

$result = Doctrine_Query::create()
    ->select('p.id, p.title, u.login')
            ->from('Ab_Model_Post p')
            ->innerJoin('p.User u')
            ->where('p.codeStatus = ?')
            ->orderBy('p.date desc')
            ->limit(2)
            ->execute(array('OK'), Doctrine::HYDRATE_ARRAY);
var_dump($result);

这是您将得到的输出类型:

array
  0 => 
    array
      'id' => string '7' (length=1)
      'title' => string 'Septième post' (length=14)
      'User' => 
        array
          'id' => string '1' (length=1)
          'login' => string 'user1' (length=5)
  1 => 
    array
      'id' => string '6' (length=1)
      'title' => string 'Sixième post (draft=7)' (length=23)
      'User' => 
        array
          'id' => string '1' (length=1)
          'login' => string 'user1' (length=5)

当然,这是考虑到模式和模型类都可以 - 对于中的示例感到抱歉法语,我使用了不久前设置的模式/模型/数据库来演示 Doctrine,它是法语的。

基本上,数据库用于博客应用程序,在这里,我们:

  • 获取一些数据 的用户来看,
  • 从帖子和发布有效帖子
  • 最近的帖子
  • 只有两个帖子

现在,这里有一个等价的,使用我所说的“DQL”作为“伪 SQL 语言”:

$result = Doctrine_Query::create()
    ->query(<<<DQL
select p.id, p.title, u.login
from Ab_Model_Post as p, 
    p.User u
where p.codeStatus = ?
order by p.date desc
limit 2
DQL
    , array('OK'), Doctrine::HYDRATE_ARRAY);
var_dump($result);

这里没有面向对象的 API (好吧,我的意思是编写查询):我只写了我正在考虑的伪 SQL——据我所知,这就是 BNF 所描述的。

当然,var_dump 的输出与我之前得到的输出完全相同。

我希望这能让事情变得更清楚:-)

I don't think it's used as a reference by any human-being, actually ; but it might be useful if someone want to use some automatic tool that understands BNF ; for instance, some kind of code generator.

The advantage of BNF being that's it's a formal way to describe a language -- much more easier to understand than english, when you are a program.

For reference :


Edit after the comments : Here's a quick example about the DQL / Object stuff :

Let's consider this portion of code, which is using the object-oriented API to write a query, execute it, and get the results (hydrated as arrays -- prints out only the data, this way, when debugging) :

$result = Doctrine_Query::create()
    ->select('p.id, p.title, u.login')
            ->from('Ab_Model_Post p')
            ->innerJoin('p.User u')
            ->where('p.codeStatus = ?')
            ->orderBy('p.date desc')
            ->limit(2)
            ->execute(array('OK'), Doctrine::HYDRATE_ARRAY);
var_dump($result);

And here's the kind of output you'll get :

array
  0 => 
    array
      'id' => string '7' (length=1)
      'title' => string 'Septième post' (length=14)
      'User' => 
        array
          'id' => string '1' (length=1)
          'login' => string 'user1' (length=5)
  1 => 
    array
      'id' => string '6' (length=1)
      'title' => string 'Sixième post (draft=7)' (length=23)
      'User' => 
        array
          'id' => string '1' (length=1)
          'login' => string 'user1' (length=5)

Of course, this is considering the schema and models classes are OK -- and sorry for the example in french, I used a schema/model/database I set up some time ago for a demonstration of Doctrine, which was in french.

Basically, the DB is for a blogging application, and, here, we :

  • get some data from posts and the user who posted them
  • for valid posts
  • the most recents posts
  • only two posts

Now, here's an equivalent, using what I meant by "DQL" as in "pseudo-SQL language" :

$result = Doctrine_Query::create()
    ->query(<<<DQL
select p.id, p.title, u.login
from Ab_Model_Post as p, 
    p.User u
where p.codeStatus = ?
order by p.date desc
limit 2
DQL
    , array('OK'), Doctrine::HYDRATE_ARRAY);
var_dump($result);

No object-oriented API here (well, to write the query, I mean) : I only wrote that pseudo-SQL I was thinking about -- which is what the BNF describes, as far as I can tell.

And, of course, the output of the var_dump is exactly the same as the one I got before.

I hope this makes things a bit more clear :-)

佼人 2024-08-29 18:58:51

这就是巴科斯-诺尔范式,一种描述上下文无关语法的方法。请参阅这篇维基百科文章

It's Backus–Naur Form, a method of describing context free grammars. See this wikipedia article.

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