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

Similar documents
Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Dr. D.M. Akbar Hussain

Lecture 4: Syntax Specification

3. Context-free grammars & parsing

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

CMSC 330: Organization of Programming Languages. Context Free Grammars

Syntax. A. Bellaachia Page: 1

COP 3402 Systems Software Syntax Analysis (Parser)

CPS 506 Comparative Programming Languages. Syntax Specification

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

CSE 3302 Programming Languages Lecture 2: Syntax

Building Compilers with Phoenix

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

Theory and Compiling COMP360

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

UNIT I Programming Language Syntax and semantics. Kainjan Sanghavi

EECS 6083 Intro to Parsing Context Free Grammars

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

Specifying Syntax COMP360

Lexical Analysis. Introduction

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Syntax. In Text: Chapter 3

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

CMSC 330: Organization of Programming Languages. Context Free Grammars

A Simple Syntax-Directed Translator

Part 5 Program Analysis Principles and Techniques

Lexical Scanning COMP360

Introduction to Parsing Ambiguity and Syntax Errors

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

A simple syntax-directed

CMSC 330: Organization of Programming Languages

Introduction to Parsing Ambiguity and Syntax Errors

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages. Context Free Grammars

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Programming Language Definition. Regular Expressions

CMSC 330: Organization of Programming Languages

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

Describing Syntax and Semantics

Introduction to Lexing and Parsing

Principles of Programming Languages COMP251: Syntax and Grammars

Regular Expressions. Agenda for Today. Grammar for a Tiny Language. Programming Language Specifications

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

CSCE 314 Programming Languages

Syntax Analysis. The Big Picture. The Big Picture. COMP 524: Programming Languages Srinivas Krishnan January 25, 2011

Non-deterministic Finite Automata (NFA)

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

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

announcements CSE 311: Foundations of Computing review: regular expressions review: languages---sets of strings

Chapter 3. Describing Syntax and Semantics ISBN

COLLEGE OF ENGINEERING, NASHIK. LANGUAGE TRANSLATOR

Lexical and Syntax Analysis. Top-Down Parsing

Languages and Compilers

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

Syntax Analysis. Chapter 4

The Front End. The purpose of the front end is to deal with the input language. Perform a membership test: code source language?

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

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

CSCI312 Principles of Programming Languages!

Programming Languages Third Edition. Chapter 9 Control I Expressions and Statements

Chapter 3. Describing Syntax and Semantics

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

Chapter 2 :: Programming Language Syntax

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

CSCI312 Principles of Programming Languages!

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

Week 2: Syntax Specification, Grammars

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

Formal Languages. Formal Languages

CS 314 Principles of Programming Languages. Lecture 3

Compiler Design Overview. Compiler Design 1

Intro To Parsing. Step By Step

This book is licensed under a Creative Commons Attribution 3.0 License

Programming Languages Third Edition

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

ECE251 Midterm practice questions, Fall 2010

Programming Language Syntax and Analysis

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

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Introduction to Parsing. Lecture 5

CS 314 Principles of Programming Languages

Defining syntax using CFGs

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

CS 4201 Compilers 2014/2015 Handout: Lab 1

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

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

COMPILER DESIGN LECTURE NOTES

COMPILER DESIGN UNIT I LEXICAL ANALYSIS. Translator: It is a program that translates one language to another Language.

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

1. INTRODUCTION TO LANGUAGE PROCESSING The Language Processing System can be represented as shown figure below.

Habanero Extreme Scale Software Research Project

LANGUAGE PROCESSORS. Introduction to Language processor:

Transcription:

Theoretical Part Chapter one:- - What are the Phases of compiler? Six phases Scanner Parser Semantic Analyzer Source code optimizer Code generator Target Code Optimizer Three auxiliary components Literal table Symbol table Error Handler

- What is the difference between : parse tree and syntax tree Or - Syntax tree and parse tree are all major data structures of any compiler. Differentiate between them. The Syntax tree: Syntax trees represent abstractions of the actual source code token sequences, The token sequences cannot be recovered from them (unlike parse trees). Nevertheless they contain all the information needed for translation, in a more efficient form than parse trees. The Parse tree: A parse tree corresponding to a derivation is a labeled tree. The interior nodes are labeled by non-terminals, the leaf nodes are labeled by terminals; And the children of each internal node represent the replacement of the associated non-terminal in one step of the derivation. - Syntax error and semantic errors are types of errors that a compiler must deal with. Explain each one and give examples from your popular language. A syntax error : o Occurs when you write code that violates the rules of grammar of the programming language. o Syntax errors are detected by the compiler Example: X=y+2 missing semi colon Semantic error : o Means writing a valid programming structure with invalid logic. Semantic errors can be broken down into o Static semantic errors: Which can be detected by the compiler o Run time errors: Runtime errors cause the program to crash or abort in some way o Logic errors: Cause a program to run to completion,but produce the incorrect output or result Those errors are not detected by the compiler Example: Memory overflow

