提升精神:“语义行为是邪恶的”? [英] Boost Spirit: "Semantic actions are evil"?

查看:126
本文介绍了提升精神:“语义行为是邪恶的”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

阅读和观看此演示文稿:
http://boost-spirit.com/home/2011/06/12/ast-construction-with-the-universal-tree/

我发现了此语句 - 基本上我们建议不要使用语义动作。

Reading and watching this presentation: http://boost-spirit.com/home/2011/06/12/ast-construction-with-the-universal-tree/
I've discovered this statement -- basically we are suggested not to use semantic actions.

我必须承认,我已经感觉到这样:语法动作的语法实际上看起来很丑陋。当我需要扩展/改变它们时,它采取了很多微管理完全与语义行动。在演示中演示的属性语法的方法似乎更加优雅和有前途。

I must admit, that I've already felt something like that: grammars with semantic actions actually look kinda ugly. and, when I needed to extend/change them, it took a lot of "micromanagement" exactly with semantic actions. The approach with attribute grammar, demonstrated in the presentation, seems to be much more elegant and promising.

所以我想问:这是一个官方点吗?我应该学习如何使用属性语法和避免语义动作更详细的?如果这样 - 我想要求一些基本的(甚至可能是微不足道的)示例,演示这样的方法 - LISP解释器是太复杂,我不能咀嚼...

So I'd like to ask: is this is an "official" point? Should I learn how to work with attribute grammar and avoid semantic actions in more detail? If so -- I'd like to = ask for some basic (maybe even trivial) examples, demonstrating such an approach -- the LISP interpreter is too complex for me to chew...

推荐答案

我确定Hartmut会在一秒钟内回答。到此为止,这是我的行动:

I'm sure Hartmut will answer in a second. Till then, this is my take:

,这不是官方的观点。

No that is not an official point.

语义动作有一些缺点


  • 语义动作最简单的是关注分离的风格概念。您要在一个地方表达语法,在另一个地方表达语义。这有助于维护性(特别是对于编译Spirit Grammars的编译时间较长)

  • The simplest disadvantage of semantic actions is the stylistic notion of separation of concerns. You want to express syntax in one place, and semantics in another. This helps maintainability (especially with regards to the lengthy compile times for compiling Spirit Grammars)

更复杂的含义如果他们有副作用)。想象一下,当语义动作具有副作用时,从解析的节点回溯:解析器状态将被恢复,但外部效果不会。

More complicated implications if they have side-effects (which is frequently the case). Imagine backtracking from a parsed node when the semantic action had a side-effect: the parser state will be reverted, but the external effects aren't.

在某种程度上,仅使用属性 就像在功能程序中使用确定性的纯函数一样,更容易推断程序的正确性(或者在这种情况下,

In a way, using attributes only is like using deterministic, pure functions in a functional program, it is easier to reason about the correctness of a program (or, in this case the grammar state machine) when it is composed of pure functions only.

语义动作有倾向(但不是语法)必然如此)通过价值引入更多的复制;这与重回溯结合可能会降低性能。当然,如果语义动作是'重的',这本身就会妨碍解析的性能。

Semantic actions have a tendency (but not necessarily so) to introduce more copying around by value; this, in combination with heavy backtracking, could reduce performance. Of course, if the semantic action is 'heavy' this, in itself, is going to hinder performance of parsing.

语义动作适用于各种用途。事实上,如果你需要用上下文敏感性来解析非平凡语法,你不能逃避它们。

Semantic actions are good for various purposes. In fact, if you need to parse non-trivial grammars with context sensitivity you cannot escape them.


  1. code> qi :: locals<> 和继承的属性(来自 Mini XML - ASTs!示例)语义动作:

  1. Consider the use of qi::locals<> and inherited attributes (code from the Mini XML - ASTs! sample) - they involve semantic actions:

xml =
        start_tag                   [at_c<0>(_val) = _1]
    >>  *node                      
    >>  end_tag(at_c<0>(_val)) // passing the name from the 
                               // ... start_tag as inherited attribute
;

使用qi :: locals

rule<char const*, locals<char> > rl;
rl = alpha[_a = _1] >> char_(_a); // get two identical characters
test_parser("aa", rl); // pass
test_parser("ax", rl); // fail

IMO,这些语义动作通常不会产生问题,因为当它们被回溯时,下一次执行通过(相同)语义动作时, local 将被新的,正确的值覆盖。

IMO, these semantic action pose less of a problem usually, because when they get backtracked, the next time execution passes (the same) semantic action, the local will just get overwritten by the new, correct, value.

此外,一些工作真的是快速和脏,不保证使用 utree -rolled AST type:

Also, some jobs are really 'quick-and-dirty' and don't warrant the use of utree or a hand-rolled AST type:

 qi::phrase_parse(first, last, // imagine qi::istream_iterator... 
     intesting_string_pattern  // we want to match certain patterns on the fly
            [ log_interesting_strings ], // and pass them to our logger
     noise_skipper             // but we skip all noise
 );

这里,语义动作是解析器函数的核心

Here, the semantic action is the core of the parsers function. It works, because no backtracking is involved at the level of nodes with semantic actions.

语义动作是Spirit Karma中的语义动作的镜像,在那里他们通常造成较少的问题比在齐;因此即使只是为了界面/ API的一致性,语义动作也是一件好事,提高了Boost Spirit作为一个整体的可用性。

The semantic actions are a mirror-image of semantic actions in Spirit Karma, where they usually pose less of the problems than in Qi; so even if only for interface/API consistency, semantic actions are 'a good thing' and enhance the usability of Boost Spirit as a whole.

这篇关于提升精神:“语义行为是邪恶的”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