510 likes | 530 Views
Learn how to simplify context-free grammars by applying substitution rules, removing nullable and unit productions, and eliminating useless productions. Explore examples and understand the conversion to Chomsky and Greibach normal forms.
E N D
A Substitution Rule Equivalent grammar Substitute COMP 335
A Substitution Rule Substitute Equivalent grammar COMP 335
In general: Substitute equivalent grammar COMP 335
Nullable Variables Nullable Variable: COMP 335
Removing Nullable Variables Example Grammar: Nullable variable COMP 335
Final Grammar Substitute COMP 335
Unit-Productions Unit Production: (a single variable in both sides) COMP 335
Removing Unit Productions Observation: Is removed immediately COMP 335
Example Grammar: COMP 335
Substitute COMP 335
Remove COMP 335
Substitute COMP 335
Remove repeated productions Final grammar COMP 335
Useless Production Some derivations never terminate... Useless Productions COMP 335
Useless Production Another grammar: Not reachable from S COMP 335
contains only terminals In general: if then variable is useful otherwise, variable is useless COMP 335
Productions useless Variables useless useless useless useless useless useless A production is useless if any of its variables is useless COMP 335
Removing Useless Productions Example Grammar: COMP 335
First: find all variables that can produce strings with only terminals Round 1: Round 2: COMP 335
Keep only the variables that produce terminal symbols: (the rest variables are useless) Remove useless productions COMP 335
Second: Find all variables reachable from Use a Dependency Graph not reachable COMP 335
Keep only the variables reachable from S (the rest variables are useless) Final Grammar Remove useless productions COMP 335
Removing All • Step 1: Remove -productions • Step 2: Remove Unit-productions • Step 3: Remove Useless productions COMP 335
Normal FormsforContext-free Grammars COMP 335
Chomsky Normal Form Each productions has form: or variable variable terminal COMP 335
Examples: Chomsky Normal Form Not Chomsky Normal Form COMP 335
Conversion to Chomsky Normal Form • Example: Not Chomsky Normal Form COMP 335
Introduce variables for terminals: COMP 335
Introduce intermediate variable: COMP 335
Introduce intermediate variable: COMP 335
Final grammar in Chomsky Normal Form: Initial grammar COMP 335
In general: From any context-free grammar (which doesn’t produce ) not in Chomsky Normal Form we can obtain: An equivalent grammar in Chomsky Normal Form COMP 335
The Procedure • First remove: • Nullable variables • Unit productions COMP 335
Then, for every symbol : Add production In productions: replace with New variable: COMP 335
Replace any production with New intermediate variables: COMP 335
Theorem: For any context-free grammar (which doesn’t produce ) there is an equivalent grammar in Chomsky Normal Form COMP 335
Observations • Chomsky normal forms are good • for parsing and proving theorems • It is very easy to find the Chomsky normal • form for any context-free grammar COMP 335
Greibach Normal Form All productions have form: symbol variables COMP 335
Examples: Greibach Normal Form Not Greibach Normal Form COMP 335
Conversion to Greibach Normal Form: Greibach Normal Form COMP 335
Theorem: For any context-free grammar (which doesn’t produce ) there is an equivalent grammar in Greibach Normal Form COMP 335
Observations • Greibach normal forms are very good • for parsing • It is hard to find the Greibach normal • form of any context-free grammar COMP 335
The CYK Parser COMP 335
The CYK Membership Algorithm Input: • Grammar in Chomsky Normal Form • String Output: find if COMP 335
The Algorithm Input example: • Grammar : • String : COMP 335