• Non ci sono risultati.

Principles  of  Programming  Languages

N/A
N/A
Protected

Academic year: 2022

Condividi "Principles  of  Programming  Languages"

Copied!
30
0
0

Testo completo

(1)

Principles  of  Programming  Languages  

h"p://www.di.unipi.it/~andrea/Dida2ca/PLP-­‐14/  

Prof.  Andrea  Corradini  

Department  of  Computer  Science,  Pisa  

•  Overview  of  a  Simple  Compiler  Front-­‐end  

–  Lexical  analysis  

–  Intermediate  code  generaBon   –  StaBc  checking    

Lesson 3!

(2)

Admins  

•  Office  Hours:  

–  Wednesday,  9  -­‐  11  

•  Check  your  data  and  add  the  University  ID   (matricola)  in  the  sheet  

2  

(3)

Compiler  Front-­‐  and  Back-­‐end  

Seman&c  Analysis   Scanner  

(lexical  analysis)  

Parser  

(syntax  analysis)  

Machine-­‐Independent   Code  Improvement  

Target  Code  Genera&on  

Machine-­‐Specific  Code   Improvement   Source program (character stream)

Tokens

Parse tree

Abstract syntax tree, or …

Modified intermediate form

Assembly or object code

F ron t e n d an al ys is Bac k e n d syn th es is

Intermediate  Code   Genera&on  

Three address code, or…

(4)

4

A  Translator  for  Simple  Expressions  based   on  PredicBve  Parsing  and  SemanBc  AcBons  

expr → expr + term expr → expr - term expr → term

term → 0 term → 1

term → 9

{ print(“+”) } { print(“-”) }

{ print(“0”) } { print(“1”) }

{ print(“9”) }

expr → term rest

rest → + term { print(“+”) } rest rest → - term { print(“+”) } rest rest → ε

term → 0 { print(“0”) } term → 1 { print(“1”) }

term → 9 { print(“9”) }

After left recursion elimination:

(5)

main()

{ lookahead = getchar();

expr();

}

expr()

{ term();

while (1) /* optimized by inlining rest() and removing recursive calls */

{ if (lookahead == ‘+’)

{ match(‘+’); term(); putchar(‘+’);

}

else if (lookahead == ‘-’)

{ match(‘-’); term(); putchar(‘-’);

}

else break;

} }

term()

{ if (isdigit(lookahead))

{ putchar(lookahead); match(lookahead);

}

else error();

}

match(int t)

{ if (lookahead == t)

lookahead = getchar();

else error();

}

error()

{ printf(“Syntax error\n”);

expr → term rest

term → 0 { print(“0”) } term → 1 { print(“1”) }

term → 9 { print(“9”) }

OpBmized  code  of   the  translator  

rest → + term { print(“+”) } rest rest → - term { print(“-”) } rest rest → ε

(6)

The  Structure  of  the  Front-­‐End  

Source Program (Character

stream)

Token stream

Intermediate representation Develop

parser and code

generator for translator

IR  specificaBon   Lexical  analyzer   Syntax-­‐directed  

translator  

Syntax  definiBon   (BNF  grammar)  

(7)

Adding  a  Lexical  Analyzer  

•  Typical  tasks  of  the  lexical  analyzer:  

–  Remove  white  space  and  comments   –  Encode  constants  as  tokens  

–  Recognize  keywords  

–  Recognize  idenBfiers  and  store  idenBfier  names  in  

a  global  symbol  table  

(8)

The  Lexical  Analyzer  (“lexer”)  

Lexical  analyzer

<id, “ y”> < assign, > <num, 31> <‘+’, > <num, 28> <‘*’, > <id, “ x”>

y := 31 + 28*x

Parser  

token

(lookahead)

tokenval (token attribute)

The  lookahead  of  the  Parser  can  be  a  token,  not  just  a  character  

(9)

Token  A[ributes  

factor → ( expr )

| num { print(num.value) }

#define NUM 256 /* token returned by lexer */

factor()

{ if (lookahead == ‘(‘)

{ match(‘(‘); expr(); match(‘)’);

}

else if (lookahead == NUM)

{ printf(“ %d “, tokenval); match(NUM);

}

else error();

The  parser  accesses  the  token  via  lookahead,  and    

the  token  a[ribute  via  the  global  variable  tokenval

(10)

Symbol  Table  

insert(s, t)

: returns array index to new entry for string s token t

lookup(s)

: returns array index to entry for string s or 0

The symbol table is globally accessible (to all phases of the compiler) Each entry in the symbol table contains a string and a token value:

struct entry

{ char *lexptr; /* lexeme (string) for tokenval */

int token;

};

struct entry symtable[];

Possible  implementaBons:  

-­‐  simple  C  code     -­‐  hashtables  

(11)

Handling  idenBfiers  (lexer)

Code executed by the lexer after an identifier has been recognized (stored in lexbuf):

/* lexer.c */

int lexan() { …

tokenval = lookup(lexbuf);

if (tokenval == 0) /* not found */

tokenval = insert(lexbuf, ID);

return symtable[tokenval].token;

}

(12)

Handling  idenBfiers  (parser)

factor → ( expr )

| id { print(id.string) }

#define ID 259 /* token returned by lexer */

factor()

{ if (lookahead == ‘(‘)

{ match(‘(‘); expr(); match(‘)’);

}

else if (lookahead == ID)

{ printf(“ %s “, symtable[tokenval].lexptr);

match(ID);

}

else error();

} provided  by  the  lexer  for  ID  

(13)

Handling  Reserved  Keywords  (lexer)  

/* global.h */

#define DIV 257 /* token */

#define MOD 258 /* token */

#define ID 259 /* token */

/* init.c */

insert(“div”, DIV);

insert(“mod”, MOD);

/* lexer.c */

int lexan() { …

tokenval = lookup(lexbuf);

if (tokenval == 0) /* not found */

tokenval = insert(lexbuf, ID);

return symtable[tokenval].token;

Simply initialize the global symbol table

with the set of

keywords

(14)

Handling  Reserved  Keywords  (parser)  

morefactors → div factor { print(‘DIV’) } morefactors | mod factor { print(‘MOD’) } morefactors | …

/* parser.c */

morefactors()

{ if (lookahead == DIV)

{ match(DIV); factor(); printf(“DIV”); morefactors();

}

else if (lookahead == MOD)

{ match(MOD); factor(); printf(“MOD”); morefactors();

} else }

(15)

Symbol  Tables  and  Scopes  

•  The  same  idenBfier  can  be  declared  several   Bmes  in  a  program  (e.g.  in  different  blocks)  

•  Each  declaraBon  has  its  own  a[ributes  (e.g.  

type)  

•  A  soluBon:  one  Symbol  Table  per  scope  

–  Chain  of  symbol  tables  for  nested  blocks   –  Hash  table  +  auxiliary  stack    

–  Entries  have  to  be  created  by  the  parser  

(16)

The  Structure  of  the  Front-­‐End  

Source Program (Character

stream)

Token stream

Intermediate representation Develop

parser and code

generator for translator

Syntax-­‐directed   translator  

Syntax  definiBon   (BNF  grammar)   Lexical  analyzer  

IR  specificaBon  

(17)

Intermediate  Code  GeneraBon  

•  Two  main  kinds  of  intermediate   representaBons:  

–  Trees  (parse  trees,  abstract  syntax  trees)  

•  Useful  for  staBc  semanBc  analysis  (“staBc  checking”)  

–  Linear  representaBons  (“three-­‐address  code”)  

•  Good  for  machine-­‐independent  opBmizaBon  

•  Oaen  compilers  produce  the  linear  code  

during  on-­‐the-­‐fly  generaBon  of  the  syntax  tree  

(18)

TranslaBon  Scheme  for  generaBng  the   Abstract  Syntax  Tree:  Expressions  

•  Each  operator  is  a  node,  with  “semanBcally  meaningful   components”  as  children  

•  For  each  producBon  the  semanBc  acBon  either  builds  a  new   node  (with  suitable  parameters),  or  returns  the  node  of  the   only  subexpression  

18  

¾|Ϯ  ̓h~̓ ̓ ̓·̓ ̓

ǐ+ ͆ )&͆ ᅼ਺ዏᎰᅼཱུᗮ)&͆Œ͆ j͆

)&͆ ·ϮÎĈϮ  ͆ĈŽĈϮ ;͆ )&͆͆'R  ͆Œ͆j͆

 ͆  ƒ͆  ͆ ;͆  -͆'R 'ɼ V͆? ƒ -͆ -8 Œ͆ j͆

8j 5ҿ ;͆  ͆-͆ 'R ŸŽNjNjŒ͆ j͆

 ͆   ñ͆ ;͆  -͆'R 'ɼ> ? -8 Œ͆ j͆

$

:ɼŸ͆   š͆ ėƒ͆

;͆  -͆'R'ɼ¼? -͆ ƒ -8 Œ͆ j͆

'ɼ Ÿ͆   š͆ ėƒ͆

;͆  -͆'R'ɼ ƺ5͆? -͆ ƒ -8 Œ͆ j͆

Œ ɼ  ƒ͆ 'ɼ Ÿ͆  ͆š͆ñ͆

;  -͆'R 'ɼ,ʙ ? ƒ -  -8 Œ j͆

)&͆ ;͆  -͆'R )&Œ͆j͆

 ͆ ͆Ϯ  ƒ͆ ;͆  -͆'R 'ɼx5͆˘ 'R-͆ ƒ -8Œ͆ j͆

8j ͆ ;͆  -͆'R ͆Œ͆ j͆

͆ ҿ ŭ͆ Ϯ  ͆ ;͆ -͆'R 'ɼ‚ ?Ĩ͆ƒ -͆ -8 Œ͆ j͆

$

ŭ͆ Ϯ  ͆ ;͆ -͆'R 'ɼ‚ ?ɐɑ͆ŭ -͆ -8Œ͆j͆

$

 ͆ ;͆ ͆'R  Œ͆ j͆

 ͆  b͆ n͆  ͆ ;͆  -͆'R 'ɼ Ĭ ?Ê͆ Ɇ -͆ -8 Œ͆ j͆

 ͆ ;͆  -͆ 'R  -Œ͆ j͆

 ͆  ƒ͆ É͆͆ ;͆  -͆'R'ɼ Ĭ ͆ ··.Ϯ ƒ - -8 Œ͆ j͆

Â؛ ͆ ;͆  -͆'R-Œ͆j͆

͆ ҿ Ÿ͆   š͆ ;͆-͆'R  -Œ͆j͆

ŸŽǍ͆ ; -͆'R 'ɼ ?ŸŽǍ8 Œ͆ j͆

֍್ஸᎰᅼ਺ᗮIˆK¾ƒϮԦအཱུበዏᅼᎰ।ዏ್အཱུᗮအ૤ᗮበᒹཱུዏࡐᒏᗮዏᅼ਺਺በᗮ૤အᅼᗮ਺ᒏႼᅼ਺በበ್အཱུበᗮࡐཱུূᗮበዏࡐዏ਺༌਺ཱུዏበᗮ

(19)

TranslaBon  Scheme  for  generaBng  the   Abstract  Syntax  Tree:  Statements  

•  Statements  as  operators:  note  that  the  concrete   syntax  is  dropped  

19  

¾|Ϯ

 ̓h~̓ ̓ ̓·̓ ̓

ǐ+ ͆ )&͆ ;͆

ᅼ਺ዏᎰᅼཱུᗮ

)&͆Œ͆ j͆

)&͆ ·ϮÎĈϮ  ͆ĈŽĈϮ ;͆ )&͆͆'R  ͆Œ͆j͆

 ͆  ƒ͆  ͆ ;͆  -͆'R 'ɼ V͆? ƒ -͆ -8 Œ͆ j͆

