Lecture 1a: Grammars and Recursive Descent Parsing

Background Material



Context free grammars and parse trees are reviewed through several simple examples. We show how to, mechanically, write a parser given a context free grammar for which it is possible to determine which rule to apply with a single look ahead.


  1. Motivating example: reading a list
  2. Scanning and regular expressions (sec. 2.2)
  3. Context free grammars (sec. 2.1)

    Nonterminals (categories or structure names), terminals (words or tokens), production rules (rhs → lhs, where rhs is a nonterminal and lhs is a sequence of terminals and nonterminals), start symbol (distinguished nonterminal).

  4. Derivations and parse trees

    Derive a sequence of terminals by repeatedly applying production rules starting with the start symbol until no nonterminals appear. The application of rules can be recorded in a tree called a parse tree. The language generated by a grammar is the set of strings of terminals that can be derived from the grammar.

  5. Ambigous grammars

    A grammar is ambiguous if there is a string with two distinct parse trees (or equivalently two different leftmost derivations).

  6. EBNF and syntax diagrams

    Extend BNF (context free grammar notation) to include choice []and iteration {}.

  7. Recursive descent parsing (sec. 2.3)

    A parser is a program to recognize valid strings in the language defined by a grammar. It is possible in many cases to systematically (automatically) derive a parser from a given grammar. In this lecture we consider a method called recursive descent parsing. In this approach, we Turn nonterminals into mutually recursive procedures corresponding to the production rules. Each procedure attempts to match the sequence of terminals and nonterminals in the rhs of the appropriate rule. It is necessary to be able to determine which rule to apply by looking at the next token. Such a parser is called a predictive parser.

  8. Languages that are not context free

Lecture Notes

References and programs


Created: April 3, 2008 (updated April 1, 2010) by jjohnson AT cs DOT drexel DOT DOT edu