230 likes | 250 Views
Learn about regular grammars, language definitions, and Context-Free Grammars (CFGs) through examples and concepts explained intuitively.
E N D
Context-Free Grammars Section 3.1 Fri, Oct 14, 2005
Intuitive Notion of a Grammar • Consider the following replacement rules: • S → aT • S → bS • S → e • T → aS • T → bT • S and T are nonterminals. • S is the start symbol. • a and b are terminals.
Example: Derivation • Start with S and use the rules to make replacements, until only terminals remain. • S aT • abT • abaS • ababS • ababaT • 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. • 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
b b a S T S a Regular Grammars and Regular Languages • The transition diagram for the previous example:
Regular Grammars and Regular Languages • What is a regular grammar for the language L(b*(abb*)*)? • First, build the NFA. • Then convert it to a DFA. • Then minimize it: b a S T S b
Regular Grammars and Regular Languages • Now it is easy to write a regular grammar for this language. • S aT • S bS • S e • T bS
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, alternation, and recursion. • Regular grammars incorporate only sequence, repetition, and alternation. • When designing a grammar, look for • Concatenated patterns (sequence). • Repeated patterns (repetition). • Alternate patterns (alternation). • Nested patterns (recursion).
Examples: Repetition and Recursion • Sequence (and repetition) • 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. • Identifiers can be described by a regular grammar: • S aT | … | zT | AT | … | ZT • T aT | … | zT | AT | … | ZT | 0T | … | 9T | e
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 nonterminals S for a statement, T for a sequence of statements. • 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)SelseS • S {T} • S s; • S ; • T TS | S • 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.