Semantic Analysis Attribute Grammars

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

Syntax-Directed Translation Part I

Summary: Semantic Analysis

Syntax-Directed Translation Part II

[Syntax Directed Translation] Bikash Balami

COP5621 Exam 3 - Spring 2005

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

Syntax-Directed Translation

Lecture 14 Sections Mon, Mar 2, 2009

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

Principles of Programming Languages

Principles of Programming Languages

Syntax-Directed Translation. Introduction

Abstract Syntax Trees Synthetic and Inherited Attributes

Syntax-Directed Translation

Syntax-Directed Translation

Syntax-Directed Translation

Semantic Analysis with Attribute Grammars Part 3

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

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

Context-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis

Programming Languages

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

Abstract Syntax Tree

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

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

CS 314 Principles of Programming Languages

Chapter 4. Action Routines

5. Syntax-Directed Definitions & Type Analysis

Syntax Directed Translation

Static and Dynamic Semantics

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

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

More On Syntax Directed Translation

Semantic Analysis. Role of Semantic Analysis

Chapter 4 :: Semantic Analysis

COP4020 Spring 2011 Midterm Exam

Syntax Directed Translation

CSE302: Compiler Design

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

Syntax-Directed Translation

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

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

Defining syntax using CFGs

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Chapter 3 (part 3) Describing Syntax and Semantics

Chapter 2: Syntax Directed Translation and YACC

Compiler Design (40-414)

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

4. Semantic Processing and Attributed Grammars

Topic 3: Syntax Analysis I

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

Last Time. What do we want? When do we want it? An AST. Now!

LECTURE 11. Semantic Analysis and Yacc

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

LR Parsing LALR Parser Generators

Context-sensitive Analysis. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved.

Compiler Lab. Introduction to tools Lex and Yacc

TDDD55 - Compilers and Interpreters Lesson 3

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

Compiler principles, PS1

COMP 181. Prelude. Prelude. Summary of parsing. A Hierarchy of Grammar Classes. More power? Syntax-directed translation. Analysis

Compiler Design Overview. Compiler Design 1

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

TDDD55- Compilers and Interpreters Lesson 3

PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309

CS A331 Programming Language Concepts

Software II: Principles of Programming Languages

Parsing II Top-down parsing. Comp 412

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Describing Syntax and Semantics

Abstract Syntax Trees & Top-Down Parsing

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

Downloaded from Page 1. LR Parsing

1. (a) What are the closure properties of Regular sets? Explain. (b) Briefly explain the logical phases of a compiler model. [8+8]

Semantic Analysis. Lecture 9. February 7, 2018

Chapter 4 - Semantic Analysis. June 2, 2015

CSCI312 Principles of Programming Languages

Syntax Analysis. Chapter 4

Syntactic Directed Translation

Syntax Analysis Part I

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

4. Lexical and Syntax Analysis

Last time. What are compilers? Phases of a compiler. Scanner. Parser. Semantic Routines. Optimizer. Code Generation. Sunday, August 29, 2010

Syntax-Directed Translation. Lecture 14

CMSC 330: Organization of Programming Languages

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

Chapter 4. Lexical and Syntax Analysis

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

The Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis

UNIT IV INTERMEDIATE CODE GENERATION

4. Lexical and Syntax Analysis

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

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

CMPT 379 Compilers. Parse trees

LECTURE 3. Compiler Phases

COMPILER CONSTRUCTION Seminar 02 TDDB44

Transcription:

Semantic Analysis Attribute Grammars Martin Sulzmann Martin Sulzmann Semantic Analysis Attribute Grammars 1 / 18

Syntax versus Semantics Syntax Analysis When is a program syntactically valid? Formalism: Regular expressions + context-free grammars Semantic Analysis What is the meaning of a program? Mostly context-sensitive conditions: Proper variable declarations Type correctness Dynamic method dispatch... Martin Sulzmann Semantic Analysis Attribute Grammars 2 / 18

Attribute Grammars (AGs) Objective A tool for simplifying semantic analysis (Knuth 1968). Approach Decorate CFG rules with actions (semantic rules). Example Rule E E 1 +T E E 1 T E T T (E) T num Action E.val = E 1.val +T.val E.val = E 1.val T.val E.val = T.val T.val = E.val T.val = num.vale For us mainly construction of AST, something we yet have to discuss Martin Sulzmann Semantic Analysis Attribute Grammars 3 / 18

