Download presentation
Presentation is loading. Please wait.
1
Chapter 1 Language Processor
2
Introduction Semantic gap Solve by PL Design and coding
PL implementation steps Introduced new PL Domain Specification Gap: Semantic gap between two specification of same task. Execution Gap: Gap between the semantics of the program written in different programming language. Application domain Execution domain Semantic Gap Application domain PL Domain Execution domain Execution Gap Specification gap
3
Language Processor Definition: LP is a software which bridges a specification or execution gap. Parts of LP: Language translator: bridges an execution gap like compiler, assembler Detranslator Preprocessor language migrator Interpreter: is a language processor which bridges an execution gap without generating m/c lang. program.
4
Procedure oriented lang.
Problem oriented lang. Less specification gap, more execution gap Procedure oriented lang. More specification gap, less specification gap
5
Language processing activities
Program generation activity Program Execution activity: Translation and Interpretation Application domain Program generator domain Target PL Domain Execution Domain Specification Gap
6
Program Interpretation:
Program Translation Translate program from SL to m/c language. Characteristics A program must be translated before it can be executed. A translated program may saved in a file and saved program may be executed repeatedly. A program must be retranslated following modifications. Program Interpretation: Reads the source program and stores in to memory. Determines it meaning and performs action.
7
Program interpretation and Execution
Program Execution Fetch the instruction cycle Decode the instruction to determine the operation. Execute the instruction Program Interpretation Fetch the statement Analyze the instruction to determine the meaning. Execute the statement
8
Comparison ?????
9
Fundamentals of language processing
LP= Analysis of SP+ Synthesis of TP. Analysis of SP Lexical rule: valid lexical units Syntax rule: formation of valid statements Semantic rule: Associate mening with valid statements.
10
Phases of LP Forward Reference: A forward reference of a program entity is a reference to the entity which precedes its definition in the program. Ex. struct s { struct t *pt}; . . struct t { struct s *ps }; Issues concerning memory requirements and organization of LP. Analysis Phase Synthesis Phase Source program IR Target program Errors Errors
11
Passes of LP Language Processor pass: A language processor pass is the processing of every statement in a source program, or its equivalent representation, to perform language processing function. Pass-I: Perform Analysis of SP. Pass-II : Perform synthesis of TP.
12
Intermediate Representation of Programs
Intermediate Representation: An Intermediate representation(IR) is a representation of a source program which reflects the effect of some, but not all, analysis and synthesis tasks performed during language processing. SP Front End Back End TP Intermediate Representation(IR)
13
IR and Semantic actions
Properties of IR Ease of Use: Processing Efficiency: Memory efficiency: compact Semantic actions: All actions performed by the front end, except lexical and syntax analysis are called semantic actions., which includes Checking semantic validity Determine the meaning Constructing IR
14
Symbol table Generation
Toy Compiler Gcc or cc compiler- c or c++ Toy compiler- ??? Front End Lexical analysis Syntax analysis Semantic analysis Back End Memory Allocation Code Generation Symbol Type length address a int b float temp Symbol table Generation
15
Front End Lexical (scanning) Syntax (Parsing)
Ex. a:=b+i ; id#2 op#5 id #3 op#3 id #1 op#10 Syntax (Parsing) a,b : real; a:=b+i; Semantic: IC tree is generated real a b
16
Back End Memory Allocation: Code Generation: Generating Assembly Lang.
issues: Determine the places where IR should be kept. Determine which instructions should be used for type conversion. Determine which addressing mode should be used for accessing variables. Symbol Type length address a int 2 2000 b float 4 2002 temp 2006
17
Fundamentals of Language Specification
Programming language Grammars: Terminal symbols lowercase letters, punctuation marks, null Concatenation(.) Nonterminal symbols: name of syntax category of language Productions: called rewriting a rule, is a rule of the grammar NT = String of T’s and NT’s. Production form: <article>= a/an/the <Noun> =<boy ><apple> <Noun phrase>= <artical><Noun>
18
Grammar Def: A grammar G of a language Lg is a quadruple (∑,SNT,S,P) where, ∑ is the set of terminals SNT is the set of NT’s S is the distinguished symbol P is the set of productions Ex: Derive a sentence “A boy ate an apple” <sentence> = <Noun Phrase> <verb phrase> <Noun phrase> =<article><Noun> <verb phrase>=<verb ><noun phrase> <Article> = a/an/the <Noun> = boy/apple <Verb> = ate
19
Grammar Derive a + b * c /5 and construct parse tree.(top down)
<exp>=<exp> + <term> | <term> <term>=<term>*<factor> | <factor> <factor>=<factor>/<number> <number>=0/1/2/3/../9 Classification of grammar: Type-0: phrase structure grammar Type-1 : context sensitive grammar Type-2 : context free grammar Type-3 : linear grammar or regular grammar
20
Binding Definition: A binding is the association of an attribute of a program entity with a value. Static Binding: Binding is a binding performed before the execution of a program begins. Dynamic Binding: Binding is a binding performed after the execution of a program begins.
21
Chapter - 3 Scanning and Parsing Unit-2
22
Role of lexical Analyzer
23
Scanning Definition: Scanning is the process of recognizing the lexical components in a source string. Type-3 grammar or regular grammar Regular grammar used to identify identifiers Regular language obtained from the operation or , concatenation and Kleen* Ex. Write a regular expression which used to identify strings which ends with abb. (a+b)*abb. Ex. Write a regular expression which used to identify strings which recognize identifiers. R.E. = (letter)(letter/ digit)* Digit = 0/1/2/…/9 Letter = a/b/c/…./z
24
Regular Expression and Meaning
String r s String s r.s and rs Concatenation of r and s (r) Same meaning as r r/s or (r/s) alternation (r or s) (r)/(s) Alternation [r] An optional occurrence of r (r)* 0 or more occurrence of string r (r)+ 1 or more occurrence of string r
25
Examples of regular expression
Integer :[+/-](d)† Real : [+/-](d)†.(d) † Real with optional fraction : [+/-](d)†.(d) * Identifier : l(l/d)*
26
Example of Regular expression
String ending with 0 : (0+1)*0 String ending with 11: (0+1)*11 String with 0 EVEN and 1 ODD. (0+1)*(01*01*)*11*0* The language of all strings containing exactly two 0’s. :1*01*01* The language of all strings that do not end with 01 : ^+1+(0+1)*+(0+1)*11
27
Finite state automaton
FSA: is a triple (S,∑,T) where, S is a finite set of states, ∑ is the alphabet of source symbols, T is a finite set of state transitions FSA DFA NFA
28
DFA from Regular Expression
(0+1)*0 (11+10)* (0+1)*(1+00) (0+1)*
29
Transition table from DFA
States/input 1 qo q1 (0+1)*0 Transition Table (11+10)* (0+1)*(1+00) (0+1)*
30
DFA and it’s transition Diagram
Check for the given string aabab
31
Types of Parser Types of Parser Top down Parser Bottom Up Parser
Backtracking Predictive Parser LR Parser Shift Reduce Parser SLR LR LALR
32
Example Expression grammar (with precedence) Input string x – 2 * y #
Production rule 1 2 3 4 5 6 7 8 expr → expr + term | expr - term | term term → term * factor | term / factor | factor factor → number | identifier
33
Example Problem: Can’t match next terminal We guessed wrong at step 2
Current position in the input stream Problem: Can’t match next terminal We guessed wrong at step 2 Rule Sentential form Input string - expr expr x - 2 * y expr + term x - 2 * y term + term x – 2 * y expr + term factor + term x – 2 * y <id> + term x – 2 * y <id,x> + term x – 2 * y term fact x
34
Backtracking Rollback productions
Choose a different production for expr Continue Rule Sentential form Input string - expr x - 2 * y expr + term x - 2 * y term + term x – 2 * y Undo all these productions factor + term x – 2 * y <id> + term x – 2 * y ? <id,x> + term x – 2 * y
35
Retrying Problem: More input to read Another cause of backtracking - 2
Rule Sentential form Input string - expr expr x - 2 * y expr - term x - 2 * y expr - term term - term x – 2 * y factor - term x – 2 * y <id> - term x – 2 * y term fact <id,x> - term x – 2 * y 3 <id,x> - factor x – 2 * y 7 <id,x> - <num> x – 2 * y fact 2 x
36
Successful Parse All terminals match – we’re finished - * y 2 x Rule
Sentential form Input string - expr All terminals match – we’re finished expr x - 2 * y expr - term x - 2 * y expr - term term - term x – 2 * y factor - term x – 2 * y term * fact <id> - term x – 2 * y term <id,x> - term x – 2 * y 4 <id,x> - term * fact x – 2 * y fact y 6 <id,x> - fact * fact x – 2 * y fact 7 <id,x> - <num> * fact x – 2 * y 2 <id,x> - <num,2> * fact x – 2 * y x 8 <id,x> - <num,2> * <id> x – 2 * y
37
Problems in Top down Parsing
Backtracking( we have seen) Left recursion Left Factoring
38
Left Recursion Problem: termination
Wrong choice leads to infinite expansion (More importantly: without consuming any input!) May not be as obvious as this Our grammar is left recursive Rule Sentential form Input string - expr x - 2 * y expr + term x - 2 * y expr + term + term x – 2 * y expr + term + term + term x – 2 * y expr + term + term + term + term x – 2 * y
39
Rules for Left Recursion
If A-> Aa1/Aa2/Aa3/………/Aan/b1/b2/…/bn After removal of left Recursion A-> b1A’/b2A’/b3A’ A’-> a1A’/a2A’/є Ex. Apply for A-> Aa/Ab/c/d A-> Ac/Aad/bd/є
40
Removing Left Recursion
Two cases of left recursion: Transform as follows # Production rule 1 2 3 expr → expr + term | expr - term | term # Production rule 4 5 6 term → term * factor | term / factor | factor # Production rule 1 2 3 4 expr → term expr2 expr2 → + term expr2 | term expr2 | e # Production rule 4 5 6 term → factor term2 term2 → * factor term2 | / factor term2 | e
41
Left Factoring When the choice between two production is not clear, we may be able to rewrite the productions to defer decisions is called as left factoring. Ex. Stmt-> if expr then stmt else stmt | if expr then stmt Stmt-> if expr then stmt S’ S’-> if expr then stmt | є Rules: if A-> ab1/ab2 then A-> aA’ A’-> b1/b2
42
Some examples for Left factoring
S-> Assig_stmt/call_stmt/other Assig_stmt-> id=exp call_stmt->id(exp_list)
43
Recursive Descent Parsing
Example Rule 1: S a S b Rule 2: S b S a Rule 3: S B Rule 4: B b B Rule 5: B Parse: a a b b b Has to use R1: S a S b Again has to use R1: a S b a a S b b Now has to use Rule 2 or 3, follow the order (always R2 first): a a S b b a a b S a b b a a b b S a a b b a a b b b S a a a b b Now cannot use Rule 2 any more: a a b b b B a a a b b a a b b b B a a a b b incorrect, backtrack After some backtracking, finally tried a S b a a S b b a a b B b b a a b b b worked
44
Predicative Parsing Need to immediately know which rule to apply when seeing the next input character If for every non-terminal X We know what would be the first terminal of each X’s production And the first terminal of each X’s production is different Then When current leftmost non-terminal is X And we can look at the next input character We know exactly which production should be used next to expand X
45
But, R3’s first terminal is also b
Predicative Parsing Need to immediately know which rule to apply when seeing the next input character If for every non-terminal X We know what would be the first terminal of each X’s production And the first terminal of each X’s production is different Example Rule 1: S a S b Rule 2: S b S a Rule 3: S B Rule 4: B b B Rule 5: B First terminal is a First terminal is b If next input is a, use R1 If next input is b, use R2 But, R3’s first terminal is also b Won’t work!!!
46
Predicative Parsing Need to immediately know which rule to apply when seeing the next input character If for every non-terminal X We know what would be the first terminal of each X’s production And the first terminal of each X’s production is different What grammar does not satisfy the above? If two productions of the same non-terminal have the same first symbol (N or T), you can see immediately that it won’t work S b S a | b B S B a | B C If the grammar is left recursive, then it won’t work S S a | b B, B b B | c The left recursive rule of S can generate all terminals that the other productions of S can generate S b B can generate b, so, S S a can also generate b
47
Predicative Parsing Need to rewrite the grammar
Left recursion elimination This is required even for recursive descent parsing algorithm Left factoring Remove the leftmost common factors
48
First() First() = { t | * t } For all terminals t T
Consider all possible terminal strings derived from The set of the first terminals of those strings For all terminals t T First(t) = {t}
49
First() For all non-terminals X N
If X add to First(X) If X 1 2 … n i is either a terminal or a non-terminal (not a string as usual) Add all terminals in First(1) to First(X) Exclude If First(1) … First(i-1) then add all terminals in First(i) to First(X) If First(1) … First(n) then add to First(X) Apply the rules until nothing more can be added For adding t or : add only if t is not in the set yet
50
First() Grammar First E TE’ E’ +TE’ | T FT’ T’ *FT’ |
F (E) | id | num First First(*) = {*}, First(+) = {+}, … First(F) = {(, id, num} First(T’) = {*, } First(T) = First(F) = {(, id, num} First(E’) = {+, } First(E) = First(T) = {(, id, num}
51
First() Grammar First S AB A aA | B bB | First(A) = {a, }
First(B) = {b, } First(S) = First(A) ={a, } Is this complete?
52
First() Grammar First Productions S AB | B (R1 | R2)
If we see a If we see b If we see c If we see d When expanding S Use R1 Use R2 When expanding A Use R3 - Use R4 When expanding B Use R5 Use R6 Grammar S AB | B (R1 | R2) A aA | c (R3 | R4) B bB | d (R5 | R6) First First(A) = {a, c} First(B) = {b, d} First(S) = First(A) First(B) = {a, b, c, d} Productions First (R1) = {a, c}, First (R2) = {b, d} First (R3) = {a}, First (R4) = {c} First (R5) = {b}, First (R6) = {d} Input: acbd Expands S, seeing a, use R1: S AB Expands A, seeing a, use R3: AB aAB Expands A, seeing c, use R4: aAB acB Expands B, seeing b, use R5: acB acbB Expands B, seeing d, use R6: acbB acbd
53
First() Grammar First Productions S AB (R1) A aA | (R2 | R3)
If we see a If we see b If we see When expanding S Use R1 When expanding A Use R2 - Use R3 When expanding B Use R4 Use R5 Grammar S AB (R1) A aA | (R2 | R3) B bB | (R4 | R5) First First(A) = {a, } First(B) = {b, } First(S) = First(A) First(B) ={a, b, } Productions First (R1) = {a, b, } First (R2) = {a}, First (R3) = {} First (R4) = {b}, First (R5) = {} Input: aabb Use R1: S AB Expands A, seeing a, use R2: AB aAB Expands A, seeing a, use R2: aAB aaAB Expands A, seeing b, What to do? Not in table!
54
Follow() Follow() = { t | S * t } Assumptions
Consider all strings that may follow The set of the first terminals of those strings Assumptions There is a $ at the end of every input string S is the starting symbol For all non-terminals only Add $ into Follow(S) If A B add First() – {} into Follow(B) If A B or A B and First() add Follow(A) into Follow(B)
55
Follow() First Productions Follow First(A) = {a, } First(B) = {b, }
Grammar S AB (R1) A aA | (R2 | R3) B bB | (R4 | R5) First First(A) = {a, } First(B) = {b, } First(S) = First(A) ={a, b, } Productions First (R1) = {a, b, } First (R2) = {a}, First (R3) = {} First (R4) = {b}, First (R5) = {} Follow Follow(S) = {$} Follow(B) = Follow(S) = {$} Follow(A) = First(B) Follow(S) = {b, $} Since First(B), Follow(S) should be in Follow(A) If we see a If we see b When expanding S Use R1 When expanding A Use R2 ? When expanding B - Use R4 If we see a If we see b If we see $ When expanding S Use R1 When expanding A Use R2 Use R3 When expanding B - Use R4 Use R5
56
Construct a Parse Table
Construct a parse table M[N, T{$}] Non-terminals in the rows and terminals in the columns For each production A For each terminal a First() add A to M[A, a] Meaning: When at A and seeing input a, A should be used If First() then for each terminal a Follow(A) In order to continue expansion to X AC A B B b | C cc If First() and $ Follow(A) add A to M[A, $] Same as the above
57
First() and Follow() – another example
Grammar E TE’ E’ +TE’ | T FT’ T’ *FT’ | F (E) | id | num First(*) = {*} First(F) = {(, id, num} First(T’) = {*, } First(T) = First(F) = {(, id, num} First(E’) = {+, } First(E) = First(T) = {(, id, num} Follow(E) = {$, )} Follow(E’) = Follow(E) = {$, )} Follow(T) = {$, ), +} Since we have TE’ from first two rules and E’ can be Follow(T) = (First(E’)–{}) Follow(E’) Follow(T’) = Follow(T) = {$, ), +} Follow(F) = {*, $, ), +} Follow(F) = (First(T’)–{}) Follow(T’)
58
Construct a Parse Table
Grammar E TE’ E’ +TE’ | T FT’ T’ *FT’ | F (E) | id | num First(*) = {*} First(F) = {(, id, num} First(T’) = {*, } First(T) {(, id, num} First(E’) = {+, } First(E) {(, id, num} Follow(E) = {$, )} Follow(E’) = {$, )} Follow(T) = {$, ), +} Follow(T’) = {$, ), +} Follow(F) = {*, $, ), +} E TE’: First(TE’) = {(, id, num} T FT’: First(FT’) = {(, id, num} E’ +TE’: First(+TE’) = {+} E’ : Follow(E’) = {$,)} T’ *FT’: First(*FT’) = {*} T’ : Follow(T’) = {$, ), +} id num * + ( ) $ E E TE’ E’ E’ +TE’ E’ T T FT’ T’ T’ *FT’ T’ F F id F num F (E)
59
Pop F from stack Remove id from input Pop T’ from stack
Action E $ id + num * id $ ETE’ T E’ $ T FT’ F T’ E’ $ F id T’ E’ $ + num * id $ T’ E’ $ E’ +TE’ num * id $ F num * id $ T’ *FT’ id $ $ E’ Accept Pop F from stack Remove id from input Pop T’ from stack Input unchanged +TE’: Only TE’ in stack Remove + from input id num * + ( ) $ E E TE’ E’ E’ +TE’ E’ T T FT’ T’ T’ *FT’ T’ F F id F num F (E)
60
More about LL Grammar What grammar is not LL(1)? S A | B A aaA |
$ S S A S B A A aaA A B B abB B b What grammar is not LL(1)? S A | B A aaA | B abB | b First(A) = {a, }, First(B) = {a, b}, First(S) = {a, b, } Follow(S) = {$}, Follow(A) = {$}, Follow(B) = {$} But this grammar is LL(2) If we lookahead 2 input characters, predictive parsing is possible First2(A) = {aa, }, First2(B) = {ab, b$}, First2(S) = {aa, ab, b$, } aa ab b$ $ ba, bb, a$ S S A S B A A aaA A B B abB B b
61
A Shift-Reduce Parser E E+T | T Right-Most Derivation of id+id*id
T T*F | F E E+T E+T*F E+T*id E+F*id F (E) | id E+id*id T+id*id F+id*id id+id*id Right-Most Sentential Form Reducing Production id+id*id F id F+id*id T F T+id*id E T E+id*id F id E+F*id T F E+T*id F id E+T*F T T*F E+T E E+T E Handles are red and underlined in the right-sentential forms
62
A Stack Implementation of A Shift-Reduce Parser
There are four possible actions of a shift-parser action: Shift : The next input symbol is shifted onto the top of the stack. Reduce: Replace the handle on the top of the stack by the non-terminal. Accept: Successful completion of parsing. Error: Parser discovers a syntax error, and calls an error recovery routine. Initial stack just contains only the end-marker $. The end of the input string is marked by the end-marker $.
63
A Stack Implementation of A Shift-Reduce Parser
Stack Input Action $ id+id*id$ shift $id +id*id$ reduce by F id $F +id*id$ reduce by T F $T +id*id$ reduce by E T $E +id*id$ shift $E+ id*id$ shift $E+id *id$ reduce by F id $E+F *id$ reduce by T F $E+T *id$ shift $E+T* id$ shift $E+T*id $ reduce by F id $E+T*F $ reduce by T T*F $E+T $ reduce by E E+T $E $ accept
64
Operator-Precedence Parser
Operator grammar small, but an important class of grammars we may have an efficient operator precedence parser (a shift-reduce parser) for an operator grammar. In an operator grammar, no production rule can have: at the right side two adjacent non-terminals at the right side. Ex: EAB EE+E | Aa E*E | Bb E/E | id not operator grammar operator grammar
65
Precedence Relations In operator-precedence parsing, we define three disjoint precedence relations between certain pairs of terminals. a <. b b has higher precedence than a a =· b b has same precedence as a a .> b b has lower precedence than a The determination of correct precedence relations between terminals are based on the traditional notions of associativity and precedence of operators. (Unary minus causes a problem).
66
Using Operator-Precedence Relations
The intention of the precedence relations is to find the handle of a right-sentential form, <. with marking the left end, =· appearing in the interior of the handle, and .> marking the right hand. In our input string $a1a2...an$, we insert the precedence relation between the pairs of terminals (the precedence relation holds between the terminals in that pair).
67
Using Operator -Precedence Relations
E E+E | E-E | E*E | E/E | E^E | (E) | -E | id The partial operator-precedence table for this grammar. Then the input string id+id*id with the precedence relations inserted will be: $ <. id .> + <. id .> * <. id .> $ id + * $ .> <.
68
To Find The Handles Scan the string from left end until the first .> is encountered. Then scan backwards (to the left) over any =· until a <. is encountered. The handle contains everything to left of the first .> and to the right of the <. is encountered. $ <. id .> + <. id .> * <. id .> $ E id $ id + id * id $ $ <. + <. id .> * <. id .> $ E id $ E + id * id $ $ <. + <. * <. id .> $ E id $ E + E * id $ $ <. + <. * .> $ E E*E $ E + E * .E $ $ <. + .> $ E E+E $ E + E $ $ $ $ E $
69
Operator-Precedence Parsing Algorithm -- Example
id + * $ .> <. stack input action $ id+id*id$ $ <. id shift $id +id*id$ id .> + reduceE id $ +id*id$ shift $+ id*id$ shift $+id *id$ id .> * reduce E id $+ *id$ shift $+* id$ shift $+*id $ id .> $ reduce E id $+* $ * .> $ reduce E E*E $+ $ + .> $ reduce E E+E $ $ accept
70
Introduction to Compiler
Chapter - 6 Introduction to Compiler Unit-6
71
Aspects of Compilation
Compiler bridges semantic gap between a PL domain and an execution domain. Two aspects of compilations are:- Generate code to implement meaning of a source program in execution domain. Provide diagnostics for violations of PL semantics in a source program. Data Types Data Structures Scope rules Control Structures
72
Three address Code Generate code for x=a+b+c+d
In three-address code, there is at most one operator on the right side of an instruction; that is, no built-up arithmetic expressions are permitted. Example: A source-language expression x+y*z might be translated into the sequence of three-address instructions below where tl and tz are compiler-generated temporary names. Generate code for x=a+b+c+d Generate the code for x= -a *b + -a *b
73
Quadruple t1 = uminus a t2 = t1*b t3 = uminus a t4 = t3*b t5 = t2 + t4
OP Arg1 Arg2 Result (0) uminus a t1 (1) * b t2 (2) t3 (3) t4 (4) + t5 (5) = x t1 = uminus a t2 = t1*b t3 = uminus a t4 = t3*b t5 = t2 + t4 X=t5
74
Triple t1 = uminus a t2 = t1*b t3 = uminus a t4 = t3*b t5 = t2 + t4
OP Arg1 Arg2 (0) uminus a (1) * B (2) A (3) (4) + (5) = x t1 = uminus a t2 = t1*b t3 = uminus a t4 = t3*b t5 = t2 + t4 X=t5
75
Indirect Triples (0) (11) (1) (12) (2) (13) (3) (14) (4) (15) (5) (16)
statement (0) (11) (1) (12) (2) (13) (3) (14) (4) (15) (5) (16) OP Arg1 Arg2 (0) uminus a (1) * B (2) A (3) (4) + (5) = x t1 = uminus a t2 = t1*b t3 = uminus a t4 = t3*b t5 = t2 + t4 X=t5
76
Example Construct Quadruple , Triple , Indirect Triple Representations of a = b * - c + b * - c
77
Example (c) Indirect triple
78
Aspects of compilation
A compiler bridges a specification gap between a PL domain and an execution domain. Generate code to implement meaning of a source program in the execution domain. Provide diagnosis for violation of a PL semantics in a source program PL features are: Data types: Specification of legal values for variables of the type Data structures: Scope rules: Accessibility of variables declared in different blocks of a program. Control structures:
79
Memory Allocation Memory binding: is an association between the ‘memory address’ attribute of a data item and the address of memory area. Static memory Allocation: Allocates Before Execution Dynamic memory Allocation Allocates After Execution
80
Static memory Allocation
Program consist Three units A,B,C. Advantage?????? Procedure A Data(A) B() Procedure B Data (B) C() Procedure C Data (C) Code(A) Data(A) Code(B) Data(B) Code(C) Data(C)
81
Dynamic memory Allocation
Program consist Three units A,B,C. Program A is active Data(A) is allocated Procedure A Data(A) B() Procedure B Data (B) C() Procedure C Data (C) Code(A) Code(B) Code(C) Data(A)
82
Dynamic memory Allocation
Program consist Three units A,B,C. Pro. A calls B. and Data(B) gets allocated Procedure A Data(A) B() Procedure B Data (B) C() Procedure C Data (C) Code(A) Code(B) Code(C) Data(B)
83
Dynamic memory Allocation
Program consist Three units A,B,C. Pro. B calls C. and Data(C) gets allocated Procedure A Data(A) B() Procedure B Data (B) C() Procedure C Data (C) Code(A) Code(B) Code(C) Data(C)
84
Dynamic memory Allocation
Different Scenario…. Memory allocation in Block structured language(same as above.) Advantage?????? Procedure A Data(A) B() C() Procedure B Data (B) Procedure C Data (C) Code(A) Code(A) Code(A) Code(B) Code(B) Code(A) Code(C) Code(C) Code(A) Data(A) Data(A) Data(A) Data(B) Data(C) (a) (b) (c)
85
Stack Last In, First Out (LIFO) data structure stack main () { a(0); }
Stack grows down void a (int m) { b(1); } Stack Pointer Stack Pointer void b (int n) { c(2); } Stack Pointer void c (int o) { d(3); } Stack Pointer void d (int p) { } Stack Pointer
86
Activation Records: also called frames
Information(memory) needed by a single execution of a procedure A general activation record: Store result of function call Return value actual parameters optional control link optional access link machine status local variables temporaries Information of actual Parameter Points to calling Procedure Non local data of other Procedure Information of Program counter Store local data Store temporary value
87
Activation Record for Factorial Program
main() { int f; f=factorial(3); } int factorial(int n) if(n==1) return 1; else{ return(n*factorial(n-1));
88
Activation Record for Factorial Program
89
Activation Record for Factorial Program
90
Parameter passing The method to associate actual parameters with formal parameters. The parameter passing method will effect the code generated. Call-by-value: The actual parameters are evaluated and their r-values are passed to the called procedure. Implementation: a formal parameter is treated like a local name, so the storage for the formals is in the activation record of the called procedure. The caller evaluates the actual parameters and places their r-values in the storage for the formals.
91
Call-by-reference: Copy-restore:
also called call-by address or call-by-location. The caller passes to the called procedure a pointer to the storage address of each actual parameter. Actual parameter must have an address -- only variables make sense, an expression will not (location of the temporary that holds the result of the expression will be passed). Copy-restore: A hybrid between call-by-value and call-by-reference. The actual parameters are evaluated and its r-values are passed to the called procedure as in call-by-value. When the control returns, the r-value of the formal parameters are copied back into the l-value of the actuals.
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.