什么是可重入解析器? [英] What is a re-entrant parser?

查看:91
本文介绍了什么是可重入解析器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释吗?尤其是以下两者之间的区别:

Can someone explain this to me? In particular the difference between:

http://github.com/whymirror/greg 前者是后者的可重入版本.

The former being a re-entrant version of the later.

推荐答案

最简单的是,可重入解析器不使用全局变量,因此可以同时具有多个活动实例(不一定与线程相关,但是这是我怀疑的主要用例.

At its simplest a re-entrant parser doesn't use global variables and thus can have multiple instances active at the same time (not necessarily related to threading, but this is the main use case I suspect).

但是,在更复杂的用例中,您可以拥有一个解析器,该解析器实际上可以解析同一源文档中的多种语言.例如,考虑一个JSP解析器,它必须解析同一文件中的Java代码和HTML.您可以制作两个解析器并在它们之间进行切换,而无需制作一个涵盖两种语言的大型解析器(这在某些情况下可能是非常不切实际的).但是,如果您的解析器在它们之间使用全局状态切换可能会出现问题.重入解析器使您可以以协程形式或简单的"parser-A调用parser-B嵌入代码然后返回"的情况轻松地在解析器之间进行切换.

In more complex use cases, however, you can have a parser that parses, in effect, multiple languages in the same source document. Consider a JSP parser, for example, that has to parse Java code and HTML in the same file. Instead of making one huge parser that covers both languages (something likely highly impractical) you can make two parsers and switch between them. If your parsers, however, use global state switching between them could be problematical. A re-entrant parser allows you to switch between parsers easily, either in the form of coroutines or in simple "parser-A calls parser-B for embedded code and then returns" situations.

编辑后添加:

如果您想要极端的重入解析,请看一下解析器组合器(如Parsec),其中语法"中的每个子表达式完全是一个单独的解析器.您可以通过组合大量的小型解析器来构建大型解析器.

If you want an extreme form of re-entrant parsing, take a look at parser combinators (like Parsec) where each sub-expression in the "grammar" is a separate parser entirely. You build a large parser by combining a myriad of small ones.

这篇关于什么是可重入解析器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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