Error Processing, Intermediate Representation, Semantic Analysis

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

11th lecture, June 29, 2018

Language Theory and Compilers

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

Martin J. Dürst

AGU

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

Today's Schedule

Leftovers from Last Lecture

Summary of Last Lecture

 

Last Week's Homework

Complete calc.y so that with test.in as an input, it produces test.check

Caution: How to deal with unary MINUS

 

Hints for Homework

Deadline: July 12, 2018 (Thursday in two weeks), 19:00

Expand the simple calculator of calc.y to a calculator for dates and time periods (numbers of days).

Additional hints:

Now is your chance to ask questions!

 

Additional Homework

(no need to submit)

Use calc.output and #define YYDEBUG 1 to understand how bison works

 

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

 

Data Stored by 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
    }
}

 

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

 

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
遅延評価