COP4020 Programming Languages Semantics Prof. Xin Yuan.

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
Intermediate Code Generation
Chapter 5 Syntax-Directed Translation. Translation of languages guided by context-free grammars. Attach attributes to the grammar symbols. Values of the.
1 Compiler Construction Intermediate Code Generation.
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.
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.
Intermediate Code Generation Professor Yihjia Tsai Tamkang University.
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)
Syntax Directed Translation
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.
Syntax-Directed Translation Context-free grammar with synthesized and/or inherited attributes. The showing of values at nodes of a parse tree is called.
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
Syntax-Directed Translation
CS784 (Prasad)L167AG1 Attribute Grammars Attribute Grammar is a Framework for specifying semantics and enables Modular specification.
1 Lecture 11: Semantic Analysis (Section ) CSCI 431 Programming Languages Fall 2002 A modification of slides developed by Felix Hernandez-Campos.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
Copyright © 2005 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
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.
COP4020 Programming Languages
Topic #5: Translations EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Topic: Syntax Directed Translations
Lesson 11 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS 363 Comparative Programming Languages Semantics.
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.
Chapter 5: Syntax directed translation –Use the grammar to direct the translation The grammar defines the syntax of the input language. Attributes are.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
Review: Syntax directed translation. –Translation is done according to the parse tree. Each production (when used in the parsing) is a sub- structure of.
Overview of Previous Lesson(s) Over View  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values of.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Syntax Directed Definition and Syntax directed Translation
Copyright © 2009 Elsevier Chapter 4 :: Semantic Analysis Programming Language Pragmatics Michael L. Scott.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Chapter 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Semantic Analysis Attribute Grammar. Semantic Analysis  Beyond context free grammar  Is x declared before it is used?  Is x declared but never used?
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
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.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 9 Ahmed Ezzat Semantic Analysis.
Lecture 9 Symbol Table and Attributed Grammars
Semantic analysis Jakub Yaghob
Semantic Analysis Chapter 4.
Context-Sensitive Analysis
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
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
Chapter 5. Syntax-Directed Translation
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
CS 3304 Comparative Languages
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Syntax-Directed Translation Part I
COP4020 Programming Languages
COP4020 Programming Languages
Presentation transcript:

COP4020 Programming Languages Semantics Prof. Xin Yuan

COP4020 Spring /14/2015 Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees

COP4020 Spring /14/2015 Static Semantics Syntax concerns the form of a valid program, while semantics concerns its meaning  Context-free grammars are not powerful enough to describe certain rules, e.g. checking variable declaration with variable use Static semantic rules are enforced by a compiler at compile time  Implemented in semantic analysis phase of the compiler Examples:  Type checking  Identifiers are used in appropriate context  Check subroutine call arguments  Check labels

COP4020 Spring /14/2015 Dynamic Semantics Dynamic semantic rules are enforced by the compiler by generating code to perform the checks at run-time Examples:  Array subscript values are within bounds  Arithmetic errors  Pointers are not dereferenced unless pointing to valid object  A variable is used but hasn't been initialized Some languages (Euclid, Eiffel) allow programmers to add explicit dynamic semantic checks in the form of assertions, e.g. assert denominator not= 0 When a check fails at run time, an exception is raised

COP4020 Spring /14/2015 Attribute Grammars An attribute grammar “connects” syntax with semantics Each grammar production has a semantic rule with actions (e.g. assignments) to modify values of attributes of (non)terminals  A (non)terminal may have any number of attributes  Attributes have values that hold information related to the (non)terminal General form: Semantic rules are used by a compiler to enforce static semantics and/or to produce an abstract syntax tree while parsing tokens  Syntax directed translation. Can also be used to build simple language interpreters productionsemantic rule ::= A.a :=...; B.a :=...; C.a :=...

COP4020 Spring /14/2015 Example Attributed Grammar The val attribute of a (non)terminal holds the subtotal value of the subexpression Nonterminals are indexed in the attribute grammar to distinghuish multiple occurrences of the nonterminal in a production productionsemantic rule ::= + E 1.val := E 2.val + T.val ::= - E 1.val := E 2.val - T.val ::= E.val := T.val ::= * T 1.val := T 2.val * F.val ::= / T 1.val := T 2.val / F.val ::= T.val := F.val ::= - F 1.val := -F 2.val ::= ( )F.val := E.val ::= unsigned_intF.val := unsigned_int.val

COP4020 Spring /14/2015 Decorated Parse Trees A parser produces a parse tree that is decorated with the attribute values Example decorated parse tree of (1+3)*2 with the val attributes

