Eng. Maha Talaat Page 1

Similar documents
Context-Free Grammars

Defining syntax using CFGs

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

CMSC 330: Organization of Programming Languages. Context-Free Grammars Ambiguity

Principles of Programming Languages COMP251: Syntax and Grammars

Syntax Analysis Check syntax and construct abstract syntax tree

Defining syntax using CFGs

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

CMSC 330: Organization of Programming Languages

CPS 506 Comparative Programming Languages. Syntax Specification

Dr. D.M. Akbar Hussain

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

Ambiguity. Lecture 8. CS 536 Spring

Principles of Programming Languages COMP251: Syntax and Grammars

CS 314 Principles of Programming Languages

Compiler Design Concepts. Syntax Analysis

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

CMSC 330: Organization of Programming Languages

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

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

A Simple Syntax-Directed Translator

3. Context-free grammars & parsing

Habanero Extreme Scale Software Research Project

CS 536 Midterm Exam Spring 2013

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

Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5

EECS 6083 Intro to Parsing Context Free Grammars

Context-Free Grammars

CSE 401 Midterm Exam Sample Solution 2/11/15

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

COP 3402 Systems Software Syntax Analysis (Parser)

CMSC 330: Organization of Programming Languages

Outline. Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Parsing II Top-down parsing. Comp 412

CMSC 330: Organization of Programming Languages. Context Free Grammars

Fall Compiler Principles Context-free Grammars Refresher. Roman Manevich Ben-Gurion University of the Negev

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

Context-Free Grammars

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

MIDTERM EXAMINATION - CS130 - Spring 2003

CSE 3302 Programming Languages Lecture 2: Syntax

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

Syntax. In Text: Chapter 3

Describing Syntax and Semantics

A simple syntax-directed

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

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

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

Parsing: Derivations, Ambiguity, Precedence, Associativity. Lecture 8. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

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

Natural Language Processing

ECE251 Midterm practice questions, Fall 2010

( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

This book is licensed under a Creative Commons Attribution 3.0 License

CSCI312 Principles of Programming Languages!

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Optimizing Finite Automata

Chapter 3. Describing Syntax and Semantics

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

Introduction to Parsing. Lecture 5

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

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

Compilers Course Lecture 4: Context Free Grammars

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

Chapter 3. Describing Syntax and Semantics ISBN

CMPT 755 Compilers. Anoop Sarkar.

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Chapter 3. Describing Syntax and Semantics

CMSC330 Fall 2016 Midterm #2 2:00pm/3:30pm

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Syntax and Semantics

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

Introduction to Parsing. Lecture 8

Grammars and ambiguity. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 8 1

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

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Chapter 4: Syntax Analyzer

R10 SET a) Construct a DFA that accepts an identifier of a C programming language. b) Differentiate between NFA and DFA?

Context-Free Languages and Parse Trees

Exercise 1: Balanced Parentheses

Compilers and computer architecture From strings to ASTs (2): context free grammars

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

2.2 Syntax Definition

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

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

Introduction to Parsing. Lecture 5. Professor Alex Aiken Lecture #5 (Modified by Professor Vijay Ganesh)

CSE 401 Midterm Exam 11/5/10

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Introduction to Parsing Ambiguity and Syntax Errors

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

Context-Free Grammars and Languages (2015/11)

CSCE 531 Spring 2009 Final Exam

Related Course Objec6ves

Part 5 Program Analysis Principles and Techniques

Properties of Regular Expressions and Finite Automata

Transcription:

El-Shorouk cademy cad. Year : 2013 / 2014 Higher Institute for Computer & Term : 2nd Information Technology Year : 4th Computer Science Department Supervisor: Prof. Dr. hmed bbassy Section contents: mbiguity in Grammar Dangling Else Problem Compilers Section (6) mbiguous Grammars Consider the grammar exp exp PLUS exp exp exp MINUS exp exp exp TIMES exp exp exp DIVIDE exp exp INT_LIT Construct Parse tree for 3-4/2 re there more than one parse trees? If there is more than one parse tree for a string then the grammar is ambiguous mbiguity causes problems with parsing (what is the correct structure)? Precedence in Grammars To write a grammar whose parse trees express precedence correctly, use a different nonterminal for each precedence level. Start by writing a rule for the operator(s) with the lowest precedence ("-" in our case), then write a rule for the operator(s) with the next lowest precedence, etc: exp exp MINUS exp term term term DIVIDE term factor factor INT_LIT LPREN exp RPREN Now try constructing multiple parse trees for 3-4/2 Grammar is still ambiguous. Look at associativity. Construct 2 parses tree for 5-3-2. Recursion on CFGs grammar is recursive in nonterminal X if: X derives a sequence of symbols that includes an X. grammar is left recursive in X if: X derives a sequence of symbols that starts with an X. grammar is right recursive in X if: X derives a sequence of symbols that ends with an X. For left associativity, use left recursion. For right associativity, use right recursion. mbiguity Removed in CFG exp exp MINUS term term term term DIVIDE factor factor factor INT_LIT LPREN exp RPREN Eng. Maha Talaat Page 1

One level for each order of operation Left recursion for left assiciativity Now try constructing 2 parse trees for 5-3-2. Write down an unambiguous grammar that generates the set of strings { s;, s;s;, s;s;s;,... } and give leftmost and rightmost derivation for the string s;s; using your grammar. stmt-sequence s ; stmt-sequence s; Left most derivation (1) stmt-sequence => s;stmt-sequence (2) => s;s; Right most derivation (1) stmt-sequence => s;stmt-sequence (2) => s;s; leftmost will be the same as rightmost derivation for s;s; Given the grammar ( ) e a- describe the language it generates generates the strings of all "balanced parentheses." Description : This grammar produces an infinite set of strings of the characters ( and ) in which 0 or more ( ) can be generated and each ( ) can contain 0 or more ( ) inside. b- show that it is ambiguous consider the expression ( ) ( ) ( ) It has 2 different parse trees Parse Tree(1) ( ) ( ) ( ) Eng. Maha Talaat Page 2

Parse Tree(2) ( ) ( ) ( ) So, this grammar is ambiguous The following grammar generates all regular expressions over the alphabet of letters rexp rexp rexp rexp rexp rexp * ( rexp ) letter a- give a derivation for the regular expressions ( ab b )* using this grammar. (1) rexp => rexp * [exp rexp * ] (2) rexp => ( rexp ) * [exp ( rexp ) ] (3) => (rexp rexp ) * [exp rexp rexp] (4) => (rexp rexp rexp ) * [exp rexp rexp] (5) => (letter rexp rexp ) * [exp letter] (6) =>( a rexp rexp) * [letter a] (7) =>( a letter rexp) * [exp letter] (8) =>( a b rexp) [letter b] (9) =>( a b rexp) [exp letter] (10) =>( a b b) [letter c] b- Show that this grammar is ambiguous this grammar is ambiguous because it generates 2 different parse trees for the expression ab c c- Rewrite this grammar to establish the correct precedence of the operators rexp rexp rterm rterm rterm rterm rfactor rfactor rfactor rfactor * (rexp) letter d- What associativity does your answer in part c give to the binary operator? why? the associativity is left because a series of operators with same precedence will be evaluated from left to right Eng. Maha Talaat Page 3

Write an unambiguous grammar for Boolean expressions that includes the constants true and false, and the operator and,or,not and parentheses. Be sure to give or lowest precedence and give the not the highest precedence.lso allow repeated not s. Similar to the arithmetic expression example bexp bexp or term b term or b term b term and notfactor notfactor and && notfactor not bfactor b factor not! b factor (b exp ) b literal bliteral true false Given the following grammar if-stmt other e if-stmt if( exp ) else-part else-part else e exp 0 1 a- draw a parse tree for the string if (0) if(1) other else else other If-stmt if ( exp ) Else-part else 0 other if ( exp ) Else-part 1 other else Eng. Maha Talaat Page 4

b- what is the purpose of 2 else s the purpose of 2 else is to match with 2 if c- is similar code permissible in C and C#? explain this code is permissible in C Eng. Maha Talaat Page 5