1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.

Slides:



Advertisements
Similar presentations
CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
Advertisements

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Pushdown Automata Part II: PDAs and CFG Chapter 12.
CS21 Decidability and Tractability
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
Introduction to Computability Theory
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Fall 2004COMP 3351 NPDA’s Accept Context-Free Languages.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
Courtesy Costas Busch - RPI1 Context-Free Languages.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2005Costas Busch - RPI1 Context-Free Languages.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
Context-Free Languages Hinrich Schütze CIS, LMU, Slides based on RPI CSCI 2400 Thanks to Costas Busch.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
Pushdown Automata (PDAs)
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
Grammars CPSC 5135.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Lex. 2 Lex is a lexical analyzer Var = ; if (test > 20) temp = 0; else while (a < 20) temp++; Lex Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn:
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2010.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University 2006.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Context-Free Languages. Regular Languages Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Formal Languages, Automata and Models of Computation
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2011.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
Theory of Languages and Automata By: Mojtaba Khezrian.
6. Pushdown Automata CIS Automata and Formal Languages – Pei Wang.
Formal Languages, Automata and Models of Computation
Context-Free Languages
G. Pullaiah College of Engineering and Technology
Simplifications of Context-Free Grammars
CSE 105 theory of computation
PDAs Accept Context-Free Languages
NPDAs Accept Context-Free Languages
CHAPTER 2 Context-Free Languages
Context-Free Grammars
Deterministic PDAs - DPDAs
Parsing Costas Busch - LSU.
Context-Free Languages
… NPDAs continued.
CSE 105 theory of computation
CSE 105 theory of computation
Presentation transcript:

1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010

2 Content Midterm results Regular vs. Nonregular Languages Context-Free Languages Context-Free Grammars Derivation Trees. Ambiguity Push-Down Automata, PDA Applications

Midterm 1 Solutions Problem 1. (4 points) Circle true statements among the following: The number of outgoing arcs from a state of a DFA is always equal to |  |. (True) The number of outgoing arcs from a state of a NFA is always equal to |  |. (False) Not all finite languages are regular. (False) If L is regular language, then L R is also a regular language. (True) If L is regular language, then L 2 may not be a regular language. (False) The family of regular languages is closed under intersection. (True) The grammar is not regular. (True) The grammar is regular. (True) 3

Problem 2. (6 points) Convert the following NFA into regular expression: 4

Solution: Create a new initial state and a new, unique final state, neither of which is part of a loop. 5

Removed state 3: 6

Removed state 2: 7 Removed state 1:

b) Let M be the following DFA. 8 Minimize M by set partitioning.

b) Let M be the following DFA. 9 Minimize M by set partitioning.

10 Step 0: Partition the states into two groups accepting and non-accepting. State Reduction by Set Partitioning { 1, 3 }{ 2, 4, 5, 6 } P1P2

11 Step 1: Get the response of each state for each input symbol. P 1 P 2 p 2 p 2 p 1 p 2 p 2 p 2 a    a     {1, 3 } {2, 4, 5, 6 } b    b      p 2 p 2 p 2 p 1 p 2 p 1 State Reduction by Set Partitioning

12 P 11 P 21 P 22 P 23 p 21 p 21 p 11 p 23 p 23 p 22 a    a    {1, 3} {2} {4, 6} {5} b    b    p 22 p 22 p 23 p 11 p 11 p 21 Step 2: Partition the sets according to the responses, and go to Step 1 until no partition occurs. So the final partition results are as follows: {1, 3} {2} {4, 6} {5}

13 {1, 3} {2} {5} {4, 6} Minimized DFA consists of four states of the final partition, and the transitions are the one corresponding to the starting DFA. Minimized DFA Starting DFA 1,32 54,6

Problem 3 (4 points) Regular or not? If regular, construct an automaton, regular expression or a grammar. If not regular, use pumping lemma for regular languages. a) Set of all strings of form over alphabet  = {0, 1}. Solution: Regular. A simple finite state machine with finite number of states which takes care of the order of symbols and counts the total number of characters. 14

Solution by John E.

16 Solution by Otto N

17 Solution by Fredrik P

18 b) Set of all strings of form over alphabet  = {0, 1}. Solution: Not regular. Language L consists of all strings of the form 0*1* where the number of 0’s is eight more than the number of 1’s. We can show that L is not regular by applying pumping lemma. Let w = 0 m+8 1 m. Since |xy|  m, y must equal 0 k for some k > 0. We can pump y out once, which will generate the string 0 m+8-k 1 m, which is not in L because the number of 0’s is is less than 8 more than the number of 1’s.

19 A Regular Language also There is a cycle in the graph The pumped strings

20 The Pumping Lemma Given an infinite regular language there exists an integer for any string with length we can write with and such that:

21 The Pumping Lemma States the property of regular language Cannot be used to prove that a language is regular! An example: If something is a square it always has four edges (a property of square) But: having proved that something has four edges does not necessarily mean that the object is a square.

22 The Pumping Lemma So we use pumping lemma not to prove that a language is regular, but to show that language (not obeying pumping lemma) can not be regular. It is usually done using contrapositive (proof by contradiction)

