230 likes | 287 Views
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.
E N D
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 • Start with S and use the rules to make replacements, until only terminals remain. • S aA • abA • abaS • ababS • ababaA • ababaaS • ababaa.
Example: Derivation • This is called a derivation. • We write S*ababaa. • What strings can be derived by the preceding set of rules?
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. • SV – Σ is the start symbol. • and each rule is of the form XwY or Xe where X, YV – Σ and w Σ*.
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?
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 Xe means that the state X is a final state. • The rule XwY corresponds to the transition
Context-Free Grammars • A grammar is called context-free if each rule is of the form X where XV – Σ and V*. • The rules allow nonterminals to be replaced by strings, regardless of the “context” in which the nonterminals occur.
Example • Let V = {S, A, B, a, b}. • Let the rules be • SAB • AaA • Ae • BaBb • Be • What is the language of this grammar?
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).
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}.
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}.
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.
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.
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
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.
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.
Example of a CFG • If uv, then they must differ in some position, say the k-th position. • Now divide w as uv, 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.)
Example of a CFG • Thus, we may design a grammar for strings of odd length with a in the center: • A XAXa • X ab • Similarly, we may design a grammar for strings of odd length with b in the center: • B XBXb • X ab
Example of a CFG • Thus, a grammar for the language is • S ABABBA • A XAXa • B XBXb • X ab.