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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With