Chapter two:- - Explain the function of lexical analyzer. Or - What is the purpose of lexical analysis? The job of the lexical analyzer, or scanner, is to transform a stream of characters into a stream of tokens. - What is meant by Regular Expression? The single characters from alphabet matching themselves A matches the character a by writing L (a) = {a} ε denotes the empty string, by L(ε)={ε} {} or Φ matches no string at all, by L (Φ) = { } - What is the purpose of Regular Expression? - To represent patterns of strings of characters. - Define DFA. Definition of DFA: A DFA (Deterministic Finite Automation) M consist of (1) An alphabet, (2) A set of states S, (3) A transition function T: S S, (4) A start state s0 S,

(5)And a set of accepting states A S - What is the purpose of DFA? Describing particular kinds of algorithms, using Automata where the next state is uniquely given by the current state and the current input character. - What is Transition table of DFA? - Is a data structure which is two-dimensional array, indexed by state and input character that expresses the values of the transition function T

Chapter three (part one):- - Explain the Purpose of syntax analysis. - Determining the syntax, or structure, of a program. - Describe the parsing process. Or - What is the function of the parser? Function of a Parser Takes the sequence of tokens produced by the scanner as its input and produces the syntax tree as its output. Parser Sequence of tokens --------> Syntax-Tree - What are the Rules of CFG and its representation BNF? Grammars Rules Vertical bar appears as meta-symbol for choice. Concatenation is used as a standard operation. No meta-symbol for repetition (like the * of regular expressions) Use the arrow symbol instead of equality to express the definitions of names Names are written in italic( in a different font) Grammar rules use regular expressions as components The notation was developed by John Backus and adapted by Peter Naur for the Algol60 report Grammar rules in this form are usually said to be in Backus-Naur form, or BNF Describe the derivation process.. Derivations Grammar rules determine the legal strings of token symbols by means of derivations A derivation is a sequence of replacements of structure names by choices on the right-hand sides of grammar rules

A derivation begins with a single structure name and ends with a string of token symbols At each step in a derivation, a single replacement is made using one choice from a grammar rule

Chapter three (part two):- (Parse tree. Answered in chapter one.) What is Ambiguity Parse trees and syntax trees uniquely express the structure of syntax But it is possible for a grammar to permit a string to have more than one parse tree What are the Two Basic Methods dealing with Ambiguity One is to state a rule that specifies in each ambiguous case which of the parse trees (or syntax trees) is the correct one, called a disambiguating rule. The advantage: it corrects the ambiguity without changing (and possibly complicating) the grammar. The disadvantage: the syntactic structure of the language is no longer given by the grammar alone. Change the grammar into a form that forces the construction of the correct parse tree, thus removing the ambiguity. Of course, in either method we must first decide which of the trees in an ambiguous case the correct one is. - What is Precedence and Associativity? - And how can we solve it? Precedence: We can solve it by: Grammar that have operators with different precedence Such as multiplication and addition Grouping operators into different precedence levels. Cascade is a standard method in syntactic specification using BNF.. Make the lower precedence to be evaluated first Associativity: Grammar that have operators with same precedence and not know which operator will be evaluated first. We ca solves it by: Change the grammar rules to force one way recursion. not in both sides

- What is dangling else problem and how can we solve it?. Dangling else problem Which tree is correct depends on associating the single else-part with the first or the second if-statement. The first associates the else-part with the first if-statement; The second associates it with the second if-statement. This ambiguity called dangling else problem We can solve it by Using a disambiguating rule is the most closely nested rule Implies which parse tree is the correct one. Require the presence of the else-part, and this method has been used in LISP and other functional languages (where a value must also be returned). Uses a bracketing keyword for the if-statement languages that use this solution include Algol68 and Ada. - Define EBNF and Syntax Diagram. - EBNF: Is a grammar that has its own notation such as the asterisk * for representing repetition the [] to represent optional - Is an extended grammar of BNF grammar?