Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.

Slides:



Advertisements
Similar presentations
1 Compiler Construction Intermediate Code Generation.
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Context-Free Grammars Lecture 7
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.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
Chapter 2 A Simple Compiler
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Compilers and Syntax.
Chapter 2 A Simple Compiler
UMBC Introduction to Compilers CMSC 431 Shon Vick 01/28/02.
CSC 8310 Programming Languages Meeting 2 September 2/3, 2014.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
1 Chapter 2 A Simple Compiler. 2 Outlines 2.1 The Structure of a Micro Compiler 2.2 A Micro Scanner 2.3 The Syntax of Micro 2.4 Recursive Descent Parsing.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 8: Semantic Analysis and Symbol Tables.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Introduction to Parsing
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Introduction to Compiling
Context-free grammars. Roadmap Last time – Regex == DFA – JLex for generating Lexers This time – CFGs, the underlying abstraction for Parsers.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Overview of Previous Lesson(s) Over View  Syntax-directed translation is done by attaching rules or program fragments to productions in a grammar. 
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax and Semantics Form and Meaning of Programming Languages Copyright © by Curt Hill.
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Context-free grammars. Roadmap Last time – Regex == DFA – JLex for generating Lexers This time – CFGs, the underlying abstraction for Parsers.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Syntax.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CSE 420 Lecture Program is lexically well-formed: ▫Identifiers have valid names. ▫Strings are properly terminated. ▫No stray characters. Program.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
A Simple Syntax-Directed Translator
Constructing Precedence Table
Introduction to Parsing
CS510 Compiler Lecture 4.
Introduction to Parsing (adapted from CS 164 at Berkeley)
Abstract Syntax Trees Lecture 14 Mon, Feb 28, 2005.
PROGRAMMING LANGUAGES
Basic Program Analysis: AST
CS 3304 Comparative Languages
CSE401 Introduction to Compiler Construction
Lecture 7: Introduction to Parsing (Syntax Analysis)
R.Rajkumar Asst.Professor CSE
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Chapter 10: Compilers and Language Translation
COMPILER CONSTRUCTION
Presentation transcript:

Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015

Outline An Informal Definition of the ac Language Formal Definition of ac Phases of a Simple compiler Scanning Parsing Abstract Syntax Trees Semantic Analysis Code Generation

Introduction An overview of compilation process by considering a simple language A quick overview of a compiler ’ s phases and their associated data structures

An Informal Definition of the ac Language ac : adding calculator Types –integer –float: allows 5 fractional digits after the decimal point –Automatic type conversion from integer to float Keywords –f: float –i: integer –p: print Variables –23 names from lowercase Roman alphabet except the three reserved keywords f, i, and p Target of translation: dc (desk calculator) –Reverse Polish notation (RPN)

An Example ac Program Example ac program: –f b i a a = 5 b = a p b $ Corresponding dc code –5 sa la sb lb p

Formal Definition of ac Syntax specification: context-free grammar (CFG) –(Chap. 4) Token specification: regular expressions –(Sec. 3.2)

Syntax Specification

CFG: –A set of productions or rewriting rules –E.g.: Stmt  id assign Val Expr | print id –Two kinds of symbols Terminals: cannot be rewritten –E.g.: id, assign, print –Empty or null string: λ –End of input stream or file: $ Nonterminals: –Start symbol: Prog –E.g.: Val, Expr –Left-hand side (LHS) –Right-hand side (RHS)

Starting from the start symbol Choosing some nonterminal symbol and finding a production for it Replacing it with the string of symbols on the RHS Any string of terminals that can be produced: syntactically valid Otherwise: syntax error

Token Specification

Phases of a Simple Compiler Scanner: source ac program -> tokens –Chap. 3 Parser: tokens -> abstract syntax tree (AST) –Chap. 5 & 6 Symbol table: created from AST –Chap. 8 Semantic analysis: AST decoration Translation: by traversing AST

Scanning To translate a stream of characters into a stream of tokens –Automatic construction of scanners: Chap.3 –Token: Type: membership in the terminal alphabet Semantic value: additional information –For most programming languages, the scanner’s job is not so easy +, ++ //, “, \” Variable-length tokens

CANNER PEEK ADVANCE CAN IGITS EXICAL RROR

CAN IGITS PEEK ADVANCE

Parsing To determine if the stream of tokens conforms to the language’s grammar (Chap. 4, 5, 6) –e.g.: Are these valid statements? b = a p b –For ac, a simple parsing technique called recursive descent is used “Mutually recursive parsing routines that descend through a derivation tree” Each nonterminal has an associated parsing procedure for determining if the token stream contains a sequence of tokens derivable from that nonterminal

Predicting a Parsing Procedure Examine the next input token to predict which production should be applied –E.g.: Stmt  id assign Val Expr Stmt  print id – Predict set {id} [1] {print} [6]

TMT PEEK MATCH ERROR AL XPR TMT PEEK MATCH ERROR AL XPR

Consider the productions for Stmts –Stmts  Stmt Stmts –Stmts  λ The predict sets –{id, print} [8] –{$} [11]

TMTS PEEK TMT TMTS ERROR

Implementing the Production When a terminal is encountered, a call to MATCH() is placed For each nonterminal, the corresponding procedure will be called For the symbol λ, no code is executed

Abstract Syntax Trees Some aspects of compilation that can be difficult to perform during syntax analysis –Some aspects of language cannot be specified in a CFG E.g.: symbol usage consistency with type declaration –Context sensitive In Java: x.y.z –Package x, class y, static field z –Variable x, field y, another field z Operator overloading –+: numerical addition or appending of strings –Separation into phases makes the compiler much easier to write and maintain

Parse trees are large and unnecessarily detailed (Fig. 2.4) –Abstract syntax tree (AST) (Fig. 2.9) Inessential punctuation and delimiters are not included –A common intermediate representation for all phases after syntax analysis Declarations need not be in source form Order of executable statements explicitly represented Assignment statement must retain identifier and expression Nodes representing computation: operation and operands Print statement must retain name of identifier

Semantic Analysis Example processing include: –Declarations and name scopes are processed to construct a symbol table –Type consistency –Make type-dependent behavior explicit

Symbol Tables To record all identifiers and their types –23 entries for 23 distinct identifiers in ac (Fig. 2.11) Type info.: integer, float, unused (null) Attributes: scope, storage class, protection properties –Symbol table construction (Fig. 2.10) Symbol declaration nodes call VISIT(SymDeclaring n) ENTERSYMBOL checks the given symbol has not been previously declared

VISIT GET YPE NTER YMBOL OOKUP YMBOL ERROR GET D

Type Checking Only two types in ac –Integer –Float Type hierarchy –Float wider than integer –Automatic widening (or casting) integer -> float

Type Analysis VISIT ONSISTENT ONVERT OOKUP YMBOL

ONSISTENT ENERALIZE ONVERT ENERALIZE ERROR

Type checking –Constants and symbol reference: simply set the node’s type based on the node’s contents –Computation nodes: CONSISTENT(n.c1, n.c2) –Assignment operation: CONVERT(n.c2, n.c1.type) CONSISTENT() –GENERALIZE(): determines the least general type –CONVERT(): checks whether conversion is necessary

Code Generation The formulation of target-machine instructions that faithfully represent the semantics of the source program –Chap. 11 & 13 –dc: stack machine model –Code generation proceeds by traversing the AST, starting at its root VISIT (Computing n): +, - VISIT (Assigning n): = VISIT (SymReferencing n) VISIT (Printing n) VISIT (Converting n)

VISIT ODE EN MIT VISIT ODE EN MIT

End of Chapter 2