23 Applying Pumping Lemma If for arbitrary there exists a string with length such that for all decompositions with and there existssuch that: then the language is NOT REGULAR

24 Formal Definition Grammar Set of variables Set of terminal symbols Start variable Set of production rules

25 Regular Grammars A regular grammar is any right-linear or left-linear grammar Examples

26 A Nonregular Language DFA must have infinite number of states. Statesare distinct for each

27 Regular Languages Context-Free Languages Non-regular languages

28 Automata theory: formal languages and formal grammars

29 Context-Free Languages

30 Context-Free Languages Pushdown Automata Context-Free Grammars

31 Context-Free Grammars

32 A context-free grammar A derivation Example

33 A context-free grammar Another derivation

34

35 A context-free grammar A derivation Example

36 A context-free grammar Another derivation

37

38 A context-free grammar A derivation Example

39 A context-free grammar A derivation

40

41 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables

42 Definition: Context-Free Languages A language is context-free if and only if there is a grammar with

43 Derivation Order Leftmost derivation

44 Derivation Order Rightmost derivation

45 Leftmost derivation

46 Rightmost derivation

47 Derivation Trees

48

49

50

51

52 Derivation Tree

53 yield Derivation Tree

54 Partial Derivation Trees Partial derivation tree

55 Partial derivation tree

56 Partial derivation tree sentential form yield

57 Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:

58 Ambiguity

59 leftmost derivation derivation (* denotes multiplication)

60 derivation leftmost derivation

61

62 Two derivation trees

63 The grammar is ambiguous! Stringhas two derivation trees

64 stringhas two leftmost derivations The grammar is ambiguous:

65 Definition A context-free grammar is ambiguous if some string has two or more derivation trees (two or more leftmost/rightmost derivations)

66 Why do we care about ambiguity?

67 Why do we care about ambiguity?

68 Why do we care about ambiguity?

69 Correct result:

70 Ambiguity is bad for programming languages We want to remove ambiguity!

71 We fix the ambiguous grammar… …by introducing parentheses () to indicate grouping, (precedence) Non-ambiguous grammar

72

73 Unique derivation tree

74 The grammar : is non-ambiguous Every string has a unique derivation tree

75 Inherent Ambiguity Some context free languages have only ambiguous grammars! Example:

76 The string has two derivation trees

77 Definition: Context-Free Grammars Grammar Productions of the form: is string of variables and terminals VariablesTerminal symbols Start variables

78 Definition: Regular Grammars Grammar Right or Left Linear Grammars. Productions of the form: is string of terminals VariablesTerminal symbols Start variables or

79 Regular Languages Context-Free Languages Non-regular languages

80 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton

81 Pushdown Automata PDAs

82 Pushdown Automaton - PDA Input String Stack States

83 The Stack The stack allows pushdown automata to recognize some non-regular languages. All access to the stack only on the top! (Stack top is written leftmost in the string, e.g. yxz) A PDA can write symbols on stack and read them later on. A stack is valuable as it can hold an unlimited amount of information. POP reading symbol PUSH writing symbol

84 The States Input symbol Pop old reading stack symbol Push new writing stack symbol

85 top input stack Replace (An alternative is to start and finish with empty stack)

86 input Push top stack

87 input Pop top stack

88 input No Change top stack

89 Input Stack Example 3.7 Salling: A PDA for simple nested parenthesis strings Time 0

90 Input Stack Example 3.7 Time 1

91 Input Stack Example 3.7 Time 2

92 Input Stack Example 3.7 Time 3

93 Input Stack Example 3.7 Time 4

94 Input Stack Example 3.7 Time 5

95 Input Stack Example 3.7 Time 6

96 Input Example 3.7 Time 7 Stack

97 Applications: Compilers

98 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v ELSE: WHILE: cmp x,3... Machine Code

99 Lexical analyzer parser Compiler program machine code input output

100 A parser “knows” the grammar of the programming language

101 Parser PROGRAM STMT_LIST STMT_LIST STMT; STMT_LIST | STMT; STMT EXPR | IF_STMT | WHILE_STMT | { STMT_LIST } EXPR EXPR + EXPR | EXPR - EXPR | ID IF_STMT if (EXPR) then STMT | if (EXPR) then STMT else STMT WHILE_STMT while (EXPR) do STMT

102 The parser finds the derivation of a particular input * 5 Parser E  E + E | E * E | INT E  E + E  E + E * E  10 + E*E  * E  * 5 input derivation

103 derivation derivation tree E  E + E  E + E * E  10 + E*E  * E  * 5 10 E 2 E 5 E E + E *

104 derivation tree mult a, 2, 5 add b, 10, a machine code 10 E 2 E 5 E E + E *

105 Parsing

106 grammar Parser input string derivation

107 Example: Parser derivation input ?

108 Exhaustive Search Phase 1: All possible derivations of length 1 Find derivation of

109

110 Phase 2 Phase 1

111 Phase 2 Phase 3

112 Final result of exhaustive search Parser derivation input (top-down parsing)

113 Context Free Art

114 Context Free Art