Semantic Analysis Mooly Sagiv html://www.cs.tau.ac.il/~msagiv/courses/wcc03.html.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

Semantic Analysis and Symbol Tables
Semantics Static semantics Dynamic semantics attribute grammars
Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
CS7100 (Prasad)L16-7AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
1 Compiler Construction Intermediate Code Generation.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
Recap Mooly Sagiv. Outline Subjects Studied Questions & Answers.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Abstract Syntax Trees Compiler Baojian Hua
Semantic Analysis Professor Yihjia Tsai Tamkang University.
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Implementation in C Chapter 3.
Semantic Analysis Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter 2.2 (Partial) Hashlama 11:00-14:00.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
Semantic analysis Enforce context-dependent language rules that are not reflected in the BNF, e.g.a function must have a return statement. Decorate AST.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Compiler Summary Mooly Sagiv html://
Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter 2.2 (Partial)
Chapter 2 A Simple Compiler
Semantic Analysis Mooly Sagiv html://
Abstract Syntax Mooly Sagiv html://
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
COP4020 Programming Languages
Semantic Analysis (Generating An AST) CS 471 September 26, 2007.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
The TINY sample language and it’s compiler
COMPILERS Symbol Tables hussein suleman uct csc3003s 2007.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Abstract Syntax Mooly Sagiv Schrierber Wed 10:00-12:00 html://
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
CPS 506 Comparative Programming Languages Syntax Specification.
Abstract Syntax Trees Compiler Baojian Hua
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2005.
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
©SoftMoore ConsultingSlide 1 Structure of Compilers.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Parser Generation Tools (Yacc and Bison) CS 471 September 24, 2007.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Constructing Precedence Table
Introduction to Parsing (adapted from CS 164 at Berkeley)
Compiler Lecture 1 CS510.
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CSE401 Introduction to Compiler Construction
COP4020 Programming Languages
Chapter 3 Describing Syntax and Semantics.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Presentation transcript:

Semantic Analysis Mooly Sagiv html://

Outline What is Semantic Analysis Why is it needed? Syntax directed translations/attribute grammar (Chapter 3)

Semantic Analysis The “meaning of the program” Requirements related to the “context” in which a construct occurs Context sensitive requirements - cannot be specified using a context free grammar Requires complicated and unnatural context free grammars Guides subsequent phases

Basic Compiler Phases Source program (string) Fin. Assembly lexical analysis syntax analysis semantic analysis Tokens Abstract syntax tree Front-End Back-End

Example Semantic Condition In C –break statements can only occur inside switch or loop statements

Partial Grammar for C Stm  Exp; Stm  if (Exp) Stm Stm  if (Exp) Stm else Stm Stm  while (Exp) do Stm Stm  break; Stm  {StList } StList  StList Stm StList  

Refined Grammar for C Stm  Exp; Stm  if (Exp) Stm Stm  if (Exp) Stm else Stm Stm  while (Exp) do LStm Stm  {StList } StList  StList Stm StList   LStm  break; LStm  Exp; LStm  if (Exp) LStm LStm  if (Exp) LStm else LStm LStm  while (Exp) do LStm LStm  {LStList } LStList  LStList LStm LStList  

A Possible Abstract Syntax for C typedef struct A_St_ *A_St; struct A_St { enum {A_if, A_while, A_break, A_block,...} kind; A_pos pos; union { struct { A_Exp e; A_St st1; A_St st2; } if_st; struct { A_Exp e; A_St st; } while_st; struct { A_St st1; A_St st2; } block_st;... } u ; } A_St A_IfStm(A_Exp, A_St, A_St); A_St A_WhileStm(A_Exp A_St); A_St A_BreakStm(void); A_St A_BlockStm(A_St, A_St);