8j

5ҿ ;͆  ͆-͆ 'R ŸŽNjNjŒ͆ j͆

 ͆   ñ͆ ;͆  -͆'R 'ɼ> ? -8 Œ͆ j͆

$

:ɼŸ͆   š͆ ėƒ͆

;͆  -͆'R'ɼ¼? -͆ ƒ -8 Œ͆ j͆

'ɼ Ÿ͆   š͆ ėƒ͆

;͆  -͆'R'ɼ ƺ5͆? -͆ ƒ -8 Œ͆ j͆

Œ ɼ  ƒ͆ 'ɼ Ÿ͆  ͆š͆ñ͆

;  -͆'R 'ɼ,ʙ ? ƒ -  -8 Œ j͆

)&͆ ;͆  -͆'R )&Œ͆j͆

 ͆ ͆Ϯ  ƒ͆ ;͆  -͆'R 'ɼx5͆˘ 'R-͆ ƒ -8Œ͆ j͆

8j

͆ ;͆  -͆'R ͆Œ͆ j͆

͆ ҿ ŭ͆ Ϯ  ͆ ;͆ -͆'R 'ɼ‚ ?Ĩ͆ƒ -͆ -8 Œ͆ j͆

$

ŭ͆ Ϯ  ͆ ;͆ -͆'R 'ɼ‚ ?ɐɑ͆ŭ -͆ -8Œ͆j͆

$

 ͆ ;͆ ͆'R  Œ͆ j͆

 ͆  b͆ n͆  ͆ ;͆  -͆'R 'ɼ Ĭ ?Ê͆ Ɇ -͆ -8 Œ͆ j͆

 ͆ ;͆  -͆ 'R  -Œ͆ j͆

 ͆  ƒ͆ É͆͆ ;͆  -͆'R'ɼ Ĭ ͆ ··.Ϯ ƒ - -8 Œ͆ j͆

Â؛

͆ ;͆  -͆'R-Œ͆j͆

͆ ҿ Ÿ͆   š͆ ;͆-͆'R  -Œ͆j͆

ŸŽǍ͆ ; -͆'R 'ɼ ?ŸŽǍ8 Œ͆ j͆

(20)

An  example:  from  a   statement  to  the   abstract  syntax  tree  

<if> <(> <id, "peek"> <eq> <const, '\n'> <)>

<id, "line"> <assign> <id, "line"> <+> <num, 1> <;>

if ( peek == '\n' ) line = line + 1;

stmt

if ( expr ) stmt rel expr

rel == add rel = expr add term add rel term factor term add

factor num factor add + term id id term factor factor num id

+ -̓  ̓hm̓ ̓ ̓Ŭ̓ ̓

ª˜Á

*" E 7 +EKKҿ  2  E E .*2 E Kҿ .*2 E -  -

Q„Ñ“wr§à



¬r§Ôքºà

)$E˔ԕД׋) E 7 +E 8 E 41:=E  2  E  E

)E  .*2   E::)&1E ) E  .*2   E˓Г׋1>/E  B E

)1=E

 2  EˬϏ

cÔ¬ÃrÑB”º„wÄ}àir¬À§rðºà

3

:)

ÊÐ

™“§Á …ҿ ӌ׋ Ǎ׋

™“§Á ҿ

ѰҀҿ

%B *-- Kҿ  *2E*- ѓ׋DEE

- *#E.; E 7 +E KKҿ *  - '*'- Ӳ׋

@ԉ׋ .*2 E Kҿ .*2 E - -

"&B

֎೬ஸᎰᅻ਺ᗮ -È̓ ݬᑌအᗮႼအበበ೬࣓เ਺ᗮዏᅻࡐཱུበเࡐዏ೬အཱུበᗮအ૤ᗮࡐᗮበዏࡐዏ਺༌਺ཱུዏᗮ

ùᗮ ݬఀ਺ᗮበዏࡐᅻዏ೬ཱུஸᗮႼအ೬ཱུዏᗮ૤အᅻᗮࡐᗮበᒹཱུዏࡐᒏƗূ೬ᅻ਺हዏ਺ূᗮዏᅻࡐཱུበเࡐዏအᅻᗮ೬በᗮࡐᗮஸᅻࡐ༌༌ࡐᅻᗮ૤အᅻᗮዏఀ਺ᗮ በအᎰᅻह਺ᗮเࡐཱུஸᎰࡐஸ਺ȫᗮ ӕᗮ ͆ূ਺በहᅻ೬࣓਺በᗮዏఏ਺ᗮఀ೬਺ᅻࡐᅻहఀ೬हࡐเᗮበዏᅻᎰहዏᎰᅻ਺ᗮအ૤ᗮႼᅻအᔳ ஸᅻࡐཪበȫᗮ ׭ዏᗮ೬በᗮূ਺୳ཱུ਺ূᗮ೬ཱུᗮዏ਺ᅻ༌በᗮအ૤ᗮ਺เ਺༌਺ཱུዏࡐᅻᒹᗮበᒹ༌࣓အเበᗮहࡐเเ਺ূᗮ ͆ࡐཱུূᗮ ᐟࡐᅻ೬ࡐ࣓เ਺ᗮበᒹ༌࣓အเበᗮहࡐเเ਺্ᗮ ƒ͆ ݬఀ਺በ਺ᗮበᒹ༌࣓အเበᗮᅻ਺Ⴜᅻ਺በ਺ཱུዏᗮเࡐཱུஸᎰࡐஸ਺ᗮ हအཱུበዏᅻᎰहዏበȫᗮݬఀ਺ᗮᅻᎰเ਺በᗮအᅻᗮ Ą ͆အ૤ᗮࡐᗮஸᅻࡐ༌༬ࡐᅻᗮहအཱུበ೬በዏᗮအ૴ᗮࡐᗮཱུအཱུዏ਺ᅻ༌೬ཱུࡐไᗮ हࡐเเ਺ূᗮዏఀ਺ᗮĄ͆အᅻᗮø͆  Ą͆အ૤ᗮࡐᗮႼᅻအূᎰहዏ೬အཱུᗮࡐཱུূᗮࡐᗮበ਺ᅍᎰ਺ཱུह਺ᗮအ૤ᗮዏ਺ᅻ༌೬ཱུࡐเበᗮ ࡐཱུূᗮཱུအཱུዏ਺ፊ༌೬ཱུࡐเበᗮहࡐเเ਺ূᗮዏఀ਺ᗮ)Ą͆အᅻᗮ ͆  Ą͆အ૨ᗮዏఀ਺ᗮႼᅻအূᎰहዏ೬အཱུȫᗮ ཱུۖ਺ᗮ

ཱུအཱུዏ਺ᅻ༌೬ཱུࡐเᗮ೬በᗮূ਺በ೬ஸཱུࡐዏ਺ূᗮࡐበᗮዏఀ਺ᗮ͆በᒹ༌रအเȫᗮ

ùᗮ ׭ཱུᗮበႼ਺ह೬૧ᒹ೬ཱུஸᗮࡐᗮዏᅻࡐཱུበเࡐዏအᅻýᗮ೬ዏᗮ೬በᗮఀ਺เႼ૤Ꮀเᗮዏအᗮࡐዏዏࡐहఀᗮࡐዏዏሾ೬࣓Ꮀዏ਺በᗮዏအᗮႼᅻအஸᅻࡐ༌༌೬ཱུஸᗮ हအཱུበዏᅻᎰहዏýᗮᑌఀ਺ᅻ਺ᗮࡐཱུᗮ )͆೬በᗮࡐཱུᒹᗮᅍᎰࡐཱུዏ೬ዏᒹᗮࡐበበအह೬ࡐዏ਺ূᗮᑌ೬ዏఀᗮࡐᗮहအཱུበዏᅻᎰहዏȫᗮ

ܺ೬ཱུह਺ᗮ हအཱུበዏᅻᎰहዏበᗮ ࡐᅻ਺ᗮ ᅻ਺Ⴜᅻ਺በ਺ཱུዏ਺ূᗮ ࣓ᒹᗮ ஸᅻࡐ༌༌ࡐᅻᗮ በᒹ༌࣓အเበýᗮ ዏఀ਺ᗮ हအཱུह਺Ⴜዏᗮ အ૤ᗮ ࡐዏዏᅻ೬࣓Ꮀዏ਺በᗮ਺ᒏዏ਺྆ূበᗮዏအᗮஸᅻࡐ༚༌ࡐᅻᗮበᒹ༌࣓အเበȫᗮ իᒏࡐ༌Ⴜเ਺በᗮ အ૤ᗮࡐዏዏᅻ೬࣓Ꮀዏ਺በᗮ೬ཱུहเᎰূ਺ᗮ ࡐཱུᗮ೬ཱུዏ਺ஸ਺ᅻᗮᐟࡐเᎰ਺ᗮࡐበበအह೬ࡐዏ਺ূᗮᑌ೬ዏఀᗮࡐᗮዏ਺ᅻ༌೬ཱུࡐเᗮ# ɼᅽ਺Ⴜᅻ਺በ਺ཱུዏ೬ཱུஸᗮཱུᎰ༌࣓਺ᅻበýᗮ ࡐཱུূᗮࡐᗮበዏᅻ೬ཱུஸᗮࡐበበအह೬ࡐዏ਺ূᗮᑌ೬ዏఀᗮࡐᗮዏ਺ᅻ༌೬ཱུࡐเᗮȉ̺Ϯᅻ਺Ⴜᅻ਺በ਺ཱུዏ೬ཱུஸᗮ೬ূ਺ཱུዏ೬୳਺ᅻበȫᗮ

ùᗮ ӕᗮ ͆ 7͆ᅻ਺ࡐূበᗮዏ౤਺ᗮ೬ཱུႼᎰዏᗮအཱུ਺ᗮहఀࡐᅻࡐहዏ਺ᅻᗮࡐዏᗮࡐᗮዚ೬༌਺ᗮࡐཱུূᗮჇᅻအূᎰह਺በᗮ ࡐበᗮအᎰዏႼᎰዏᗮࡐᗮበዚᅻ਺ࡐ༌ᗮႪ૤ᗮ&͆ᑌఀ਺ᅻ਺ᗮࡐᗮዏအฅ਺ཱུᗮहအཱུበ೬በዏበᗮအ૤ᗮࡐᗮዏ਺ᅻ༌೬ཱུࡐเᗮበᓄ༌ࣧအเᗮ ࡐเအཱུஸᗮᑌ೬ዏఀᗮ ࡐূূ೬ዏ೬အཱུࡐเᗮ೬ཱུ૤အᅻ༌ࡐዏ೬အཱུᗮ ೬ཱུᗮ ዏఀ਺ᗮ ૤အᅻ༌ᗮ အ૤ᗮ ࡐዏዏᅻ೬࣓Ꮀዏ਺ᗮ ᐟࡐเᎰ਺በȫᗮ ץཱུᗮ

֎೬ஸȫᗮ-Æ̓ዏအฅ਺ཱུበᗮ ࡐᅻ਺ᗮᑌᅻ೬ዏዏ਺ཱུᗮࡐበᗮዏᎰႼเ਺በᗮ਺ཱུहเအበ਺ূᗮࣜ਺ዏᑌ਺਺ཱུᗮ ƱϮß ȫ ᗮ ݬఀ਺ᗮዺအฅ਺ཱུᗮ

ƱȉȂ.Ϯ qɁϮहအཱུበ೬በዏበᗮအ૤ᗮዏఀ਺ᗮ᎜਺ᅻ༌೬ཱུࡐเᗮ͞ȂϮࡐཱུূᗮࡐᗮႼအ೬ཱུዏ਺ᅻᗮዏအᗮዏఀ਺ᗮበᒹ༌࣓အเżዏࡐ࣓เ਺ᗮ

਺ཱུዏᅻᒹᗮहအཱུዏࡐ೬ཱུ೬ཱུஸᗮዏఀ਺ᗮበዏᅻ೬ཱུஸᗮq aϮݬఀ਺ᗮዏᅻࡐཱུበเࡐዏအᅻᗮᎰበ਺በᗮዏఀ਺ᗮዏࡐ࣓เ਺ᗮዏအᗮฅ਺਺Ⴜᗮ Scanner  

Parser  

GeneraBon  of    

Abstract  Syntax  Tree  

(21)

StaBc  Checking  

•  SyntacAc  properAes  (not  captured  by  the  context-­‐free   grammar  of  the  language)  are  checked  by  analyzing  the   parse  tree  or  the  abstract  syntax  tree  

•  Context-­‐dependent  syntacAc  properBes:  

1.  Every  variable  is  declared  before  used  

2.  Each  idenBfier  is  declared  at  most  once  per  scope   3.  Lea  operands  of  assignments  are  L-­‐values  

4.  Break  statements  must  have  enclosing  loop  or  switch  

•  SemanAc  analysis  is  applied  by  the  compiler  to  discover   the  meaning  of  a  program  by  analyzing  its  parse  tree  or   abstract  syntax  tree.  Useful  to  prevent  runBme  errors.  

•  StaAc  semanAc  checks  performed  at  compile  Bme:  

5.  Type  checking:  each  operator  is  applied  to  arguments  of  the   right  type  

–  Handling  of  coercion  and  overloading  

(22)

Exercise  

•  For  each  of  the  numbered  items  in  the  last   slide,  discuss  how  the  property  can  be  

checked  either  with  a  translaBon  scheme  or   with  suitable  a[ributes  of  the  parse  tree  

22  

(23)

SemanBc  Analysis  

•  Dynamic  semanBc  checks  are  performed  at  

run  Bme,  and  the  compiler  produces  code  that   performs  these  checks  

–  Array  subscript  values  are  within  bounds   –  ArithmeBc  errors,  e.g.  division  by  zero  

–  Pointers  are  not  dereferenced  unless  poinBng  to   valid  object  

–  A  variable  is  used  but  hasn't  been  iniBalized   –  When  a  check  fails  at  run  Bme,  an  excepBon  is  

raised  

(24)

GeneraBon  of  Three  Address  Code  

•  Linear  Intermediate  RepresentaBon  generated  by  structural   inducBon  execuBng  a  funcBon  on  the  nodes  of  the  tree  

•  Sequence  of  instrucBons  of  the  form   x = y op z

•  Arrays  handled  with  instrucBons   x[y] = z  

x = y[z]  

•  Sequence  control  handled  with  jump  instrucBons:  

ifFalse x goto L     ifTrue x goto L goto L  

•   Statement  may  have  any  number  of  labels,  e.g.  

L1:L2: x = y    

24  

(25)

TranslaBon  of  Statements  

•  Jumps  are  used  to  implement  the  control  flow  

•  Example:    if expr then stmt

1

     is   translated  to    

 

25  

]¡¡Ϯ  ̓hm̓ ̓ ̓C̓ ̓

ޯఀ੎ᗮ୫ᅼበዏᗮႼᎰዏበᗮዏఀ੎ᗮᐟࡐเᎰ੎ᗮအ૨ᗮ್ཱུᗮዏఀ੎ᗮเအ।ࡐዏ್အཱུᗮũʯ ͆ࡐཱུূᗮዏఀ੎ᗮበ੎।အཱུূᗮႼᎰዏበᗮዏఀ੎ᗮᐟࡐเᎰ੎ᗮ အ૨ᗮ ũ7ʮ್ཱུ͆ᗮዏఀ੎ᗮเအ।ࡐዏ್အཱུᗮ͆

ޯఀᅼ੎੎Żࡐূূᅼ੎በበᗮ್ཱུበዏᅼᎰ।ዏ್အཱུበᗮࡐᅼ੎ᗮ੎ᒏ੎।Ꮀዏ੎ূᗮ್ཱུᗮཱུᎰ༌੎ᅼ್।ࡐเᗮበ੎ᅑᎰ੎ཱུ।੎ᗮᎰཱུเ੎በበᗮ૨အᅼ।੎ূᗮ ዏအᗮূအᗮအዏఀ੎ᅼᑌ್በ੎ᗮ࣓ᒹᗮࡐᗮ।အཱུূ್ዏ್အཱུࡐเᗮအᅼᗮᎰཱུ।အཱུূ್ዏ್အཱུࡐเᗮෟᎰ༌Ⴜʾᗮ ߖ੎ᗮ।ఀအအበ੎ᗮዏఀ੎ᗮ૨အเเအᑌ್ཱུஸᗮ

್ཱུበዏᅼᎰ।ዏ್အཱུበᗮ૨အᅼᗮ।အཱུዏᅼအเᗮ஛အᑌёᗮ

Ϯƚ Ϯ͆sϮ©Ϯ

Ϯ°mϮ ͆sϮ©Ϯ sϮ+Ϯ

್૨ᗮ7ҿ್በᗮ૨ࡐเበ੎čᗮཱུ੎ᒏዏᗮ੎ᒏ੎।Ꮀዏ੎ᗮዏఀ੎ᗮ್ཱུበዏᅼᎰ।ዏ್အཱུᗮเࡐ࣓੎เ੎ূᗮ©Ϯ

್૨ᗮ್በᗮዏᅼᎰ੎čᗮཱུ੎ᒏዏᗮ੎ᒏ੎।Ꮀዏ੎ᗮዏఀ੎ᗮ್ཱུበዏᅼᎰ।ዏ್အཱུᗮเࡐ࣓੎เ੎ূᗮ©Ϯ

ཱུ੎ᒏዏᗮ੎ᒏ੎।Ꮀዏ੎ᗮዏఀ੎ᗮ್ཱུበዏᅼᎰ।ዏ್အཱུᗮเࡐ࣓੎เ੎ূᗮ©Ϯ

ӕᗮ เࡐ࣓੎เᗮ©Ϯ।ࡐཱུᗮ ࣓੎ᗮ ࡐዏዏࡐ।ఀ੎ূᗮ ዏအᗮࡐཱུᒹᗮ್ཱུበዏᅼᎰ।ዏ್အཱུᗮ࣓ᒹᗮႼᅼ੎Ⴜᗮ੎ཱུূ್ཱུஸᗮ ࡐᗮႼᅼ੎୫ᒏᗮ© ň F Ϯ ӕཱུᗮ

್ཱུበዏᅼᎰ।ዏ್အཱུᗮ।ࡐཱུᗮఀࡐᐟ੎ᗮ༌အᅼ੎ᗮዏఀࡐཱུᗮအཱུ੎ᗮเࡐ࣓੎เʾᗮ

֍್ཱུࡐเเᒹčᗮᑌ੎ᗮཱུ੎੎ূᗮ್ཱུበዏᅼᎰ।ዏ್အཱུበᗮዏఀࡐዏᗮ।အႼᒹᗮࡐᗮᐟࡐเᎰ੎ʾᗮ ޯఀ੎ᗮ૨အเเအᑌ್ཱུஸᗮዏఀᅼ੎੎Żࡐূূᅼ੎በበᗮ

್ཱུበዏᅼᎰ।ዏ್အཱུᗮ।အႼ್੎በᗮዏఀ੎ᗮᐟࡐเᎰ੎ᗮအ૨ᗮҿ್ཱུዏအᗮħϮ

 × ͆͆

Š’ ɼ :ɼ¶ ɼ

ܺ᎚ࡐዏ੎༌੎ཱུዏበᗮࡐᅼ੎ᗮዏᅼࡐཱུበเࡐዏ੎ূᗮ್ཱུዏအᗮዏఀᅼ੎੎Żࡐূূᅼ੎በበᗮ।အূ੎ᗮ࣓ᒹᗮᎰበ್ཱུஸᗮෟᎰ༌Ⴜᗮ್ཱུበዏᅼᎰ।ዏ್အཱུበᗮ ዏအᗮ್༌Ⴜเ੎༌੎ཱུዏᗮዏఀ੎ᗮ஛အᑌᗮအ૨ᗮ।အཱུዏᅼအเᗮዏఀᅼအᎰஸఀᗮዏఀ੎ᗮበዏࡐዏ੎༌੎ཱུዏȩᗮ ޯఀ੎ᗮเࡐᒹအᎰዏᗮ್ཱུᗮ֍್ஸʾᗮ~̓

್เเᎰበዏᅼࡐዏ੎በᗮዏఀ੎ᗮዏᅼࡐཱུበเࡐዏ್အཱུᗮအ૨ᗮ’:ɼ ͆ɼ ƒ 4͆ ޯఀ੎ᗮෟᎰ༌Ⴜᗮ್ཱུበዏᅼᎰ।ዏ್အཱུᗮ್ཱུᗮዏఀ૑ᗮ เࡐᒹအᎰዏᗮ

Ϯƚ Ϯ͆ sϮ ø͆

ෟᎰ༌Ⴜበᗮအᐟ੎ᅼᗮዏఀ੎ᗮዏᅼࡐཱུበเࡐዏ್အཱུᗮအ૨ᗮ ƒ್͆૨ᗮ ͆੎ᐟࡐเᎰࡐዏ੎በᗮዏအᗮ:Eɼ ۖዏఀ੎ᅼᗮበዏࡐዏ੎༌੎ཱུዏᗮ

।အཱུበዏᅼᎰ।ዏበᗮࡐᅼ੎ᗮበ್༌್เࡐᅼเᒹᗮዏᅼࡐཱུበเࡐዏ੎ূᗮᎰበ್ཱུஸᗮࡐႼႼᅼအႼᅼ್ࡐዏ੎ᗮෟᎰ༌ႼበᗮࡐᅼအᎰཱུূᗮዏఀ੎ᗮ।အূ੎ᗮ૨အᅼᗮ ዏఀ੎್ᅼᗮ।အ༌Ⴜအཱུ੎ཱུዏበʾᗮ

w°}„àðàw°ª´ËÄà 9[OP^’¬Ã°à

œкҿϔ:і҃бҿ'*'-5>9P^

w°}„à…°ºà֟ƸֈƸ\׋

5>9P $^ ! !            Ğ     !   !           ř“ř

֍್ஸᎰᅼ੎ᗮ~Ȯ̓Ԧအূ੎ᗮเࡐᒹအᎰዏᗮ૨အᅼᗮ್૨Żበዏࡐዏ੎༌੎ཱུዏበᗮ

֍အᅼᗮ।အཱུ।ᅼ੎ዏ੎ཱུ੎በበčᗮᑌ੎ᗮበఀအᑌᗮዏఀ੎ᗮႼበ੎Ꮀূအ।အূ੎ᗮ૨အᅼᗮ।เࡐበበᗮجଵᗮ್ཱུᗮ֍್ஸȩᗮ~š̓ Ԧเࡐበበᗮ جଵᗮ್በᗮࡐᗮበᎰ࣓।เࡐበበᗮအ૨ᗮ  ȃ͆ࡐበᗮࡐᅼ੎ᗮዏఀ੎ᗮ।เࡐበበ੎በᗮ૨အᅼᗮዏఀ੎ᗮအዏఀ੎ᅼᗮበዏࡐዏ੎༌੎ཱུዏᗮ।အཱུበዏᅼᎰ।ዏበʾᗮ իࡐ।ఀᗮበᎰ࣓।เࡐበበᗮအ૨ᗮ  ͆ఀࡐበᗮࡐᗮ।အཱུበዏᅼᎰ।ዏအᅼᗮȊ جଵᗮ್ཱུᗮዏఀ್በᗮ।ࡐበ੎ᗮž ࡐཱུূᗮࡐᗮ૨Ꮀཱུ।ዏ್အཱུᗮ͆

