A simple syntax-directed

Similar documents
COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

A Simple Syntax-Directed Translator

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

CPS 506 Comparative Programming Languages. Syntax Specification

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

2.2 Syntax Definition

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

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

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

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

Building Compilers with Phoenix

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

1. The output of lexical analyser is a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

Chapter 3: Lexical Analysis

2068 (I) Attempt all questions.

Principles of Programming Languages COMP251: Syntax and Grammars

Formal Languages and Compilers Lecture VI: Lexical Analysis

CSE 3302 Programming Languages Lecture 2: Syntax

Part 5 Program Analysis Principles and Techniques

COP 3402 Systems Software Syntax Analysis (Parser)

CD Assignment I. 1. Explain the various phases of the compiler with a simple example.

QUESTIONS RELATED TO UNIT I, II And III

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

UNIT -2 LEXICAL ANALYSIS

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

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Syntax and Grammars 1 / 21

Introduction to Lexical Analysis

Syntax Analysis Check syntax and construct abstract syntax tree

Introduction to Syntax Analysis. Compiler Design Syntax Analysis s.l. dr. ing. Ciprian-Bogdan Chirila

Compiler Design Concepts. Syntax Analysis

More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

Chapter 4: Syntax Analyzer

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILERS

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

Compiling Regular Expressions COMP360

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

1 Lexical Considerations

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

CA Compiler Construction

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

Question Points Score

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Lexical Scanning COMP360

Lexical and Syntax Analysis. Top-Down Parsing

B The SLLGEN Parsing System

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

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

Lexical Analysis. Chapter 2

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

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

Chapter 3. Describing Syntax and Semantics

COMPILERS BASIC COMPILER FUNCTIONS

Question Bank. 10CS63:Compiler Design

CMSC 330: Organization of Programming Languages

Compiler Design. Computer Science & Information Technology (CS) Rank under AIR 100

COMPILER DESIGN. For COMPUTER SCIENCE

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

CS 314 Principles of Programming Languages

Program Analysis ( 软件源代码分析技术 ) ZHENG LI ( 李征 )

Context-Free Grammars

Describing Syntax and Semantics

Syntax. A. Bellaachia Page: 1

Lexical Analysis. Introduction

Lexical and Syntax Analysis

Lecture 4: Syntax Specification

Parsing and Pattern Recognition

Front End. Hwansoo Han

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Lexical Analysis. Lexical analysis is the first phase of compilation: The file is converted from ASCII to tokens. It must be fast!

Lexical and Syntax Analysis

A lexical analyzer generator for Standard ML. Version 1.6.0, October 1994

LECTURE 3. Compiler Phases

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

Syntax. In Text: Chapter 3

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Syntax Analysis. Chapter 4

Lexical Analysis. Dragon Book Chapter 3 Formal Languages Regular Expressions Finite Automata Theory Lexical Analysis using Automata

CSE 401 Midterm Exam Sample Solution 2/11/15

CS606- compiler instruction Solved MCQS From Midterm Papers

Defining syntax using CFGs

Compiler Design Overview. Compiler Design 1

LANGUAGE PROCESSORS. Presented By: Prof. S.J. Soni, SPCE Visnagar.

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

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Introduction to Compiler

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

COP4020 Programming Assignment 2 Spring 2011

Theory and Compiling COMP360

This book is licensed under a Creative Commons Attribution 3.0 License

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

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

Lexical Analysis. Lecture 2-4

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

CS415 Compilers. Lexical Analysis

Consider a description of arithmetic. It includes two equations that define the structural types of digit and operator:

Transcription:

Syntax-directed is a grammaroriented compiling technique Programming languages: Syntax: what its programs look like? Semantic: what its programs mean? 1 A simple syntax-directed Lexical Syntax Character analyzer token directed intermediate stream stream representation The lexical analyzer converts the stream of input characters into a stream of token that becomes the input to the following phase Syntax directed is a combination of a syntax analyzer and an intermediate code generator. 2 1

Syntax definitions Context Free Grammar: a notation used for specifying the syntax of a language ( BNF ). A CFG has four components : A set of tokens known as terminals A set of non-terminals A set of production rules A start symbol 3 Syntax definitions Example: <list >::= < list > + < digit > <list >::= < list > - < digit > <list >::= < digit > <digit >::= 0,1,2,..,9 show that 9-5+2 is a list 4 2

