Context-Free Grammars

Context-Free Grammars

Context-Free Grammars Grammar, which knows how to control even kings. (Moliere) I am the Roman Emperor, and am above grammar. (Emperor Sigismund) CS 4313/5353 Theory of Computation Context-Free Grammars – 1 Examples 1 ⊲ Examples 1 Example 1: Balanced Parentheses Example 2 Example 3 Definition More Defs. S → SS Derivations Parse Trees S → (S) Ambiguity Chomsky Normal Form S → ǫ CYK Algorithm CYK Pseudocode CS 4313/5353 Theory of Computation Context-Free Grammars – 2 Example 2 Examples 1 ⊲ Example 2 Example 2: Arithmetic Expressions (start E) Example 3 Definition More Defs. E → EOE Derivations Parse Trees E → (E) Ambiguity Chomsky Normal a b c Form E → | | CYK Algorithm CYK Pseudocode O → + |−|∗| / CS 4313/5353 Theory of Computation Context-Free Grammars – 3 Example 3 Examples 1 0n1n 1n0n Example 2 Example 3: or ⊲ Example 3 Definition More Defs. S → A | B Derivations Parse Trees A → 0A1 Ambiguity Chomsky Normal 1 0 Form B → B CYK Algorithm CYK Pseudocode A → ǫ B → ǫ CS 4313/5353 Theory of Computation Context-Free Grammars – 4 Definition of CFGs Examples 1 Example 2 A context-free grammar is: Example 3 ⊲ Definition • V , a finite set of variables More Defs. Derivations • Σ, a finite set of terminals, Σ ∩ V = ∅ Parse Trees Ambiguity • R, a finite set of rules, each rule has the form Chomsky Normal ∗ Form A → w, where A ∈ V and w ∈ (V ∪ Σ) CYK Algorithm CYK Pseudocode • S ∈ V , a start variable CS 4313/5353 Theory of Computation Context-Free Grammars – 5 More Definitions Examples 1 ∗ Example 2 If u, v, and w are strings in (V ∪ Σ) , and A → w Example 3 Definition is a rule, then uAv ⇒ uwv (uAv yields uwv). ⊲ More Defs. Derivations Parse Trees ∗ Ambiguity u ⇒ v (u derives v) Chomsky Normal Form if u = v or u ⇒ v or CYK Algorithm ∗ CYK Pseudocode there exists an x such that u ⇒ x ⇒ v The language of a grammar is {w | w ∈ Σ∗ ∧ S ⇒∗ w}. The context-free languages are those generated by CFGs. CS 4313/5353 Theory of Computation Context-Free Grammars – 6 Derivations Examples 1 Example 2 Derivation of ()(()()) Example 3 Definition More Defs. S ⇒ SS ⇒ (S)S ⇒ ()S ⇒ ()(S) ⇒ ⊲ Derivations Parse Trees ()(SS) ⇒ ()((S)S) ⇒ ()(()S) ⇒ Ambiguity Chomsky Normal ()(()(S)) ⇒ ()(()()) Form CYK Algorithm CYK Pseudocode Derivation of a−b/c E ⇒ EOE ⇒ EOEOE ⇒ aOEOE ⇒ a−EOE ⇒ a−bOE ⇒ a−b/E ⇒ a−b/c CS 4313/5353 Theory of Computation Context-Free Grammars – 7 Parse Trees Examples 1 Example 2 Parse Tree for ()(()()) Example 3 Definition More Defs. S Derivations ⊲ Parse Trees Ambiguity Chomsky Normal SS Form CYK Algorithm CYK Pseudocode ( SS))( ε SS ( SS) ( ) ε ε CS 4313/5353 Theory of Computation Context-Free Grammars – 8 Ambiguity Examples 1 a b c Example 2 Parse Trees for − / Example 3 Definition More Defs. E E Derivations Parse Trees ⊲ Ambiguity E O E E O E Chomsky Normal Form CYK Algorithm c a _ CYK Pseudocode E OE / E OE a_ b b/ c A CFG is ambiguous if there is some string w that has more than one parse tree. CS 4313/5353 Theory of Computation Context-Free Grammars – 9 Chomsky Normal Form Examples 1 Example 2 A CFG is in Chomsky normal form if every rule has Example 3 Definition the form A → BC or A → a, where A, B, and C More Defs. Derivations are variables and a is a terminal. Also, S → ǫ is Parse Trees Ambiguity allowed, where S is the start variable. Chomsky Normal ⊲ Form CYK Algorithm CYK Pseudocode Theorem: Any CFG can be converted to Chomsky normal form. CS 4313/5353 Theory of Computation Context-Free Grammars – 10 CYK Algorithm Examples 1 Example 2 The CYK (Cocke-Younger-Kasami) algorithm is a Example 3 Definition general algorithm for parsing CFGs. It is an More Defs. Derivations example of dynamic programming. Parse Trees Ambiguity Chomsky Normal Form For a grammar G in Chomsky normal form, the ⊲ CYK Algorithm 2 3 CYK Pseudocode CYK algorithm is O(l + m n ), where l = |G|, m = |V |, n = |w|. The entry V [i,j] will contain the variable symbols that can generate the substring w[i] ...w[j]. CS 4313/5353 Theory of Computation Context-Free Grammars – 11 CYK Pseudocode Examples 1 Example 2 procedure CYK(G, w) Example 3 Definition Initialize all entries V [i,j] to ∅ More Defs. Derivations for i ← 1 to |w| Parse Trees Ambiguity V [i,i] ← {A | A → w[i]} Chomsky Normal Form end for CYK Algorithm CYK for d ← 1 to |w|− 1 ⊲ Pseudocode for i ← 1 to |w|− d for m ← i to i + d − 1 for B in V [i,m] for C in V [m +1,i + d] V [i,i + d] ← V [i,i + d] ∪ {A | A → BC} return V CS 4313/5353 Theory of Computation Context-Free Grammars – 12.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us