Presentation is loading. Please wait.

Presentation is loading. Please wait.

Grammars.

Similar presentations


Presentation on theme: "Grammars."— Presentation transcript:

1 Grammars

2 What is a grammar? Roughly speaking, a grammar is a set of rules that produce sentences (strings). For example, a simplistic grammar for the English language is the following: <SENTENCE> → <NOUN PHRASE><VERB> <NOUN PHRASE> → <ARTICLE><NOUN> <ARTICLE> → a | the <NOUN> → girl | dog <VERB> → barks | runs |sleeps

3 Production of the sentence “the dog barks”
<SENTENCE> → <NOUN PHRASE> <VERB> <NOUN PHRASE> → <ARTICLE> <NOUN> <ARTICLE> → a | the <NOUN> → girl | dog <VERB> → barks | runs |sleeps <SENTENCE> → <NOUN PHRASE> <VERB> → <ARTICLE> <NOUN> <VERB> → the <NOUN> <VERB> → the dog <VERB> → the dog barks

4 Production of the sentence “a girl runs”
<SENTENCE> → <NOUN PHRASE> <VERB> <NOUN PHRASE> → <ARTICLE> <NOUN> <ARTICLE> → a | the <NOUN> → girl | dog <VERB> → barks | runs |sleeps <SENTENCE> → <NOUN PHRASE> <VERB> → <ARTICLE> <NOUN> <VERB> → a <NOUN> <VERB> → a girl <VERB> → a girl runs

5 Production of the sentence “a girl runs”
<SENTENCE> → <NOUN PHRASE> <VERB> <NOUN PHRASE> → <ARTICLE> <NOUN> <ARTICLE> → a | the <NOUN> → girl | dog <VERB> → barks | runs |sleeps <SENTENCE> → <NOUN PHRASE> <VERB> → <ARTICLE> <NOUN> <VERB> → a <NOUN> <VERB> → a girl <VERB> → a girl runs Variables Terminals

6 Grammars (more formally)
A grammar is a quadruple (V, T, S, P) with V being a set of Variables T being a set of Terminals S in V being the start variable P being a set of productions rules

7 Derivation A string of terminals can be derived by the grammar if, starting from the start variable and substituting parts of the so far produced string (following the production rules) we can produce the string of terminals. The language that a grammar produces is the set of strings of terminals which can be derived by the grammar.

8 Another grammar S → aSb | ε Which language does this grammar produce?

9 Another grammar S → aSb | ε Which language does this grammar produce?

10 Another grammar S → aSb | ε Which language does this grammar produce?

11 Another grammar S → aSb | ε Which language does this grammar produce?

12 Another grammar S → aSb | ε Which language does this grammar produce?
S → aSb → ab

13 Another grammar S → aSb | ε Which language does this grammar produce?
→ aaSbb

14 Another grammar S → aSb | ε Which language does this grammar produce?
→ aaSbb → aabb

15 Another grammar S → aSb | ε Which language does this grammar produce?
→ aaSbb → aaaSbbb

16 Another grammar S → aSb | ε Which language does this grammar produce?
→ aaSbb → aaaSbbb → aaabbb

17 Another grammar S → aSb | ε Which language does this grammar produce?
→ aaSbb → aaaSbbb → anSbn

18 Another grammar S → aSb | ε Which language does this grammar produce?
S → aSb → ab S → aSb → aaSbb → aabb S → aSb → aaSbb → aaaSbbb → aaabbb S → aSb → aaSbb → … → anSbn → anbn

19 Another grammar S → aSb | ε Which language does this grammar produce?
S → aSb → ab S → aSb → aaSbb → aabb S → aSb → aaSbb → aaaSbbb → aaabbb S → aSb → aaSbb → … → anSbn → anbn L = {anbn : n ≥ 0}

20 L() : “proper opening and closing (, )”
Strings in the language: Strings not in the language:

