1 / 35

Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY

Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement of TINY + scanner To be determined Syntax of TINY + and Implement the parser of TINY + Semantic of TINY +

Download Presentation

Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Week 6(10.7): • The TINY sample language and it’s compiler • The TINY+ extension of TINY • Week 7 and 8(10.14 and 10.21): • The lexical of TINY+ • Implement of TINY+ scanner • To be determined • Syntax of TINY+ and Implement the parser of TINY+ • Semantic of TINY+ • Implement of semantic analyzer and intermediate code generator for TINY+

  2. Time • Week 6,7,8 • Tuesday: section 3,4 • venue • Week 6,7,8 • Computer Bilingual: B3-230 • Network Engineering: B3-231

  3. Due date • Experiment 1Implementing a Scanner for TINY+: by the end of week 10(11.09)

  4. The TINY sample language and it’s compiler

  5. 1 The TINY Sample Language and Compiler 2 Implementation of a TINY Scanner 3 Syntax of the TINY Language 4 A Recursive-Descent Parser for TINY 5 A Semantic Analyzer for the TINY Language 6 A runtime environment for the TINY Language

  6. 1 The TINY Sample Language and Compiler

  7. 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

  8. 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

  9. 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

  10. Statement Two control statements: if-statement and repeat-statement, read and write statements • Expression Boolean and integer arithmetic expressions • Comment Comments are allowed within curly bracket

  11. Example {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

  12. 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 absent No optimization phases and separate error handler or literal table

  13. 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

  14. The code that drives these passes: syntaxTree=parse(); buildSymtab(syntaxTree); typeCheck(syntaxTree); codeGen(syntaxTree,codefile);

  15. 2. Implementation of a TINY Scanner

  16. 2.1 Implementing a Scanner for the Sample Language TINY • The lexical structure of TINY

  17. Construct a DFA for the scanner directly

  18. 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

  19. 3 Syntax of the TINY Language

  20. 3.1 A Context-Free Grammar for TINY program -> stmt-seq stmt->seq -> stmt-seq;stmt | stmt stmt -> if-stmt|repeat-stmt|assign- stmt|read-stmt | write-stmt if-stmt ->if exp then stmt-seq end | if exp then stmt-seq else stmt-seq end repeat-stmt->repeat stmt-seq until exp assign-stmt-> id:= exp read-stmt -> read id write-stmt -> write exp

  21. exp -> simp-exp cop simp-exp |simp-exp cop -> < | = simp-exp -> simp-exp addop term |term term -> term mulop factor | factor factor -> (exp) |num |id

  22. seq ; s s s ; s s s syntax tree of s;s;s 3.2 Syntax Tree Structure for the TINY Compiler Basic syntax tree structures 1 A sequence of statements 2 An if-statement

  23. 3 A repeat-statement 4 An assign-statement 5 A write-statement 6 An operator-expression

  24. {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

  25. 4 A Recursive-Descent Parser for TINY

  26. TINY Grammar in EBNF … stmt->seq -> stmt {;stmt} exp -> simp-exp [cop simp-exp] simp-exp -> term {addop term} term -> factor {mulop factor} ……

  27. 5 A Semantic Analyzer for the TINY Language

  28. 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

  29. 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

  30. For example 5: read x; 6: if x>0 then 7: fact:=1; 8: repeat 9: fact:=fact*x; 10: x:=x-1 11: until x=0; 12: write fact 13:end The symbol table for this program

  31. 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 postorder traversal • Each is processed in a separate pass over the syntax tree

  32. 6 A Runtime Environment for the TINY Language

  33. 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

  34. top of memory top of temp stack 3 2 1 0 bottom of memory Runtime environment of TINY

More Related