stm: IF ‘(‘ exp ‘)’ stm { $$ = A_IfStm($3, $5, NULL) ; } | IF ‘(‘ exp ‘)’ stm ELSE stm { $$ = A_IfStm($3, $5, $7) ; } | WHILE ‘(‘ exp ‘)’ stm { $$ = A_WhileStm($3, $5); } | ‘{‘ stmList ‘}’ { $$ = $2; } | BREAK `;' { $$ = A_BreakStm(); } ; stmList : stmList st { $$ = A_BlockStm($1, $2) ;} | /* empty */ {$$ = NULL ;} Partial Bison Specification

void check_break(A_St st) { switch (st->kind) { case A_if: check_break(st-> u.if_st.st1); check_break(st->u.if_st.st2); break; case A_while: break ; case A_break: error(“Break must be enclosed within a loop”, st->pos); break; case A_block: check_break(st->u.block_st.st1) check_break(st->u.block_st.st2); break; } A Semantic Check (on the abstract syntax tree)

%{static int loop_count = 0 ;%} % stm: exp ‘;’ | IF ‘(‘ exp ‘)’ stm | IF ‘(‘ exp ‘)’ stm ELSE stm | WHILE ‘(‘ exp ‘)’ m stm { loop_count--;} | ‘{‘ stmList ‘}’ | BREAK ‘;’ { if (!loop_count) error(“Break must be enclosed within a loop”, line_count); } ; stmList : stmList st | /* empty */ ; m : /* empty */ { loop_count++ ;} ; Syntax Directed Solution

Problems with Syntax Directed Translations Grammar specification may be tedious (e.g., to achieve LALR(1)) May need to rewrite the grammar to incorporate different semantics Modularity is impossible to achieve Some programming languages allow forward declarations (Algol, ML and Java)

Example Semantic Condition: Scope Rules Variables must be defined within scope Dynamic vs. Static Scope rules Cannot be coded using a context free grammar

Dynamic vs. Static Scope Rules procedure p; var x: integer procedure q ; begin { q } … x … end { q }; procedure r ; var x: integer begin { r } q ; end; { r } begin { p } q ; r ; end { p }

Example Semantic Condition In Pascal Types in assignment must be “compatible”'

Partial Grammar for Pascal Stm  id Assign Exp Exp  IntConst Exp  RealConst Exp  Exp + Exp Exp  Exp -Exp Exp  ( Exp )

Refined Grammar for Pascal Stm  RealId Assign RealExp IntExp  IntConst RealExp  RealConst IntExp  IntExp + IntExp IntExp  IntExp -IntExp IntExp  ( IntExp ) Stm  RealId Assign IntExp Stm  IntExpAssign IntExp IntExp  IntId RealExp  RealExp -RealExp RealExp  ( RealExp ) RealIntExp  RealId RealExp  RealExp + RealExp RealExp  RealExp + IntExp RealExp  IntExp + RealExp RealExp  RealExp -RealExp RealExp  RealExp -IntExp RealExp  IntExp -RealExp

%... stm: id Assign exp {compat_ass(lookup($1), $4) ; } ; exp: exp PLUS exp { compat_op(PLUS, $1, $3); $$ = op_type(PLUS, $1, $3); } | exp MINUS exp { compat_op(MINUS, $1, $3); $$ = op_type(MINUS, $1, $3); } | ID { $$ = lookup($1); } | INCONST { $$= ty_int ; } | REALCONST { $$ = ty_real ;} | ‘(‘ exp ‘)’ { $$ = $2 ; } ; Syntax Directed Solution

Attribute Grammars [Knuth 68] Generalize syntax directed translations Every grammar symbol can have several attributes Every production is associated with evaluation rules –Context rules The order of evaluation is automatically determined –declarative Multiple visits of the abstract syntax tree

stm  id Assign exp {compat_ass(id.type, exp.type) } exp  exp PLUS exp { compat_op(PLUS, exp[1].type,exp[2].type) exp[0].type = op_type(PLUS, exp[1].type, exp[2].type) } exp  exp MINUS exp { compat_op(MINUS, exp[1].type, exp[2].type) exp[0].type = op_type(MINUS, exp[1].type, exp[2].type) } exp  ID { exp.type = lookup(id.repr) } exp  INCONST { exp.type= ty_int ; } exp  REALCONST { exp.type = ty_real ;} exp  ‘(‘ exp ‘)’ { exp[0].type = exp[1].type ; } Attribute Grammar for Types

Example Binary Numbers Z  L Z  L.L L  L B L  B B  0 B  1 Compute the numeric value of Z

Z  L { Z.v = L.v } Z  L.L { Z.v = L[1].v + L[2].v } L  L B { L[0].v = L[1].v + B.v } L  B { L.v = B.v } } B  0 {B.v = 0 } B  1 {B.v = ? }

Z  L { Z.v = L.v } Z  L.L { Z.v = L[1].v + L[2].v } L  L B { L[0].v = L[1].v + B.v } L  B { L.v = B.v } B  0 {B.v = 0 } B  1 {B.v = 2 B.s }

Z  L { Z.v = L.v } Z  L.L { Z.v = L[1].v + L[2].v } L  L B { L[0].v = L[1].v + B.v B.s = L[0].s L[1].s = L[0].s + 1} } L  B { L.v = B.v B.s = L.s } B  0 {B.v = 0 } B  1 {B.v = 2 B.s }

Z  L { Z.v = L.v L.s = 0 } Z  L.L { Z.v = L[1].v + L[2].v L[1].s = 0 L[2].s=? } L  L B { L[0].v = L[1].v + B.v B.s = L[0].s L[1].s = L[0].s + 1} } L  B { L.v = B.v B.s = L.s } B  0 {B.v = 0 } B  1 {B.v = 2 B.s }

Z  L { Z.v = L.v L.s = 0 } Z  L.L { Z.v = L[1].v + L[2].v L[1].s = 0 L[2].s=-L[2].l } L  L B { L[0].v = L[1].v + B.v B.s = L[0].s L[1].s = L[0].s + 1 L[0].l = L[1].l + 1} } L  B { L.v = B.v B.s = L.s L.l = 1 } B  0 {B.v = 0 } B  1 {B.v = 2 B.s }

Z L.L B 1 L B L B B L.l=1 L.l=2 L.l=3 L.s=0 L.s=-3 B.s=0 L.s=-1 B.s=-1 B.s=-2 B.s=-3 B.v=1 L.v=1 B.v=0.5 L.v=0.5 B.v=0 L.v=0.5 B.v=0.125 L.v=0.625 Z.v=1.625 L.s=-2

Summary Several ways to enforce semantic correctness conditions –syntax Regular expressions Context free grammars –syntax directed –traversals on the abstract syntax tree –later compiler phases? –Runtime? There are tools that automatically generate semantic analyzer from specification (Based on attribute grammars)