Topic 5 -Semantic Analysis Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Semantic Analysis Chapter 4. Role of Semantic Analysis Following parsing, the next two phases of the "typical" compiler are – semantic analysis – (intermediate)
1 Error detection in LR parsing Errors are discovered when a slot in the action table is blank. Canonical LR(1) parsers detect and report the error as.
CSE 425: Semantic Analysis Semantic Analysis Allows rigorous specification of a program’s meaning –Lets (parts of) programming languages be proven correct.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
1 Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 12: Semantic Analysis COMP 144 Programming Language Concepts Spring 2002 Felix.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
FE.1 CSE4100 Final Exam Advice and Hints Prof. Steven A. Demurjian, Sr. Computer Science & Engineering Department The University of Connecticut 191 Auditorium.
Parsing III (Eliminating left recursion, recursive descent parsing)
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Context-sensitive Analysis, II Ad-hoc syntax-directed translation, Symbol Tables, andTypes.
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
ISBN Chapter 3 Describing Syntax and Semantics.
Bottom-up parsing Goal of parser : build a derivation
1 Lecture 11: Semantic Analysis (Section ) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos.
Copyright © 2005 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
3-1 Chapter 3: Describing Syntax and Semantics Introduction Terminology Formal Methods of Describing Syntax Attribute Grammars – Static Semantics Describing.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
TextBook Concepts of Programming Languages, Robert W. Sebesta, (10th edition), Addison-Wesley Publishing Company CSCI18 - Concepts of Programming languages.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
CS 363 Comparative Programming Languages Semantics.
1 Lecture 5: Syntax Analysis (Section 2.2) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos at UNC.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
Copyright © 2006 Addison-Wesley. All rights reserved. Ambiguity in Grammars A grammar is ambiguous if and only if it generates a sentential form that has.
Introduction to Compiling
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.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Eliminating Left-Recursion Where some of a nonterminal’s productions are left-recursive, top-down parsing is not possible “Immediate” left-recursion can.
PZ03CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03CX - Language semantics Programming Language Design.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Announcements/Reading
Describing Syntax and Semantics
Context-Sensitive Analysis
Programming Languages Translator
Table-driven parsing Parsing performed by a finite state machine.
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
Compiler Lecture 1 CS510.
CS 3304 Comparative Languages
Lexical and Syntax Analysis
Syntax-Directed Translation
CPSC 388 – Compiler Design and Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Subject: Language Processor
Chapter 4 Action Routines.
SYNTAX DIRECTED DEFINITION
Kanat Bolazar February 16, 2010
Presentation transcript:

Topic 5 -Semantic Analysis Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems Concepts Fall 2002 Monday Wednesday 2:30-3:50 LI 99

Introduction to Semantic Analysis  Semantics are what a program means  Decorating the parse tree/syntax tree is done to generate code and analyze semantics.  Actions are inserted into the parser to generate the semantic analysis when language constructs are recognized.  Analysis means  Determining correct behavior  Detecting Dangerous or Nonsensical Behavior

When are Semantics Analyzed?  Semantic analysis done at compile time and run time  Can be done at most binding times.  Compilers split into front end/back end  Back end does semantic analysis  Front end does syntax analysis

Some notes on correctness  Semantic errors are hard to catch at compile time  Late binding makes static analysis hard  Correct programs can be derived  But this is hard and typically done for short heavily used code  In practice, combine static analysis and run time checking to help the programmer.

So, Why does my software crash?  Engineers know how to make working machines, why does software crash?  Software is discrete.  Failure not gradual or but discontinuous.  Analysis/understanding is hard  Bad Tools, Bad Techniques or Bad Paradigm?  Like doing calculus with roman numerals  Cost and performance trade offs  Last to market loses, Slow performance loses.

The Role of Testing and Verification  Verification - Derive correct solution  Useful but hard, expensive and not feasible for large pieces of software.  Testing - Checks for the presence of errors.  But cannot rule out errors.  Cheaper, and practical.  Catching errors late increases repair costs.

Assertions, Invariants, Pre/Post Conditions  Hardware provides some runtime checks  Languages can support testing:  Assertion - A condition which must be true at a particular point during execution.  Invariant -A condition which must hold true at all "clean points" of execution  Precondition - True before a statement  Postcondition - Ture after a statement

Attribute Grammars 1  Recall that context free grammars specify programming language syntax.  An Attribute grammar (AG) extends the CFG to specify semantics of the language.  Each nonterminal is associated with a set of attributes (semantic information)  Each production has a set of semantic rules.  Terminals can supply needed information.

