c07 - C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University Semantic Analysis : Page 1 of 24 Tiger Semantic Analysis token get next token lexical analyzer parser Tiger source program absyn correct absyn; then intermediate trees semantic analysis report all lexical errors report all syntactic errors report all semantic errors construct variable definitions to their uses checks that each expression has a correct type translates the abstract syntax into a simpler intermediate representation suitable for generating machine code. C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University Semantic Analysis : Page 2 of 24 Connecting Definition and Use ? Make sure each variable is defined ; Check the type consistency ! Solution: use a symbol table --- traverse the abstract syntax tree in certain order while maintaining a (variable -> type) symbol table. ....... function f(v : int) = let var v := 6 function g(x : int) = (print (x+v); print \n) function h(v : int) = (print v; print \n) in g v; let var v := 8 in print v end ; h v; end C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University Semantic Analysis : Page 3 of 24 Symbol Tables Conceptually, a symbol table (also called environment ) is a set of (name , attribute ) pairs. Typical Names: strings, e.g., foo, do_nothing1, ... Typical Attributes (also called bindings): type identifier type (e.g., int , string ) variable identifier type ; access info. or value function identifier arg. & result type; access info. or ... Main Issues --- for a symbol table T Given an identifier name, how to look up its attribute in T ? How to insert or delete a pair of new (id, attr) into the table T? Efficiency is important !!! C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University Semantic Analysis : Page 4 of 24 Symbol Tables (contd) How to deal with visibility (i.e., lexical scoping under nested block structure) ? ....... function f(v : int) = let var v := 6 function g(x : int) = (print (x+v); ...) function h(v : int) = (print v; ...) in g v; let var v := 8 in print v end ; h v; end .......... v 1 v 2 v 3 v 4 Initial Table T insert v 1 ; insert v 2 ; lookup sees v 2 insert v 3 ; lookup sees v 3 MUST delete v 3 ; lookup sees v 2 insert v 4 ; lookup sees v 4 MUST delete v 4 ; lookup sees v 2 MUST delete v 2 ; C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E R S Copyright 1994 - 2010 Zhong Shao, Yale University Semantic Analysis : Page 5 of 24 Symbol Table Impl....
View Full Document

This document was uploaded on 02/20/2012.

Page1 / 6

c07 - C S 4 2 1 C O M P I L E R S A N D I N T E R P R E T E...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online