ዏఀࡐዏᗮ್በᗮ।ࡐเเ੎ূᗮዏအᗮஸ੎ཱུ੎ᅼࡐዏ੎ᗮዏఀᅼ੎੎Żࡐূূᅼ੎በበᗮ।အূ੎ᗮ૨အᅼᗮዏఀ್በᗮฅ್ཱུূᗮအ૨ᗮበዏࡐዏ੎༌੎ཱུዏɉᗮ h0ùm̓ ̓̓!̓

ɼ–›ɼ Œɼ  ͆;͆

> ͆ƪ͆  ͆…o͆

cj

#4ɼ–›ɼ?> ͆͆  ͆[؛#5 …͆=؛[ V ؛ø͆. 9)T o͆ j͆

#4ɼó Œɼ ͆ƛ͆ ;͆

cj

> ͆-͆=؛4  T o ͆

  ?͆ łÝĖ͆‘ ϮᗮƇ͆ -4   T͆ E ł͆'Ϯᗮ E ø" o͆

…4 T o ͆

 ? ø͆Ƈ͆ ł ɩ8 o ͆

֍೬ஸᎰᅼ਺ᗮI ˆ|KƒϮ֑Ꮀཱུहዐ೬အཱུᗮ͆೬ཱུᗮहเࡐበበᗮ–›ɼஸ਺ཱུ਺ᅼࡐዐ਺በᗮዐఀᅼ਺਺Ÿࡐূূᅼ਺በበᗮहအূ਺ᗮ

4 4̓

ݬఀ਺ᗮहအཱུበዐᅼᎰहዐအᅼᗮ–›ɼ೬ཱུᗮ֍೬ஸȯᗮIˆ|KϮहᅼ਺ࡐዐ਺በᗮበᒹཱུዐࡐᒏŸዐᅼ਺਺ᗮཱུအূ਺በᗮ૤အᅼᗮ೬૤Ÿበዐࡐዐ਺༌਺ཱུዐበȯᗮ

׭ዐᗮ೬በᗮहࡐเเ਺ূᗮᑌ೬ዐఀᗮዐᑌအᗮႼࡐᅼࡐ༌਺ዐ਺ᅼበÿᗮࡐཱུᗮ਺ᒏႼᅼ਺በበ೬အཱུᗮཱུအূ਺ᗮ#5 ࡐཱུূᗮࡐᗮበዐࡐዐ਺༌਺ཱུዐᗮཱུအূ਺ᗮ [ ؛ᑌఀ೬हఀᗮ೬ዐᗮበࡐᐕ਺በᗮࡐበᗮࡐዐዐᅼ೬࣓Ꮀዐ਺በᗮ͆ࡐཱུূᗮ…4͆ݬఀ਺ᗮहအཱུበዐᅼᎰहዐအᅼᗮࡐเበအᗮࡐበበ೬ஸཱུበᗮࡐዐዐᅼ೬࣓Ꮀዐ਺ᗮ

ø͆ࡐᗮᎰཱུ೬ᅑᎰ਺ᗮཱུ਺ᑌᗮเࡐ࣓਺เÿᗮ࣓ᒹᗮहࡐเเ೬ཱུஸᗮ૤Ꮀཱུहዐ೬အཱུᗮ9)T 4͆ ݬఀ਺ᗮเࡐ࣓਺เᗮᑌ೬เเᗮ࣓਺ᗮᎰበ਺ূᗮ ࡐहहအᅼূ೬ཱུஸᗮዐအᗮዐఀ਺ᗮเࡐᒹအᎰዐᗮ೬ཱུᗮ֍೬ஸȴᗮI F|IFϮ

ཱུۖह਺ᗮዐఀ਺ᗮ਺ཱུዐ೬ᅼ਺ᗮበᒹཱུዐࡐᒏᗮዐᅼ਺਺ᗮ૤အᅼᗮࡐᗮበအᎰᅼह਺ᗮႼᅼအஸᅼࡐ༌ᗮ೬በᗮहအཱུበዐᅼᎰहዐ਺ূÿᗮዐఀ਺ᗮ૤Ꮀཱུहዐ೬အཱུᗮ

͆೬በᗮ हࡐเเ਺ূᗮ ࡐዐᗮ ዐఀ਺ᗮ ᅼအအዐᗮ အ૤ᗮዐఀ਺ᗮ በᒹཱུዐࡐᒏᗮ ዐᅼ਺਺ȯᗮ ܺ೬ཱུह਺ᗮ ࡐᗮ Ⴜᅼအஸᅼࡐ༌ᗮ ೬በᗮ ࡐᗮ ࣓เအहฅᗮ೬ཱུᗮ အᎰᅼᗮ በ೬༌Ⴜเ਺ᗮ เࡐཱུஸᎰࡐஸ਺ťᗮ ዐఀ਺ᗮ ᅼအအዐᗮ အ૤ᗮዐఀ਺ᗮ በᒹཱུዐࡐᒏᗮ ዐᅼ਺਺ᗮ ᅼ਺Ⴜᅼ਺በ਺ཱུዐበᗮ ዐఀ਺ᗮ በ਺ᅑᎰ਺ཱུह਺ᗮ အ૤ᗮ በዐࡐዐ਺༌਺ཱུዐበᗮ೬ཱུᗮዐఀ਺ᗮ࣓เအहฅȯᗮ ӕเเᗮበዐࡐዐ਺༌਺ཱུዐᗮहเࡐበበ਺በᗮहအཱུዐࡐ೬ཱུᗮࡐᗮ૤Ꮀཱུहዐ೬အཱུᗮ4͆

ݬఀ਺ᗮႼበ਺Ꮀূအहအূ਺ᗮ૤အᅼᗮ૤Ꮀཱུहዐ೬အཱུᗮ͆အ૤ᗮहเࡐበበᗮ–›ɼ೬ཱུᗮ֍೬ஸȯᗮIɱ|KϮ೬በᗮᅼ਺Ⴜᅼ਺በ਺ཱུዐࡐዐ೬ᐕ਺ȯᗮצዐᗮ हࡐเเበᗮ4 T͆ዐအᗮዐᅼࡐཱུበเࡐዐ਺ᗮዐఀ਺ᗮ ਺ᒏႼᅼ਺በበ೬အཱུᗮ ‘ዐఀ਺ᗮ ࣓အအเ਺ࡐཱུŸᐕࡐเᎰ਺ূᗮ਺ᒏႼᅼ਺በበ೬အཱུᗮ ዐఀࡐዐᗮ ೬በᗮႼࡐᅼዐᗮ အ૤ᗮዐఀ਺ᗮ ೬૤Ÿበዐࡐዐ਺༌਺ཱུዐበÂᗮ ࡐཱུূᗮ በࡐᐕ਺በᗮዐఀ਺ᗮ ᅼ਺በᎰเዐᗮ ཱུအূ਺ᗮ ᅼ਺ዐᎰᅼཱུ਺ূᗮ ࣓ᒹᗮ4͆

޾ࡐཱུበเࡐዐ೬အཱུᗮအ૤ᗮ਺ᒏႼᅼ਺በበ೬အཱུበᗮᑌ೬เเᗮ࣓਺ᗮূ೬በहᎰበበ਺ূᗮበఀအᅼዐเᒹȯᗮ ֍Ꮀཱུहዐ೬အཱུᗮ͆ዐఀ਺ཱུᗮ਺༌೬ዐበᗮࡐᗮ हအཱུূ೬ዐ೬အཱུࡐเᗮෟᎰ༌Ⴜᗮࡐཱུূᗮहࡐเเበᗮ…4T͆ዐအᗮዐᅼࡐཱུበเࡐዐ਺ᗮዐఀ਺ᗮበᎰ࣓በዐࡐዐ਺༌਺ཱུዐᗮ…4͆

Š ɼ :ɼ ɼ