Attribute Grammars 2  Semantic Rules are either:  Copy rules -Copy the RHS Attribute to LHS  Semantic Functions -Perform some function on the RHS attributes.  The Semantic Rules are simple  Refer to information available in the RHS of the production.  Avoid using non-local information

Attribute Grammars 3  Closely related to:  Denotational Semantics -Focus on machine independent details.  Axiomatic Semantics - Focuses on verification/theorem proving.

Attribute Grammars An Example

Attribute Tree Construction  CFGs and Attribute Grammars do not specify:  Shape of the parse/attribute tree  Order of parse/attribute tree construction  Attribute Trees can be constructed:  On the fly - At the same time as parse tree  After the parse tree is built (in another pass)

Attribute Trees  Attribute Trees are annotated parse trees  Attributes flow between nodes  Values initialized by leaf nodes  Synthesized Attributes are computed only in productions where the nonterminal appears on the RHS  S-Attributed grammars synthesize all their attributes.  Scanner gives initial attributes for tokens

Attribute Flow  Attribute Trees are annotated parse trees  Attribute flow is the pattern of information movement in the attribute grammar tree.  Synthesized Attributes are computed in productions with the nonterminal on the LHS  S-Attributed grammars synthesize all their attributes.  Inherited attributes are calculated when a symbol is on the RHS of a production.

Attribute Flow LR Example  LR Parsers have  Rightmost Trees  Bottom-Up Construction  Attributes Flow Along Parse Tree Edges.  In Bottom-Up Direction  Can Augment Parser Stack  E.g. AG for (3 + 1) * 2

Attribute Flow LR Example  Attributes initialized by scanner.  Transitive closure of copied pointers makes attributes available when evaluated.

Attribute Flow in LL Grammars  Notice that some productions have multiple rules.  TT and FT use an extra "subtotal" attribute.  If a symbol is on LHS and RHS add a subscript.

Attribute Flow in LL Grammars  Minor errata, the RHS in expression 3: should be  TT 1 } - T TT 2

Attribute Flow in LL Grammars  LL Grammars need to work with  Leftmost parse tree derivations  Top to bottom  L-Attributed grammars do as follows:  Each LHS symbol's attributes depend on  The symbol's own attributes  Attributes of a symbol on the RHS  Each inherited attribute depends only on inherited attributes of the LHS grammars.

Attribute Flow in LL Grammars

So Why are Attribute Flows Important?  A translation scheme is an algorithm that invokes the attributes of a parse tree in an order consistent with its attribute flow.  This is how compilers generate code!  One Pass compilers can evaluate attributes during parse tree construction using a stack.  LR Parsers can mirror or augment the parse stack  Since The Attribute Flow Mimics the parse tree  LL Parsers need a separate attribute stack.

Some Notation  An AG is well-defined if every parse tree has a unique set of attributes.  An AG is noncircular if no attribute ever depends (transitively) on itself.

Action Routines  Action routines realize the AG:  What Semantic Rule to apply  When (during what phase of parsing)  Can be after parsing the entire RHS (typical)  Can be in the middle of parsing the RHS  Sometimes convenient  Does LL differ from LR?  Yes, LL can evaluate any time, with LR you may get conflicts (when to shift/reduce?)

An LL Action Routine Example  Given the same LL AG as earlier  Insert action routines  Predictive nature of LL allows actions at arbitrary times

YACC and Bison Handling of Attributes 1  YACC and BISON are LALR(1) parsers  Attributes are managed with a stack  Augment the parse stack  To access attributes in a production  The attributes on the LHS is denoted $$  The symbol's attributes on the RHS are denoted $1, $2,..., $n where $i is the ith term from the left

YACC and Bison Semantic Values  Symbol attributes are pushed on the stack  Want to change the stack's element type.  To select a stack element type:  For single type stacks, define YYTYPE:  #define YYTYPE typename // A macro  For multi-type stacks, use %union  Each field in the union becomes is a type  Access them using say $ 1

YACC and Bison Semantic Values  Attribute initialization:  Synthesized Attributes are assigned, using a statement like:  $$ = f($1, $2,..., $n); /* n = |RHS|, f is a function */  Sometimes Inherited Attributes are useful  e.g. A grammar has productions:  Decl } Type Id_List; and, Id_List } Id, Id_List | Id;  So need to set an Id's type need a statement like  $1.type = $$.type; $2.type = $$.type

YACC and Bison Semantic Values  Symbol attributes are pushed on the stack  Want to change the stack's element type.  To select a stack element type:  For single type stacks, define YYTYPE:  #define YYTYPE typename // A macro  For multi-type stacks, use %union  Each field in the union becomes is a type