Parse Tree 5 Parse Tree A parse tree shows how the start symbol of a grammar derives a string in a language. Example: Consider the following CFG: 6 3

Parse Tree 7 Syntax Tree 8 4

Left and right recursive productions 9 Ambiguity A grammar is said to be ambiguous if it produces more than one parse tree for some sentence. Ambiguity is acceptable in spoken languages. Ambiguous programming languages are useless unless the ambiguity can be resolved. 10 5

Ambiguity 11 Ambiguity The above is ambiguous grammar 12 6

Ambiguity Note that in case of operators like and / these two parse tree would evaluate differently. Even in case of associate operators like + and * these two parse trees can be evaluate differently due to the possibility of overflow. 13 Associativity of operators e.g. 9+5+2 ( 9+5 ) + 2 We say that operator + associative to the left because an operand with plus sign on both sides of it is taken by the operator to its left Assignment in C : a = b = c right associative The basic four arithmetic operators are left associative. That is why the next grammar <list> stand growing from left ( the same goes for parse tree) 14 7

Associativity of operators 15 Precedence Of Operators Consider the exp. 9+5*2, there are two possible interpretations of this exp: ( 9+5 )*2 or 9 + (5*2 ) We need to know the precedence of operators when more than one kind of operators is present ( associativity of + and * do not resolve this ambiguity ). 9+5*2 9+ ( 5*2 ) * higher precedence than + 9*5+2 ( 9*5 ) +2 16 8

Syntax of expressions A grammar for arithmetic expressions can be constructed from a table showing the associativity and precedence of operators. Left associative : + - lower precedence Left associative : * / higher precedence We create < exp > and < term > for 2 levels of precedence and an extra non-terminal < factor > for generating the basic units of expressions ( digits & parenthesized exp.) 17 Syntax of expressions 18 9

Syntax of expressions This grammar is a list of terms separated by either + or - signs, and a term is a list of factors separated by * or / signs. A factor is a digit or a parenthesized exp. 19 Exercises 1) From the above grammar derive the following expressions ( and draw the Parse Tree ). 9 * 7 * ( 5 ( 3 + 2 ) ( 7 * ( 5 2 + 4 ) / 6 ) 2) Write grammar rule for arithmetic exp consider the operators *, /, -, +, ^, %. 20 10

Syntax of statements The following grammar defines a statement in Pascal: 21 Lexical Analysis Removal of white spaces & comments Recognizing the tokens of the source program. Let num be a token representing an integer, when a sequence of digit appears in an input, the lexical analyzer will pass num to the parser. 22 11

Lexical Analysis 23 Lexical Analysis Regular grammar is used to describe different tokens of programming languages and denoted as regular expressions. <id>::= <letter> (<letter> <digit>)* <int>::=<digit> <digit>* 24 12

Lexical Analysis The needs to know that lexeme count forms the first 2 instances of token ( id ), and the lexeme increment forms the third instance of ( id ) The data structure used to do such a thing is the symbol table. 25 Recognizing identifiers & keywords A grammar of a language often treats an identifier as a token (id) and a keyword as a token (kw) A mechanism is needed to distinguish between keywords & identifiers. Each identifier needs to be first checked against keywords list to decide whether it is kw or id 26 13

Symbol Table It is a data structure that is generally used to store information about various source language constructs. The symbol table interfaces with the phases using the following operations: insert (s, t) returns the index of new entry for string s, token t. lookup (s) { returns index of the entry for string s, or 0 if s is not in the table. 27 Handling keywords e.g. Consider token opr with lexemes div & mod, we initialize the symbol table using the following calls: initialization insert ( "div", opr ) ; insert ( "mod", opr ) ; The lookup ( " div " ) returns the token opr, so div cannot be used as an identifier. 28 14

Parsing Parsing is the process of determining if a string of tokens can be generated by a grammar. 29 Parsing Top down Parsing Parsing refers to the order in which the parse tree is constructed Bottom up Parsing 30 15

Top Down Parsing 31 Top Down Parsing 32 16

Top Down Parsing 33 Top Down Parsing 34 17

Top Down Parsing 35 One symbol lookahead Usually this can be implemented during a single left to right scan of the input string. The current token being scanned in the input is referred to as the lookahead symbol. 36 18

Top Down Parsing The following grammar generates a subset of types of Pascal. 37 38 19

Bottom-up parsing Used to parse arithmetic exp. 39 Bottom-up parsing 40 20