Attributes + Semantic Rules AGs = Attributes + Semantic Rules E E 1 T }{{} CFG Rule E.val = E 1.val T.val }{{} Semantic Rule Decorated CFG. Nonterminals may have attributes, e.g. E.val. Define meaning of attributes via semantic rules. Martin Sulzmann Semantic Analysis Attribute Grammars 4 / 18

Semantic Rules Examples Semantic rules involve computations: E E 1 T E.val = E 1.val T.val Semantic rules involve copy operations: E T E.val = T.val Martin Sulzmann Semantic Analysis Attribute Grammars 5 / 18

Two Types of Attributes Semantic Rule For each CFG rule A α 1...α n. some semantic rule b = f(c 1,...,c k ). Synthesized Attributes Belongs to left-hand side nonterminal (Context-insensitive) For example, b is a synthesized attribute of A and c 1,...,c k are attributes of α 1,...,α n. Terminal symbols usually have synthesized attributes only (e.g. consider id, num). Inherited Attributes Belongs to right-hand side nonterminal (Context-sensitive). For example, b is an inherited attribute of some α i and c 1,...,c k are attributes of A,α 1,...,α n. Hence, b depends on c 1,...,c k. Martin Sulzmann Semantic Analysis Attribute Grammars 6 / 18

Synthesized Attributes Example Attribute val belongs to left-hand side. Evaluate 3*5+4 E E 1 +T E T T T 1 F T F F num E.val = E 1.val +T.val E.val = T.val T.val = T 1.val F.val T.val = F.val F.val = num.value A definition that only uses syntesized attributes is an S-attributed definition. Martin Sulzmann Semantic Analysis Attribute Grammars 7 / 18

Evaluation of Synthesized Attributes Observations Evaluation of semantic rules during parsing. S-attributed definition can be evaluated as soon as right-hand side ( Handle ) is recognized. Corresponds to LR parsing strategy! Martin Sulzmann Semantic Analysis Attribute Grammars 8 / 18

Evaluation Tree Example E : val E : val + T : val T : val F : val T : val F : val num : value F : val num : value Martin Sulzmann Semantic Analysis Attribute Grammars 9 / 18

AST Construction with S-Attributes Example E E 1 +T E T T T 1 F T F F num E.node = mknode( +,E 1.node,T.node) E.node = T.node T.node = mknode(,t 1.node,F.node) T.node = F.node F.node = mkleaf(num,num.entry) Martin Sulzmann Semantic Analysis Attribute Grammars 10 / 18

From Parse Tree to AST Example Parse Tree E AST + 7 7777777 E + T 8 8888888 8 8888888 4 3 5 7 777777 T T F 4 F F 5 3 Martin Sulzmann Semantic Analysis Attribute Grammars 11 / 18

yacc Example yacc Grammar with Semantic Rules lines : lines line line ; line : expr CR {printf("= %d\n",$1); } ; expr : expr PLUS term {$$ = $1 + $3; } term ; term : term TIMES factor {$$ = $1 * $3; } factor ; factor : LPAREN expr RPAREN {$$ = $2;} INT ; Martin Sulzmann Semantic Analysis Attribute Grammars 12 / 18

S-Attributes: Short Summary Observations Evaluation of S-attributed definition corresponds to LR parsing strategy. yacc supports S-attributed definitions. Main application: AST construction Aside: Dynamic versus static semantics. Static semantics: First generate AST for later (static) processing. Dynamic semantics: See above yacc specification for a simple interpreter. Martin Sulzmann Semantic Analysis Attribute Grammars 13 / 18

Inherited Attributes Example C style type declarations such as int id1,id2. CFG + semantic rules: D TL T int T real L L 1 id L id L.in = T.type T.type = int T.type = real L 1.in = L.in; addtype(id.entry,l.in) addtype(id.entry, L.in) in is a inherited attribute as the corresponding nonterminal appears on the right-hand side. Evaluation based on LL parsing strategy Martin Sulzmann Semantic Analysis Attribute Grammars 14 / 18

L-Attributed Definitions Definition CFG rule A X 1...X n The following holds Inherited attributes of X i depend only on: 1 inherited attributes of A 2 attributes of X 1,...,X i 1 Synthesized attributes of A depend only on its inherited attributes and arbitrary rhs attributes. Syntesized attributes of an action depends only on its inherited attributes. Evaluation order: Inh(A),Inh(X 1 ),Syn(X 1 ),...,Inh(X n ),Syn(X n ),Syn(A). This is precisely the order of evaluation for an LL parser! Martin Sulzmann Semantic Analysis Attribute Grammars 15 / 18

Left-Recursion and Semantic Rules Objective Eliminate left recursion (so we can apply LL parser). Transform semantic rules accordingly. Example (incomplete) E E 1 +T E.val = E 1.val +T.val E T E.val = T.val T (E) T.val = E.val T num T.val = num.value E TR??? R +TR 1??? R ǫ??? T (E) T num Martin Sulzmann Semantic Analysis Attribute Grammars 16 / 18

Left-Recursion and Semantic Rules (2) The Trick E TR R +TR 1 R ǫ T (E) T num R.in = T.val; E.val = R.val R 1.in = R.in+T.val; R.val = R 1.val R.val = R.in Via in propagate type info down the parse tree. Martin Sulzmann Semantic Analysis Attribute Grammars 17 / 18

Summary Attributed Grammars Domain-specific (language) formalismus for semantic analysis. S-attributed definitions LR parser. L-attributed definitions LL parser. Application ( old school ) Formalize compilation (or interpretation) process via semantic rules (type checking, intermediate code, optimizations,...). Today Use semantic rules to generate AST. Semantic analysis phases operate on AST written in general-purpose languages such as OCaml. Martin Sulzmann Semantic Analysis Attribute Grammars 18 / 18