CONTEXT FREE GRAMMAR. presented by Mahender reddy

Similar documents
Context-Free Grammars and Languages (2015/11)

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

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

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.

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

EECS 6083 Intro to Parsing Context Free Grammars

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

Syntax. In Text: Chapter 3

Introduction to Parsing

CMSC 330: Organization of Programming Languages

Syntax Analysis Check syntax and construct abstract syntax tree

CMSC 330: Organization of Programming Languages

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

Describing Syntax and Semantics

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

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

Optimizing Finite Automata

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics ISBN

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

3. Context-free grammars & parsing

Exercise 1: Balanced Parentheses

COP 3402 Systems Software Syntax Analysis (Parser)

Compilers Course Lecture 4: Context Free Grammars

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

CMSC 330: Organization of Programming Languages

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

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

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

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Compiler Construction

Dr. D.M. Akbar Hussain

Chapter 3. Describing Syntax and Semantics

Context-Free Grammars

Chapter 3. Describing Syntax and Semantics ISBN

CMSC 330: Organization of Programming Languages. Context Free Grammars

Formal Languages. Formal Languages

Parsing II Top-down parsing. Comp 412

CS 314 Principles of Programming Languages

Introduction to Parsing. Lecture 5

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

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

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Introduction to Parsing. Lecture 5

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

Ambiguous Grammars and Compactification

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Context Free Grammars. CS154 Chris Pollett Mar 1, 2006.

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

Introduction to Parsing Ambiguity and Syntax Errors

Programming Language Definition. Regular Expressions

Lecture 4: Syntax Specification

Introduction to Parsing Ambiguity and Syntax Errors

Context-Free Grammars

Part 3. Syntax analysis. Syntax analysis 96

3. DESCRIBING SYNTAX AND SEMANTICS

Properties of Regular Expressions and Finite Automata

Homework. Context Free Languages. Before We Start. Announcements. Plan for today. Languages. Any questions? Recall. 1st half. 2nd half.

Lecture 8: Context Free Grammars

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

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

Habanero Extreme Scale Software Research Project

Principles of Programming Languages COMP251: Syntax and Grammars

Chapter 3: Syntax and Semantics. Syntax and Semantics. Syntax Definitions. Matt Evett Dept. Computer Science Eastern Michigan University 1999

Chapter 3. Describing Syntax and Semantics

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

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

Context Free Languages

A Simple Syntax-Directed Translator

Chapter 4: Syntax Analyzer

Compiler Design Concepts. Syntax Analysis

Defining syntax using CFGs

CS 406/534 Compiler Construction Parsing Part I

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

CS 321 Programming Languages and Compilers. VI. Parsing

Part 5 Program Analysis Principles and Techniques

Building Compilers with Phoenix

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

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

afewadminnotes CSC324 Formal Language Theory Dealing with Ambiguity: Precedence Example Office Hours: (in BA 4237) Monday 3 4pm Wednesdays 1 2pm

COL728 Minor1 Exam Compiler Design Sem II, Answer all 5 questions Max. Marks: 20

Syntax. A. Bellaachia Page: 1

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

CSE302: Compiler Design

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

Principles of Programming Languages COMP251: Syntax and Grammars

7. Introduction to Denotational Semantics. Oscar Nierstrasz

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

CMPT 755 Compilers. Anoop Sarkar.

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Context-Free Languages and Parse Trees

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Programming Languages

Syntax Analysis Part I

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Lexical Analysis (ASU Ch 3, Fig 3.1)

Transcription:

CONTEXT FREE GRAMMAR presented by Mahender reddy

What is Context Free Grammar? Why we are using Context Free Grammar? Applications of Context free Grammar.

Definition of CFG: A Context free grammar is 4-tuple G=(V, T, P, S) where, V= set of variables or non-terminals. T= set of terminals. P= set of productions where each production is of the form V-> 1 2. where i is an arbitrary string of variables and terminals. S= Start variable

String membership: How to say a string belong to the language generated by CFG? 1. Derivation: Head to Body. 2. Recursive inference: Both are equivalent forms. Body to Head.

Example of CFG: How CFG works for Palindrome G: S-> 0A0 1A1 0 1 Consider a string w=001100 G can generate w as follows S->0A0 ->00A00 ->001A100 ->001100

Example for even no. of 0 s: A CFG for all binary strings with an even number of 0 s. Find the decomposition. If first symbol is 1, then even number of 0 s remain. If first symbol is 0, then go to next 0; after that again an even number of 0 s remain. This yields: S 1 S 0 A 0 S ε A 1 A ε Here is another CFG for the same language. Note that when first symbol is 0, what remains has odd number of 0 s. S 1 S 0 T ε T 1 T 0 S

More examples of CFG: L 1 = {0 n n 0 } L 2 = {0 n n 1 } L 3 ={0 i 1 j 2 k i=j or j=k, where i,j,k 0} L 4 ={0 i 1 j 2 k i=j or i=k, where i,j,k 1}

APPLICATIONS OF CONTEXT FREE GRAMMAR

1.CFG for Parsers: The component of compiler discovers the structure of source program and represents that structure by a parse tree. Programming languages use parentheses or brackets in a nested and balanced fashion. Example of balanced parentheses. A-> AA (A) Strings of balanced parentheses are (), (( )),(() ()).. Beginnings and endings of code blocks, such as begin and end in Pascal, or the curly braces {..} of C.

Example : A grammar that generates the possible sequences of if and else in C (represented as i and e, respectively) is: S -> SS is ises Q: Can we generate the following strings using the above grammar, And how?: ieie, iie, iei,? Some illegal strings are ei,ieeii. What about: iieie? The answer is yes.

