Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | godfrey-thornton |
View: | 215 times |
Download: | 2 times |
Languages & Languages & GrammarsGrammars
GrammarsGrammars
A set of rules which govern the A set of rules which govern the structure of a languagestructure of a language
<sentence> <nounPhrase><predicate><sentence> <nounPhrase><predicate>
<nounPhrase> Fritz<nounPhrase> Fritz
<nounPhrase> The dog<nounPhrase> The dog
<predicate> ate <predicate> ate
<predicate> left<predicate> left
Formal Grammar NotationFormal Grammar Notation
G = {V,T,S,P}G = {V,T,S,P}– V are variables (<predicate>)V are variables (<predicate>)– T are terminals (Fritz)T are terminals (Fritz)– S is the start variable (<sentence>)S is the start variable (<sentence>)– P are the production rulesP are the production rules
Let W be a string of variables and Let W be a string of variables and terminalsterminals
W Y means that W can be transformed W Y means that W can be transformed into Y using the production rulesinto Y using the production rules
Languages + GrammarsLanguages + Grammars
A grammar defines a languageA grammar defines a language Many grammars can define the same Many grammars can define the same
languagelanguage Grammars that generate the same Grammars that generate the same
language are equivalent.language are equivalent.
Formal languagesFormal languages
Forget words; letters onlyForget words; letters only S is usually start symbolS is usually start symbol What language?What language? S -> aA | S -> aA | A -> bSA -> bS
Exercises... or Puzzles?Exercises... or Puzzles? Write a grammar that generates:Write a grammar that generates:
L={aL={annbbmm, n<2, m<=2}, n<2, m<=2} Write a grammar that generates:Write a grammar that generates:
L={aL={annbbmm, n>0, m>=0}, n>0, m>=0} Write a grammar that generates all Write a grammar that generates all
strings on {a,b}* with exactly 2 astrings on {a,b}* with exactly 2 a ’’ss Write a grammar that generates:Write a grammar that generates:
L={aL={annbbnn, n>0}, n>0} Write a grammar that generates:Write a grammar that generates: L = {w L = {w {a}*: |w| mod 4 = 0} {a}*: |w| mod 4 = 0}
Mathy QuestionsMathy Questions
Suppose a grammar for L1 has start Suppose a grammar for L1 has start symbol S1 and a grammar for L2 has start symbol S1 and a grammar for L2 has start symbol S2. What grammar describessymbol S2. What grammar describes– L1 U L2? L1 U L2? – L1L2? L1L2? – L1*?L1*?
Can you prove that your answer is correct?Can you prove that your answer is correct? Can you prove your neighborCan you prove your neighbor’’s answer is s answer is
wrong?wrong?
RecursionRecursion
Generate a+Generate a+– S -> aS | aS -> aS | a
Generate a*Generate a*– S -> aS | S -> aS |
Impose OrderImpose Order
Language: aLanguage: a++bb++
– S -> ABS -> AB– A -> aA | aA -> aA | a– B -> bB | bB -> bB | b
Relationship between symbolsRelationship between symbols
Language: aLanguage: annbbn n n>0n>0– S -> aSb | abS -> aSb | ab
Context-Free GrammarsContext-Free Grammars
A grammar is context-free if all A grammar is context-free if all production rules have only one non-production rules have only one non-terminal on the left-hand sideterminal on the left-hand sideA -> aSaA -> aSa
A -> ABA -> AB
A -> aA -> a Not context-free:Not context-free:
ABB -> aaSBABB -> aaSB
Members of CFLsMembers of CFLs To see if a string is a member of a CFL, replace To see if a string is a member of a CFL, replace
every non-terminal with the right side of one of every non-terminal with the right side of one of its production rules. its production rules.
S -> ABS -> ABA -> aaA | A -> aaA | λλB -> Bb | B -> Bb | λλ
Derivation of string aab:Derivation of string aab:S -> AB -> aaAB -> aaB -> aaBb -> aabS -> AB -> aaAB -> aaB -> aaBb -> aab
So, aab is a So, aab is a sentencesentence in the language, and aaAB in the language, and aaAB is a is a sententialsentential form. form.
TheoryTheory
Not all Context-free languages are Not all Context-free languages are regular.regular.
Example: aExample: annbbnn
Can you write a CF grammar?Can you write a CF grammar? Can you write an automaton?Can you write an automaton?
TheoryTheory
BUT: All regular languages are also BUT: All regular languages are also context-free.context-free.
How could we prove this?How could we prove this?
CF
Reg
Automata to GrammarAutomata to Grammar
States are non-terminalsStates are non-terminals Alphabet letters are the terminalsAlphabet letters are the terminals Start state corresponds to Start Start state corresponds to Start
SymbolSymbol Final States go to lambdaFinal States go to lambda
S -> aAS -> aA A -> aA | aS | bB | CA -> aA | aS | bB | C B -> bCB -> bC C -> λC -> λ
DerivationsDerivations
Leftmost derivation always replaces Leftmost derivation always replaces the leftmost variable in the sentential the leftmost variable in the sentential form next.form next.
Rightmost derivation always replaces Rightmost derivation always replaces the one on the right.the one on the right.
Derivations can be shown using a Derivations can be shown using a derivation tree. derivation tree.
ExampleExampleS -> aABS -> aAB
A -> bBbA -> bBb
B -> A | B -> A | λλ
Derive the string abbbbDerive the string abbbb
Leftmost: S-> a A B -> a bBb B -> a bAb B Leftmost: S-> a A B -> a bBb B -> a bAb B -> a b bBb b B -> a b b b b B -> -> a b bBb b B -> a b b b b B -> a b b b ba b b b b
Rightmost:Rightmost:
Derivation TreesDerivation Trees
Ordered tree in which:Ordered tree in which:– Interior nodes are left-hand sides of Interior nodes are left-hand sides of
rules (variables) rules (variables) – Children of a node are right-hand sidesChildren of a node are right-hand sides– Root is start symbolRoot is start symbol– Leaves are terminalsLeaves are terminals
Reading the leaves from left to right Reading the leaves from left to right is the yield of the tree (a sentence in is the yield of the tree (a sentence in the language)the language)
Example:Example:
S -> aABS -> aABA -> bBbA -> bBbB -> A | B -> A | λλ
Show derivation tree Show derivation tree for derivation of for derivation of abbbbabbbb
S
a A B
b B b λ
A
b B b
λ
AmbiguityAmbiguity For any string in a context-free language, there may be For any string in a context-free language, there may be
more than one derivation tree that produces it. This is more than one derivation tree that produces it. This is ambiguity.ambiguity.
Programming Languages cannot have ambiguity.Programming Languages cannot have ambiguity. Try to find equivalent, unambiguous grammars, but it canTry to find equivalent, unambiguous grammars, but it can ’’t t
always be done.always be done. If there is any unambiguous grammar for a language, it is If there is any unambiguous grammar for a language, it is
unambiguousunambiguous.. If the language has no unambiguous grammar, the language If the language has no unambiguous grammar, the language
is is inherently ambiguousinherently ambiguous..
ExercisesExercises Grammar: Grammar:
– S -> Sa | SSb | b | S -> Sa | SSb | b | Give rightmost derivation of string Give rightmost derivation of string
bbaababbaaba Draw derivation tree Draw derivation tree Show that the grammar is ambiguousShow that the grammar is ambiguous