Parsing Expression Grammar - Implementing Parsers From Parsing Expression Grammars

Implementing Parsers From Parsing Expression Grammars

Any parsing expression grammar can be converted directly into a recursive descent parser. Due to the unlimited lookahead capability that the grammar formalism provides, however, the resulting parser could exhibit exponential time performance in the worst case.

It is possible to obtain better performance for any parsing expression grammar by converting its recursive descent parser into a packrat parser, which always runs in linear time, at the cost of substantially greater storage space requirements. A packrat parser is a form of parser similar to a recursive descent parser in construction, except that during the parsing process it memoizes the intermediate results of all invocations of the mutually recursive parsing functions, ensuring that each parsing function is only invoked at most once at a given input position. Because of this memoization, a packrat parser has the ability to parse many context-free grammars and any parsing expression grammar (including some that do not represent context-free languages) in linear time. Examples of memoized recursive descent parsers are known from at least as early as 1993. Note that this analysis of the performance of a packrat parser assumes that enough memory is available to hold all of the memoized results; in practice, if there were not enough memory, some parsing functions might have to be invoked more than once at the same input position, and consequently the parser could take more than linear time.

It is also possible to build LL parsers and LR parsers from parsing expression grammars, with better worst-case performance than a recursive descent parser, but the unlimited lookahead capability of the grammar formalism is then lost. Therefore, not all languages that can be expressed using parsing expression grammars can be parsed by LL or LR parsers.

Read more about this topic:  Parsing Expression Grammar

Famous quotes containing the words expression and/or grammars:

    We think of religion as the symbolic expression of our highest moral ideals; we think of magic as a crude aggregate of superstitions. Religious belief seems to become mere superstitious credulity if we admit any relationship with magic. On the other hand our anthropological and ethnographical material makes it extremely difficult to separate the two fields.
    Ernst Cassirer (1874–1945)

    A sure proportion of rogue and dunce finds its way into every school and requires a cruel share of time, and the gentle teacher, who wished to be a Providence to youth, is grown a martinet, sore with suspicions; knows as much vice as the judge of a police court, and his love of learning is lost in the routine of grammars and books of elements.
    Ralph Waldo Emerson (1803–1882)