提高小型Ç教程 [英] Boost Mini C Tutorial

查看:128
本文介绍了提高小型Ç教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在使用Boost精神编译器教程。目前,它以整数的伟大工程。我工作的方式来扩展它来处理字符串。这里是链接到源$ C ​​$ C。

http://www.boost.org/doc/libs/1_57_0/libs/spirit/example/qi/compiler_tutorial/mini_c/

对于那些熟悉升压,下面应该很熟悉 - 它是一个初级的前pression生产的规则:

  primary_expr =
            uint_
        | FUNCTION_CALL
        |识别码
        | bool_
        | (>&expr的GT;')
        ;

uint_是允许primary_expr被分配一个int。通常情况下,我们可以为一个char或通过创建一些更多的生产操作规程,否则使用标识类似的报价什么的正则表达式一个简单的文本解析字符串添加一些简单的功能。有吨的例子,如果你备份我发送了链接的根。

真正的问题来自与实施编译事实上,code推字节code业务并入一个载​​体。是微不足道的这里推单个字符,因为所有的字符具有一个伴随的ASCII code,这将是隐式转换为,但不为字符数组的情况下,因为它们将失去它们的上下文中的过程的一部分一个较大的字符串(形成一个句子,例如)。

我可以想出最好的办法是改变

 矢量<&INT GT;

 矢量<&uintptr_t的GT;

从我的理解,这种类型的指针可以指向整数和字符。虽然,它不是简单的上述生产规则中改变uint_到uintptr_t形式的问题。编译器告诉我,这是在这个特殊的情况下非法使用。

顺便问一下,你会看到我们的矢量持有compiler.cpp / .HPP文件中的字节code的实施。

任何帮助将是AP preciated,如果你需要任何更多的信息,请询问。谢谢你。


解决方案

  

通常情况下,我们可以为一个字符或字符串或者通过添加一些简单的功能,使用该标识引号或类似的东西正则表达式创建一些更多的生产规则,否则,一个简单的文本分析器


不支持正则表达式。您的可以的使用常规的前pression语法的升压精神莱克斯模式的一个子集(可以在 token_def 使用),但会复杂化画面增色不少。


  

真正的问题来自与实施编译事实上,code推字节code业务并入一个载​​体。是微不足道的这里推单个字符,因为所有的字符具有一个伴随的ASCII code,这将是隐式转换为,但不为字符数组的情况下,因为它们将失去它们的上下文中的过程的一部分一个较大的字符串(形成一个句子,例如)。


在行话:AST的不适应非整数值

的simples方法是延长的AST为一个操作数:

 的typedef的boost ::变体LT;
        零
      ,布尔
      ,无符号整型
      ,标识符
      ,标准::字符串// ADDED
      ,提振:: recursive_wrapper<一元>
      ,提振:: recursive_wrapper<&FUNCTION_CALL GT;
      ,提振:: recursive_wrapper<前pression>
    >
操作;

注:这也是属性由 primary_expr 暴露的类型和 unary_expr

现在可以延长规则:

  quoted_string =''>> *('\\\\'>> char_ |〜char_())>> '';    primary_expr =
            uint_
        | FUNCTION_CALL
        |识别码
        | quoted_string
        | bool_
        | ('('>&expr的GT;'))
        ;

请注意,我们声明 quoted_string 没有队长,所以我们不必做语义[] 咒语(<一href=\"http://stackoverflow.com/questions/17072987/boost-spirit-skipper-issues/17073965#17073965\">Boost精神队长问题)。


编译器支持

接下来,编译时原来的编译访客不知道弦呢。因此,我们添加

  op_string,//推常量字符串入堆栈

 布尔编译器::运算符()(标准::字符串常量和放大器; X)
{
    BOOST_ASSERT(电流!= 0);
    电流 - &GT; OP(op_string,X);
    返回true;
}

在相应的地方。


(仍然 https://www.livecoding.tv/sehe/ 编码,推答案这样你就可以提前阅读)

So I've been working with the Boost Spirit Compiler tutorial. Currently, it works great with integers. I am working on a way to extend it to handle strings. Here is the link to the source code.

http://www.boost.org/doc/libs/1_57_0/libs/spirit/example/qi/compiler_tutorial/mini_c/

For those familiar with Boost, the following should look familiar - it is a production rule for a primary expression:

 primary_expr =
            uint_
        |   function_call
        |   identifier
        |   bool_
        |   '(' > expr > ')'
        ;

uint_ is what allows a primary_expr to be assigned an int. Normally, we could add some simple functionality for a char or a string by either creating a few more production rules, or else a simple text parser using a regex that identifies quotes or something like that. There are tons of examples if you back up the root of the link I sent.

The real problem comes with the fact that to implement the compiler, the code pushes bytecode operations into a vector. It's trivial to push a single char here, since all chars have an accompanying ASCII code that it will be implicitly converted to, but not the case for an array of chars, since they would lose their context in the process as part of a larger string (that forms a sentence, eg).

The best option I can come up with is to change the

vector<int>

to

vector<uintptr_t> 

From my understanding, this type of pointer can point to both integers and chars. Though, it's not simply a matter of changing the 'uint_' to 'uintptr_t' within the above production rule. The compiler tells me that it's an illegal use in this particular instance.

By the way, you will see the implementation of our vector holding the bytecode within the compiler.cpp/.hpp files.

Any help would be appreciated, and if you need any more information, please ask. Thanks.

解决方案

Normally, we could add some simple functionality for a char or a string by either creating a few more production rules, or else a simple text parser using a regex that identifies quotes or something like that

Regex is not supported. You can use a subset of regular expression syntax in Boost Spirit Lex patterns (which can be used in token_def) but that would complicate the picture considerably.

The real problem comes with the fact that to implement the compiler, the code pushes bytecode operations into a vector. It's trivial to push a single char here, since all chars have an accompanying ASCII code that it will be implicitly converted to, but not the case for an array of chars, since they would lose their context in the process as part of a larger string (that forms a sentence, eg).

In jargon: the AST doesn't accommodate non-integral values.

The simples way would be to extend the AST for an operand:

typedef boost::variant<
        nil
      , bool
      , unsigned int
      , identifier
      , std::string                          // ADDED
      , boost::recursive_wrapper<unary>
      , boost::recursive_wrapper<function_call>
      , boost::recursive_wrapper<expression>
    >
operand;

(Note: this is also the type of attribute exposed by primary_expr and unary_expr)

Now lets extend the rules:

    quoted_string = '"' >> *('\\' >> char_ | ~char_('"')) >> '"';

    primary_expr =
            uint_
        |   function_call
        |   identifier
        |   quoted_string
        |   bool_
        |   ('(' > expr > ')')
        ;

Note that we declared quoted_string without a skipper so we don't have to do the lexeme[] incantation (Boost spirit skipper issues).


Compiler support

Next, when compiling it turns out the compiler visitor doesn't know the strings yet. So, we add

    op_string,      //  push constant string into the stack

and

bool compiler::operator()(std::string const& x)
{
    BOOST_ASSERT(current != 0);
    current->op(op_string, x);
    return true;
}

in the respective places.


(still https://www.livecoding.tv/sehe/ coding, pushed the answer so you can read it ahead of time)

这篇关于提高小型Ç教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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