Consider an example of C program: if(condition) {.. if (condition) statement; else statement; if (condition) statement; else statement;. } The two else s match their previous if s and the first if is unmatched.

Consider the following grammar: <if_stmt> if <logic_expr> then <stmt> if <logic_expr> then <stmt> else <stmt> <stmt> <if_stmt> <any non-if statement> We can prove it as an Ambiguous Grammar: <if_stmt> if <logic_expr> then if <logic_expr> then <stmt> else <stmt> Two distinct parse trees can be constructed from the same sentential form.

First Parse tree <if_stmt> if <logic_expr> then <stmt> else <stmt> <if_stmt> if <logic_expr> then <stmt>

Second parse tree <if_stmt> if <logic_expr> then <stmt> <if_stmt> if <logic_expr> then <stmt> else <stmt>

Unambiguous grammar for the above language is: <stmt> -> <matched> <unmatched> <matched> -> if <logic_expr> then <matched> else <matched> any non-if statement <unmatched> -> if <logic_expr> then <stmt> if <logic_expr> then <matched> else <unmatched> Example CFG for while and for loop. Expr -> for( Expr; Expr; Expr) loop Expr pool while(expr) loop Expr pool id

2.CFG for Algebraic expression

Consider the following grammar for algebraic expression S -> x y z S -> S+S S-S S*S S/S S -> (S) Generate the following string using the above grammar ( x + y ) * x - z * y / ( x + x ) It is as follows.

S (Start symbol) -> S - S -> S * S - S -> S * S - S / S -> ( S ) * S S / S -> ( S ) * S S / ( S ) -> ( S + S ) * S S / ( S ) -> ( S + S ) * S S * S /( S ) -> ( S + S ) * S S * S / ( S + S ) -> ( x + S ) * S S * S/ ( S+ S ) -> ( x +y ) *S S *S / ( S + S ) -> ( x +y ) * x- S * y / ( S +S ) -> ( x +y ) *x z * y / ( S +S ) -> ( x +y )* x z * y / ( x + S) -> (x + y ) * x- z * y / ( x + x )

Picking a different order of expansion will produce a different derivation, but the same parse tree. The parse tree will only change if we pick a different rule to apply at some position in the tree. Can different parse tree will generate same terminal string? Yes, for this particular grammar. Such grammars are called ambiguous grammar. Take an example of x + y * z for the above grammar. And construct a parse tree by applying different rules than two different parse trees can be constructed as follows which is ambiguous.

Two different Parse trees for x + y * z: S S S + S S * S x S * S S + S z y z x y However, the language described above grammar is not inherently ambiguous, an alternate unambiguous grammar can be developed.

S -> S + T S - T S * T S / T S -> T T -> x y z T -> ( S ) The alternative grammar will produce parse tree that is similar to the right one of the previous tree i.e; Implicitly assuming ( x + y ) * z which is not according to standard operator precedence. More elaborate unambiguous and context free grammar can be constructed that produce parse trees that obeys all desired operator precedence rules.

Syntactic expression in programming languages result = a*b + score + 10 * distance + c terminals variables Operators are also terminals Regular languages have only terminals but context free grammar has both terminals and non terminals. 23

3.Markup languages Tags tell us something about the semantics of various strings within the document. HTML has two major functions i.e; creating links between documents and describing the format of a document. Example <p> people who <em>drive</em> <ol> <li>too slow <li>in the fast lane. </ol> The text can be viewed as : People who drive 1. too slow 2. in the fast lane. HTML source

HTML GRAMMAR Char a A Text ε Char Text Doc ε Element Doc Element Text <EM> Doc </EM> <P> Doc <OL>List</OL> ListItem <LI> Doc </LI> List ε ListItem List

HTML is a fixed standard i.e; HTML has predefined tags. XML lets everyone define the data structures they need i.e; XML has user defined tags.

XML and DTD XML describes semantics of the text but not the format of the document. To make clear what the different kinds of tags are, and what structures may appear between matching pair of these tags. Example: 39 maple St A DTD is essentially CFG, with its own notation for describing the variables and productions The form of DTD is <!DOCTYPE name-of-dtd [ ]> list of element definitions An element definition has the form <!ELEMENT element-name (description of elements)>

Example: <!DOCTYPE PcSpecifications [ <!ELEMENT PCS (PC*)> <!ELEMENT PC (MODEL, PRICE, PROCESSOR, DISK+)> <!ELEMENT MODEL (#PCDATA)> <!ELEMENT PRICE (#PCDATA)> <!ELEMENT PROCESSOR (MANF, MODEL)> <!ELEMENT MANF (#PCDATA)> <!ELEMENT MODEL (#PCDATA)> <!ELEMENT DISK (HD CD)> <!ELEMENT HD (MANF, MODEL, SIZE)> <!ELEMENT CD (SPEED)> <!ELEMENT SPEED (#PCDATA)> <!ELEMENT SIZE (#PCDATA)> ]>

<!ELEMENT PROCESSOR (MANF, MODEL, SPEED)> Is similar to the production PROCESSOR -> MANF MODEL SPEED In the similar way <!ELEMENT DISK (HD CD)> Disk -> HD CD <!ELEMENT PC (MODEL, PRICE, PROCESSOR, DISK+)> PC -> Model Price Processor Disks Disks -> Disk Disk Disks

CFG is used where there is a general need of: Matching a symbol with another symbol, or Matching a count of one symbol with that of another symbol, or Recursively substituting one symbol with a string of other symbols

References: http://en.wikipedia.org/wiki/context-free_grammar http://www.epaperpress.com Inroduction to Automata theory,languages and computation, second edition by JOHN E.HOPCROFT,RAJEEV MOTWANI,JEFFREY D.ULLMAN