CONTEXT FREE GRAMMAR presented by Mahender reddy.

Slides:



Advertisements
Similar presentations
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
CS5371 Theory of Computation
176 Formal Languages and Applications: We know that Pascal programming language is defined in terms of a CFG. All the other programming languages are context-free.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Chapter 3: Formal Translation Models
Specifying Languages CS 480/680 – Comparative Languages.
COP4020 Programming Languages
Markup Languages & XML - BY VISHAL KAMTAM VENKATESH.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
EECS 6083 Intro to Parsing Context Free Grammars
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Syntax Analysis The recognition problem: given a grammar G and a string w, is w  L(G)? The parsing problem: if G is a grammar and w  L(G), how can w.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Chapter 5 Context-Free Grammars
Grammars CPSC 5135.
1 Chapter 5 Context-Free Grammars and Languages Cathedral of St. Basil the Blessed, Red Square, Moscow, Russia.
CS 3240: Languages and Computation Context-Free Languages.
ISBN Chapter 3 Describing Syntax and Semantics.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
LESSON 04.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Overview of Previous Lesson(s) Over View  In our compiler model, the parser obtains a string of tokens from the lexical analyzer & verifies that the.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
Chapter 5 Context-free Languages
Syntax Analyzer (Parser)
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
AUTOMATA THEORY. Chapter 05 CONTEX-FREE GRAMMERS AND LANGUAGES.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
5. Context-Free Grammars and Languages
Chapter 3 – Describing Syntax
Context-Free Languages & Grammars (CFLs & CFGs)
Context-Free Languages & Grammars (CFLs & CFGs)
Context-Free Languages
Formal Language & Automata Theory
CS510 Compiler Lecture 4.
Chapter 3 – Describing Syntax
Context free grammar.
Compiler Construction
Syntax versus Semantics
5. Context-Free Grammars and Languages
Finite Automata and Formal Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Theory of Computation Lecture #
Context-Free Grammars
Faculty of Computer Science and Information System
Presentation transcript:

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= 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 then | if then else → | We can prove it as an Ambiguous Grammar: → if then if then else Two distinct parse trees can be constructed from the same sentential form.

First Parse tree if then else if then

if then if then else Second parse tree

Unambiguous grammar for the above language is: -> | -> if then else | any non-if statement -> if then | if then else 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 x S * S yz S S * S S +S xy z 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.

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

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 people who drive too slow HTML source in the fast lane. The text can be viewed as : People who drive 1. too slow 2. in the fast lane.

HTML GRAMMAR Char  a | A | … Text  ε | Char Text Doc  ε | Element Doc Element  Text | Doc | Doc | List ListItem  Doc 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

Example: <!DOCTYPE PcSpecifications [ ]>

Is similar to the production PROCESSOR -> MANF MODEL SPEED In the similar way Disk -> HD|CD 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: Inroduction to Automata theory,languages and computation, second edition by JOHN E.HOPCROFT,RAJEEV MOTWANI,JEFFREY D.ULLMAN