Introduction to Parsing

Outline

Languages and Automata

Limitations of Regular Languages

The Role of the Parser

Context-Free Grammars

Context-Free Grammars

Notational Conventions

CFG Example

CFG Example

The Language of a CFG

The Language of a CFG: Key Idea

  1. Begin with a string consisting of the start symbol \(S\)

  2. Replace any non-terminal \(X\) in the string by a right-hand side of some production \(X \rightarrow Y_1 \ldots Y_n\)

  3. Repeat step 2 until there are no non-terminals in the string

The Language of a CFG

Terminals

Parentheses Example

Example

Example Continued

Arithmetic Example

Notes

Derivations and Parse Trees

Derivation Example

Derivation Example

\[\begin{aligned} & E\\ \rightarrow & E + E\\ \rightarrow & E * E + E\\ \rightarrow & id * E + E\\ \rightarrow & id * id + E\\ \rightarrow & id * id + id \end{aligned}\]

Notes on Derivations

Left-most and Right-most Derivations

Right-most Derivation Example

\[\begin{aligned} & E\\ \rightarrow & E + E\\ \rightarrow & E + id\\ \rightarrow & E * E + id\\ \rightarrow & E * id + id\\ \rightarrow & id * id + id \end{aligned}\]

Derivations and Parse Trees

Summary of Derivations

Ambiguity

Ambiguity

Dealing with Ambiguity

Ambiguity: The Dangling Else

The Dangling Else: a Fix

Ambiguity

Precedence and Associativity Declarations

Error Handling

Syntax Error Handling

Approaches to Syntax Error Recovery

Syntax Error Recovery: Panic Mode

Syntax Error Recovery: Error Productions

Syntax Error Recovery: Past and Present