SlideShare a Scribd company logo
1 of 17
Assignment Statements
Topics
• Names in the symbol table
• Reusing temporary names
• Addressing array elements
• The translation scheme for addressing array
elements.
• Accessing fields in records
Names in the Symbol table
• Emit (instruction): emit a three address statement to the
output
• newtemp: return a new temporary
• lookup(identifier): check if the identifier is in the
symbol table.
• Grammar:
S->id:=E
E->E1+E2
E->E1*E2
E->-E
E->(E1)
E->id
Translation scheme to produce three address
code for assignment statement
S->id:=E {p:=lookup(id.name);
if (p!=nil) then
emit(p ‘:=‘ E.place); else error}
E->E1+E2 {E.place = newtemp;
emit(E.place ‘:=‘ E1.place ‘+’ E2.place);}
E->E1*E2 {E.place = newtemp;
emit(E.place ‘:=‘ E1.place ‘*’ E2.place);}
E->-E1 {E.place = newtemp;
emit(E.place ‘:=‘ ‘-’ E1.place);}
E->(E1) {E.place = E1.place;}
E->id {p = lookup(id.name);
if (p!= nil) then E.place := p; else error;}
Reusing temporary names
– The code generated by E->E1+E2 has the general form:
Evaluate E1 into t1
Evaluate E2 into t2
t3 := t1 + t2
» All temporaries used in E1 are dead after t1 is evaluated
» All temporaries used in E2 are dead after t2 is evaluated
» T1 and t2 are dead after t3 is assigned
– Temporaries can be managed as a stack:
» Keep a counter c, newtemp increases c, when a
temporary is used, decreases c.
» See the previous example.
Addressing array elements
– Arrays are typically stored in block of consecutive locations.
– One-dimensional arrays
• A: array[low..high] of …
• the ith elements is at:
base + (i-low)*width  i*width + (base – low*width)
– Multi-dimensional arrays:
• Row major or column major forms
– Row major: a[1,1], a[1,2], a[1,3], a[2,1], a[2,2], a[2,3]
– Column major: a[1,1], a[2,1], a[1, 2], a[2, 2],a[1, 3],a[2,3]
• In raw major form, the address of a[i1, i2] is
Base+((i1-low1)*(high2-low2+1)+i2-low2)*width
Addressing array elements
• In raw major form, the address of a[i1, i2] is
Base+((i1-low1)*(high2-low2+1)+i2-low2)*width
– In general, for any dimensional array, the address of
a[i1, i2, i3, …, ik] can be computed as follows:
• Let highj and lowj be for bounds for ij
• Let nj = highj – lowj + 1
• The address is
• ((…(i1*n2+i2)*n3+…)nk+ik)*width + base –
((…low1*n2+low2)*n3+…)nk+lowk) * width
– When generating code for array references, we need to
explicitly compute the address.
Translation scheme for array elements
• Limit(array, j) returns nj=highj-lowj+1
• .place: the temporarys or variables
• .offset: offset from the base, null if not an array reference
• Grammar:
S->L := E
E->E+E
E->(E)
E->L
L->Elist ]
L->id
Elist->Elist, E
Elist->id[E
S->L := E { if L.offset = null then emit(L.place ‘:=‘ E.place)
else emit(L.place’[‘L.offset ‘]’ ‘:=‘ E.place);}
E->E1+E2 {E.place := newtemp;
emit(E.place ‘:=‘ E1.place ‘+’ E2.place);}
E->(E1) {E.place := E1.place;}
E->L {if L.offset = null then E.place = L.place
else {E.place = newtemp;
emit(E.place ‘:=‘ L.place ‘[‘ L.offset ‘]’);
}
}
L->Elist ] {L.place = newtemp; L.offset = newtemp;
emit(L.place ‘:=‘ c(Elist.array));
emit(L.offset ‘:=‘ Elist.place ‘*’ width(Elist.array);
}
L->id {L.place = lookup(id.name);
L.offset = null;
}
Elist->Elist1, E {
t := newtemp;
m := Elist1.ndim + 1;
emit(t ‘:=‘ Elist1.place ‘*’limit(Elist1.array, m));
emit(t, ‘:=‘ t ‘+’ E.place);
Elist.array = Elist1.array;
Elist.place := t;
Elist.ndim := m;
}
Elist->id[E {Elist.array := lookup(id.name);
Elist.place := E.place
Elist.ndim := 1;
}
E.place := newtemp;
if E1.type = integer and E2.type = integer then begin
emit(E.place’:=’E1.place ’int+’E2.place);
E.type: = integer end else if E1.type = real and E2.type = real then begin
emit (E.place ’:=’E1.place ‟real +‟ E2.place);
E.type := real end else if E1.type = integer and E2.type = real then begin
u := newtemp;
emit(u ’:=’‟inttoreal‟ E1.place);
emit(E.place ’:=’u ‟real+‟ E2.place);
E.type:= real end else if E1.type = real and E2.type = integer then begin
u := newtemp;
emit(u ’:=’‟inttoreal‟ E2.place);
emit(E.place ’:=’E1.place ‟real+‟ u);
E.type: = real end else
E.type:= type error;
Fig. 8.19. Semantic action for E E1 + E2.
Type conversions with Assignments
Cont…
• The semantic action of Fig. 8. l9 uses two
attributes E.place and E.type for the non-terminal
E.
• As the number of types subject to conversion
increases.
• The number of cases that arise increases
quadratically (or worse, if there are operators with
more than two arguments).
• Therefore with large numbers of types, careful
organization of the semantic actions becomes
more important.
Accessing Fields in Records
• The compiler must keep track of both the types and relative
addresses of the fields of a record.
• An advantage of keeping this information in symbol-table
entries for the field names is that the routine for looking up
names in the symbol table can also be used for field names.
T  record L D end {T.type := record(top(tblptr));
T.width := top(offset);
Pop(tblptr); pop(offset) }
L ε { t:= mktable(nil);
Push(t, tblptr); push (0, offset) }
Cont…
• lf r is a pointer to the symbol table for a record type, then the
type record(t) formed by applying the constructor record to the
pointer was returned as T.type
We use the expression ,
p↑.info + 1
• From the operations in this expression it follows that p must be
a pointer to a record with a field name info whose type is
arithmetic.
Cont….
pointer (record(t))
• The type of pt is then record(t), from which t can be extracted.
The field name info is looked up in the symbol table pointed to
by t.
?

More Related Content

What's hot

Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generationrawan_z
 
Topological Sorting
Topological SortingTopological Sorting
Topological SortingShahDhruv21
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy TutorialAfzal Badshah
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Codesanchi29
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptFamiDan
 
trees in data structure
trees in data structure trees in data structure
trees in data structure shameen khan
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSGokul Hari
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler DesignKuppusamy P
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryTsegazeab Asgedom
 
DAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptxDAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptxArbabMaalik
 

What's hot (20)

Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
 
Back patching
Back patchingBack patching
Back patching
 
linked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutoriallinked list in Data Structure, Simple and Easy Tutorial
linked list in Data Structure, Simple and Easy Tutorial
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
Heap sort
Heap sortHeap sort
Heap sort
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Distributed Hash Table
Distributed Hash TableDistributed Hash Table
Distributed Hash Table
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Hashing
HashingHashing
Hashing
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
Chap4
Chap4Chap4
Chap4
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
Chapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata TheoryChapter1 Formal Language and Automata Theory
Chapter1 Formal Language and Automata Theory
 
DAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptxDAA-Floyd Warshall Algorithm.pptx
DAA-Floyd Warshall Algorithm.pptx
 
Hashing
HashingHashing
Hashing
 

Similar to Assignment statements

Assignment statements
Assignment statementsAssignment statements
Assignment statementsDivya Devan
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdfSHUJEHASSAN
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...venkatapranaykumarGa
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationRamchandraRegmi
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdfMariappanR3
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory MappingQundeel
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniquesd72994185
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++NUST Stuff
 
Presentation(intermediate code generation)
Presentation(intermediate code generation)Presentation(intermediate code generation)
Presentation(intermediate code generation)Sourov Kumar Ron
 

Similar to Assignment statements (20)

Assignment statements
Assignment statementsAssignment statements
Assignment statements
 
Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Ch8a
Ch8aCh8a
Ch8a
 
12IRGeneration.pdf
12IRGeneration.pdf12IRGeneration.pdf
12IRGeneration.pdf
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
 
Ch8b
Ch8bCh8b
Ch8b
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
R_CheatSheet.pdf
R_CheatSheet.pdfR_CheatSheet.pdf
R_CheatSheet.pdf
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Lecture#9 Arrays in c++
Lecture#9 Arrays in c++Lecture#9 Arrays in c++
Lecture#9 Arrays in c++
 
Left factor put
Left factor putLeft factor put
Left factor put
 
scripting in Python
scripting in Pythonscripting in Python
scripting in Python
 
Presentation(intermediate code generation)
Presentation(intermediate code generation)Presentation(intermediate code generation)
Presentation(intermediate code generation)
 
Array
ArrayArray
Array
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Python revision tour II
Python revision tour IIPython revision tour II
Python revision tour II
 

Recently uploaded

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Assignment statements

  • 2. Topics • Names in the symbol table • Reusing temporary names • Addressing array elements • The translation scheme for addressing array elements. • Accessing fields in records
  • 3. Names in the Symbol table • Emit (instruction): emit a three address statement to the output • newtemp: return a new temporary • lookup(identifier): check if the identifier is in the symbol table. • Grammar: S->id:=E E->E1+E2 E->E1*E2 E->-E E->(E1) E->id
  • 4. Translation scheme to produce three address code for assignment statement S->id:=E {p:=lookup(id.name); if (p!=nil) then emit(p ‘:=‘ E.place); else error} E->E1+E2 {E.place = newtemp; emit(E.place ‘:=‘ E1.place ‘+’ E2.place);} E->E1*E2 {E.place = newtemp; emit(E.place ‘:=‘ E1.place ‘*’ E2.place);} E->-E1 {E.place = newtemp; emit(E.place ‘:=‘ ‘-’ E1.place);} E->(E1) {E.place = E1.place;} E->id {p = lookup(id.name); if (p!= nil) then E.place := p; else error;}
  • 5. Reusing temporary names – The code generated by E->E1+E2 has the general form: Evaluate E1 into t1 Evaluate E2 into t2 t3 := t1 + t2 » All temporaries used in E1 are dead after t1 is evaluated » All temporaries used in E2 are dead after t2 is evaluated » T1 and t2 are dead after t3 is assigned – Temporaries can be managed as a stack: » Keep a counter c, newtemp increases c, when a temporary is used, decreases c. » See the previous example.
  • 6. Addressing array elements – Arrays are typically stored in block of consecutive locations. – One-dimensional arrays • A: array[low..high] of … • the ith elements is at: base + (i-low)*width  i*width + (base – low*width) – Multi-dimensional arrays: • Row major or column major forms – Row major: a[1,1], a[1,2], a[1,3], a[2,1], a[2,2], a[2,3] – Column major: a[1,1], a[2,1], a[1, 2], a[2, 2],a[1, 3],a[2,3] • In raw major form, the address of a[i1, i2] is Base+((i1-low1)*(high2-low2+1)+i2-low2)*width
  • 7. Addressing array elements • In raw major form, the address of a[i1, i2] is Base+((i1-low1)*(high2-low2+1)+i2-low2)*width
  • 8. – In general, for any dimensional array, the address of a[i1, i2, i3, …, ik] can be computed as follows: • Let highj and lowj be for bounds for ij • Let nj = highj – lowj + 1 • The address is • ((…(i1*n2+i2)*n3+…)nk+ik)*width + base – ((…low1*n2+low2)*n3+…)nk+lowk) * width – When generating code for array references, we need to explicitly compute the address.
  • 9. Translation scheme for array elements • Limit(array, j) returns nj=highj-lowj+1 • .place: the temporarys or variables • .offset: offset from the base, null if not an array reference • Grammar: S->L := E E->E+E E->(E) E->L L->Elist ] L->id Elist->Elist, E Elist->id[E
  • 10. S->L := E { if L.offset = null then emit(L.place ‘:=‘ E.place) else emit(L.place’[‘L.offset ‘]’ ‘:=‘ E.place);} E->E1+E2 {E.place := newtemp; emit(E.place ‘:=‘ E1.place ‘+’ E2.place);} E->(E1) {E.place := E1.place;} E->L {if L.offset = null then E.place = L.place else {E.place = newtemp; emit(E.place ‘:=‘ L.place ‘[‘ L.offset ‘]’); } } L->Elist ] {L.place = newtemp; L.offset = newtemp; emit(L.place ‘:=‘ c(Elist.array)); emit(L.offset ‘:=‘ Elist.place ‘*’ width(Elist.array); }
  • 11. L->id {L.place = lookup(id.name); L.offset = null; } Elist->Elist1, E { t := newtemp; m := Elist1.ndim + 1; emit(t ‘:=‘ Elist1.place ‘*’limit(Elist1.array, m)); emit(t, ‘:=‘ t ‘+’ E.place); Elist.array = Elist1.array; Elist.place := t; Elist.ndim := m; } Elist->id[E {Elist.array := lookup(id.name); Elist.place := E.place Elist.ndim := 1; }
  • 12. E.place := newtemp; if E1.type = integer and E2.type = integer then begin emit(E.place’:=’E1.place ’int+’E2.place); E.type: = integer end else if E1.type = real and E2.type = real then begin emit (E.place ’:=’E1.place ‟real +‟ E2.place); E.type := real end else if E1.type = integer and E2.type = real then begin u := newtemp; emit(u ’:=’‟inttoreal‟ E1.place); emit(E.place ’:=’u ‟real+‟ E2.place); E.type:= real end else if E1.type = real and E2.type = integer then begin u := newtemp; emit(u ’:=’‟inttoreal‟ E2.place); emit(E.place ’:=’E1.place ‟real+‟ u); E.type: = real end else E.type:= type error; Fig. 8.19. Semantic action for E E1 + E2. Type conversions with Assignments
  • 13. Cont… • The semantic action of Fig. 8. l9 uses two attributes E.place and E.type for the non-terminal E. • As the number of types subject to conversion increases. • The number of cases that arise increases quadratically (or worse, if there are operators with more than two arguments). • Therefore with large numbers of types, careful organization of the semantic actions becomes more important.
  • 14. Accessing Fields in Records • The compiler must keep track of both the types and relative addresses of the fields of a record. • An advantage of keeping this information in symbol-table entries for the field names is that the routine for looking up names in the symbol table can also be used for field names. T  record L D end {T.type := record(top(tblptr)); T.width := top(offset); Pop(tblptr); pop(offset) } L ε { t:= mktable(nil); Push(t, tblptr); push (0, offset) }
  • 15. Cont… • lf r is a pointer to the symbol table for a record type, then the type record(t) formed by applying the constructor record to the pointer was returned as T.type We use the expression , p↑.info + 1 • From the operations in this expression it follows that p must be a pointer to a record with a field name info whose type is arithmetic.
  • 16. Cont…. pointer (record(t)) • The type of pt is then record(t), from which t can be extracted. The field name info is looked up in the symbol table pointed to by t.
  • 17. ?