COP4020 Programming Languages

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

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.
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.
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
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,
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.
Slide 1 Chapter 3 Attribute Grammars. Slide 2 Attribute Grammars Certain language structures cannot be described using EBNF. Attribute grammars are extensions.
Compiler Summary Mooly Sagiv html://
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.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
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
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
Syntax-Directed Translation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
COP4020 Programming Languages Semantics Prof. Xin Yuan.
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.
Scribe Sumbission Date: 28 th October, 2013 By M. Sudeep Kumar.
Semantic Analysis CPSC 388 Ellen Walker Hiram College.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
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.
C H A P T E R T W O Linking Syntax And Semantics Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
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.
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.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
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.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Semantic analysis Jakub Yaghob
Semantic Analysis Chapter 4.
Context-Sensitive Analysis
A Simple Syntax-Directed Translator
Constructing Precedence Table
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 5 Syntax Directed Translation
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
CS 3304 Comparative Languages
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Chapter 6 Intermediate-Code Generation
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
CS 3304 Comparative Languages
COP4020 Programming Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 4 Action Routines.
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Syntax-Directed Translation Part I
COP4020 Programming Languages
Chapter 5 Syntax Directed Translation
Compiler Construction
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
Presentation transcript:

COP4020 Programming Languages Semantics Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)

Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees 5/31/2019 COP4020 Fall 2006 COP4020 Fall 2006

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 5/31/2019 COP4020 Fall 2006

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 5/31/2019 COP4020 Fall 2006

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 Can also be used to build simple language interpreters production semantic rule <A> ::= <B> <C> A.a := ...; B.a := ...; C.a := ... 5/31/2019 COP4020 Fall 2006

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 production semantic rule <E1> ::= <E2> + <T> E1.val := E2.val + T.val <E1> ::= <E2> - <T> E1.val := E2.val - T.val <E> ::= <T> E.val := T.val <T1> ::= <T2> * <F> T1.val := T2.val * F.val <T1> ::= <T2> / <F> T1.val := T2.val / F.val <T> ::= <F> T.val := F.val <F1> ::= - <F2> F1.val := -F2.val <F> ::= ( <E> ) F.val := E.val <F> ::= unsigned_int F.val := unsigned_int.val 5/31/2019 COP4020 Fall 2006

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 5/31/2019 COP4020 Fall 2006

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 production semantic rule <E1> ::= <E2> + <T> E1.val := E2.val + T.val 5/31/2019 COP4020 Fall 2006

Inherited Attributes Inherted attributes of child nodes are set by the parent node and therefore information flows downwards production semantic rule <E> ::= <T> <TT> TT.st := T.val; E.val := TT.val <TT1> ::= + <T> <TT2> TT2.st := TT1.st + T.val; TT1.val := TT2.val <TT> ::=  TT.val := TT.st 5/31/2019 COP4020 Fall 2006

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) production semantic rule <E> ::= <T> <TT> TT.st := T.val 5/31/2019 COP4020 Fall 2006

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) production semantic rule <TT1> ::= + <T> <TT2> TT2.st := TT1.st + T.val 5/31/2019 COP4020 Fall 2006

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) production semantic rule <TT> ::=  TT.val := TT.st 5/31/2019 COP4020 Fall 2006

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) production semantic rule <TT1> ::= + <T> <TT2> TT1.val := TT2.val 5/31/2019 COP4020 Fall 2006

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) production semantic rule <E> ::= <T> <TT> E.val := TT.val 5/31/2019 COP4020 Fall 2006

S- and L-Attributed Grammars A grammar is called S-attributed if all attributes are synthesized A grammar is called L-attributed if the parse tree traversal to update attribute values is always left-to-right and depth-first Synthesized attributes always OK 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 5/31/2019 COP4020 Fall 2006

Example L-Attributed Grammar Implements a calculator production semantic rule <E> ::= <T> <TT> <TT1> ::= + <T> <TT2> <TT1> ::= - <T> <TT2> <TT> ::=  <T> ::= <F> <FT> <FT1> ::= * <F> <FT2> <FT1> ::= / <F> <FT2> <FT> ::=  <F1> ::= - <F2> <F> ::= ( <E> ) <F> ::= unsigned_int TT.st := T.val; E.val := TT.val TT2.st := TT1.st + T.val; TT1.val := TT2.val TT2.st := TT1.st - T.val; TT1.val := TT2.val TT.val := TT.st FT.st := F.val; T.val := FT.val FT2.st := FT1.st * F.val; FT1.val := FT2.val FT2.st := FT1.st / F.val; FT1.val := FT2.val FT.val := FT.st F1.val := -F2.val F.val := E.val F.val := unsigned_int.val 5/31/2019 COP4020 Fall 2006

Example Decorated Parse Tree Fully decorated parse tree of (1+3)*2 5/31/2019 COP4020 Fall 2006

Recursive Descent Parsing with L-Attributed Grammars Semantic rules are added to the bodies of the recursive descent functions and placed appropriately between the function calls Inherited attribute values are input arguments to the functions Argument passing flows downwards in call graphs Synthesized attribute values are returned by functions Return values flow upwards in call graphs 5/31/2019 COP4020 Fall 2006

Example production semantic rule <E> ::= <T> <TT> <TT1> ::= + <T> <TT2> <TT1> ::= - <T> <TT2> <TT> ::=  TT.st := T.val; E.val := TT.val TT2.st := TT1.st + T.val; TT1.val := TT2.val TT2.st := TT1.st - T.val; TT1.val := TT2.val TT.val := TT.st procedure E() Tval = T(); Eval = TT(Tval); return Eval; procedure TT(TTst) case (input_token()) of '+': match('+'); TTval = TT(TTst + Tval); of '-': match('-'); TTval = TT(TTst - Tval); otherwise: TTval = TTst; return TTval; 5/31/2019 COP4020 Fall 2006

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 5/31/2019 COP4020 Fall 2006

An L-Attributed Grammar to Construct ASTs Semantic rules to build up an AST production semantic rule <E> ::= <T> <TT> <TT1> ::= + <T> <TT2> <TT1> ::= - <T> <TT2> <TT> ::=  <T> ::= <F> <FT> <FT1> ::= * <F> <FT2> <FT1> ::= / <F> <FT2> <FT> ::=  <F1> ::= - <F2> <F> ::= ( <E> ) <F> ::= unsigned_int TT.st := T.ptr; E.ptr := TT.ptr TT2.st := mk_bin_op("+", TT1.st, T.ptr); TT1.ptr := TT2.ptr TT2.st := mk_bin_op("-", TT1.st, T.ptr); TT1.ptr := TT2.ptr TT.ptr := TT.st FT.st := F.ptr; T.ptr := FT.ptr FT2.st := mk_bin_op("*", FT1.st, F.ptr); FT1.ptr := FT2.ptr FT2.st := mk_bin_op("/", FT1.st, F.ptr); FT1.ptr := FT2.ptr FT.ptr := FT.st F1.ptr := mk_un_op("-", F2.ptr) F.ptr := E.ptr F.ptr := mk_leaf(unsigned_int.val) 5/31/2019 COP4020 Fall 2006

Example Decorated Parse Tree with AST Decorated parse tree of (1+3)*2 with AST 5/31/2019 COP4020 Fall 2006