COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Similar documents
COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

Static and Dynamic Semantics

Chapter 4. Action Routines

Programming Languages

Chapter 4 :: Semantic Analysis

Semantic Analysis. Role of Semantic Analysis

Abstract Syntax Trees Synthetic and Inherited Attributes

Syntax-Directed Translation Part I

Syntax-Directed Translation

COP5621 Exam 3 - Spring 2005

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

COP4020 Spring 2011 Midterm Exam

[Syntax Directed Translation] Bikash Balami

Lecture 14 Sections Mon, Mar 2, 2009

We now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a

Abstract Syntax Tree

Syntax-Directed Translation Part II

Semantic Analysis Attribute Grammars

A programming language requires two major definitions A simple one pass compiler

Syntax-Directed Translation. Introduction

Summary: Semantic Analysis

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

LECTURE 3. Compiler Phases

CSE302: Compiler Design

A Simple Syntax-Directed Translator

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Principles of Programming Languages

5. Syntax-Directed Definitions & Type Analysis

Compiler Principle and Technology. Prof. Dongming LU April 15th, 2019

Syntax-Directed Translation

Syntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions

10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG

Syntax Directed Translation

COMPILER CONSTRUCTION (Evaluation Orders for SDD s)

Chapter 4 - Semantic Analysis. June 2, 2015

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

Syntax-Directed Translation

More On Syntax Directed Translation

Syntax Directed Translation

Compilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1

Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.

CS 406: Syntax Directed Translation

Syntax-Directed Translation

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Abstract Syntax Trees & Top-Down Parsing

Syntactic Directed Translation

Principles of Programming Languages

Examples of attributes: values of evaluated subtrees, type information, source file coordinates,

ChAmElEoN Parse Tree

UNIT-3. (if we were doing an infix to postfix translator) Figure: conceptual view of syntax directed translation.

Intermediate Code Generation

Recursive Descent Parsers

UNIT IV INTERMEDIATE CODE GENERATION

Semantic actions for expressions

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

CSE 12 Abstract Syntax Trees

Semantic analysis. Syntax tree is decorated with typing- and other context dependent

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Syntax Analysis Part I

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

CMPT 379 Compilers. Parse trees

More Assigned Reading and Exercises on Syntax (for Exam 2)

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Compilers. Compiler Construction Tutorial The Front-end

Semantic Analysis. Compiler Architecture

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Semantic actions for declarations and expressions

Program Assignment 2 Due date: 10/20 12:30pm

CSC 467 Lecture 13-14: Semantic Analysis

Question Points Score

Error Handling Syntax-Directed Translation Recursive Descent Parsing

Semantic actions for declarations and expressions. Monday, September 28, 15

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

Error Handling Syntax-Directed Translation Recursive Descent Parsing. Lecture 6

Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

MIDTERM REVIEW. Lectures 1-15

COMPILER DESIGN. Syntax-Directed Translation. COMP 442/6421 Compiler Design. Department of Computer Science and Software Engineering

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

22c:111 Programming Language Concepts. Fall Syntax III

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Syntax Directed Translation

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos

BSCS Fall Mid Term Examination December 2012

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

9/5/17. The Design and Implementation of Programming Languages. Compilation. Interpretation. Compilation vs. Interpretation. Hybrid Implementation

Pioneering Compiler Design

Semantic Analysis and Intermediate Code Generation

1 Lexical Considerations

Syntax-Directed Translation. Lecture 14

Principles of Programming Languages COMP251: Syntax and Grammars

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Time : 1 Hour Max Marks : 30

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

Working of the Compilers

Transcription:

COP4020 Programming Languages Semantics Prof. Robert van Engelen

Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees COP4020 Spring 2011 2

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 2011 3

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 2011 4

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: <A> ::= <B> <C> A.a :=...; B.a :=...; C.a :=... 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 COP4020 Spring 2011 5

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 <E 1 > ::= <E 2 > + <T> E 1.val := E 2.val + T.val <E 1 > ::= <E 2 > - <T> E 1.val := E 2.val - T.val <E> ::= <T> E.val := T.val <T 1 > ::= <T 2 > * <F> T 1.val := T 2.val * F.val <T 1 > ::= <T 2 > / <F> T 1.val := T 2.val / F.val <T> ::= <F> T.val := F.val <F 1 > ::= - <F 2 > F 1.val := -F 2.val <F> ::= ( <E> ) F.val := E.val <F> ::= unsigned_int F.val := unsigned_int.val COP4020 Spring 2011 6

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 COP4020 Spring 2011 7

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 <E 1 > ::= <E 2 > + <T> E 1.val := E 2.val + T.val COP4020 Spring 2011 8

