1 / 21

Context-Free Languages

Context-Free Languages. Reading: 5.1 & 5.2. Context-Free Grammars. A grammar is context-free if all production rules have only one non-terminal on the left-hand side A -> aSa A -> AB A -> a Regular Languages are a proper subset of context-free languages. Members of CFLs.

gareth
Download Presentation

Context-Free Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Context-Free Languages Reading: 5.1 & 5.2

  2. Context-Free Grammars • A grammar is context-free if all production rules have only one non-terminal on the left-hand side • A -> aSa • A -> AB • A -> a • Regular Languages are a proper subset of context-free languages.

  3. Members of CFLs • To see if a string is a member of a CFL, replace every non-terminal with the right side of one of its production rules. • S -> ABA -> aaA | λB -> Bb | λ • Derivation of string aab: S -> AB -> aaAB -> aaB -> aaBb -> aab • So, aab is a sentence in the language, and aaAB is a sentential form.

  4. Derivations • Leftmost derivation always replaces the leftmost variable in the sentential form next. • Rightmost derivation always replaces the one on the right. • Derivations can be shown using a derivation tree.

  5. Example S -> aAB A -> bBb B -> A | λ Derive the string abbbb Leftmost: S-> a A B -> a bBb B -> a bAb B -> a b bBb b B -> a b b b b B -> a b b b b Rightmost:

  6. Derivation Trees • Ordered tree in which: • Interior nodes are left-hand sides of rules (variables) • Children of a node are right-hand sides • Root is start symbol • Leaves are terminals • Reading the leaves from left to right is the yield of the tree (a sentence in the language)

  7. Example: S • S -> aABA -> bBbB -> A | λ • Show derivation tree for derivation of abbbb a A B b B b λ A b B b λ

  8. Partial Derivation Trees • Part of a derivation tree • (Doesn’t have to start with start symbol or end with all terminals.) • The yield of a derivation tree is a sentence in the language. • The yield of a partial derivation tree is a sentential form of the language. a A B b B b λ

  9. Parsing and Membership • Parsing: Showing the order of production rules that result in a derivation of the string. • Given a string, is it in the language? • Exhaustive Search Parsing: Apply all possible combinations of production rules and see if any derive the string. • Problems: Tedious, inefficient, may not terminate.

  10. Example: Exhaustive Search Parsing • S -> SS | aSb | bSa |  • Is aabb in the language? • Start with S. • Round one substitutions: • SS • aSb • bSa •  • Can remove last 2 sentential forms

  11. Example: Exhaustive Search Parsing • S -> SS | aSb | bSa | , looking for aabb • SS • aSb • Round 2 - replace first S with all possibilities • SSS, aSbS, bSaS, S • aSSb, aaSbb, abSab, ab • Delete ones that don’t match pattern: • SSS, aSbS, S • aSSb, aaSbb • Notice how the process grows wrt # production rules • Notice this process may never terminate if string is not in language.

  12. Better Membership • If the grammar does not have productions of the form: • A -> λ • A -> B • Exhaustive Search Parsing tweaked so that it always terminates for a string in Σ*

  13. New Exhaustive Search Parsing • Now, each non-terminal must represent at least one symbol. So once the length of the sentential form is longer than the string, you can terminate.

  14. New Exhaustive Search Parsing: Example • S -> SS | aSb | bSa | ab | ba; look for abba • New rule: can stop iterating if length of sentential form is bigger than 4. • Round 1: SS, aSb, bSa, ab, ba • Delete impossibilities: SS, aSb • Round 2: • SSS, aSbS, bSaS, abS, baS • aSSb, aaSbb, abSab, aabb, abab • Delete impossibilities (including anything with length > 4): • SSS, aSbS, abS

  15. Analyzing Exhaustive Search Parsing • String of length |w| terminates after 2|w| iterations. (Why?) • Since the algorithm might add lots of sentential forms in each iteration, it is still an exponential (inefficient) algorithm.

  16. Other Parsing Results for CFLs • There exists a parsing algorithm of order (|w|)3. • There is no known linear algorithm for parsing CFLs.

  17. Simple Grammars • A grammar is called Simple if all productions have the form: A -> aX where A is one variable, a is a terminal and X is a string of variables. Also, the pair (A,a) occurs at most once.

  18. Simple Grammar Examples • Simple Grammar Example: S -> aS S -> bSS S -> c • Not a Simple Grammar: S -> aS S -> bSS S -> aSSS -> c

  19. S.G. Parsing • Simple Grammars can be parsed in time proportional to |w| (linear). • Each step processes one symbol in the string. • Now, back to CFLs…

  20. Ambiguity • For any string in a context-free language, there may be more than one derivation tree that produces it. This is ambiguity. • Programming Languages cannot have ambiguity. • Try to find equivalent, unambiguous grammars, but it can’t always be done. • If there is any unambiguous grammar for a language, it is unambiguous. • If the language has no unambiguous grammar, the language is inherently ambiguous.

  21. Exercises • Grammar: • S -> Sa | SSb | b |  • Give rightmost derivation of string bbaaba • Draw derivation tree • Show that the grammar is ambiguous • Create an s-grammar for the language ((a+b)aa*b)+bb

More Related