Presentation laddar. Vänta.

Presentation laddar. Vänta.

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

Liknande presentationer


En presentation över ämnet: "1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2006."— Presentationens avskrift:

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

2 2 RECAPITULATION: REGULAR LANGUAGES - Languages, Alphabets and Strings - Operations on Strings - Operations on Languages - Regular Expressions - Finite Automata - Regular Grammars - Pumping lemma INTRODUCTION: CONTEXT FREE LANGUAGES

3 3 NEXT WEEK! Midterm Exam 1 Regular Languages Place: U2114 Time: Tuesday 2007-04-24, 10:15-12:00 It is OPEN BOOK. (This means you are allowed to bring in one book of your choice.) It will cover lectures 1 through 5 (Regular Languages).

4 4 Med det som vi har lärt oss hittills kan vi klara följande tentauppgifter…

5 5 Tenta 29 okt 1999; uppgift 2 (L Salling) Konstruera (med motiveringar) vars strängar innehåller samtliga tre tecken! a) Ett reguljärt uttryck över

6 6 Lösning eller

7 7 Konstruera (med motiveringar) vars strängar innehåller samtliga tre tecken! b) En minimal DFA för ett språk L över

8 8

9 9 Särskiljningsalgoritm

10 10 c) En reguljär grammatik för L

11 11 Tenta 24 okt 1994; uppgift 2 (L Salling) Reguljära? vars strängar innehåller ett jämnt antal a:n! a) Språket över Ja, språket är reguljärt och beskrivs med ett reguljärt uttryck:

12 12 Tenta 24 okt 1994; uppgift 2 (L Salling) b) De välformade aritmetiska uttrycken formade i alfabetet Nej, språket är inte reguljärt: Ta följande sträng: N stycken a adderas Om språket vore reguljärt skulle det kunna pumpas. Men de N avslutande tecknen består enbart av höger- parenteser och kan inte ändras utan att balansen med vänsterparenteserna förstörs.

13 13 Tenta 15 mars 1995; uppgift 3 (L Salling) Reguljära? c) Ja, språket är reguljärt och beskrivs med ett reguljärt uttryck:

14 14 Tenta 15 mars 1995; uppgift 3 (L Salling) Reguljära? d) Nej. Strängen vars enda palindromprefix längre än 2 är strängen själv, kan inte pumpas någonstans inuti b-block utan att falla ur språket.

15 15 Tenta 15 mars 1995; uppgift 3 (L Salling) Reguljära? e) Nej. Om det vore reguljärt skulle även föregående språk vara det (eftersom det är komplementspråk, och regulariteten bevaras under komplementbildning).

16 16 Pumping Lemma is necessary but not sufficient for RL OBS! The pumping lemma does not give a sufficient condition for a language to be regular! You can not use it to show that language is regular. For example, the language (strings over the alphabet {0,1} consisting of a nonempty even palindrome followed by another nonempty string) is not regular but can still be "pumped" with m = 4: Suppose w=uuRv has length at least 4. If u has length 1, then |v| ≥ 2 and we can take y to be the first character in v. Otherwise, take y to be the first character of u and note that yk for k ≥ 2 starts with the nonempty palindrome yy. For a practical test that exactly characterizes regular languages, see the Myhill-Nerode theorem. The typical method for proving that a language is regular is to construct either a Finite State Machine or a Regular Expression for the language.

17 17 Minimizing DFA’s By Partitioning (Delmängdskonstruktion)

18 18 Minimizing DFA Different methods All involve finding equivalent states: States that go to equivalent states under all inputs We will use the Partitioning Method

19 19 Minimizing DFA’s by Partitioning Consider the following DFA (from Forbes Louis): Accepting states are yellow Non-accepting states are blue Are any states really the same?

20 20 S 2 and S 7 are really the same: Both Final states Both go to S6 under input b Both go to S3 under an a S 0 and S 5 really the same. Why? We say each pair is equivalent Are there any other equivalent states? We can merge equivalent states into 1 state

21 21 Partitioning Algorithm First Divide the set of states into Final and Non-final states Partition I Partition II a b S0S0 S1S1 S4S4 S1S1 S5S5 S2S2 S3S3 S3S3 S3S3 S4S4 S1S1 S4S4 S5S5 S1S1 S4S4 S6S6 S3S3 S7S7 *S 2 S3S3 S6S6 *S 7 S3S3 S6S6

22 22 Partitioning Algorithm Now See if states in each partition each go to the same partition S 1 & S 6 are different from the rest of the states in Partition I (but like each other) We will move them to their own partition a b S0S0 S 1 I S 4 I S1S1 S 5 I S 2 II S3S3 S 3 I S4S4 S 1 I S 4 I S5S5 S 1 I S 4 I S6S6 S 3 I S 7 II *S 2 S 3 I S 6 I *S 7 S 3 I S 6 I

