Chapter 1
Overview
This chapter introduces the main ideas of the language. The chapters that follow define every part precisely; each part named here links to its chapter.
What a Grammar Describes
A grammar file contains an optional package clause followed by Type and Rule Declaration Statements. Type definitions use Type Expressions; ordinary Rule Declaration Statements combine a Rule Expression with an optional Value Action containing an Action Expression. An Operators Rule is a specialized Rule Declaration Statement. Rule Expressions use PEG choice and repetition, along with PEGO-specific constructs. A parse starts at a start rule and has to match the whole input (see Parsing).
PEGO extends PEG (Parsing Expression Grammar), but it is not plain PEG: the language adds typed values, actions, contextual recognition, and error recovery:
| A grammar says | By means of | Chapter |
|---|---|---|
| How the text is structured | Rules, sequences, ordered choice, repetition, lookahead, left recursion | Rule Expressions |
| How expressions with operators are parsed | Operators Rule declarations: operators grouped by precedence and associativity | Operators Rule |
| What the result looks like | Values that follow the shape of the rule by default: strings, tuples for sequences, lists for repetitions; #atomic and - to shape them; captures label:a to name elements |
Values and the concrete syntax tree, Nodes and Values |
| What the types of the values are | type definitions: struct and join types, checked when the grammar is compiled |
Type System, Type Checking |
| How the value of a rule is built | Value Actions => expr inside rule bodies build structs, lists and other values from captures |
Actions |
| What depends on context | Predicate Actions {expr} that read and define variables |
Predicates and Variables |
| How a parse reports and survives errors | @error messages, #recover |
Attributes, Pragmas, Syntax errors |
| What may be handed over early | #stream repetitions |
Pragmas |
A grammar file is written in the language that Lexical Structure and Grammar Files define; Grammar Syntax collects its grammar in one place and the Glossary defines the terms. The portable bytecode that a grammar compiles to, and the virtual machine that runs it, are specified for those who port the runtime.