Error Processing, Intermediate Representation, Semantic Analysis

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

11th lecture, June 17, 2016

Language Theory and Compilers

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

Martin J. Dürst

AGU

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

Today's Schedule

Leftovers from Last Week

Summary of Last Week

 

Hints for Homework

Deadline: June 23, 2016 (Thursday in two weeks), 19:00

Create a calculator for complex numbers (details see last week)

Now is your chance to ask questions!

Additional hints:

Example of inputs and outputs (not very complete): test.in; test.check

 

 

Grammar Patterns: Repetition

One or more times:

items: items item
| item
;

Zero or more times:

items: items item
|
;

Instead of "items item", "item items" is also possible, but bison's stack may become a problem

 

Grammar Patterns: Associativity

Left associative:

big_exp: big_exp left_associative_operator small_exp
| small_exp
;

Right associative:

big_exp: small_exp right_associative_operator big_exp
| small_exp
;

 

Grammar Patterns: Priority

(priority is small_exp > middle_exp > big_exp; assuming left associative)

big_exp: big_exp operator middle_exp
| middle_exp
;
middle_exp: middle_exp operator small_exp
| small_exp
;

 

Grammar Patterns: Parentheses

small_exp: open_paren big_exp close_paren
| literal
;

 

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

 

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
名前表
compound statement
複文
inheritance
継承
type inference
型推論
type equivalence
型の等価
functional (programming) language
関数型 (プログラミング) 言語
lazy evaluation
遅延評価