ߖ਺ᗮཱུအᑌᗮ೬เเᎰበዐᅼࡐዐ਺ᗮዐఀ਺ᗮዐᅼࡐཱུበเࡐዐ೬အཱུᗮအ૤ᗮ਺ᒏႼᅼ਺በበ೬အཱུበᗮ࣓ᒹᗮहအཱུበ೬ূ਺ᅼ೬ཱུஸᗮ਺ᒏႼᅼ਺በበ೬အཱུበᗮहအཱུᔳ ዐࡐ೬ཱུ೬ཱུஸᗮ࣓೬ཱུࡐᅼᒹᗮအႼ਺ᅼࡐዐအᅼበᗮ Žɼ ࡐᅼᅼࡐᒹᗮ ࡐहह਺በበ਺በÿᗮ ࡐཱུূᗮࡐበበ೬ஸཱུ༌਺ཱུዐበÿᗮ೬ཱུᗮ ࡐূূ೬ዐ೬အཱུᗮዐအᗮ हအཱུበዐࡐཱུዐበᗮࡐཱུূᗮ೬ূ਺ཱུዐ೬୫਺ᅼበȯᗮ ֍အᅼᗮበ೬༌Ⴜเ೬ह೬ዐᒹÿᗮ೬ཱུᗮࡐཱུᗮࡐᅼᅼࡐᒹᗮࡐहह਺በበᗮ[ båq ؛ᑌ਺ᗮᅼ਺ᅑᎰ೬ᅼ਺ᗮዐఀࡐዐᗮ [؛࣓਺ᗮࡐཱུᗮ೬ূ਺ཱུዐ೬୫਺ᅼȯ̎ͱᗮ ֍အᅼᗮࡐᗮূ਺ዐࡐ೬เ਺ূᗮ ূ೬በहᎰበበ೬အཱུᗮအ૤ᗮ೬ཱུዐ਺ᅼ༌਺ূ೬ࡐዐ਺ᗮहအূ਺ᗮஸ਺ཱུ਺ᅼࡐዐ೬အཱུᗮ

૤အᅼᗮ਺ᒏႼᅼ਺በበ೬အཱུበÿᗮበ਺਺ᗮܺ਺हዐ೬အཱུᗮʈ|ˆϮ

ߖ਺ᗮበఀࡐเเᗮዐࡐฅ਺ᗮዐఀ਺ᗮበ೬༌Ⴜเ਺ᗮࡐႼႼᅼအࡐहఀᗮအ૤ᗮஸ਺ཱུ਺ᅼࡐዐ೬ཱུஸᗮအཱུ਺ᗮዐఀᅼ਺਺ǽࡐূূᅼ਺በበᗮ೬ཱུበዐᅼᎰहᕪ ዐ೬အཱུᗮ૤အᅼᗮ਺ࡐहఀᗮအႼ਺ᅼࡐዐအᅼᗮཱུအূ਺ᗮ೬ཱུᗮዐఀ਺ᗮበᒹཱུዐࡐᒏᗮዐᅼ਺਺ᗮ૤အᅼᗮࡐཱུᗮ਺ᒏႼᅼ਺በበ೬အཱུȯᗮ ڴအᗮहအূ਺ᗮ೬በᗮ ஸ਺ཱུ਺ᅼࡐዐ਺ূᗮ૤အᅼᗮ೬ূ਺ཱུዐ೬୫਺ᅼበᗮࡐཱུূᗮहအཱུበ᎛ࡐཱུ᎛በÿᗮበ೬ཱུह਺ᗮዐఀ਺ᒹᗮहࡐཱུᗮࡐႼႼ਺ࡐᅼᗮࡐበᗮࡐূূᅼ਺በበ਺በᗮ೬ཱུᗮ ೬ཱུበዐᅼᎰहዐ೬အཱུበȯᗮ ׭૤ᗮࡐᗮཱུအূ਺ᗮ͆အ૤ᗮहเࡐበበᗮ> ͆ఀࡐበᗮအႼ਺ᅼࡐዐအᅼᗮ ÷ɼዐఀ਺ཱུᗮࡐཱུᗮ೬ཱུበዐᅼᎰहዐ೬အཱུᗮ೬በᗮ

਺༌೬ዐዐ਺ূᗮዐအᗮहအ༌ႼᎰዐ਺ᗮዐఀ਺ᗮᐕࡐเᎰ਺ᗮࡐዐᗮཱུအূ਺ᗮ͆೬ཱུዐအᗮࡐᗮहအ༌Ⴜ೬เ਺ᅼᗮஸ਺ཱུ਺ᅼࡐዐ਺ূᗮዐ਺༌Ⴜအᅼࡐᅼᒹᗮ

ཱུࡐ༌਺ÿᗮበࡐᒹᗮˆ͆ ݬఀᎰበÿᗮݵű ë˳͆ዐᅼࡐཱུበเࡐዐ਺በᗮ೬ཱུዐအᗮዐᑌအᗮ೬ཱུበዐᅼᎰहዐ೬အཱུበᗮ

(26)

TranslaBon  of  expressions  

•  One  operaBon  at  a  Bme,  using  temporaries  

26  

tl = i - j t2 = tl + k i-j+k !  

tl = a[i]

t2 = 2 * tl 2*a[i] !  

t3 = j – k

t2 = a [ t3 ] tl = 2 * t2

a [ i ] = t1 a[i]=2*a[j-k] !  

•  L-­‐values  in  assignements  cannot  be  translated  

into  temporaries  

(27)

TranslaBon  of  expressions:  the  pseudocode  for   L-­‐value  and  R-­‐value  

27  

4 ̓  ̓hm̓ ̓ ̓C̓ ̓

N = Ϯ ҿ î ű͆

NϮ /Ϯ N = Ϯ ë͆ qϮ

ߖ್ዏఀᗮࡐᅼᅼࡐᒹᗮࡐहह਺በበ਺በᗮࡐཱུূᗮࡐበበ್ஸཱུ༌਺ཱུዏበᗮहအ༌਺በᗮዏఀ਺ᗮཱུ਺਺ূᗮዏအᗮূ್በዏ್ཱུஸᎰ್በఀᗮ࣓਺ዏᑌ਺਺ཱུᗮ ثȋᐟࡐไᏇ਺በᗮࡐཱུূᗮሼŻᐟࡐไᎰ਺በȫᗮ ֍အᅼᗮ਺ᒏࡐ༌Ⴜไ਺čᗮϮϮहࡐཱུᗮ࣓਺ᗮዏᅼࡐཱུበไࡐዏ਺ূᗮ࣓ᒹᗮहအ༌ႼᏇዏ್ཱུஸᗮዏఀ਺ᗮ ሼŻᐟࡐไᏇ਺ᗮအ૤ᗮϮ4Ϯ್ཱུዏအᗮࡐᗮዏ਺༌Ⴜအᅼࡐᅼᒹčᗮࡐበᗮ್ཱུᗮ

N = Ϯ ҿ Ϯ Ϯ Ϯ Ϯ NϮ /Ϯ Ϯ Ϯ N = Ϯ

ԠᎰዏčᗮᑌ਺ᗮ हࡐཱཱུུအዏᗮበ್༌ႼไᒹᗮᎰበ਺ᗮࡐᗮዏ਺༌Ⴜအᅼࡐᅼᒹᗮ್ཱུᗮႼไࡐह਺ᗮအ૤ᗮϮ .Ϯ್૤ᗮϮϮࡐႼႼ਺ࡐᅼበᗮအཱུᗮ ዏఀ਺ᗮไ਺૤ዏᗮበ್ূ਺ᗮအ૤ᗮࡐཱུᗮࡐበበ್ஸཱུ༌਺ཱུዏɊᗮ

ݬఀ਺ᗮበ್༌Ⴜไ਺ᗮࡐႼႼᅼအࡐहఀᗮᎰበ਺በᗮዏఀ਺ᗮዏᑌအᗮ૤Ꮀཱུहዏೱအཱུበᗮ͆ࡐཱུূᗮ͆ᑌఀ್हఀᗮࡐႼႼ਺ࡐᅼᗮ

್ཱུᗮ֍್ஸȫᗮ,̓ࡐཱུূᗮă#Æ̓ᅼ਺በႼ਺हዏ್ᐟ਺ไᒹȫᗮ ߖఀ਺ཱུᗮ૤Ꮗཱུहዏ್အཱུᗮ್͆በᗮࡐႼႼไ್਺ূᗮዏအᗮࡐᗮཱུအཱུไ਺ࡐ૤ᗮ

ཱུအূ਺ᗮ್͆ዏᗮஸ਺ཱུ਺ᅼࡐዏ਺በᗮ್ཱུበዏᅼᎰहዏ್အཱུበᗮዏအᗮहအ༌ႼᏇዏ਺ᗮ್ཱུዏအᗮࡐᗮዏ਺༌Ⴜအᅼࡐᅼᒹčᗮࡐཱུূᗮᅼ਺ዏᎰᅼཱུበᗮ ࡐᗮཱུ਺ᑌᗮཱུအূ਺ᗮᅼ਺Ⴜᅼ਺በ਺ཱུዏ್ཱུஸᗮዏఀ਺ᗮዏ਺༌Ⴜအᅼࡐᅼᒹȫᗮ ߖఀ਺ཱུᗮ૤Ꮀཱུहዏ್အཱུᗮ್͆በᗮࡐႼႼไ್਺ূᗮዏအᗮࡐᗮ

ཱུအཱུไ਺ࡐ૤čᗮ ್ዏᗮ ࡐไበအᗮ ஸ਺ཱུ਺ᅼࡐዏ਺በᗮ ್ཱུበዏᅼᏇहዏ್အཱུበᗮ ዏအᗮ हအ༌ႼᎰዏ਺ᗮ ዏఀ਺ᗮ በᎰ࣓ዏᅼ਺਺በᗮ ࣓਺ไအᑌᗮ͆ ࡐཱུূᗮ ᅼ਺ዏᎰᅼཱུበᗮࡐᗮཱུအূ਺ᗮᅼ਺Ⴜᅼ਺በ਺ཱུዏ್ཱུஸᗮዏఀ਺ᗮ ࡐূূᅼ਺በበᗮ૤အᅼᗮ)  /

ߖ਺ᗮ ূ਺በहᅼ್࣓਺ᗮ૤Ꮀཱུहዏ್အཱུᗮ͆୫ᅼበዏčᗮ በ್ཱུह਺ᗮ್ዏᗮఀࡐበᗮ૤਺ᑌ਺ᅼᗮ हࡐበ਺በȫᗮ ߖఀ਺ཱུᗮ ࡐႼႼไ್਺ূᗮ ዏအᗮࡐᗮཱུအূ਺ᗮ͆૤Ꮀཱུहዏ್အཱུᗮ͆በ್༌Ⴜไᒹᗮᅼ਺ዏᎰᅼཱུበᗮ್૤ᗮ್ዏᗮ್በᗮዏఀ਺ᗮཱུအূ਺ᗮ૤အᅼᗮࡐཱུᗮ್ূ਺ཱུዏ್୫਺ᅼᗮ

˕್ɉ਺ɉčᗮ್૤ᗮ್በᗮအ૤ᗮहไࡐበበᗮ ʖ' x ؛ צཱུᗮအᎰᅼᗮ በ್༌Ⴜไ਺ᗮไࡐཱུஸᎰࡐஸ਺čᗮ ዏఀ਺ᗮအཱུไᒹᗮအዏఀ਺ᅼᗮहࡐበ਺ᗮᑌఀ਺ᅼ਺ᗮ ࡐཱུᗮ਺ᒏႼᅼ਺በበ್အཱུᗮఀࡐበᗮࡐཱུᗮ๵ŻᐟࡐไᎰ਺ᗮအहहᎰᅼበᗮᑌఀ਺ཱུᗮᅼ਺Ⴜᅼ਺በ਺ཱུዏበᗮࡐཱུᗮࡐᅼᅼࡐᒹᗮࡐहह਺በበčᗮበᏇहఀᗮࡐበᗮ

Ϯ4 ˆϮ צཱུᗮዏఀ್በᗮ हࡐበ਺čᗮᑌ್ไไᗮ ఀࡐᐟ਺ᗮዏఀ਺ᗮ ૤အᅼ༌ᗮ ᑌఀ਺ᅼ਺ᗮ हไࡐበበᗮ ್በᗮࡐᗮ በᎰ࣓हไࡐበበᗮအ૤ᗮ> ͆ č͆ᅼ਺Ⴜᅼ਺በ਺ཱུዏበᗮዏఀ਺ᗮཱུࡐ༌਺ᗮအ૤ᗮዏఀ਺ᗮࡐहह਺በበ਺ূᗮࡐᅼᅼࡐᒹčᗮࡐཱུূᗮᅼ਺Ⴜᅼ਺በ਺ཱུዏበᗮ ዏఀ਺ᗮအୁበ਺ዏᗮ˖್ཱུূ਺ᒏñ׋အ૤ᗮዏఀ਺ᗮहఀအበ਺ཱུᗮ਺ไ਺༌਺ཱུዏᗮ್ཱུᗮዏఀࡐዏᗮࡐᅼᅼࡐᒹȫᗮ ַအ༌ᗮዏఀ਺ᗮႼበ਺ᎰূအŻहအূ਺ᗮ

್ཱུᗮ֍್ஸȫᗮ Ǚ̓૤Ꮀཱུहዏ್အཱུᗮ͆हࡐไไበᗮ?78͆ዏအᗮஸ਺ཱུ਺ᅼࡐዏ਺ᗮ್ཱུበዏᅼᎰहዏ್အཱུበčᗮ್૤ᗮཱུ਺਺ূ਺ূčᗮ ዏအᗮहအ༌ႼᎰዏ਺ᗮዏఀ਺ᗮሼǛᐟࡐไᎰ਺ᗮအ૤ᗮ7ˆ͆צዏᗮዏఀ਺ཱུᗮहအཱུበዏᅼᏇहዏበᗮࡐཱུূᗮᅼ਺ዏᎰᅼཱུበᗮࡐᗮཱུ਺ᑌᗮ ཱུအূ਺ᗮ ᑌ್ዏఀᗮहఀ್ไূᅼ਺ཱུᗮ૤အᅼᗮዏఀ਺ᗮࡐᅼᅼࡐᒹᗮཱུࡐ༌਺ᗮč͆ࡐཱུূᗮዏఀ਺ᗮሼŻᐟࡐไᎰ਺ᗮအ૤ᗮ74͆

> ͆?͆Ʀ͆> 8͆ ;͆

57

ùǂ͆?್͆͆በᗮࡐཱུᗮʖ'؛ཱུအূ਺ ñ׋#ɼ#5

ɼùǂ͆?್͆͆በᗮࡐཱུᗮ ཱུအূ਺ᗮࡐཱུূᗮč್͆በᗮࡐཱུᗮʖ'؛ཱུအূ਺ ñ׋

#ɼ'ɼ

ý؛ɼ  DŽɼ

֍್ஸᎰᅼ਺ᗮ ¦̓ ۼበ਺Ꮀূအहအূ਺ᗮ૤အᅼᗮ૤Ꮀཱུहዏ್အཱུᗮ͆

 ɼiƒȎƁ ȱ̓ ߖఀ਺ཱུᗮ ཱུအূ਺ᗮᅼ਺Ⴜᅼ਺በ਺ཱུዏበᗮዏఀ਺ᗮࡐᅼᅼࡐᒹᗮࡐहह਺በበᗮ q . Ϯዏఀ਺ᗮ हࡐไไᗮ

?͆8͆ஸ਺ཱུ਺ᅼࡐዏ਺በᗮࡐཱུᗮ್ཱུበዏᅼᎰहዏ್အཱུᗮ

ࡐཱུূᗮ ᅼ਺ዏᎰᅼཱུበᗮ ࡐᗮ ཱུ਺ᑌᗮ ཱུအূ਺ᗮ)/ ᅼ਺Ⴜᅼ਺በ਺ཱུዏ್ཱུஸᗮዏఀ਺ᗮ ثŻᐟࡐไᎰ਺ᗮϮN .Ϯ ᑌఀ਺ᅼ਺ᗮ ್በᗮ ࡐᗮ ཱུ਺ᑌᗮ ዏ਺༌Ⴜအᅼࡐᅼᒹᗮཱུࡐ༌਺ȩᗮ

