semantic analysis
play

Semantic Analysis Aslan Askarov aslan@cs.au.dk Partially based on - PowerPoint PPT Presentation

Compilation 2016 Semantic Analysis Aslan Askarov aslan@cs.au.dk Partially based on slides by E. Ernst Where are we Earlier this week: Abstract syntax trees tree representation of a program Scoping rule how to


  1. 
 
 Compilation 2016 Semantic Analysis Aslan Askarov aslan@cs.au.dk 
 Partially based on slides by E. Ernst

  2. Where are we • Earlier this week: • Abstract syntax trees – tree representation of a program • Scoping rule – how to interpret declarations • Environments – implementation infrastructure • Today: • Semantic analysis

  3. Goals of the semantic analysis • Checking that input program is well-typed • i.e., checking that AST produced by the parser adheres to the typing rules of the language. • Generally, after this phase, most errors are reported to the user • Generating typed-AST for later compiler phases

  4. From AST to typed-AST • Typed AST is an abstract syntax tree decorated with type information • we may also throw away source-level position information, because it is strictly speaking not needed from this point on

  5. TigerLight 0 • The language of missing features :) • No loops } ⇒ let declarations only include vars • No functions • No user-def types • types range only over int, string, and unit • No arrays or records • No references • No dangling else • Only basic integer operators over expressions

  6. TigerLight 0 let var x : Int := 10 var s : String := “hi” in if x then s else 42 end Is this program well-typed?

  7. TigerLight 0 >_

  8. Tiger Types structure Types : TYPES = struct type unique = unit ref datatype ty = INT | STRING | RECORD of (Symbol.symbol * ty) list * unique | ARRAY of ty * unique | NIL | UNIT | NAME of Symbol.symbol * ty option ref | ERROR (* ambiguity exists due to type error *) end

  9. Uniqueness: nominal type equivalence • Nominal type equivalence known from Java etc.: 
 class C1 {.. /* some declarations */ ..} 
 class C2 {.. /* identical to C1, rename as needed */ ..} • Even though declarations are identical, these classes are not the same type • Tiger uses nominal type eq. for records and arrays: ‘unit ref’ is always a unique value structure Types : TYPES = struct type unique = unit ref datatype ty = ... | RECORD of (Symbol.symbol * ty) list * unique | ARRAY of ty * unique ... end

  10. ⇒ Uniqueness: Quiz • Q: How else could you implement this? TypeDec TypeDec another AST traversal 'rectype2' 'rectype2' RecordTy RecordTy unique_id 'name' 'dates' 'name' 'dates' 238378 'string' 'arrtype 'string' 1' 'arrtype1' A separate AST traversal to generate unique identifiers for each record/array declaration would be a cleaner implementation approach, but at the cost of writing/maintaining an extra AST pass; so the unit ref hack is okay

  11. 
 Tiger declarations • let function = … 
 type = 
 … • Compared to TigerLight 0 semantic analysis for the full Tiger language needs two environments • variable (and function) environment • type environment • We extend our implementation so transExp and friends take an extra environment argument 
 transExp (venv, tenv) exp

  12. Summary • Semantic analysis: Above CFG, general • Major element: Type-checking: well-formedness, representation, choice of variant, … • From AST to typed AST • Tool: Symbol table (imp./func.), stack discipline • Representation of types, environments • nominal types • Type-checking declarations

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend