Chapter 4 - Semantic Analysis. June 2, 2015

Similar documents
Chapter 4. Action Routines

Chapter 4 :: Semantic Analysis

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

Programming Languages

Semantic Analysis. Role of Semantic Analysis

Syntax-Directed Translation. Introduction

Syntax-Directed Translation. Lecture 14

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

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

Syntax Analysis. Chapter 4

CS A331 Programming Language Concepts

CSE 12 Abstract Syntax Trees

Summary: Semantic Analysis

COP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher

COP4020 Programming Languages. Semantics Prof. Robert van Engelen

Syntax-Directed Translation

Chapter 3 (part 3) Describing Syntax and Semantics

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

Alternatives for semantic processing

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

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

G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

MIDTERM EXAM (Solutions)

CMPT 379 Compilers. Parse trees

Semantic Analysis with Attribute Grammars Part 1

Syntactic Directed Translation

COP4020 Spring 2011 Midterm Exam

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

Chapter 3. Syntax - the form or structure of the expressions, statements, and program units

Semantic actions for expressions

Static and Dynamic Semantics

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Error Handling Syntax-Directed Translation Recursive Descent Parsing

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

Semantic actions for declarations and expressions

[Syntax Directed Translation] Bikash Balami

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

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

List of Figures. About the Authors. Acknowledgments

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

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

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CST-402(T): Language Processors

CSCI Compiler Design

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

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

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

LECTURE 3. Compiler Phases

2068 (I) Attempt all questions.

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

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

Context-free grammars (CFG s)

Crafting a Compiler with C (II) Compiler V. S. Interpreter

Chapter 3. Describing Syntax and Semantics

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

Syntax-Directed Translation

Semantic actions for declarations and expressions

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

CS606- compiler instruction Solved MCQS From Midterm Papers

Semantic Analysis Attribute Grammars

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

CS 406: Syntax Directed Translation

Context-Free Grammars

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

More On Syntax Directed Translation

A Simple Syntax-Directed Translator

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

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

Semantic Analysis. Lecture 9. February 7, 2018

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Time : 1 Hour Max Marks : 30

CS5363 Final Review. cs5363 1

Earlier edition Dragon book has been revised. Course Outline Contact Room 124, tel , rvvliet(at)liacs(dot)nl

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

Trustworthy Compilers

Writing Evaluators MIF08. Laure Gonnord

Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:

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

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

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

Context-sensitive Analysis

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

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

The Structure of a Syntax-Directed Compiler

Programming Language Specification and Translation. ICOM 4036 Fall Lecture 3

Recursive Descent Parsers

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Syntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

CS 406/534 Compiler Construction Putting It All Together

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Syntax Analysis Part I

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Transcription:

Chapter 4 - Semantic Analysis June 2, 2015

The role of the semantic analyzer Compilers use semantic analysis to enforce the static semantic rules of a language It is hard to generalize the exact boundaries between semantic analysis and the generation of intermediate representations (or even just straight to final represenations); this demarcation is the logical boundary between the front-end of a compiler (lexical analysis and parsing) and the back-end of the compiler (intermediate representations and final code.)

The role of the semantic analyzer For instance, a completely separated compiler could have a well-defined lexical analysis and parsing stage generating a parse tree, which is passed wholesale to a semantic analyzer, which could then create a syntax tree and populate a symbol table, and then pass it all on to a code generator; Or a completely interleaved compiler could intermix all of these stages, literally generating final code as part of the parsing engine.

The role of the semantic analyzer The text focuses on an organization where the parser creates a syntax tree (and no full parse tree), and semantic analysis is done over a separate traversal of the syntax tree.

Dynamic checks The Tony Hoare observation about disabling semantic checks being akin to a sailing enthusiast who wears a life jacket when training on dry land, but removes it when going to sea. Assertions, invariants, preconditions, and post-conditions: let the programmer express logical requirements for values. (If you continue along this route, you see run into thickets of various ideas about formalizing semantics, such as operational semantics, axiomatic semantics, and denotational semantics.)

Static analysis In general, compile-time algorithms that predict run-time behavior are known as static analysis. In Ada, ML, and Haskell, type checking is static. (An amusing, related paper: Dynamic Typing in Haskell).

Static analysis Language design is where you make the decisions that drive what can be statically checked.

Attribute grammars Attribute grammars add two concepts to a CFG: Semantic rules (also called semantic actions or grammar rules ) Attributes; these are values associated with terminals and non-terminals. Synthesized attributes are derived from children; inherited attributes come a parent or a sibling; L-attributes ( L from Left-to-Right, all in a single pass ) come either from a left sibling or from the parent.

Evaluating attributes The process of assigning values to a tree is called annotation or decoration.

Evaluating attributes in an S-attributed grammar If all of the attributes in an attribute grammar are synthesized (i.e., derived from children), then the attributed grammar is said to be S-attributed. All of the rules assign attributes only to the left-hand side (LHS) symbol, and all are based on the set of attribute values of the right-hand side (RHS) symbols.

Evaluating attributes that are inherited If any of the attributes in an attribute grammar are inherited (i.e., a LHS symbol which has an attribute derived either from the RHS or from a left sibling symbol), then the attributed grammar is said to be L-attributed.

Evaluating attributes that are inherited Consider the following (Lemon syntax rather than the text s syntax): expr(a) ::= CONST(B) exprtail(c). {C.st = B.val; A.val = C.val} exprtail(a) ::= MINUS CONST(B) exprtail(c). {C.st = C.st - B.val; A.val = C.val} exprtail(a) ::=. {A.val = A.st}

Evaluating attributes that are inherited expr(a) ::= CONST(B) exprtail(c). {C.st = B.val; A.val = C.val} exprtail(a) ::= MINUS CONST(B) exprtail(c). {C.st = C.st - B.val; A.val = C.val} exprtail(a) ::=. {A.val = A.st} Consider the inherited actions: In the first line, exprtail (a LHS symbol) has its subtotal ( st ) attribute set to the value of its left sibling, CONST. In the second line, the LHS exprtail s subtotal is reduced by CONST s value. In the third line, exprtail has its value derived from its subtotal.

Upshot Practical programming languages generally require some L-attributed flow.

Attribute flow As a notation, grammars are declarative and do not imply an ordering; a grammar is well-defined iff the rules determine a unique set for each and every possible parse tree. A grammar is noncircular if no attribute depends on itself. Your text defines a translation scheme is an algorithm that annotates a tree using the rules of an attribute grammar in an order consistent with the tree s attribute flow. (See, however, pp.37-40 of the Dragon Book for a slightly different take.)

Attribute flow Clearly, a S-attributed grammar can be decorated in the same order as a LR parser, allowing a single pass that interleaves parsing and attribute evaluation. Equally clearly, an L-attributed grammar can be decorated in the same order as LL parser, allowing a single pass that interleaves parsing and attribute evaluation.

Attribute flow is not just LR <-> S-attribute Page 188:... it is often possible for a bottom-up parser to accommodate some non-s-attributed attribute flow...

One pass compilation Compilers that interleave both semantic analysis and target code generation are said to be one-pass compilers. (The text notes on page 189 that some authors also allow intermediate code generation to count as single-pass.)

Building a syntax tree A parser can use semantic actions to just build a syntax tree, and then use a separate semantic analyzer to decorate the tree.

Building a syntax tree Nice feature of S-attributed grammars is that the LR parser s semantic actions are very straightforward (see pseudo-code in figure 4.5 on page 190):

Building a syntax tree Figure 4.7 from text

Building a syntax tree Using LL is not as simple since syntax tree information has to flow from left siblings to right siblings (see figure 4.6 on page 191). You end up with both an inherited syntax tree (st) attribute and a tree pointer:

Building a syntax tree Figure 4.8 from text

Action Routines Page 191: Most production compilers, however, use an ad hoc, handwritten translation scheme, interleaving parsing with at least the initial construction of a syntax tree, and possibly all of semantic analysis and intermediate code generation. In LL parsing, one can embed action routines anywhere on the RHS; if you are writing a recursive descent parser, the action routine at the beginning of a production rule is placed at the beginning of the implementing subroutine, and so on.

Action Routines In LR parsing, in general, one can always put action routines at the end of a production rule (indeed, Lemon only allows this placement), though various tools such Bison allow more flexibility. (See here for more about mid-rule-actions in Bison.)

Space managment for attributes If you are building a tree, you can use those nodes to hold the attribute information. If you don t build a tree, then for bottom-up parsing with an S-attributed grammar, one can use an attribute stack mirroring the parse stack. For top-down parsing, while you can use a stack, it s not simple. It s probably just better to build a tree, though, or at least have an automated tool keep up with items. (See ANTLR or Coco/R)

Decorating a Syntax Tree As mentioned earlier, the focus in this text is having the parser create a syntax tree and then using a separate stage for semantic analysis and intermediate code generation. Tree grammars augmented with semantic rules are used to decorate syntax trees, analogous to the way that context-free grammars augmented with semantic rules can create decorated parse trees. Generally, these are implemented with mutually recursive subroutines. For instance, take a look at the compiler passes for GCC 4.1

Example of S-attributed grammar Example here