Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a re-entrant parser?

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.

like image 291
cloudhead Avatar asked Mar 14 '10 06:03

cloudhead


2 Answers

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.

like image 73
JUST MY correct OPINION Avatar answered Jan 02 '23 15:01

JUST MY correct OPINION


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.

like image 36
KMån Avatar answered Jan 02 '23 16:01

KMån