Theory of Computation Lecture #22-23-24.

Slides:



Advertisements
Similar presentations
4.5 Inherently Ambiguous Context-free Language For some context-free languages, such as arithmetic expressions, may have many different CFG’s to generate.
Advertisements

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.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\}
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Specifying Languages CS 480/680 – Comparative Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
COP4020 Programming Languages
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CMSC 330: Organization of Programming Languages
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.
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.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
CS 3240: Languages and Computation Context-Free Languages.
Homework #3 J. H. Wang Nov. 1, Homework #3 Chap. 4 –4.1 (c) –4.7 (c) –4.8 (a)(b)(c) –4.11.
CMSC 330: Organization of Programming Languages
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Lecture 3: Parsing CS 540 George Mason University.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Regular Grammars Reading: 3.3. What we know so far…  FSA = Regular Language  Regular Expression describes a Regular Language  Every Regular Language.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
Lecture 15 Ambiguous Grammars Topics: Context Free Grammars Language generated by a grammar Proofs with L(G) Ambiguous grammars October 20, 2008 CSCE 355.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
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.
Introduction to Parsing
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
5. Context-Free Grammars and Languages
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
PROGRAMMING LANGUAGES
CONTEXT-FREE LANGUAGES
Context-Free Grammars: an overview
Formal Language & Automata Theory
Fall Compiler Principles Context-free Grammars Refresher
Introduction to Parsing (adapted from CS 164 at Berkeley)
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
Context-Free Grammars
CSE 105 theory of computation
Compiler Construction
Context Free Languages
Syntax Analysis Sections :.
Context-Free Grammars
Context-Free Grammars
Programming Language Syntax 2
5. Context-Free Grammars and Languages
Chapter 2: A Simple One Pass Compiler
CHAPTER 2 Context-Free Languages
CSC 4181Compiler Construction Context-Free Grammars
فصل دوم Context-Free Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSC 4181 Compiler Construction Context-Free Grammars
Context-Free Grammars
Fall Compiler Principles Context-free Grammars Refresher
CSE 105 theory of computation
Task Construct CFG that generates the language L = {w  {a,b}*: length(w)  2 and second letter of w from right is a}
COSC 3340: Introduction to Theory of Computation
Context-Free Grammars
Context Free Grammars-II
Programming Languages 2nd edition Tucker and Noonan
Answer Questions about Exam2 problems
Faculty of Computer Science and Information System
CSE 105 theory of computation
Presentation transcript:

Theory of Computation Lecture #22-23-24

Definition The language generated by CFG Consists of those strings which can be produced from the start symbol S using the production Rules The language generated by CFG is called Context Free Language(CFL)

Ambiguity A CFG is said to be ambiguous if there is at least 1 string in L(G) having two or more distinct derivations L(G) language generated by grammer

Ambiguity

Arithmetic Expression Example

Arithmetic Expression Example 4+2*3

So Ambiguity is A CFG is ambiguous if there is some word it generates which has two different parse trees A CFG that is not ambiguous is called unambiguous

Arithmetic Expressions E - Expression T - Term F - Factor E is expression, T is term of expression and F is factor of a term E is expression, T is term of expression and F is factor of a term

Derivation of 4+2*3 using AE Productions Derivation E - Expression T - Term F - Factor E is expression, T is term of expression and F is factor of a term

Simple Example 1 + 1 + a Suppose CFG is (1) S → S + S (2) S → 1 (3) S → a Generate the string from the given CFG 1 + 1 + a

Derivation S → S+S (rule 1 on first S) → S+S+S (rule 1 on second) → S+1+S (rule 2 on second S) → S+1+a (rule 3 on third S) → 1+1+a (rule 2 on first S)

Types of Derivations Leftmost derivation, it is always the leftmost nonterminal Rightmost derivation, it is always the rightmost nonterminal

Left Most Derivation S → S+S (rule 1 on first S) → 1+1+a (rule 3 on first S)

Left Most Derivation

Right Most Derivation S → S + S (Rule-1) → S + a (Rule-3) → S + S + a (Rule-1) → S + 1 + a (Rule-2) → 1 + 1 + a (Rule-2)

Right Most Derivation

Parse Tree from given input string Grammar E - Expression T - Term F - Factor E is expression, T is term of expression and F is factor of a term

Parse Tree from given input string Language: Input String: Grammar Parse Tree

Algebraic expressions Here is a context-free grammar for syntactically correct infix algebraic expressions in the variables x, y and z: S → x S → y S → z S → S + S S → S - S S → S * S S → S / S S → ( S ) Normally algebraic expression is given in he infix form where operator is placed between operands

Generate Following String ( x + y ) * x - z * y / ( x + x ) - then Note that: x, y, z are terminals S is non-Terminal

String Generation S → x S → y S → z S → S + S S → S - S S → S * S S → S - S (by rule 5) → S * S - S (by rule 6, applied to the leftmost S) → S * S - S / S (by rule 7, applied to the rightmost S) → ( S ) * S - S / S (by rule 8, applied to the leftmost S) → ( S ) * S - S / ( S ) (by rule 8, applied to the rightmost S) → ( S + S ) * S - S / ( S ) (etc.) → ( 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 - S * y / ( x + S ) → ( x + y ) * x - z * y / ( x + S ) → ( x + y ) * x - z * y / ( x + x ) S → x S → y S → z S → S + S S → S - S S → S * S S → S / S S → ( S ) x,y,z are terminals and S is non-Terminal

Parse Tree

NFA-Ʌ to CFG

NFA-Ʌ to CFG

NFA-Ʌ to CFG

NFA to CFG S aS S bS S bZ S aZ Z ^

NFA to CFG

NFA to CFG NFA CFG Here, the variables V1, V2, and V3 represent the states q1, q2, and q3 of the NFA. And, since, the state q3 is a final state, we added the rule, V3 → Ɛ to the grammar