׮ཱུᗮূ਺ዏࡐ್ไčᗮዏఀ਺ᗮहအূ਺ᗮ૤ᅼࡐஸ༌਺ཱུዏᗮ

hmùm̓ ɱƊɞ̓̓!ɟ̓ 4 ̓

̂X¼w/ö̓öXè̓

೬በᗮᅼ਺ࡐहఀ਺ূᗮᑡ೬ዏఀᗮ࣒਺೬ཱུஸᗮዏఀ਺ᗮཱུအূ਺ᗮ૤အᅼᗮࡐཱུূᗮ࣒਺೬ཱུஸᗮዏ౮਺ᗮཱུအূ਺ᗮ૤အᅼᗮ਺ᒏႼᅼ਺በበ೬အཱུᗮͮ—Ϯ ݫఀ਺ᗮहࡐ̿ไᗮˍ?78͆ஸ਺ཱུ਺ᅼࡐዏ਺በᗮहအূ਺ᗮ૤အᆶᗮዏಀ਺ᗮ਺ᒏႼᅼઓበበ೬အཱུᗮqϮ‘೬ȩ੩ȩčᗮዏఀ਺ᗮዏఀᅼ਺਺Żࡐূূᅼ਺በበᗮ በዏࡐዏ਺༌਺ཱུዏᗮϮ Ϯ Ϯ Ϯ q8Ϯࡐཱུূᗮᅼ਺ዏᎰᅼཱུበᗮዏఀ਺ᗮྣ਺ᑌᗮཱུအূ਺ᗮĶ˽͆ᅼ਺Ⴜᅼઓበ਺ཱུዏ೬ཱུஸᗮዏఀ਺ᗮዏ਺༌Ⴜအᅼࡐᅼᓾᗮ

ཱུࡐ༌਺ᗮaϮ ޲ఀࡐዏᗮཱུအৈ਺ᗮĶŵ࣒͆਺हအ༌਺በᗮዏఀ਺ᗮᐕࡐไᎰ਺ᗮအ૤ᗮዏఀ਺ᗮበ਺हအཱུূᗮ஖਺ไূᗮ೬ཱུᗮዏఀ਺ᗮཱུ਺ᑌᗮ

