Presentation is loading. Please wait.

Presentation is loading. Please wait.

Normal Forms for Context-free Grammars

Similar presentations


Presentation on theme: "Normal Forms for Context-free Grammars"— Presentation transcript:

1 Normal Forms for Context-free Grammars

2 Chomsky Normal Form All productions have form: and variable variable
terminal

3 Examples: Chomsky Normal Form Not Chomsky Normal Form

4 Convertion to Chomsky Normal Form
Example: Not Chomsky Normal Form

5 Introduce variables for terminals:

6 Introduce intermediate variable:

7 Introduce intermediate variable:

8 Final grammar in Chomsky Normal Form:
Initial grammar

9 In general: From any context-free grammar not in Chomsky Normal Form we can obtain: An equivalent grammar in Chomsky Normal Form

10 The Procedure First remove: Nullable variables Unit productions

11 For every symbol : Add production In productions: replace with New variable:

12 Replace any production
with New intermediate variables:

13 Theorem: For any context-free grammar there is an equivalent grammar
in Chomsky Normal Form

14 Observations Chomsky normal forms are good
for parsing and proving theorems It is very easy to find the Chomsky normal form of any context-free grammar

15 Greinbach Normal Form All productions have form: symbol variables

16 Examples: Greinbach Normal Form Not Greinbach Normal Form

17 Conversion to Greinbach Normal Form:

18 Theorem: For any context-free grammar there is an equivalent grammar
in Greinbach Normal Form

19 Observations Greinbach normal forms are very good for parsing
It is hard to find the Greinbach normal form of any context-free grammar

20 An Application of Chomsky Normal Forms

21 The CYK Membership Algorithm
Input: Grammar in Chomsky Normal Form String Output: find if

22 The Algorithm Input example: Grammar : String :

23

24

25

26

27 Therefore: Time Complexity: Observation: The CYK algorithm can be easily converted to a parser

28 Pushdown Automata PDAs

29 Pushdown Automaton -- PDA
Input String Stack States

30 Initial Stack Symbol Stack Stack bottom special symbol

31 The States Pop symbol Input symbol Push symbol

32 input stack top Replace

33 input stack top Push

34 input stack top Pop

35 input stack top No Change

36 Non-Determinism

37 NPDA: Non-Deterministic PDA
Example:

38 Execution Example: Time 0 Input Stack current state

39 Time 1 Input Stack

40 Time 2 Input Stack

41 Time 3 Input Stack

42 Time 4 Input Stack

43 Time 5 Input Stack

44 Time 6 Input Stack

45 Time 7 Input Stack

46 Time 8 Input Stack accept

47 A string is accepted if:
All the input is consumed The last state is a final state We do not care about the stack contents

48 The input string is accepted by the NPDA:

49 In general, is the language accepted by the NPDA:

50 Another NPDA example NPDA

51 Execution Example: Time 0 Input Stack

52 Time 1 Input Stack

53 Time 2 Input Stack

54 Time 3 Input Stack

55 Time 4 Input Stack

56 Time 5 Input Stack

57 Time 6 Input Stack accept


Download ppt "Normal Forms for Context-free Grammars"

Similar presentations


Ads by Google