What is a re-entrant parser?

蓝咒 提交于 2019-12-20 10:37:49

问题


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

http://github.com/whymirror/greg and http://piumarta.com/software/peg/

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


回答1:


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).

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.


Edited to add:

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.




回答2:


From wiki:

To be reentrant, a computer program or routine:

  • Must hold no static (or global) non-constant data.
  • Must not return the address to static (or global) non-constant data.
  • Must work only on the data provided to it by the caller.
  • Must not rely on locks to singleton resources.

Not necessarily related to thread safety, but parsing multiple grammars within a document.



来源:https://stackoverflow.com/questions/2441351/what-is-a-re-entrant-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!