Synthesized and inherited attributes Each grammar production A->a is associated with a set of semantic rules of the form b=f(c1, c2, …, ck)  If b is an attributed associated with A, it is called a synthesized attribute.  If b is an attributed associated with a grammar symbol on the right side of the production, b is called an inherited attribute. COP4020 Spring /14/2015

COP4020 Spring /14/2015 Synthesized Attributes Synthesized attributes of a node hold values that are computed from attribute values of the child nodes in the parse tree and therefore information flows upwards productionsemantic rule ::= + E 1.val := E 2.val + T.val

COP4020 Spring /14/2015 Inherited Attributes Inherted attributes of child nodes are set by the parent node or sibling nodes and therefore information flows downwards production semantic rules D ->T L L.in = T.type T->int T.type = integer T->real T.type = real L->L1, id L1.in = L.in, addtype(id.entry, L.in) L->id addtype(id.entry, L.in) real id1, id2, id3

COP4020 Spring /14/2015 Inherited Attributes Another example productionsemantic rule ::= TT.st := T.val; E.val := TT.val ::= + TT 2.st := TT 1.st + T.val; TT 1.val := TT 2.val ::=  TT.val := TT.st

COP4020 Spring /14/2015 Attribute Flow An attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used) productionsemantic rule ::= TT.st := T.val

COP4020 Spring /14/2015 Attribute Flow An attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used) productionsemantic rule ::= + TT 2.st := TT 1.st + T.val

COP4020 Spring /14/2015 Attribute Flow An attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used) productionsemantic rule ::=  TT.val := TT.st

COP4020 Spring /14/2015 Attribute Flow An attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used) productionsemantic rule ::= + TT 1.val := TT 2.val

COP4020 Spring /14/2015 Attribute Flow An attribute flow algorithm propagates attribute values through the parse tree by traversing the tree according to the set (write) and use (read) dependencies (an attribute must be set before it is used) productionsemantic rule ::= E.val := TT.val

COP4020 Spring /14/2015 S- and L-Attributed Grammars A grammar is called S-attributed if all attributes are synthesized Production semantic rules L ->E n print(E.val) E->E1 + T E.val = E1.val + T.val E->T E.val = T.val T->T1 * F T.val = T1.val * F.val T->F T.val = F.val F->(E) F.val = E.val F->digits F.val = digits.lexval

COP4020 Spring /14/2015 S- and L-Attributed Grammars A grammar is called L-attributed if the parse tree traversal to update attribute values is always left-to-right and depth-first  For a production A -> X1 X2 X3 … Xn The attributes of Xj (1<=j <= n) only depends on  The attributes of X1, X2, …., Xj-1  The inherited attributed of A  Values of inherited attributes must be passed down to children from left to right  Semantic rules can be applied immediately during parsing and parse trees do not need to be kept in memory  This is an essential grammar property for a one-pass compiler An S-attributed grammar is a special case of an L- attributed grammar

COP4020 Spring /14/2015 Example L-Attributed Grammar Implements a calculator productionsemantic rule ::= ::= + ::= - ::=  ::= ::= * ::= / ::=  ::= - ::= ( ) ::= unsigned_int TT.st := T.val; E.val := TT.val TT 2.st := TT 1.st + T.val; TT 1.val := TT 2.val TT 2.st := TT 1.st - T.val; TT 1.val := TT 2.val TT.val := TT.st FT.st := F.val; T.val := FT.val FT 2.st := FT 1.st * F.val; FT 1.val := FT 2.val FT 2.st := FT 1.st / F.val; FT 1.val := FT 2.val FT.val := FT.st F 1.val := -F 2.val F.val := E.val F.val := unsigned_int.val

COP4020 Spring /14/2015 Constructing Abstract Syntax Trees with Attribute Grammars Three operations to create nodes for an AST tree that represents expressions:  mk_bin_op(op, left, right): constructs a new node that contains a binary operator op and AST sub-trees left and right representing the operator’s operands and returns pointer to the new node  mk_un_op(op, node): constructs a new node that contains a unary operator op and sub-tree node representing the operator’s operand and returns pointer to the new node  mk_leaf(value): constructs an AST leaf that contains a value and returns pointer to the new node

Constructing AST with attribute grammar E->E1+ E2 E.ptr = mk_bin_op(‘+’, E1.ptr, E2.ptr); E->E1 – E2 E.ptr = mk_bin_op(‘-’, E1.ptr, E2.ptr); E->E1* E2 E.ptr = mk_bin_op(‘*’, E1.ptr, E2.ptr); E->E1 / E2 E.ptr = mk_bin_op(‘/’, E1.ptr, E2.ptr); E->(E1) E.ptr = E1.ptr E-> -E1 E.ptr = mk_un_op(‘-’, E1.ptr); E->number E.ptr = mk_leaf(number.val); COP4020 Spring /14/2015