Skip to Main Content
Language Implementation Patterns
book

Language Implementation Patterns

by Terence Parr
December 2009
Intermediate to advanced content levelIntermediate to advanced
380 pages
9h 2m
English
Pragmatic Bookshelf
Content preview from Language Implementation Patterns
Pattern 14Tree Grammar

Purpose

Tree grammars are a terse and formal way of building an external visitor.

Visitors generated from tree grammars are usually called tree parsers because they are the two-dimensional analog of conventional parsers.

Discussion

In Automatically Generating Visitors from Grammars, we saw that tree grammars look just like conventional parser grammars except that we can match subtree patterns as well. As with parser grammars, we can embed actions to extract information or reorganize the input (a tree, in this case).

In order to convert tree grammars to tree parsers (visitors), we can follow Pattern 1, Mapping Grammars to Recursive-Descent Recognizers. The only extra mapping we need is for tree structures:

 
// match ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Implementation Patterns

Implementation Patterns

Kent Beck

Publisher Resources

ISBN: 9781680500097Errata Page