1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.

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

Context-Free Grammars
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
CS5371 Theory of Computation
1 、 Alphabet Non-empty set of symbols , usually expressed in  、 V or Other Upper-case Greece Letter 2 、 Symbol(Character) Elements in alphabet, finest.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
COP4020 Programming Languages
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Context-Free Grammars
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
1 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the strings of a language. Notational Example.
1 Chapter Construction Techniques. 2 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
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.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Chapter 5 Context-Free Grammars
Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Lecture 15 Naveen Z Quazilbash Ambiguity. Overview S-Grammars Ambiguity in Grammars Ambiguous grammars and Unambiguous Grammars.
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.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
Context-Free Grammars Chapter 11. Languages and Machines.
CS 3240: Languages and Computation Context-Free Languages.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
Midterm 1 Breakdown >79 3 >29 7 >69 5 >19 5 >59 7 >49 9 >39 7.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
1 Efficient Parsing and Ambiguity. Ambiguity A grammar G is ambiguous if – There exists w ∊ L(G) such that – There are two parse trees for w Allows multiple.
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.
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.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Context-Free Languages. Regular Languages Context-Free Languages.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Chapter 5 Context-free Languages
Context-Free Languages
Three Basic Concepts Languages Grammars Automata.
Chapter 5 Context-Free Languages Regular language are effective in describing certain simple patterns. But it is not enough to describe programming languages.
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Grammars.
CONTEXT-FREE LANGUAGES
Formal Language & Automata Theory
Recap lecture 33 Example of trees, Polish Notation, examples, Ambiguous CFG, example,
PARSE TREES.
Context-Free Languages
Context-Free Grammars and Languages
REGULAR LANGUAGES AND REGULAR GRAMMARS
5. Context-Free Grammars and Languages
Finite Automata and Formal Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Derivations and Languages
Theory of Computation Lecture #
CFGs: Formal Definition
Presentation transcript:

1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties of programming languages

2 Context-Free Grammars G = (V, T, S, P) Productions are of the form: A  x A  V and x  (V  T) *

3 Context-Free Grammars A regular language is also a context-free language.

4 Context-Free Grammars A regular language is also a context-free language. Why the name?

5 Example G = ({S}, {a, b}, S, P) P = { S  aSa S  bSb S  } S  aSa  aaSaa  aabSbaa  aabbaa

6 Example G = ({S}, {a, b}, S, P) P = { S  aSa S  bSb S  } S  aSa  aaSaa  aabSbaa  aabbaa L(G) = {ww R | w  {a, b}*}

7 Example G = ({S, A, B}, {a, b}, S, P) P = { S  abB A  aaBb B  bbAa A  } S  abB  abbbAa  abbbaaBba  abbbaabbAaba  abbbaabbaba

8 Example L = {a n b m | n  m} is context-free. G = (?, {a, b}, S, ?)

9 Example L = {a n b m | n  m} is context-free. G = (?, {a, b}, S, ?) P = { S  AS 1 | S 1 B S 1  aS 1 b | A  aA | a B  bB | b }

10 Example G = ({S}, {a, b}, S, P) P = { S  aSb | SS | } S  aSb  aaSbb  aaSSbb  aaabSbb  aaababbb

11 Example G = ({S}, {a, b}, S, P) P = { S  aSb | SS | } S  aSb  aaSbb  aaSSbb  aaabSbb  aaababbb L(G) = ?

12 Derivations G = ({S, A, B}, {a, b}, S, {S  AB, A  aaA, A , B  Bb, B  }) S  AB  aaAB  aaB  aaBb  aab S  AB  ABb  aaABb  aaAb  aab

13 Derivations Leftmost: in each step the leftmost variable in the sentential form is replaced. Rightmost: in each step the rightmost variable in the sentential form is replaced.

14 Example G = ({S, A, B}, {a, b}, S, {S  AB, A  aaA, A , B  Bb, B  }) S  AB  aaAB  aaB  aaBb  aab leftmost S  AB  ABb  aaABb  aaAb  aab

15 Derivation Trees A  abABc A bacBA ordered tree

16 Derivation Trees Let G = (V, T, S, P) be a context-free grammar. An ordered tree is a derivation tree iff: 1.The root is labeled S. 2.Every leaf has a label from T  { }. 3.Every interior vertex has a label from V. 4.A vertex has label A  V and its children are labeled a 1, a 2,..., a n iff P contains the production A  a 1 a 2... a n. 5.A leaf labeled has no siblings.

17 Derivation Trees Let G = (V, T, S, P) be a context-free grammar. An ordered tree is a partial derivation tree iff: 1.The root is labeled S. 2.Every leaf has a label from T  { }. Every leaf has a label from V  T  { }. 1.Every interior vertex has a label from V. 2.A vertex has label A  V and its children are labeled a 1, a 2,..., a n iff P contains the production A  a 1 a 2... a n. 5.A leaf labeled has no siblings.

