Lecture 3: Grammars, Derivations, Parse Trees, Scanning, Introduction to Oz

Syntactiv Analysis of Programs

How are programs processed?

We have seen some of these steps implemented in the mdc interpreter.

Program: if X == 1 then ...
Input: โ€˜iโ€™ โ€˜fโ€™ โ€˜ โ€™ โ€˜Xโ€™ โ€˜ โ€™ โ€˜=โ€™ โ€˜=โ€™ โ€˜ โ€™ โ€˜1โ€™ โ€˜ โ€™ โ€˜tโ€™ โ€˜hโ€™ โ€˜eโ€™ โ€˜nโ€™ ...
Lexemization: โ€˜ifโ€™ โ€˜Xโ€™ โ€˜==โ€™ โ€˜1โ€™ โ€˜thenโ€™ ...
Tokenization: key(โ€˜ifโ€™) var(โ€˜Xโ€™) op(โ€˜==โ€™) int(1) key(โ€˜thenโ€™) ...
Parsing: program(ifthenelse(eq(var(โ€˜Xโ€™)
                            int(1))
                        ...
                        ...)
                ...)
Interpretation: execution according to language semantics
Compilation: code generation according to language semantics

Derivations

Following the recipe for using a grammar explained earlier, we can derive sentences in the language L(ฮ“)L(\Gamma) specified by a grammar ฮ“\Gamma in a sequence of steps.

Rightmost and leftmost derivations

A derivation is a sequence of sentential forms beginning with a single nonterminal and ending with a (valid) sequence of terminals.

Syntax Trees

A parse tree (a syntax tree) is a structured representation of a program.

The data structure returned by the parser is an internal (intermediate) representation of the program. A parse tree can be used to:

Ambiguity

A grammar is ambiguous if a sentence can be parsed in more than one way the program has more than one parse tree.

Avoiding Abiguity

Scanning

Introduction to Oz

The Declarative Model of Computation

Kernel Language-Based Semantics

Syntax of the Declarative Kernel Language