c07 - CS421 COMPILERS AND INTERPRETERS CS421 Tiger Semantic...

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

View Full Document Right Arrow Icon
CS421 COMPILERS AND INTERPRETERS 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. CS421 COMPILERS AND INTERPRETERS 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 CS421 COMPILERS AND INTERPRETERS 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 !!! CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Semantic Analysis : Page 4 of 24 Symbol Tables (cont’d) • 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 ;
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS421 COMPILERS AND INTERPRETERS Copyright 1994 - 2010 Zhong Shao, Yale University Semantic Analysis : Page 5 of 24 Symbol Table Impl. Hash Table
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 01/06/2012.

Page1 / 6

c07 - CS421 COMPILERS AND INTERPRETERS CS421 Tiger Semantic...

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