Error Processing, Intermediate Representation, Semantic Analysis

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

11th lecture, June 16, 2017

Language Theory and Compilers

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

Martin J. Dürst

AGU

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

Today's Schedule

Leftovers from Last Week

Summary of Last Week

 

Hints for Homework

Deadline: June 29, 2017 (next Thursday), 19:00

Expand the simple calculator of calc.y to a small programming language (see last week for details).

Now is your chance to ask questions!

Additional hints:

For the variables, use an arrary of appropriate length

Example of inputs and outputs: test.in; test.check

 

 

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