Semantic Analysis CPSC 388 Ellen Walker Hiram College.

Slides:



Advertisements
Similar presentations
CPSC 388 – Compiler Design and Construction
Advertisements

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.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
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.
Compiler Principle and Technology Prof. Dongming LU Mar. 28th, 2014.
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,
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
1 CMPSC 160 Translation of Programming Languages Fall 2002 Lecture-Modules slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Department.
Abstract Syntax Tree (AST)
Attribute Grammars Recall the yacc program Parse a given expression
Context-sensitive Analysis. Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { … } fee() { int f[3],g[0],
Topic 5 -Semantic Analysis Dr. William A. Maniatty Assistant Prof. Dept. of Computer Science University At Albany CSI 511 Programming Languages and Systems.
Chapter 5 Intermediate Code Generation. Chapter 5 -- Intermediate Code Generation2  Let us see where we are now.  We have tokenized the program and.
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax & Semantic Introduction Organization of Language Description Abstract Syntax Formal Syntax The Way of Writing Grammars Formal Semantic.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
CSc 453 Semantic Analysis Saumya Debray The University of Arizona Tucson.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Semantic Analysis1 Checking what parsers cannot.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
Syntax Directed Translation. Tokens Parser Semantic checking TAC Peephole, pipeline, …… TAC  assembly code/mc Cmm subexpression,……
COMPILER CONSTRUCTION
410/510 1 of 18 Week 5 – Lecture 1 Semantic Analysis Compiler Construction.
Overview of Previous Lesson(s) Over View  An ambiguous grammar which fails to be LR and thus is not in any of the classes of grammars i.e SLR, LALR.
Introduction to Code Generation and Intermediate Representations
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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 CPSC 388 Ellen Walker Hiram College.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
More LR Parsing and Bison CPSC 388 Ellen Walker Hiram College.
Compiler Principle and Technology Prof. Dongming LU Apr. 15th, 2015.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture Ahmed Ezzat.
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.
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
Lecture 9 Symbol Table and Attributed Grammars
Semantic analysis Jakub Yaghob
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Constructing Precedence Table
Chapter 5 Syntax Directed Translation
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
Syntax-Directed Translation Part I
Semantic Analysis Chapter 6.
CS 3304 Comparative Languages
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Context-sensitive Analysis
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
COP4020 Programming Languages
COP4020 Programming Languages
Chapter 5 Syntax Directed Translation
Presentation transcript:

Semantic Analysis CPSC 388 Ellen Walker Hiram College

Syntax vs. Semantics Syntax: completely described by CFG –Correct keywords, matched parentheses, etc. Semantics: everything else –Variables, functions declared before use –Type checking –Function parameter checking

Attribute Grammar A way to represent semantics Good when syntax drives semantics Rules that express relation of semantics to syntax –E.g. Number-> digitnumber.val = digit.val –Look familiar??

Attributes Each non-terminal can have multiple attributes (X has X.a, X.b, X.c etc) Attributes can include –Data type of variable –Value of expression –Location of variable or function in memory –Size of object (e.g. max length of array)

Vocabulary Attribute: –Property of an object / nonterminal Attribute grammar –Rules that compute attributes (parallel to grammar rules) Binding –Associating an attribute/value to an object

Static vs. Dynamic Binding Static = compile-time –Variable type –Constant value –Array max size Dynamic = run-time –Variable value –Array contents

Syntax-Directed Semantics Attribute grammar rules for integers D -> 0D.val=0 D->1D.val=1 N 0 -> N 1 DN 0.val = N 1.val*10+D.val Subscript when element appears more than once in a rule Result: parse tree “decorated” with attribute values

Type Computation Attribute “dtype” = data type Decl -> Type VsVs.dtype = Type.dtype Vs 1 -> id, Vs 2 id.dtype = Vs.dtype Vs -> idid.dtype = Vs.dtype

Multiple Attributes D -> 0N.val=0 N.type= int D->1N.val=1 N.type = int N 0 -> N 1 DN 0.val = N 1.val*10+D.val N 0.type = int F = N. F.type = double F 0 = F 1 DF 0.val = (10*F 1.val+D)/10

Computation in rules Control constructs & computations allowed in rules, e.g. –B-int -> int Baseint.base=Base.base –Base -> oBase.base = 8 –Base ->  Base.base = 10 –D -> 0digit.val = 0 –D -> 8if D.base > 8 then 8 else error

Dependency Rules You cannot compute an equation unless its pieces have already been computed E.g. N 0.val = N 1.val*10+D.val –N 0.val depends on N 1.val and –N 0.val depends on D.val Dependency tree: parent is left side, children are items from right side Item used in “if” is also a child

Drawing Dependency Graphs Often superimposed on parse tree Multiple colors for –Parse tree –Each attribute’s dependencies Example, p. 273

Dependency Graph Example basecharbase digit valbase basenum val digit valbase numvalbase numvalbase numvalbase

Dependency Graph Example basecharbase digit valbase basenum val digit valbase numvalbase numvalbase numvalbase

Dependency Graph Example basecharbase digit valbase basenum val digit valbasenumvalbasenumvalbasenumvalbase

Dependency Graph -> Evaluation Order Standard algorithm: Topological Sort –Mark all nodes without parents (in any order), then remove them from the graph –Repeat until no items are left Requires links from child to parent Cycles not allowed in dependency graph (Directed Acyclic Graph - DAG)

Attribute Computation Algorithm Generate parse tree (using TD or BU parsing techniques) Generate dependencies (using parse tree and attribute rules) Perform topological sort on dependency graph Evaluate attribute rules

Rule-based method Fixed evaluation order of attribute rules (independent of parse tree) Hard-coded into compiler (applied to parse tree at compile-time) Requires strongly non-circular attribute grammar –There is no parse tree for which this grammar can have a cycle

Attributes in YACC “Rules” can compute attributes (stored on separate stack; $$ pushed each time) Intermediate steps allowed, e.g. –A : b {compute} c d {compute more} Inherited attributes need external structures (globals) to handle

Attribute Propagation Synthesized attributes –Child -> parent dependencies only A->BCA.val = f(B.val, C.val) Inherited attributes –Parent->child dependencies A-> BCB.val = A.val –sibling->sibling dependencies A->BCC.val = B.val

Order of Evaluation Synthesized attributes: –Compute all children before their parents Post-order traversal of the parse tree Inherited attributes: –Compute parents before children –Make sure siblings are in the right order (parents can be in between) Pre-order or mixed pre-order/in-order traversal

Example: Traversal Orders Preorder = P, C1, C2 –A B D C E F Postorder = C1, C2, P –D B E F C A Inorder = C1, P, C2 –D B A E C F

Computing Attributes During Parsing Simplifies parser / semantic analyzer into one stage Requires no right-to-left inherited attribute –E.g. based-num cannot be done this way

3 ways to compute attributes 1. Compute syntax tree and dependency tree, sort and evaluate 2. Determine evaluation order in advance, apply to parse tree 3. Add computation directly to parse (as in Bison)