Pattern 5Backtracking Parser


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


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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.