Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr. Mohsen Chitsaz
Chapter 8: Semantic Analyzer2 Semantic Analyzer A CFG can be used to help guide the translation of programs. This technique is known as Syntax-Directed Translation
Chapter 8: Semantic Analyzer3 Syntax-Directed Translation: (Each symbol has a set of attributes) Example: E --> E+E E --> E*E E --> id E.value := id E.value := E 1.value + E 2.value E.value := E 1.value * E 2.value E E+Eid Introduction:
Chapter 8: Semantic Analyzer4 Example: id * id + id E E E* EE+id E.val := E 1.val * E 2.val E.val := E 1.val + E 2.val E.val := id When Parsing is Done Code is Generated E.val := id Introduction:
Chapter 8: Semantic Analyzer5 Syntax Directed Translation: Definition Syntax Tree: S --> if A then S 1 else S 2 ifthenelse A S 1 S 2 Key words do not appear as leaves
Chapter 8: Semantic Analyzer6 Construction Syntax Tree: Implement each node as a class Example: a + b * 3 * +3 ab Constant 3 Entry to symbol table for bEntry to symbol table for a Introduction:
Chapter 8: Semantic Analyzer7 Syntax Directed Definition With each Grammar Symbol, it associates a set of Attributes, and with each Production, a set of Semantic Rules, for computing values of the attributes associated with the symbol appearing in that production. Example 1: Exp --> Exp + Term {Print ‘+’} Exp --> Term Term --> 0|1|2|3|…|9 {Print ‘0’|’1’|…}
Chapter 8: Semantic Analyzer8 Parse: Exp.t := Exp.t := 2+3Term.t := 4 Exp.t := 2 Term.t := Term.t := Example 1:
Chapter 8: Semantic Analyzer9 Grammar for a Robot --> --> Begin --> Up | Down | Left | Right Example 2:
Chapter 8: Semantic Analyzer10 Assume: Up x=1 Down x= -1 Left x= 0 Right x = 0 y= 0 y= 0 y= -1 y= 1 Start (0, 0)(-1, 0) (-1, -1) (0, -1)(1, -1)(2, -1) Input Begin, L, D, R, R, R Example 2:
Chapter 8: Semantic Analyzer11 Start y=2 Start x=-1 Start y=1 Start x=-1 ???? Start y=0 Start x=-1 Dir y=1 Dir x= 0 Start y=-1 Start x=-1 Dir y=1 Dir x=0 Start y=-1 Start x=0 Dir y=0 Dir x=-1 Start y=0 Start x=0 Start y=-1 Start x=0 Begin(0,0) Right Down Left Start.x=0 Start.y=0 Example 2:
Chapter 8: Semantic Analyzer12 --> Start1.x = Dir.x + Start2.x Start1.y = Dir.y + Start2.y Print(Start1.x, Start1.y) --> Begin Start.x = 0 Start.y = 0 Example 2:
Chapter 8: Semantic Analyzer13 --> Up (Down, Left, Right) Dir.x = 0 Dir.y = 1 What do I need to produce the Code? --> (0.0) (-1,0) (-1,-1) (0,-1) (1,-1) (2,-1) --> Begin, Left, Down, Right, Right, Right Example 2:
Chapter 8: Semantic Analyzer14 1. MakeNode (Op, Left, Right) 2. MakeLeafId(id, entry); 3. MakeLeafNum(Num, Val) + Op is a label Left and Right are pointers id Pointer to Symbol Table We need the Following Functions: Num2 Val
Chapter 8: Semantic Analyzer15 Create Syntax Tree For: a+b*3 P 1 – MakeLeafId (a, Entry a) P 2 – MakeLeafId (b, Entry b) P 3 – MakeNode (+, P 1, P 2 ) (Print P 1, + P 2 ) P 4 – MakeLeafNum (Num, 3) P 5 – MakeNode (*, P 3, P 4 ) (Print P 4 = P 3 * P 5 ) Example:
Chapter 8: Semantic Analyzer16 Tree constructed Bottom-Up Every time a production is used, some code is generated When parsing is done, we have the intermediate code Facts:
Chapter 8: Semantic Analyzer17 E--> E1 + T E.Ptr := MakeNode (‘+’, E 1.Ptr, T.Ptr) E --> E1 – T E.Ptr := MakeNode (‘-’, E 1.Ptr, T.Ptr) E --> T E.Ptr := T.Ptr T --> (E) T.Ptr := E.Ptr T --> id T.Ptr := MakeLeafId (id, id.entry) T --> Num T.Ptr := MakeLeafNum (Num, Num.Val) Semantic Rules
Chapter 8: Semantic Analyzer18 Draw the Syntax Directed Tree: E T.Ptr E.Ptr E1E1 + T Num T id T.Ptr + a Num2 a + 2 Semantic Rules
Chapter 8: Semantic Analyzer19 We use Attributed Grammar (AG) notation to describe Syntax Tree Structure Attributed Grammar is a CFG where attributes are associated with each grammar symbol in a production Summary
Chapter 8: Semantic Analyzer20 Attribute: 1. Synthesized Attributes: Information Flows-up from leaves to the root of the tree 2. Inherited Attributes: Information Flows-down to the leaves Summary
Chapter 8: Semantic Analyzer21 L --> E${Print (E.val)} E --> E + T {E.val := E 1.val + T.val} E --> T{E.val := T.val} T --> T*F {T.val := T 1.val * F.val} T --> F {T.val := F.val} F --> (E){F.val := E.val} F --> digit {F.val := Scanner Value} Example of Desk Calculator:
Chapter 8: Semantic Analyzer22 Synthesized Attributes: Parse: 2 * L {Print (10)} E.val :=10 E.val :=6 T.val :=4 T.val :=2 F.val :=3 F.val :=4 F.val :=2 Digit (Lex 3) Digit(Lex 4) Digit (Lex 2) * + T.val :=6
Chapter 8: Semantic Analyzer23 Inherited Attributes: (Example) D --> T L {L.Insert := T.Type} T --> integer{T.Type := integer} T --> Real {T.Type := Real} L --> L 1, id {L 1.Insert := L.Insert} {Addtype(id.Entry,L.Insert)} L --> id {Addtype(id.Entry,L.Insert)}
Chapter 8: Semantic Analyzer24 Parse: integer a, b, c D T.Type := integer L.Insert := integer id integer,, c b a Inherited Attributes: (Example)
Chapter 8: Semantic Analyzer25 Type Checking: 1. Variable Type Checking A: integer; B: boolean; C: Real; A:= C; A[I] := C:= A; 2. Flow of Control Checking Boolean Expression While Exp Do
Chapter 8: Semantic Analyzer26 3. Uniqueness Checking An object must be defined exactly once (Name of the Program) 4. Name Related Checking (Ada, Modula) Procedure Lookup …. End Lookup; Type Checking:
Chapter 8: Semantic Analyzer27 Type Checking (Top-down or Bottom-up) --> ; --> id : {AddSymbolTable (id.entry, T.type)} --> char {T.type := char} --> integer {T.type := integer} --> {T.type := pointer(T 1.type)} --> Array[Num] of {T.type := Array(Num, T 1.type)} 1…Num 0…Num
Chapter 8: Semantic Analyzer28 Type Checking for expression: --> Literal{E.type := char} --> Num{E.type := integer} --> id {E.type := LookupSymbolTable(id.entry)} --> MOD {E.type :=IF E1.type = integer AND E2.type = integer THEN integer ELSE Type_Error} --> {E.type := IF E 1.type = Pointer(t) THEN t ELSE Type_Error Add other Types to this …
Chapter 8: Semantic Analyzer29 Type Checking of Statements: --> id := {S.type := IF id.type = E.type THEN void ELSE Type_Error --> IF THEN {S.type := IF E.type = boolean THEN void ELSE Type_Error} --> WHILE DO {S.type := IF E.type = boolean THEN void ELSE Type_Error} -->, {S.type := IF S 1.type = void AND S 2.type = void THEN void ELSE Type_Error}
Chapter 8: Semantic Analyzer30 Type Checking: 1. Static 2. Dynamic Data Types: 1. Simple Data Type Int Long char 2. Structured Data Type Record Array StructClass Summary: