在没有文件 I/O 的情况下检索可重入词法分析的结果

发布于 2024-12-09 15:30:42 字数 913 浏览 3 评论 0原文

我在 C++ 项目中有一个 Flex 模块,我想从多个线程调用它。 我的 .lex 文件的选项包括堆栈和可重入。 假设可以同时从不同线程对不同扫描仪对象调用 yylex 并且一个词法分析序列不会阻止另一个词法分析序列(我认为是这种情况,但我不完全确定),如何做我恢复其中每一个的输出?我看到的唯一工具是 yyset_out(FILE* handle, yyscan_t Scanner),但我不想必须使用文件,因为我只想要词法分析的结果,而不必进行文件写入。

我设置输入为:

void scanProcedure(...threadInfo...) {
   yyscan_t tscanner;

   yylex_init(tscanner);
   yy_scan_string(threadInfo->lexMe, tscanner);
   yylex(tscanner);

   /* how to retrieve results of lexing in a 
      reentrant manner ( no global state info ) 
      without having to do any file i/o */

   /* threadInfo-> ?? */

   yylex_destroy(tscanner);
}

...

pthread_create( ... scanProcedure ... threadInfoA ... );
pthread_create( ... scanProcedure ... threadInfoB ... );
pthread_create( ... scanProcedure ... threadInfoC ... );

pthread_join( ... );

另外,我意识到我可以在临时文件上调用 yyset_out ,然后访问文件缓冲区,但在我看来,似乎应该有一个比这更漂亮(更少黑客)的解决方案。

I have a flex module in a C++ project that I want to call from multiple threads.
My .lex file's options include stack and reentrant.
Assuming that it is possible to call yylex on different scanner objects from different threads simultaneously and not have one lexing sequence block the other (which I think is the case but i'm not entirely sure), how do I recover the output for each of these? The only facility I see for this is yyset_out(FILE* handle, yyscan_t scanner), but I don't want to have to have to use files because I only want the results of the lexing without having to do a file write.

I set the input with:

void scanProcedure(...threadInfo...) {
   yyscan_t tscanner;

   yylex_init(tscanner);
   yy_scan_string(threadInfo->lexMe, tscanner);
   yylex(tscanner);

   /* how to retrieve results of lexing in a 
      reentrant manner ( no global state info ) 
      without having to do any file i/o */

   /* threadInfo-> ?? */

   yylex_destroy(tscanner);
}

...

pthread_create( ... scanProcedure ... threadInfoA ... );
pthread_create( ... scanProcedure ... threadInfoB ... );
pthread_create( ... scanProcedure ... threadInfoC ... );

pthread_join( ... );

Also, I realize that I could call yyset_out on a temporary file and then access the file buffer but it seems to me as though there should be a prettier (less hackish) solution than that.

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

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

发布评论

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

评论(2

温折酒 2024-12-16 15:30:42

好吧,如果您使用默认输出,它会发送到一个文件,但没有必要这样做——您只需编写操作即可将输出发送到您想要的任何地方。如果您需要一些额外的每个扫描仪状态来跟踪输出的去向,您可以定义自己的 YY_EXTRA_TYPE 数据结构并使用 yyget_extra/yyset_extra 来操作它。

Well, if you're using the default output, it goes to a FILE, but there's no need to do that --you can just write your actions to send the output to anywhere you want. You can define your own YY_EXTRA_TYPE data structure and use yyget_extra/yyset_extra to manipulate it, if you need some extra per-scanner state to keep track of where the output is going.

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