90 likes | 414 Vues
Normal Forms. Reading: 6.2. Normal Forms. Normal forms are special types of context-free languages. Having more restricted (but still powerful) grammar forms make important algorithms efficient. Two widely-known forms: Chomsky Normal and Greibach Normal. Chomsky Normal Form.
E N D
Normal Forms Reading: 6.2
Normal Forms • Normal forms are special types of context-free languages. • Having more restricted (but still powerful) grammar forms make important algorithms efficient. • Two widely-known forms: Chomsky Normal and Greibach Normal
Chomsky Normal Form • No more than 2 symbols on the right hand side. • All productions are of the form:A -> BC A -> a • A,B,C variables, a terminal
Power of Chomsky Normal • Every CFG can be rewritten in Chomsky Normal Form • Step 1: For any production rule with more than one terminal on the right, substitute all with variables. • Step 2: Substitute more variables in order to make the variable strings shorter.
Example: Convert to CNF Step 2: S -> AD1 D1 ->BBa A -> BaD2 D2 ->BaBb B -> ABc Ba -> a Bb -> b Bc -> c Original: S -> ABa A -> aab B -> Ac Step 1: S -> ABBa A -> BaBaBb B -> ABc Ba -> a Bb -> b Bc -> c
Greibach Normal Form • No restriction on length of right-hand side • Restriction on placement of variables and terminals • All productions must have the form: • A -> ax where a is a terminal and x is a string of (0 or more) variables. • Similar to s-grammars.
Conversion • Any CFG has an equivalent Greibach Normal Form Grammar. • Conversion algorithm is tricky – instead just manipulate symbols
Example S -> AB A -> aA | bB | b B -> b
Example S -> abSb | aa