languages & grammars. grammars a set of rules which govern the structure of a language fritz...

22
Languages & Languages & Grammars Grammars

Upload: godfrey-thornton

Post on 02-Jan-2016

215 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

Languages & Languages & GrammarsGrammars

Page 2: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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

Page 3: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left 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

Page 4: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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.

Page 5: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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

Page 6: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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}

Page 7: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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?

Page 8: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

RecursionRecursion

Generate a+Generate a+– S -> aS | aS -> aS | a

Generate a*Generate a*– S -> aS | S -> aS |

Page 9: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

Impose OrderImpose Order

Language: aLanguage: a++bb++

– S -> ABS -> AB– A -> aA | aA -> aA | a– B -> bB | bB -> bB | b

Page 10: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

Relationship between symbolsRelationship between symbols

Language: aLanguage: annbbn n n>0n>0– S -> aSb | abS -> aSb | ab

Page 11: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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

Page 12: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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.

Page 13: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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?

Page 14: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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

Page 15: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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

Page 16: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

S -> aAS -> aA A -> aA | aS | bB | CA -> aA | aS | bB | C B -> bCB -> bC C -> λC -> λ

Page 17: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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.

Page 18: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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:

Page 19: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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)

Page 20: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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

λ

Page 21: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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..

Page 22: Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left

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