9.4 Variant Records

A variant record is an aggregate data type that is a union of records (i.e., a union of structs); it can hold any one of a variety of records. Each constituent record type is called a variant of the union type. Variant records can also be inductive. Consider the idea that context-free grammars can be used to define data structures in addition to languages, which we explored in Chapter 5. A variant record is an effective building block for building a data structure defined with a context-free grammar because the variant record mirrors the EBNF definition of the data structure. We can use a linked list of integers in C to illustrate a variant record. Consider the following EBNF definition of a list:


The following ...

Get Programming Languages: Concepts and Implementation 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.