21 L() : “proper opening and closing (, )”
Strings in the language: (), (()), ()(), ((()())()) Strings not in the language: (, )(, (()((

22 A grammar for the language L() : “proper opening and closing (, )”
S → (S) | SS | ε Example: Derivation of the string ((()())()) S → (S) → (SS) → ((S)S) → ((SS)S) → (((S)S)S) → (((ε)S)S) → ((()S)S) → ((()(S))S) → ((()(ε))S) → ((()S)S) → ((()())(S)) → ((()())(ε)) → ((()())())

23 Try it yourself Find a grammar that produces the language
LR = {wwR : w in {a,b}*}

24 Try it yourself Find a grammar that produces the language
LR = {wwR : w in {a,b}*} S → aSa | bSb | ε

25 Context free grammars All the grammars that we saw so far are called Context Free grammars. The CF grammars have a special rule form: The production rules must be of the form A ⟶ α A is in V α is in (V∪T)*

26 Context Free Languages
A language which is produced by a context free grammar is called context free language. Example of CF languages: L = {anbn : n ≥ 0} L() = set of strings of proper opening and closing (, ) LR = {wwR : w in {a,b}*}

27 Context Free Languages
A language which is produced by a context free grammar is called context free language. Example of CF languages: L = {anbn : n ≥ 0} S → aSb | ε L() = set of strings of proper opening and closing (, ) S → (S) | SS | ε LR = {wwR : w in {a,b}*} : S → aSa | bSb | ε

28 Parse Trees A parse tree for a string of terminals is a tree with the following properties: The root is the start variable Each intermediate node has as children from left to right all the variables and terminals in the sequence they appear in the right part of a derivation rule in which this variable appears in the left part. All the leafs are terminals appearing in the sequence they are in the string.

29 Parse Trees Consider the grammar: S → (S) → SS → ε
A parse tree for (()()) is the following

30 Parse Trees Consider the grammar: S → (S) → SS → ε
A parse tree for (()()) is the following S

31 Parse Trees Consider the grammar: S → (S) → SS → ε
A parse tree for (()()) is the following S S ( )

32 Parse Trees Consider the grammar: S → (S) → SS → ε
A parse tree for (()()) is the following S S ( ) S S

33 Parse Trees Consider the grammar: S → (S) → SS → ε
A parse tree for (()()) is the following S S ( ) S S ( S ) ( S )

34 Parse Trees Consider the grammar: S → (S) → SS → ε
A parse tree for (()()) is the following S S ( ) S S ( S ) ( S ) ε ε

35 Parse Trees Consider the grammar: S → (S) → SS → ε
A parse tree for (()()) is the following S S ( ) S S ( S ) ( S ) ε ε

36 Ambiguity For the previous language there is a unique parse tree for every string in the language. This is not always the case!!! Consider the language for mathematical expressions: E → E + E | E • E | (E) | N N → 0 | 1 | … | 9

37 Ambiguity This language is ambiguous because the string 2+2•2 has two different derivation trees. E E + E N E E 2 N N 2 2

38 Ambiguity This language is ambiguous because the string 2+2•2 has two different derivation trees. E E E N E + E 2 N N 2 2

39 Ambiguity The problem with that is that the same string can have two different meanings according to the way it is parsed. Example: 2 + 2•2 can denote: 2 + (2 • 2) = 6 –first parse tree (2 + 2) • 2 = 8 –second parse tree This is bad because there are two different ways to evaluate the same expression.

40 Ambiguity We can design an unambiguous grammar for the same language:
E → E + T | T T → | T • F | F F → (E) | N N → 0 | 1 | … | 9

41 Unique parse tree for 2+2•2

42 Closure under the reg. operations
Context Free languages are closed under the regular operations: Union ∪ Concatenation ∘ Star *

43 CF languages are closed under ∪
Assume that we have two CF languages L1, L2. There are CF grammars (V1, T1, S1, R1) and (V2, T2, S2, R2) producing them. Create a CF grammar for the union: Add a new variable S and the rules S ⟶ S1 | S2. Make sure that the sets of variables are completely disjoint. If not change the names of common symbols. The grammar for the union is (V1 ∪ V2, T1 ∪ T2, S, R1 ∪ R2).

44 Example Say we want to find a grammar for the union of L = {anbn : n ≥ 0} and LR = {wwR : w in {a,b}*} G1: S → aSb | ε and G2: S → aSa | bSb | ε The grammar for L ∪ LR is: S → S1 | S2 S1 → aS1b | ε S2 → aS2a | bS2b | ε

45 Try it yourself Prove that CF languages are closed under ∘

46 Try it yourself Prove that CF languages are closed under ∘ Same as before. Just add a new start variable S and a new rule S → S1S2

47 CF languages are closed under *
Assume that L is a CF language. There is a CF grammar (V, T, S, R) for L. Create a CF grammar for L*: Create a new start variable S’ Add the rules S’ ⟶ S’S’| S | ε


Download ppt "Grammars."

Similar presentations


Ads by Google