300 likes | 558 Views
Languages & Grammars. Languages. FORMAL languages, not natural languages (not even machine languages) Alphabet is all symbols allowed. Σ is alphabet, λ is null letter String variables: w = abba Concatenation, reverse, length Σ * is 0 or more letters, Σ + is 1 or more letters.
E N D
Languages • FORMAL languages, not natural languages (not even machine languages) • Alphabet is all symbols allowed. Σ is alphabet, λ is null letter String variables: w = abba Concatenation, reverse, length Σ* is 0 or more letters, Σ+ is 1 or more letters
Set Notation for Languages • Σ = {a,b} • Σ* = {λ,a,b,aa,ab,ba,bb,aaa,aab,…} • Σ+ = Σ* - {λ} • L = {anbn : n >0} • Which are in L? • λ • ab • aaa • aba • aaaaaabbb
More Language Notation Σ = {a,b,c}, L1 = {ab, bc, aba}, L2 = {c,bc,bcc} • L1 U L2 = • L1 ∩ L2 = • L1 = • L1 ∩ L2 = • L1L2 = • L22 = • L1* = • L2R =
Grammars • A set of rules which govern the structure of a language <sentence> <nounPhrase><predicate> <nounPhrase> Fritz <nounPhrase> The dog <predicate> ate <predicate> left
Formal Grammar Notation • G = {V,T,S,P} • V are variables (<predicate>) • T are terminals (Fritz) • S is the start variable (<sentence>) • P are the production rules • Let W be a string of variables and terminals • W Y means that W can be transformed into Y using the production rules
Languages + Grammars • A grammar defines a language • Many grammars can define the same language • Grammars that generate the same language are equivalent.
Formal languages • Forget words; letters only • S is usually start symbol • What language? • S -> aA | • A -> bS
Exercises • If L = {anbn ,n>0} what is L2? • Write a grammar that generates:L={anbm, n<2, m<=2}
... or Puzzles? • Write a grammar that generates:L={anbm, n>0, m>=0} • Write a grammar that generates all strings on {a,b}* with exactly 2 a’s • Write a grammar that generates:L={anbn, n>0} • Write a grammar that generates: • L = {w {a}*: |w| mod 4 = 0}
Mathy Questions • Suppose a grammar for L1 has start symbol S1 and a grammar for L2 has start symbol S2. What grammar describes • L1 U L2? • L1L2? • L1*? • Can you prove that your answer is correct? • Can you prove your neighbor’s answer is wrong?
Recursion • Generate a+ • S -> aS | a • Generate a* • S -> aS |
Impose Order • Language: a+b+ • S -> AB • A -> aA | a • B -> bB | b
Relationship between symbols • Language: anbn n>0 • S -> aSb | ab
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 • Not context-free: ABB -> aaSB
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.
Theory Not all Context-free languages are regular. Example: anbn Can you write a CF grammar? Can you write an automaton?
Theory CF Reg BUT: All regular languages are also context-free. How could we prove this?
Automata to Grammar States are non-terminals Alphabet letters are the terminals Start state corresponds to Start Symbol Final States go to lambda
S -> aA A -> aA | aS | bB | C B -> bC C -> λ
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 λ
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