23 23 Partitioning Algorithm a b S0S0 S1S1 S4S4 S5S5 S1S1 S4S4 S3S3 S3S3 S3S3 S4S4 S1S1 S4S4 S1S1 S5S5 S2S2 S6S6 S3S3 S7S7 *S 2 S3S3 S6S6 *S 7 S3S3 S6S6

24 24 Partitioning Algorithm Now again See if states in each partition each go to the same partition In Partition I, S 3 goes to a different partition from S 0, S 5 and S 4 We’ll move S3 to its own partition a b S0S0 S 1 III S 4 I S5S5 S 1 III S 4 I S3S3 S 3 I S4S4 S 1 III S 4 I S1S1 S 5 I S 2 II S6S6 S 3 I S 7 II *S 2 S 3 I S 6 III *S 7 S 3 I S 6 III

25 25 Partitioning Algorithm Note changes in S 6, S 2 and S 7 a b S0S0 S 1 III S 4 I S5S5 S 1 III S 4 I S4S4 S 1 III S 4 I S3S3 S 3 IV S1S1 S 5 I S 2 II S6S6 S 3 IV S 7 II *S 2 S 3 IV S 6 III *S 7 S 3 IV S 6 III

26 26 Partitioning Algorithm Now S 6 goes to a different partition on an a from S 1 S 6 gets its own partition. We now have 5 partitions Note changes in S 2 and S 7 a b S0S0 S 1 III S 4 I S5S5 S 1 III S 4 I S4S4 S 1 III S 4 I S3S3 S 3 IV S1S1 S5 IS5 I S 2 II S6S6 S 3 IV S 7 II *S 2 S 3 IV S 6 V *S 7 S 3 IV S 6 V

27 27 Partitioning Algorithm All states within each of the 5 partitions are identical. We might as well call the states I, II III, IV and V. a b S0S0 S 1 III S 4 I S5S5 S 1 III S 4 I S4S4 S 1 III S 4 I S3S3 S 3 IV S1S1 S 5 I S 2 II S6S6 S 3 IV S 7 II *S 2 S 3 IV S 6 V *S 7 S 3 IV S 6 V

28 28 Partitioning Algorithm a b I III I *II IVV III I II IV V II Here they are: V a a a a a b b b b b b

29 29 Chomsky Hierarchy

30 30 Automata theory: formal languages and formal grammars

31 31 Automata theory: formal languages and formal grammars

32 32 Regular Languages Context-Free Languages Non-regular languages

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

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

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

36 36 Context-Free Languages

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

38 38 Context-Free Grammars

39 39 A context-free grammar A derivation Example

40 40 A context-free grammar Another derivation

41 41

42 42 A context-free grammar A derivation Example

43 43 A context-free grammar Another derivation

44 44

45 45 A context-free grammar A derivation Example

46 46 A context-free grammar A derivation

47 47

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

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

50 50 Derivation Order Leftmost derivation

51 51 Derivation Order Rightmost derivation

52 52 Leftmost derivation

53 53 Rightmost derivation

54 54 Derivation Trees

55 55

56 56

57 57

58 58

59 59 Derivation Tree

60 60 yield Derivation Tree

61 61 Partial Derivation Trees Partial derivation tree

62 62 Partial derivation tree

63 63 Partial derivation tree sentential form yield

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

65 65 Ambiguity

66 66 leftmost derivation derivation (* denotes multiplication)

67 67 derivation leftmost derivation

68 68 Two derivation trees

69 69 The grammar is ambiguous! Stringhas two derivation trees

70 70 stringhas two leftmost derivations The grammar is ambiguous:

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

72 72 Why do we care about ambiguity? Let’s see the case

73 73 Why do we care about ambiguity?

74 74 Why do we care about ambiguity?

75 75 Correct result:

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

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

78 78

79 79 Unique derivation tree

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

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

82 82 The string has two derivation trees

83 83 Compilers

84 84 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

85 85 Lexical analyzer parser Compiler program machine code input output

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

87 87 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

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

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

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

91 91 Parsing

92 92 grammar Parser input string derivation

93 93 Example: Parser derivation input ?

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

95 95

96 96 Phase 2 Phase 1

97 97 Phase 2 Phase 3

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

99 99 Context Free Art http://www.contextfreeart.org/wiki/index.php?page=AboutPage http://www.contextfreeart.org/wiki/index.php?page=AboutPage

100 100 Context Free Art http://www.contextfreeart.org/wiki/index.php?page=AboutPage http://www.contextfreeart.org/wiki/index.php?page=AboutPage


Ladda ner ppt "1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 6 Mälardalen University 2006."

Liknande presentationer


Google-annonser