boost之spirit学习-mini_c(2)

main.cpp就一个main函数,倒是直接明了

main函数大概的流程:

  • 读代码文件
  • 使用spirit将代码解析成抽象语法树
  • 使用compiler把语法树编译成字节码
  • 从编译后的字节中找出main函数
  • 为main函数传入参数(通过虚拟栈传递)
  • 使用VM执行字节码
  • 输出main函数的返回值

一点点把玩其代码:

1. 读"代码文件":

点击(此处)折叠或打开

  1.     std::string source_code; // We will read the contents here.
  2.     in.unsetf(std::ios::skipws); // No white space 
  3.     std::copy(
  4.         std::istream_iterator<char>(in),
  5.         std::istream_iterator<char>(),
  6.         std::back_inserter(source_code));

居然用copy、istream_iterator、back_inserter来读文件,文节青年的代码啊

像我等普通青年,估计就用二进制方式打开文件,判断一下文件长度,直接分析好内存,一次read搞定了

2. 解析语法树

点击(此处)折叠或打开

  1. bool success = phrase_parse(iter, end, +function, skipper, ast);

其中iter和end给出了解析的字符串的起始位置

+function表示解析的语法规则。注意其中加号是不能省的,它表示匹配function一次或多次。spirit重载了+号*号,以和BNF范式统一,不过由于C++语言的限制,它们只能放在规则之前,而不是像传统一样写在规则之后。

skipper表示使用的跳过语法规则。命中skipper的字符串部分将直接跳过,不参与解析。

后面的代码如下。比较直接,不再赘述

点击(此处)折叠或打开

  1.     if (success && iter == end)
  2.     {
  3.         if (compiler(ast))
  4.         {
  5.             boost::shared_ptr<client::code_gen::function>
  6.                 p = compiler.find_function("main");
  7.             if (!p)
  8.                 return 1;

  9.             int nargs = argc-2;
  10.             if (p->nargs() != nargs)
  11.             {
  12.                 std::cerr << "Error: main function requires " << p->nargs() << " arguments." << std::endl;
  13.                 std::cerr << nargs << "supplied." << std::endl;
  14.                 return 1;
  15.             }

  16.             std::cout << "Success\n";
  17.             std::cout << "-------------------------\n";
  18.             std::cout << "Assembler----------------\n\n";
  19.             compiler.print_assembler();

  20.             // Push the arguments into our stack
  21.             for (int i = 0; i < nargs; ++i)
  22.                 vm.get_stack()[i] = boost::lexical_cast<int>(argv[i+2]);

  23.             // Call the interpreter
  24.             int r = vm.execute(compiler.get_code());

  25.             std::cout << "-------------------------\n";
  26.             std::cout << "Result: " << r << std::endl;
  27.             std::cout << "-------------------------\n\n";
  28.         }
  29.         else
  30.         {
  31.             std::cout << "Compile failure\n";
  32.         }
  33.     }
  34.     else
  35.     {
  36.         std::cout << "Parse failure\n";
  37.     }

 

注意:编译后的字节码是放在compiler对象里的。但虚拟栈是由vm对象提供的。

有点奇怪function为什么用shared_ptr来返回,貌似完全没有share的必要。或者仅仅是为了避免少写compiler的析构函数?

另一个奇怪之处是从compiler获取了main函数的指针,但仅仅用来判断传入参数的个数。vm.execute并没有指定从main函数开始执行。那么程序是怎么跳到main函数去执行的呢?直觉猜想编译出的代码里起始位置应该有直接jump到main函数的代码。翻了一下compiler.cpp,果然!

点击(此处)折叠或打开

  1.     bool compiler::operator()(ast::function_list const& x)
  2.     { 
  3.         // Jump to the main function
  4.         code.push_back(op_jump);
  5.         code.push_back(0); // we will fill this in later when we finish compiling
  6.                            // and we know where the main function is

  7.         BOOST_FOREACH(ast::function const& f, x)
  8.         { 
  9.             if (!(*this)(f))
  10.             { 
  11.                 code.clear();
  12.                 return false;
  13.             } 
  14.         } 
  15.         // find the main function
  16.         boost::shared_ptr<code_gen::function> p = 
  17.             find_function("main");

  18.         if (!p) // main function not found
  19.         { 
  20.             std::cerr << "Error: main function not defined" << std::endl;
  21.             return false;
  22.         } 
  23.         code[1] = p->get_address()-1; // jump to this (main function) address

  24.         return true;
  25.     }


其中又有一个boost的奇技淫巧的应用:BOOST_FOREACH,在<boost/foreach.hpp>中,用于简化遍历容器

不过C++11中已经从语言层面对此进行了支持,语法上略有不同:

点击(此处)折叠或打开

  1. int arr[] = {1, 2, 3, 4, 5};
  2. for (int& i : arr)
  3. {
  4.     cout << i << endl;
  5. }

main.cpp解析完了,明天看看ast.hpp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MATLAB是一种用于进行数值计算和数据可视化的软件平台。使用MATLAB可以进行各种科学计算、工程计算以及数据分析等。MATLAB提供了丰富的工具箱和函数库,能够满足不同领域的科学计算需求。 在MATLAB中,boost_thread-vc120-mt-1_56.dll是一个动态链接库文件,用于支持多线程编程。多线程编程可以提高程序的性能和效率,使程序能够同时执行多个任务。这个库文件是用于Windows平台上使用Visual Studio 2013进行编译的。 要下载boost_thread-vc120-mt-1_56.dll,可以按照以下步骤进行: 1. 打开MATLAB软件。 2. 在MATLAB命令窗口中输入"mex -setup",回车。 3. 在弹出的窗口中选择"Y",表示需要重新配置MEX编译器。 4. 在编译器选项中选择"Microsoft Visual C++ Compiler",回车。 5. 在弹出的窗口中选择"Y",表示需要下载并安装支持的编译器。 6. 下载和安装完成后,重新打开MATLAB。 7. 在MATLAB命令窗口中输入"mex -setup",回车。 8. 在弹出的窗口中选择"Y",表示要重新配置MEX编译器。 9. 选择相应的编译器选项,回车。 10. 设置完成后,在MATLAB命令窗口中输入"mex -setup",回车。 11. 在编译器选项中选择"Microsoft Visual C++ Compiler",回车。 12. 在弹出的窗口中选择"Y",表示需要下载并安装支持的编译器。 13. 下载和安装完成后,重新打开MATLAB。 14. 在MATLAB命令窗口中输入"mex -setup",回车。 15. 在弹出的窗口中选择"Y",表示要重新配置MEX编译器。 16. 选择相应的编译器选项,回车。 17. 设置完成后,可以在MATLAB环境中使用boost_thread-vc120-mt-1_56.dll库文件进行多线程编程了。 以上是关于MATLAB下载boost_thread-vc120-mt-1_56.dll的简单步骤说明,希望对您有所帮助。如果有任何其他问题,请随时向我们提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值