Pattern 1Mapping Grammars to Recursive-Descent Recognizers

Purpose

This translates a grammar to a recursive-descent recognizer that matches phrases and sentences in the language specified by the grammar.

This pattern identifies the core control-flow framework for any recursive-descent lexer, parser, or tree parser.

Discussion

Even when building lexers and parsers by hand, the best starting point is a grammar. Grammars are a very concise way to express the languages you intend to recognize. Not only that, but grammars are excellent documentation that can go into a reference manual and into parsing code as comments. This pattern gives us a way to build recognizers directly from grammars.

A word of caution: this pattern works for many but not all ...

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.