18 Derivation Trees The string of symbols obtained by reading the leaves of a tree from left to right (omitting any 's encountered) is called the yield of the tree.

19 Derivation Trees S  aAB A  bBb B  A | yield: abbbb S b a B b A B bb A B

20 Derivation Trees S  aAB A  bBb B  A | S b a B b A B bb A B partial derivation tree

21 Sentential Forms & Derivation Trees Let G = (V, T, S, P) be a context-free grammar.  w  L(G) iff there exists a derivation tree of G whose yield is w.  If t G is a partial derivation tree of G whose root label is S, then the yield of t G is a sentential form of G.

22 Membership and Parsing Membership algorithm: tells if w  L(G). Parsing: finding a sequence of productions by which w  L(G) is derived.

23 Membership and Parsing S  SS | aSb | bSa | w = aabb

24 Exhaustive Search Parsing Top-down parsing. S  SS | aSb | bSa | w = aabb 1. S  SS S  SS  SSS S  aSb  aSSb 2. S  aSb S  SS  aSbS S  aSb  aaSbb 3. S  bSa S  SS  bSaS S  aSb  abSab 4. S  S  SS  S S  aSb  ab S  aSb  aaSbb  aabb

25 Exhaustive Search Parsing It is not efficient. If w  L(G) then it may never terminate, due to: A  B A 

26 Exhaustive Search Parsing Suppose G = (V, T, S, P) is a context-free grammar which does not have any rule of the form A  B or A . Then the exhaustive search parsing method can decide if w  L(G) or not.

27 Exhaustive Search Parsing Suppose G = (V, T, S, P) is a context-free grammar which does not have any rule of the form A  B or A . Then the exhaustive search parsing method can decide if w  L(G) or not. Proof: no more than |w| rounds are involved.

28 Exhaustive Search Parsing Suppose G = (V, T, S, P) is a context-free grammar which does not have any rule of the form A  B or A . Then the exhaustive search parsing method can decide if w  L(G) or not. Proof: no more than |w| rounds are involved. Complexity: |P| |w|.

29 Theorem For every context-free grammar G, there exists an algorithm that parses any w  L(G) in a number of steps proportional to |w| 3. Not satisfactory as linear time parsing algorithm (proportional to the length of a string)

30 Simple Grammars (S-Grammars) G = (V, T, S, P) Productions are of the form: A  ax A  V, a  T, and x  V *, and any pair (A,a) can occur in at most one rule.

31 Simple Grammars (S-Grammars) Any w  L(G) can be parsed in at most |w| steps. w = a 1 a 2... a n S  a 1 A 1 A 2... A m  a 1 a 2 B 1 B 2... B k A 1 A 2... A m

32 Simple Grammars (S-Grammars) Many features of Pascal-like programming languages can be expressed with s-grammars.

33 Ambiguity A context-free grammar G is said to be ambiguous if there exits some w  L(G) that has at least two distinct derivation trees.

34 Ambiguity S  aSb | SS | w = aabb S a a b b S S S a a b b S S S S

35 Ambiguity G = ({E, I}, {a, b, c, +, *, (, )}, E, P) w = a + b * c E  I E  E + E E  E * E E  (E) I  a | b | c

36 Ambiguity G = ({E, T, F, I}, {a, b, c, +, *, (, )}, E, P) w = a + b * c E  T | E + T T  F | T * F E  E * E F  I | (E) I  a | b | c

37 Ambiguity If L is a context-free language for which there exists an unambiguous grammar, then L is said to be unambiguous.

38 Ambiguity If L is a context-free language for which there exists an unambiguous grammar, then L is said to be unambiguous. If every grammar that generates L is ambiguous, then L is called inherently ambiguous.

39 Ambiguity L = {a n b n c m }  {a n b m c m } is inherently ambiguous. L = L 1  L 2 S  S 1 | S 2 S 1  S 1 c | A S 2  aS 2 | B A  aAb | B  bBc |

40 CFGs and Programming Languages Important uses of formal languages:  to define precisely a programming language.  to construct an efficient translator for it. RLs are used for simple patterns, while CFLs for more complicated aspects.

41 CFGs and Programming Languages S-grammars: ::= if ::= then ::= else

42 Homework Exercises: 2, 3, 4, 6, 7, 9, 15, 17, 22 of Section Linz’s book. Exercises: 1, 2, 5, 6, 8, 10, 11, 12, 13, 15 of Section Linz’s book. Exercises: 1, 2, 3 of Section Linz’s book.