Error Processing, Intermediate Representation, Semantic Analysis


11th lecture, June 28, 2019

Language Theory and Compilers

Martin J. Dürst


© 2005-19 Martin J. Dürst 青山学院大学

Today's Schedule

Leftovers from Last Lecture

Summary of Last Lecture


Hints for Homework

Deadline: July 4, 2018 (Thursday in one week), 19:00

Change the simple calculator of calc.y to a calculator that can handle integers and rationals.

Additional hints:

Now is your chance to ask questions!


Processing Syntax Errors


Why is Error Processing Difficult


Requirements for Error Processing


Techniques for Error Processing


Error Processing in bison


Compilation Stages

  1. Lexical analysis
  2. Parsing (syntax analysis)
  3. Semantic analysis
  4. Optimization (or 5)
  5. Code generation (or 4)


Intermediate Representation: Symbol Table


Example of Scope

extern int a; // declaration only, global scope
static int b; // file scope
int f (int a) // f: file scope; a: function scope
    int a;    // function scope
    static int b; // function scope, but persists across function calls
    while (...) {
        int a;    // block scope


Data Stored by Symbol Table


Intermediate Representation: Abstract Syntax Tree

How to construct an abstract syntax tree:
Create nodes of the syntax tree as attributes of the attributed grammar.

For example, rewrite this

exp: exp '+' term { $$ = $1 + $3; }

to this:

exp: exp '+' term
{ $$ = newnode(PLUS, $1, $3); }

(YYSTYPE has to be changed)

Most parts of an abstract syntax tree are binary (two branches), but for some constructs (e.g. arguments of a function), special treatment is necessary.

(For very simple programming languages (e.g. Pascal) and simple architectures (e.g. stack machine), it is possible to create code during parsing and to avoid the creation of an abstract syntax tree.)


Semantic Analysis


Type Equivalence

There are different ways to define type equivalence:

Example for C: type-equivalence.c (does this program compile?)


Type Equivalence in Haskell


Summary of this Lecture

Topic next week: Turing Machines



syntax error
secondary error
symbol table
compound statement
type inference
type equivalence
functional (programming) language
関数型 (プログラミング) 言語
lazy evaluation