Lecture Notes

Notes (handout) Notes (overhead presentation) Resources
Introduction Introduction N/A
History History N/A
Functional programming Functional programming functional programming1
Models of computation Models models
Logic programming Logic programming logic programming 2 3
Scanning and parsing - a recap Scanning and parsing scanning and parsing
Semantic analysis Semantic analysis semantic analysis
Types and classes Types and classes types and classes
Subprograms Subprograms subprograms
Pointers, references, and memory management Pointers, references, and memory management pointers

Lecture Recording

Note: Lecture 14 was a review for the first examination (plus a fire alarm and a subsequent evacuation) and so was not recorded. The first examination happened in Lecture 16.

A Simple Parser Example

Consider these two figures. Figure 1 shows the grammar of a simple yet pretty realistic programming language. The possible token types are shown in the figure as capitalized words, parentheses, and operators. Note that the actual lexemes for the tokens IF, ELSE, WHILE, and BREAK are all in lower case. BASIC types are bool, int, char, and double.

We now aim to construct a recursive descent parser for this grammar. In order to do that we must convert the grammar in a form that is suitable for recursive descent parsing. The result is shown in Figure 2.

We can then proceed with the development of a recursive descent parser. The result is parser.cc. The input (program to be parsed) is read from the standard input stream. The output is a printout of the respective parse tree, produced to the standard output stream. Note that the parser is simplified as much as possible. In particular the lexical units in the language are separated by blank or newline characters (so that the lexical analysis does not need to use finite automata).

Here are a few simple programs that can be successfully parsed by this parser (and as far as I can see are also semantically correct; you tell me).

1Here are some possibly interesting Haskell programs (with types): expressing dates (kind of silly but features a few explicit instantiations of type classes) and binary search trees.

2Here is a definition of binary search trees in Prolog: bst.pl.

3State space search in Prolog: the cabbage, goat, and wolf problem; the Knight’s tour problem.