Regular languages and CF grammars From lecture 7: S → S 1 | S 2 union S → S 1 S 2 concatenation S → SS 1 | Λ star CFG for ∅ . . . CFG for { σ } . . . Example L = bba ( ab ) ∗ + ( ab + ba ∗ b ) ∗ ba [M] E 4.11 Automata Theory Context-Free Languages Regular grammars 215 / 326
Regular languages and CF grammars S → S 1 | S 2 union concatenation S → S 1 S 2 S → SS 1 | Λ star Example L = bba ( ab ) ∗ + ( ab + ba ∗ b ) ∗ ba S → S 1 | S 2 S 1 → S 1 ab | bba S 2 → TS 2 | ba T → ab | bUb U → aU | Λ [M] E 4.11 Automata Theory Context-Free Languages Regular grammars 216 / 326
above We have seen constructions to apply the regular operations (union, concatenation and star) to context-free grammars. These we can now use to build CFG for regular expressions. There is a better way to build CFG for regular languages. Use finite automata, and simulate these using a very simple type of context-free grammar. These simple grammars are called regular.
Regular languages and CF grammars systematic approach Example a b a b S A B b a Automata Theory Context-Free Languages Regular grammars 217 / 326
Regular languages and CF grammars systematic approach Example axiom S initial state a b S → bA | aS transitions a b A → bA | aB S A B b B → bA | aS B → Λ accepting state a Automata Theory Context-Free Languages Regular grammars 218 / 326
Definition regular grammar (or right-linear grammar ) productions are of the form – A → σ B variables A , B , terminal σ – A → Λ variable A Theorem A language L is regular, if and only if there is a regular grammar generating L. Proof. . . [M] Def 4.13, Thm 4.14 Automata Theory Context-Free Languages Regular grammars 219 / 326
Leftmost derivation Definition A derivation in a context-free grammar is a leftmost derivation, if at each step, a production is applied to the leftmost variable-occurrence in the current string. A rightmost derivation is defined similarly. [M] D 4.16 α = α 1 A α 2 ⇒ G α 1 γα 2 = β for A → γ ∈ P derivation step The derivation step is leftmost iff α 1 ∈ Σ ∗ ℓ We write α ⇒ β Automata Theory Context-Free Languages Regular grammars 220 / 326
Expressions S → a | S + S | S ∗ S | ( S ) Σ = { a , + , ∗ , ( , ) } S ⇒ S + S ⇒ S +( S ) ⇒ S +( S ∗ S ) ⇒ S +( a ∗ S ) ⇒ a + ( a ∗ S ) ⇒ a + ( a ∗ a ) Derivation tree. . . [M] E 4.2, Fig 4.15 Automata Theory Context-Free Languages Expression, ambiguity 221 / 326
Expressions S S + S S → a | S + S | S ∗ S | ( S ) Σ = { a , + , ∗ , ( , ) } a ( ) S S ⇒ S + S ⇒ S +( S ) ⇒ S +( S ∗ S ) ⇒ S +( a ∗ S ) ⇒ a + ( a ∗ S ) ⇒ a + ( a ∗ a ) S * S a a [M] E 4.2, Fig 4.15 Automata Theory Context-Free Languages Expression, ambiguity 222 / 326
Expressions S S → a | S + S | S ∗ S | ( S ) Σ = { a , + , ∗ , ( , ) } S + S S ⇒ S + S ⇒ S +( S ) ⇒ S +( S ∗ S ) ⇒ S +( a ∗ S ) ⇒ a + ( a ∗ S ) ⇒ a + ( a ∗ a ) a ( ) S ℓ ℓ ℓ ℓ ℓ ⇒ S + S ⇒ a + S ⇒ a + ( S ) ⇒ a + ( S ∗ S ) S ⇒ S * S ℓ a + ( a ∗ S ) ⇒ a + ( a ∗ a ) a a [M] E 4.2, Fig 4.15 Automata Theory Context-Free Languages Expression, ambiguity 223 / 326
Well-formed formula ψ ::= p | ( ¬ ψ ) | ( ψ ∧ ψ ) | ( ψ ∨ ψ ) | ( ψ → ψ ) ((( ¬ p ) ∧ q ) → ( p ∧ ( q ∨ ( ¬ r )))) → (( ¬ p ) ∧ q ) ( p ∧ ( q ∨ ( ¬ r ))) ∧ ∧ ( ¬ p ) ( q ∨ ( ¬ r )) q p ¬ ∨ ( ¬ r ) p q ¬ r [H&R] Fig 1.3 Automata Theory Context-Free Languages Expression, ambiguity 224 / 326
Well-formed formula S ::= p | q | r | ( ¬ S ) | ( S ∧ S ) | ( S ∨ S ) | ( S → S ) derivation tree 2 parse tree vs. S → ( ) S → S ∧ ∧ ( )( ) S S S S ∧ ∧ q p ¬ ∨ ( ¬ ) q p ( ) S S S ∨ p q ¬ p q ( ¬ ) S r r 2 with all brackets explicit Automata Theory Context-Free Languages Expression, ambiguity 225 / 326
leftmost derivation ← → derivation tree Theorem If G is a context-free grammar, then for every x ∈ L ( G ) , these three statements are equivalent: 1 x has more than one derivation tree 2 x has more than one leftmost derivation 3 x has more than one rightmost derivation Proof. . . [M] Thm 4.17 Automata Theory Context-Free Languages Expression, ambiguity 226 / 326
Ambiguity leftmost derivation ← → derivation tree Theorem If G is a context-free grammar, then for every x ∈ L ( G ) , these three statements are equivalent: 1 x has more than one derivation tree 2 x has more than one leftmost derivation 3 x has more than one rightmost derivation [M] Thm 4.17 Definition A context-free grammar G is ambiguous , if for at least one x ∈ L ( G ) , x has more than one derivation tree (or, equivalently, more than one leftmost derivation). Otherwise: unambiguous [M] D 4.18 Automata Theory Context-Free Languages Expression, ambiguity 227 / 326
Ambiguity (1) Σ = { a , + , ∗ , ( , ) } S → a | S + S | S ∗ S | ( S ) S S a + a ∗ a S + S S * S ℓ ℓ ℓ ℓ S ⇒ S ∗ S ⇒ S + S ∗ S ⇒ a + S ∗ S ⇒ ℓ a + a ∗ S ⇒ a + a ∗ a S + S a a S * S ℓ ℓ ℓ ℓ ℓ S ⇒ S + S ⇒ a + S ⇒ a + S ∗ S ⇒ a + a ∗ S ⇒ a a a a a + a ∗ a leftmost derivation ← → derivation tree Automata Theory Context-Free Languages Expression, ambiguity 228 / 326
Ambiguity (2) Σ = { a , + , ∗ , ( , ) } S → a | S + S | S ∗ S | ( S ) a + a + a S S ℓ ℓ ℓ ℓ S ⇒ S + S ⇒ S + S + S ⇒ a + S + S ⇒ ℓ S + S S + S a + a + S ⇒ a + a + a S ⇒ S + S ⇒ S + S + S ⇒ a + S + S ⇒ S + S a a S + S a + a + S ⇒ a + a + a a a a a ℓ ℓ ℓ ℓ ⇒ S + S ⇒ a + S ⇒ a + S + S S ⇒ ℓ a + a + S ⇒ a + a + a leftmost derivation ← → derivation tree Automata Theory Context-Free Languages Expression, ambiguity 229 / 326
above This example is a little weird. In the derivation step S + S ⇒ S + S + S we cannot really see which S has been rewritten. In general we will assume that we know (without introducing extra notation to our definitions).
(un)ambiguous grammars Expr ambiguous: S → a | S + S | S ∗ S | ( S ) [M] E 4.20 a + a ∗ a unambiguous: . . . Automata Theory Context-Free Languages Expression, ambiguity 230 / 326
(un)ambiguous grammars Expr ambiguous: S → a | S + S | S ∗ S | ( S ) [M] E 4.20 a + a ∗ a unambiguous: S → S + T | T T → T ∗ F | F F → a | ( S ) [M] Thm 4.25 The proof of the unambiguity does not have to be known for the exam Automata Theory Context-Free Languages Expression, ambiguity 231 / 326
Expressions railroad diagram Expression Terme + Terme Facteur × Facteur Variable ( Expression ) Variable X Y Z http://math.et.info.free.fr/TikZ/index.html Chapitre 7 Automata Theory Context-Free Languages Expression, ambiguity 232 / 326
Equal number AeqB = { x ∈ { a , b } ∗ | n a ( x ) = n b ( x ) } aaabbb , ababab , aababb , . . . S → Λ | aB | bA A → aS | bAA A generates n a ( x ) = n b ( x ) + 1 B → bS | aBB B generates n a ( x ) + 1 = n b ( x ) S ⇒ aB ⇒ aaBB ⇒ aabSB ⇒ . . . (different options) (1) aabB ⇒ aabaBB ⇒ aababSB ⇒ aababB ⇒ aababbS ⇒ aababb (2) aabaBB ⇒ aababSB ⇒ aababB ⇒ aababbS ⇒ aababb (2’) aabaBB ⇒ aabaBbS ⇒ aababSbS ⇒ aababSb ⇒ aababb [M] E 4.8 Automata Theory Context-Free Languages Expression, ambiguity 233 / 326
above When a string has multiple variables, like aabSB in the above example, then we are not forced to rewrite the first variable, we can as well rewrite another one. Thus we can do aabSB ⇒ aabB , but also aabSB ⇒ aabSaBB , for instance. below In detail, two different derivation trees for the same string, corresponding to derivations (1) and (2,2’) respectively, together with two associated leftmost derivations. Given these to trees we conclude the grammar is ambiguous.
Derivation tree & leftmost derivations S 1 S 1 a B 2 a B 2 a B 3 B 7 a B 3 B 5 b S 4 b S 8 S 4 a B 6 B 8 b a B 5 Λ S 7 S 9 Λ b b S 6 b Λ Λ Λ S ⇒ aB ⇒ aaBB ⇒ aabSB ⇒ S ⇒ aB ⇒ aaBB ⇒ aabSB ⇒ aabB ⇒ aabaBB ⇒ aababSB ⇒ aabaBB ⇒ aababSB ⇒ aababB ⇒ aababB ⇒ aababbS ⇒ aababb aababbS ⇒ aababb Automata Theory Context-Free Languages Expression, ambiguity 234 / 326
Dangling else S → if ( E ) S | if ( E ) S else S | . . . if ( E ) if ( E ) S else S [M] E 4.19 Automata Theory Context-Free Languages Expression, ambiguity 235 / 326
Dangling else S → if ( E ) S | if ( E ) S else S | . . . S ( ) if E S ( ) if E S else S S ( ) E if S else S ( ) if E S [M] E 4.19 Automata Theory Context-Free Languages Expression, ambiguity 236 / 326
Dangling else ambiguous: S → if ( E ) S | if ( E ) S else S | A | . . . unambiguous: S → S 1 | S 2 . . . (matched) S 1 → if ( E ) S 1 else S 1 | A | S 2 → if ( E ) S | if ( E ) S 1 else S 2 (open) [M] E 4.19 Automata Theory Context-Free Languages Expression, ambiguity 237 / 326
(un)ambiguous grammars Balanced ambiguous: S → SS | ( S ) | Λ (more or less the definition of balanced) unambiguous: S → ( S ) S | Λ [M] Exercise 4.45 Automata Theory Context-Free Languages Expression, ambiguity 238 / 326
Recommend
More recommend