290 likes | 641 Views
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.
E N D
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 • 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.
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.
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:
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)
Example: S • S -> aABA -> bBbB -> A | λ • Show derivation tree for derivation of abbbb a A B b B b λ A b B b λ
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 λ
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.
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
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.
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 Σ*
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.
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
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.
Other Parsing Results for CFLs • There exists a parsing algorithm of order (|w|)3. • There is no known linear algorithm for parsing CFLs.
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.
Simple Grammar Examples • Simple Grammar Example: S -> aS S -> bSS S -> c • Not a Simple Grammar: S -> aS S -> bSS S -> aSSS -> c
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…
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.
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