Inherited Attributes Inherted attributes of child nodes are set by the parent node and therefore information flows downwards <E> ::= <T> <TT> TT.st := T.val; E.val := TT.val <TT 1 > ::= + <T> <TT 2 > TT 2.st := TT 1.st + T.val; TT 1.val := TT 2.val <TT> ::= ε TT.val := TT.st COP4020 Spring 2011 9

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) <E> ::= <T> <TT> TT.st := T.val COP4020 Spring 2011 10

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) <TT 1 > ::= + <T> <TT 2 > TT 2.st := TT 1.st + T.val COP4020 Spring 2011 11

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) <TT> ::= ε TT.val := TT.st COP4020 Spring 2011 12

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) <TT 1 > ::= + <T> <TT 2 > TT 1.val := TT 2.val COP4020 Spring 2011 13

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) <E> ::= <T> <TT> E.val := TT.val COP4020 Spring 2011 14

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 COP4020 Spring 2011 15

Example L-Attributed Grammar Implements a calculator <E> ::= <T> <TT> TT.st := T.val; E.val := TT.val <TT 1 > ::= + <T> <TT 2 > TT 2.st := TT 1.st + T.val; TT 1.val := TT 2.val <TT 1 > ::= - <T> <TT 2 > TT 2.st := TT 1.st - T.val; TT 1.val := TT 2.val <TT> ::= ε TT.val := TT.st <T> ::= <F> <FT> FT.st := F.val; T.val := FT.val <FT 1 > ::= * <F> <FT 2 > <FT 1 > ::= / <F> <FT 2 > 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> ::= ε FT.val := FT.st <F 1 > ::= - <F 2 > F 1.val := -F 2.val <F> ::= ( <E> ) F.val := E.val <F> ::= unsigned_int F.val := unsigned_int.val COP4020 Spring 2011 16

Example Decorated Parse Tree Fully decorated parse tree of (1+3)*2 COP4020 Spring 2011 17

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 COP4020 Spring 2011 18

Example <E> ::= <T> <TT> TT.st := T.val; E.val := TT.val <TT 1 > ::= + <T> <TT 2 > TT 2.st := TT 1.st + T.val; TT 1.val := TT 2.val <TT 1 > ::= - <T> <TT 2 > TT 2.st := TT 1.st - T.val; TT 1.val := TT 2.val <TT> ::= ε TT.val := TT.st procedure E() Tval = T(); Eval = TT(Tval); return Eval; procedure TT(TTst) case (input_token()) of '+': match('+'); Tval = T(); TTval = TT(TTst + Tval); of '-': match('-'); Tval = T(); TTval = TT(TTst - Tval); otherwise: TTval = TTst; return TTval; COP4020 Spring 2011 19

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 COP4020 Spring 2011 20

An L-Attributed Grammar to Construct ASTs Semantic rules to build up an AST <E> ::= <T> <TT> TT.st := T.ptr; E.ptr := TT.ptr <TT 1 > ::= + <T> <TT 2 > TT2.st := mk_bin_op( +, TT 1.st, T.ptr); TT 1.ptr := TT 2.ptr <TT 1 > ::= - <T> <TT 2 > TT2.st := mk_bin_op( -, TT 1.st, T.ptr); TT 1.ptr := TT 2.ptr <TT> ::= ε TT.ptr := TT.st <T> ::= <F> <FT> FT.st := F.ptr; T.ptr := FT.ptr <FT 1 > ::= * <F> <FT 2 > FT 2.st := mk_bin_op( *, FT 1.st, F.ptr); FT 1.ptr := FT 2.ptr <FT 1 > ::= / <F> <FT 2 > FT 2.st := mk_bin_op( /, FT 1.st, F.ptr); FT 1.ptr := FT 2.ptr <FT> ::= ε FT.ptr := FT.st <F 1 > ::= - <F 2 > F 1.ptr := mk_un_op( -, F 2.ptr) <F> ::= ( <E> ) F.ptr := E.ptr <F> ::= unsigned_int F.ptr := mk_leaf(unsigned_int.val) COP4020 Spring 2011 21

Example Decorated Parse Tree with AST Decorated parse tree of (1+3)*2 with AST COP4020 Spring 2011 22