Download - The TINY sample language and it ’ s compiler
![Page 2: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/2.jpg)
1 The TINY Sample Language and Compiler2 Implementation of a TINY Scanner3 Syntax of the TINY Language4 A Recursive-Descent Parser for TINY5 A Semantic Analyzer for the TINY
Language6 A runtime environment for the TINY
Language
![Page 3: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/3.jpg)
1 The TINY Sample Language and Compiler
![Page 4: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/4.jpg)
• TINY Compiler
– The construction of TINY compiler uses the techniques studied in each chapter and show how all the parts of a compiler fit together.
– Using experiment language TINY as source language– Using TM(the assembly language for a simple
hypothetical processor) as the target language– Compiler is written in C
![Page 5: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/5.jpg)
• The construction of compiler for a concrete language– Familiar with the source language (lexical, syntax,
semantic)– Familiar with the target language (addressing
mode, the number of register, data representation )
– Determine the structure of compiler
![Page 6: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/6.jpg)
1.1 The TINY Language
• Syntax description – A program is a sequence of statements separated by
semicolons– Declarations
No procedures and no declarations– Data Type
All variables are integer variables, and variables are declared by assigning values to them
![Page 7: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/7.jpg)
– StatementTwo control statements: if-statement and repeat-statement, read and write statements
– ExpressionBoolean and integer arithmetic expressions
– CommentComments are allowed within curly bracket
![Page 8: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/8.jpg)
{sample program in TINY language- computes factorial}
read x; { input an integer }if 0<x then { don’t compute if x<=0 }
fact:=1;repeat
fact := fact*x;x := x-1
until x=0;write fact{output factorial of x}
end
Example
![Page 9: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/9.jpg)
1.2 The TINY Compiler
• Components of the TINY Compiler– It has the following components: Scanner, parser, semantic
analyzer, and code generator phases together with a symbol table
– Following components are absentNo optimization phases and separate error handler or literal table
![Page 10: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/10.jpg)
Structure of TINY Compiler
• Four-pass compiler– The first pass consists of the scanner and parser, which
construct the syntax tree;– The second and third passes undertake semantic analysis• The second pass constructs the symbol table• The third pass performs type checking
– The forth pass is the code generator
![Page 11: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/11.jpg)
The code that drives these passes:syntaxTree=parse();buildSymtab(syntaxTree);typeCheck(syntaxTree);codeGen(syntaxTree,codefile);
![Page 12: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/12.jpg)
2. Implementation of a TINY Scanner
![Page 13: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/13.jpg)
2.1 Implementing a Scanner for the Sample Language TINY
• The lexical structure of TINY
Reserved Words Special Symbols Otherif then else endrepeat untilread write
+ - * /= < ( ) ; :=
numberidentifier
![Page 14: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/14.jpg)
Construct a DFA for the scanner directly
![Page 15: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/15.jpg)
Explanation:All accepting states are collected into one
state “DONE”, the different token recognized is saved in a variable
Construct a table of reserved words, reserved words are considered only after an identifier has been recognized, and then to look up the identifier in the table
The implementation of the DFA uses the doubly nested case analysis
![Page 16: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/16.jpg)
3 Syntax of the TINY Language
![Page 17: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/17.jpg)
3.1 A Context-Free Grammar for TINY
program -> stmt-seqstmt->seq -> stmt-seq;stmt | stmtstmt -> if-stmt|repeat-stmt|assign-
stmt|read-stmt | write-stmtif-stmt ->if exp then stmt-seq end
| if exp then stmt-seq else stmt-seq end
repeat-stmt->repeat stmt-seq until expassign-stmt-> id:= expread-stmt -> read idwrite-stmt -> write exp
![Page 18: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/18.jpg)
exp -> simp-exp cop simp-exp |simp-exp
cop -> < | =simp-exp -> simp-exp addop term |termterm -> term mulop factor | factorfactor -> (exp) |num |id
![Page 19: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/19.jpg)
3.2 Syntax Tree Structure for the TINY Compiler
Basic syntax tree structures1 A sequence of statements
2 An if-statement;
; s
s ssyntax tree of s;s;s
seq
s s s
![Page 20: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/20.jpg)
3 A repeat-statement 4 An assign-statement
5 A write-statement 6 An operator-expression
![Page 21: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/21.jpg)
{sample program in TINY language- computes factorial}
read x;{input an integer}if 0<x then {don’t compute if x<=0}
fact:=1;repeat
fact:=fact*x;x:=x-1
until x=0;write fact{output factorial of x}
end
![Page 22: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/22.jpg)
![Page 23: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/23.jpg)
4 A Recursive-Descent Parser for TINY
![Page 24: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/24.jpg)
• TINY Grammar in EBNF …stmt->seq -> stmt {;stmt}exp -> simp-exp [cop simp-exp]simp-exp -> term {addop term}term -> factor {mulop factor}……
![Page 25: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/25.jpg)
5 A Semantic Analyzer for the TINY Language
![Page 26: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/26.jpg)
• We separate the discussion of the TINY semantic analyzer into two parts– The structure of the symbol table and its associated
operations– The operations of the semantic analyzer itself, including
the construction of the symbol table and type checking
![Page 27: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/27.jpg)
5.1 A Symbol Table for TINY
• What information needs to be held in the table
– It does not need to contain scope information, and data type
– It contains locations for variables for code generation– It also contains a cross-reference listing of line numbers
where variables are accessed
![Page 28: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/28.jpg)
For example5: read x;6: if x>0 then7: fact:=1;8: repeat9: fact:=fact*x;10: x:=x-111: until x=0;12: write fact13:endThe symbol table for this program
Variable Name
Location Line numbers
x 0 5,6,9,10,10,11fact 1 7,9,9,12
![Page 29: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/29.jpg)
5.2 A Semantic Analyzer for TINY
• The symbol table is an inherited attribute, while the data type of an expression is a synthesized attribute
• Thus, the symbol table can be built by a preorder traversal of the syntax tree, and type checking can be performed by a post-order traversal
• Each is processed in a separate pass over the syntax tree
![Page 30: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/30.jpg)
6 A Runtime Environment for the TINY Language
![Page 31: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/31.jpg)
• The structure of a runtime environment for the TINY language – Place the variables in absolute addresses at the bottom
end of program memory– Allocate the temporary (dynamic storage during
expression evaluation) stack at the top end
![Page 32: The TINY sample language and it ’ s compiler](https://reader035.vdocument.in/reader035/viewer/2022062816/568163a1550346895dd4a037/html5/thumbnails/32.jpg)
temp1temp2temp3
free memory
wzyx
bottom of memory
top of memory
top of temp stack
0123
Runtime environment of TINY