ཱུအূ਺ᗮ͆ዏఀࡐዏᗮ೬በᗮहᅼ਺ࡐዏ਺ূȩᗮ 5

> ͆?͆͆ ɪ͆> ǵ͆ ;͆

57

Ȩɼ?͆͆೬በᗮࡐཱུᗮ̙'؛အᅼᗮࡐᗮཱུ͆အূ਺ Óᗮ/X¼w/ö̓#5

˝.R˞̓:ɼ‘ ᒙ ೬በᗮࡐཱུᗮĬ ?ĊĘ  78͆အᅼᗮࡐᗮʜ ?ĊĘ  78ཱུ͆အূ਺ Óᗮ

ཱུ਺ᑌᗮዏ਺༌Ⴜအᅼࡐᅼᒹ҃ᗮ

57

਺༌೬ዏᗮበዏᅼ೬ཱུஸᗮ૤အᅼᗮ͆Ϯ ̱?̵8͆ ĊĘ͆ ?78 o͆

/X¼w/ö̓ࡐᗮཱུ਺ᑌᗮཱུအূ਺ᗮ૤အᅼᗮo͆

X.̅X̓:ɼ?͆͆೬በᗮࡐཱུᗮ ཱུအূ਺ Óᗮ

ĬĭĮǂ ཱུ਺ᑌᗮዏ਺༌Ⴜအᅼࡐᅼᒹ҃ᗮ

57

हࡐไไᗮ?8 ͆ᑌఀ೬हఀᗮᅼ਺ዏᎰᅼཱུበᗮ

਺༌೬ዏᗮበዏᅼ೬ཱུஸᗮ૤အᅼᗮ

/X¼w/˺̓ࡐᗮཱུ਺ᑌᗮཱུအূ਺ᗮ૤အᅼᗮo͆

X.RX̓:ɼ?͆͆೬በᗮࡐཱུᗮx5͆?͆78ཱུ͆အূ਺ Óᗮ

7͆.̙?͆7͆8 o ͆ 57

਺༌೬ዏᗮቯዏᅼ೬ཱུஸᗮ૤အᅼᗮ?8͆/Ϯ7o͆

/X¼w/ö̓7 o͆

֍೬ஸᎰᅼ਺ᗮILJ|ŠʹϮۼበ਺Ꮀূအहအূ਺ᗮ૤အᅼᗮ૤Ꮀཱུहዏ೬အཱུᗮ͆

ֻཱུहዏ೬အཱུᗮ͆೬ཱུᗮ֍೨ஸȴᗮIɲ|ŠϮஸ਺ཱུ਺ᅼࡐዏ਺በᗮ೬྆በዏᅼᎰहዏ೬အཱུበᗮࡐཱུূᗮᅼ਺ዏᎰᅼཱུበᗮࣂᗮႼအበበ೬࣒ไᓾᗮ

ཱུ਺ᑠᗮཱུအূ਺ᗧᗮߖఀ਺ཱུᗮᅼ਺Ⴜᅼ਺በ਺ཱུዏበᗮࡐཱུᗮ೬ূ਺ཱུዏ೬୫਺ᅼᗮအᅼᗮࡐᗮॖအཱུበዏࡐཱུዏčᗮ͆ᅼ਺ዏᎰᅼཱུበᗮ#5೬ዏበ਺ไ૤ȴᗮ

׭ཱུᗮࡐไไᗮအዏఀ਺ᅼᗮहࡐበ਺በčᗮ೬ዏᗮᅼ਺ዏᎰᅽཱུበᗮࡐཱུᗮ̙'؛ཱུအূ਺ᗮ૤အᅼᗮࡐᗮཱུ਺ᑌᗮዏ਺༌Ⴜအᅽࡐሽᒹᗮˆ͆ ݫఀ਺ᗮলࡐበ਺በᗮࡐᅼ਺ᗮ ࡐበᗮ૤အไไအᑌበоᗮ

ǂ ߖఀ਺ཱུᗮ7ҿ ᅼ਺Ⴜᅼ਺በ਺ཱུዏበᗮ͆ ĊĘ͆ 7͆ዏఀ਼ᗮहအূ਺ᗮ୫ᆼበዏᗮ हအ༌ႼᎰዏ਺በᗮ͆?8͆ࡐཱུূᗮ

7͆. ?7Ƕ ˆ͆ ׭ዏᗮ हᅼ਺ࡐዏ਺በᗮ ࡐᗮཱུ਺ᑌᗮዏ਺༌Ⴜအᅼࡐᅼᒹᗮࡐཱུূᗮஸ਺਼ཱུᅼࡐዢ਺በᗮࡐཱུᗮ වཱུበዱᅼᎰह˛ᗮ ዏ೬အཱུᗮ͆ Ϯ ͆ Ċʐ͆ 7͆ ‘༌အᅼ਺ᗮႼᅼ਺ह೬በ਺ไᒹčᗮ ࡐཱུᗮ೬ཱུበዏᅼᏬहዏ೬အཱུᗮ૤အᅼ༚਺ৎᗮ૤ᅼအ༌ᗮዏఀ਺ᗮበዏᅼ೬ཱུஸᗮ ᅼ਺Ⴜᅼ਺በ਺ཱུዏࡐዏ೬အཱུበᗮအ૤ᗮ͆ ͆ĊĘ͆ࡐཱུূᗮĶŵ8 ˆ͆׫ዏᗮᅼ਺ዏᎰᅽཱུበᗮࡐᗮཱུအূ਺ᗮ૤ုᅼᗮ೬ৈ਺ཱུዏ೬୫਺ᅼᗮˆ͆

ǂ ߖఀ਺ཱུᗮ ᅼ਺Ⴜᅼ਺በ਼ཱུዏበᗮࡐཱུᗮࡐᅼᅼࡐᒹᗮࡐहह਺በበᗮқҿʓ̶œ ͆ᑌ਺ᗮहࡐཱུᗮ ᅼ਺Ꮀበ਺ᗮ૤Ꮀཱུहዏ೬အཱུᗮˆ͆

ީఀ਺ᗮहࢢไไ ඦī?ҕᅼ਺ዏᎰᅼཱུበᗮࡐཱུᗮࡐहह਺በበᗮ͆ķĶŵœ͆͆ᑌ౤਺ᅼ਺ᗮ7͆ᅼ਺჈ᅼ਺በ਺ཱུዏበᗮࡐཱུᗮ೬৖਺ཱུዏ೬୫਺ᅼᗮ

(28)

The Phases of a Compiler

Phase Output Sample

A=B+C;

Scanner (performs lexical analysis) Token string ‘A’, ‘=’, ‘B’, ‘+’, ‘C’, ‘;’

And symbol table with names Parser (performs syntax analysis

based on the grammar of the programming language)

Parse tree or abstract syntax tree ;

| = / \ A + / \ B C

Semantic analyzer (type checking, etc)

Annotated parse tree or abstract syntax tree

Intermediate code generator Three-address code, quads, or RTL (Register Transfer

Language)

int2fp B t1 + t1 C t2 := t2 A

Optimizer Three-address code, quads, or

RTL

int2fp B t1 + t1 #2.3 A

Code generator Assembly code MOVF #2.3,r1

ADDF2 r1,r2 MOVF r2,A

Peephole optimizer Assembly code ADDF2 #2.3,r2

MOVF r2,A 28  

(29)

Reviewing  the  EnBre  Process  

Errors position := initial + rate * 60

lexical analyzer

syntax analyzer

semantic analyzer

intermediate code generator id1 := id2 + id3 * 60

:=

id1 id2

id3 +

*

60 :=

id1 id2

id3 +

*

inttoreal 60

Symbol Table position ....

initial ….

rate….

(30)

Reviewing  the  EnBre  Process  

Errors intermediate code generator

code optimizer

final code generator t1 := inttoreal(60) t2 := id3 * t1 temp3 := id2 + t2 id1 := t3

t1 := id3 * 60.0 id1 := id2 + t1

MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R1, R2 MOVF R1, id1 position ....

initial ….

rate….

Symbol Table

3  address  code  

30  

Riferimenti

Documenti correlati

•  An NFA accepts an input string x (over Σ ) if and only if there is some path with edges labeled with symbols from x in sequence from the start state to some.. accepting state

–  Transla=ng short-circuit Boolean expressions and flow-of- control statements with backpatching lists.. How does the code

special end symbol # to make accepting states important: the new expression is r#. •  Construct a syntax tree

•  A backward analysis propagates informa@on in the opposite direc@on in which the program flows. •  A forward analysis propagates informa@on in the same direc@on in which

/* Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to

•  LALR parsing (Look-Ahead LR) merges two or more LR(1) state into one state to reduce table size. •  Less powerful

§  The types of variables and parameters are not declared, and cannot be inferred by the Python compiler.. So run-time type checks are needed to detect

•  Each state is a mul=way branch based on the next input character.. Puxng the