1 / 20

Context-Free Grammars

Context-Free Grammars. Section 3.1 Fri, Oct 15, 2004. Intuitive Notion of a Grammar. Consider the following replacement rules : S → aA S → bS S → e A → aS A → bA S is the start symbol . S and A are nonterminals . a and b are terminals. Example: Derivation.

Download Presentation

Context-Free Grammars

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Context-Free Grammars Section 3.1 Fri, Oct 15, 2004

  2. Intuitive Notion of a Grammar • Consider the following replacement rules: • S → aA • S → bS • S → e • A → aS • A → bA • S is the start symbol. • S and A are nonterminals. • a and b are terminals.

  3. Example: Derivation • Start with S and use the rules to make replacements, until only terminals remain. • S aA •  abA •  abaS •  ababS •  ababaA •  ababaaS •  ababaa.

  4. Example: Derivation • This is called a derivation. • We write S*ababaa. • What strings can be derived by the preceding set of rules?

  5. The Definition of a Regular Grammar • A regular grammar is a quadruple (V, Σ, R, S) where • V is a finite alphabet. • Σ is a finite set of terminals. • (V – Σ is the set of nonterminals.) • R is finite set of production, or rewrite, rules. • SV – Σ is the start symbol. • and each rule is of the form XwY or Xe where X, YV – Σ and w Σ*.

  6. The Language of a Regular Grammar • Let G be a regular grammar. • The language of G is L(G) = {w Σ*S*w}. • What is the language of the previous example?

  7. w X Y Regular Grammars and Regular Languages • Theorem: A language is regular if and only if it is the language of a regular grammar. • Proof: • The connection between regular grammars and regular languages is clear. (See Ex. 3.1.10, p. 122.) • There is a state for every nonterminal X. • The rule Xe means that the state X is a final state. • The rule XwY corresponds to the transition

  8. Context-Free Grammars • A grammar is called context-free if each rule is of the form X where XV – Σ and V*. • The rules allow nonterminals to be replaced by strings, regardless of the “context” in which the nonterminals occur.

  9. Example • Let V = {S, A, B, a, b}. • Let the rules be • SAB • AaA • Ae • BaBb • Be • What is the language of this grammar?

  10. Repetition and Recursion in CFGs • CFGs incorporate sequence, repetition, and recursion. • Regular grammars incorporate only sequence and repetition. • When designing a grammar, look for • Concatenated patterns (sequence). • Repeated patterns (repetition). • Nested patterns (recursion).

  11. Examples: Repetition and Recursion • Sequence • Design a grammar G such that L(G) = {ambn | m, n 0}. • Repetition • Design a grammar G such that L(G) = {(ab)n | n 0}. • Recursion • Design a grammar G such that L(G) = {anbn | n 0}.

  12. Examples of CFGs • Sequence, repetition, and recursion. • Design a grammar G such that L(G) = {ambn | m > n  0}. • Sequence? Repetition? Recursion? • Design a grammar G such that L(G) = {ambn | m ≠ n}.

  13. Example of a CFG • The grammar in this example describes C++ expressions consisting of addition and multiplication of identifiers with the use of parentheses. • Let the nonterminals be {E, T, F}, where E is the start symbol. • Let the terminals be (, ), +, *, and id.

  14. Example of a CFG • The rules are • E E+T • E  T • T  T*F • T  F • F  (E) • F  id • Derive the string (id + id)*id.

  15. Example of a CFG • The following grammar describes C++ if statements. That is, statements of the form if(condition){one or more statements}else{one or more statements} • Use the nonterminal S for a statement. • Use the terminals • (, ), {, } • c for condition • if • else • s for statement

  16. Example of a CFG • Use the rules • S if(c){S} • S if(c){S}else{S} • S {S} • S SS • S  s • S e • This example includes sequence, repetition, and recursion.

  17. Example of a CFG • Let  = {a, b}. • Design a grammar G such that L(G) = {w *for all z *, w  zz}. • It is easy to design a grammar for all strings of odd length. (How?) • Suppose w has even length, |w| = 2n. • Let w = uv, where |u| = |v| = n.

  18. Example of a CFG • If uv, then they must differ in some position, say the k-th position. • Now divide w as uv, where |u| = 2k – 1 and |v| = 2n – (2k – 1) = 2(n – k) + 1. • Then the k-th symbol in u is in the center of u and the k-th symbol of v is in the center of v. (Verify this.)

  19. Example of a CFG • Thus, we may design a grammar for strings of odd length with a in the center: • A XAXa • X  ab • Similarly, we may design a grammar for strings of odd length with b in the center: • B XBXb • X  ab

  20. Example of a CFG • Thus, a grammar for the language is • S ABABBA • A XAXa • B XBXb • X  ab.

More Related