2.2 A Simple Syntax-Directed Translator 1. 2 2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

CPSC 388 – Compiler Design and Construction
Intermediate Code Generation
CS 31003: Compilers Introduction to Phases of Compiler.
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
Compilers and Language Translation
Semantic analysis Parsing only verifies that the program consists of tokens arranged in a syntactically-valid combination, we now move on to semantic analysis,
CPSC Compiler Tutorial 9 Review of Compiler.
Yu-Chen Kuo1 Chapter 1 Introduction to Compiling.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 2 Syntax A language that is simple to parse.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Chapter 2 A Simple Compiler
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
Chapter 5 Syntax-Directed Translation Section 0 Approaches to implement Syntax-Directed Translation 1、Basic idea Guided by context-free grammar (Translating.
Syntax Directed Definitions Synthesized Attributes
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
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.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Introduction Fan Wu Department of Computer Science and Engineering
Joey Paquet, Lecture 12 Review. Joey Paquet, Course Review Compiler architecture –Lexical analysis, syntactic analysis, semantic.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
D. M. Akbar Hussain: Department of Software & Media Technology 1 Compiler is tool: which translate notations from one system to another, usually from source.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
Chapter 2. Design of a Simple Compiler J. H. Wang Sep. 21, 2015.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Introduction to Code Generation and Intermediate Representations
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
IN LINE FUNCTION AND MACRO Macro is processed at precompilation time. An Inline function is processed at compilation time. Example : let us consider this.
Introduction to Compiling
Week 6(10.7): The TINY sample language and it ’ s compiler The TINY + extension of TINY Week 7 and 8(10.14 and 10.21): The lexical of TINY + Implement.
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  In syntax-directed translation 1 st we construct a parse tree or a syntax tree then compute the values of.
LESSON 04.
Overview of Previous Lesson(s) Over View  Syntax-directed translation is done by attaching rules or program fragments to productions in a grammar. 
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Chap. 7, Syntax-Directed Compilation J. H. Wang Nov. 24, 2015.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
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.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Chapter 2: A Simple One Pass Compiler
C HAPTER 2. A S IMPLE S YNTAX -D IRECTED T RANSLATOR DR. NIDJO SANDJOJO, M.Sc.
Lecture 9 Symbol Table and Attributed Grammars
Chapter 3 – Describing Syntax
Compiler Design (40-414) Main Text Book:
A Simple Syntax-Directed Translator
Constructing Precedence Table
Introduction to Parsing
PROGRAMMING LANGUAGES
CS 3304 Comparative Languages
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Faculty of Computer Science and Information System
Presentation transcript:

2.2 A Simple Syntax-Directed Translator 1

2

2.3 Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis 2.7 Symbol Tables 2.8 Intermediate Code Generation 2.9 Summary of Chapter 2 3

Syntax-Directed Translation Syntax-directed translation is done by attaching rules or program fragments to productions in a grammar. For example, consider an expression expr generated by the production: We can translate expr by exploiting its structure, as in the following pseudo-code: 4

Concepts of syntax-directed translation Attributes. – An attribute is any quantity associated with a programming construct. Examples of attributes are data types of expressions, the number of instructions in the generated code, or the location of the first instruction in the generated code for a construct, among many other possibilities. (Syntax- directed) translation schemes. – A translation scheme is a notation for attaching program fragments to the productions of a grammar. The program fragments are executed when the production is used during syntax analysis. 5

Postfix Notation The postfix notation for an expression E can be defined inductively as follows: 6

Postfix Notation Example 2.8 : The postfix notation for (9-5)+2 What about?? 9- (5+2) Evaluate?? 7

Postfix Notation How to evaluate?? – The "trick" is to repeatedly scan the postfix string from the left, until you find an operator. – Then, look to the left for the proper number of operands, – group this operator with its operands. – Evaluate the operator on the operands, and replace them by the result. – repeat the process, continuing to the right and searching for another operator. 8

Postfix Notation Example 2.9 : evaluate the following postfix notation: * 9

Synthesized Attributes We associate attributes with nonterminals and terminals. Then, we attach rules to the productions of the grammar; these rules describe how the attributes are computed at those nodes of the parse tree where the production in question is used to relate a node to its children. 10

11

12

Tree Traversals Tree traversals will be used for describing attribute evaluation and for specifying the execution of code fragments in a translation scheme. – A traversal of a tree starts at the root and visits each node of the tree in some order. – A depth-first traversal starts at the root and recursively visits the children of each node in any order, not necessarily from left to right. It is called "depth first“ because it visits an unvisited child of a node whenever it can, so it visits nodes as far away from the root (as "deep" ) as quickly as it can. 13

14

Translation Schemes Preorder and Postorder Traversals 15

16

17

Semantic actions 18

Example 19 Actions for postfix translating into 95-2+

2.4 Parsing Parsing is the process of determining how a string of terminals can be generated by a grammar. – Top-Down Parsing 20

steps 21

22

Use ϶-Productions Predictive Parsing (self study) When to Use ϶-Productions – predictive parser uses an ϶ -production as a default when no other production can be used 23

24

2.5 A Translator for Simple Expressions Using the techniques of the last three sections, we now construct a syntax directed translator – A syntax-directed translation scheme often serves as the specification for a translator. – The scheme in Fig (repeated from Fig. 2.15) defines the translation to be performed here 25

Abstract and Concrete Syntax A useful starting point for designing a translator is a data structure called an abstract syntax tree. In an abstract syntax tree for an expression, each interior node represents an operator; the children of the node represent the operands of the operator. 26

Example 27

28

syntax tree vs. parse tree in the syntax tree, interior nodes represent programming constructs while in the parse tree, the interior nodes represent nonterminals. 29

30

2.6 Lexical Analysis A lexical analyzer reads characters from the input and groups them into "token objects.“ The lexical analyzer in this section allows numbers, identifiers, and "white space" (blanks, tabs, and newlines) to appear within expressions. 31

Reading Ahead A lexical analyzer may need to read ahead some characters before it can decide on the token to be returned to the parser. For example, a lexical analyzer for C or Java must read ahead after it sees the character >. – If the next character is =, then > is part of the character sequence >=, the lexeme for the token for the "greater than or equal to" operator. – Otherwise > itself forms the "greater than" operator, and the lexical analyzer has read one character too many. 32

Constants 33

Recognizing Keywords and Identifiers Most languages use fixed character strings such as for, do, and if, as punctuation marks or to identify constructs. Such character strings are called keywords. Character strings are also used as identifiers to name variables, arrays, functions, and the like. Grammars routinely treat identifiers as terminals to simplify the parser, which can then expect the same terminal, say id, each time any identifier appears in the input. For example, on input 34

Recognizing Keywords and Identifiers the parser works with the terminal stream id = id + id. The token for id has an attribute that holds the lexeme. Writing tokens as tuples, we see that the tuples for the input stream (2.6) are Keywords generally satisfy the rules for forming identifiers, so a mechanism is needed for deciding when a lexeme forms a keyword and when it forms an identifier. The problem is easier to resolve if keywords are reserved; i.e., if they cannot be used as identifiers. Then, a character string forms an identifier only if it is not a keyword. 35

2.7 Symbol table Symbol tables are data structures that are used by compilers to hold information about source-program constructs. The information is collected incrementally by the analysis phases of a compiler and used by the synthesis phases to generate the target code. Entries in the symbol table contain information about an identifier such as its character string (or lexeme), its type, its position in storage, and any other relevant information. Symbol tables typically need to support multiple declarations of the same identifier within a program. 36

Scope Symbol Table Per Scope: The term "scope of identifier x' really refers to the scope of a particular declaration of x. Scopes are important, because the same identifier can be declared for different purposes in different parts of a program. If blocks can be nested, several declarations of the same identifier can appear within a single block. 37

38

2.8 Intermediate Code Generation Two Kinds of Intermediate Representations 39

Construction of Syntax Trees 40

L-values and R-values There is a distinction between the meaning of identifiers on the left and right sides of an assignment. In each of the assignments the right side specifies an integer value, while the left side specifies where the value is to be stored. 41

Type Checking 42

Three-Address Code 43

Example 44

2.9 Summary o f Chapter 2 45