Pattern 5Backtracking Parser

Purpose

This pattern adds speculative parsing support (arbitrary lookahead) to any recursive-descent recognizer.

Discussion

As we saw in Pattern 1, Mapping Grammars to Recursive-Descent Recognizers, we can’t map all grammars to recursive-descent parsers. Only non-left-recursive grammars work (no rule can directly or indirectly invoke itself without consuming a token). Then Deterministic Parsing Decisions showed that we can’t always get properly functioning (deterministic) parsers even from non-left-recursive grammars. The problem is that fixed lookahead LL parsers need the lookahead sets predicting alternatives to be disjoint.

This pattern overcomes this lookahead issue by allowing arbitrary lookahead, which lets ...

Get Language Implementation Patterns now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.