Error Processing, Intermediate Representation, Semantic Analysis

(エラー処理、中間表現、意味解析)

11th lecture, June 28, 2019

Language Theory and Compilers

http://www.sw.it.aoyama.ac.jp/2019/Compiler/lecture11.html

Martin J. Dürst

AGU

© 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

 

Glossary

syntax error
構文エラー
secondary error
二次エラー
symbol table
名前表
scope
有効範囲、スコープ
compound statement
複文
inheritance
継承
type inference
型推論
type equivalence
型の等価
functional (programming) language
関数型 (プログラミング) 言語
lazy evaluation
遅延評価