Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005."— Presentation transcript:

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

2 2 Content - Grammars - Linear Grammars - Regular Grammars - Regular Grammars Generate Regular - Languages - Regular vs. Nonregular Languages - Context-Free Languages - Context-Free Grammars - Derivation Trees - Definition: Context-Free Languages - Ambiguity

3 3 Grammars

4 4 Grammars express languages Example: the English language

5 5 barksverb singsverb   dognoun birdnoun   thearticle a  

6 6 A derivation of “the bird sings”:         birdthe verbbirdthe verbnounthe verbnounarticle predicatenounarticle predicatephrasenounsentence sings _

7 7 A derivation of “a dog barks”: barksdoga verbdoga verbnouna verbnounarticle verbphrasenoun predicatephrasenounsentence       _ _

8 8 The language of the grammar:

9 9 Notation Non-terminal (Variable) Terminal Production rule

10 10 Example Derivation of sentence: Grammar:

11 11 Grammar: Derivation of sentence

12 12 Other derivations

13 13 The language of the grammar

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

15 15 Example Grammar

16 16 Sentential Form A sentence that contains variables and terminals Example sentential formsSentence (sats)

17 17 We write: Instead of:

18 18 In general we write if By default ()

19 19 Example Grammar Derivations

20 20 baaaaaSbbbbaaSbb   S   Grammar Example Derivations

21 21 Another Grammar Example Derivations Grammar

22 22 More Derivations

23 23 The Language of a Grammar For a grammar with start variable String of terminals

24 24 Example For grammar Since

25 25 Notation

26 26 Linear Grammars

27 27 Linear Grammars Grammars with at most one variable (non-terminal) at the right side of a production Examples:

28 28 A Non-Linear Grammar Grammar

29 29 Another Linear Grammar Grammar

30 30 Right-Linear Grammars All productions have form: or Example

31 31 Left-Linear Grammars All productions have form or Example

32 32 Regular Grammars

33 33 Regular Grammars Generate Regular Languages

34 34 Theorem Languages Generated by Regular Grammars Regular Languages

35 35 Theorem - Part 1 Languages Generated by Regular Grammars Regular Languages Any regular grammar generates a regular language

36 36 Theorem - Part 2 Any regular language is generated by a regular grammar Languages Generated by Regular Grammars Regular Languages

37 37 Proof – Part 1 The language generated by any regular grammar is regular Languages Generated by Regular Grammars Regular Languages

38 38 The case of Right-Linear Grammars Let be a right-linear grammar We will prove: is regular Proof idea We will construct NFA with

39 39 Grammar is right-linear Example

40 40 Construct NFA such that every state is a grammar variable: special final state

41 41 Add edges for each production:

42 42

43 43

44 44

45 45

46 46

47 47 Grammar NFA

48 48 In General A right-linear grammar has variables: and productions: or

49 49 We construct the NFA such that: each variable corresponds to a node: special final state ….

50 50 For each production: we add transitions and intermediate nodes ………

51 51 Example

52 52 The case of Left-Linear Grammars Let be a left-linear grammar We will prove: is regular Proof idea We will construct a right-linear grammar with

53 53 Since is left-linear grammar the productions look like:

54 54 Construct right-linear grammar In :

55 55 Construct right-linear grammar In :

56 56 It is easy to see that: Since is right-linear, we have: Regular Language Regular Language Regular Language

57 57 Proof - Part 2 Any regular language is generated by some regular grammar Languages Generated by Regular Grammars Regular Languages

58 58 Proof idea Any regular language is generated by some regular grammar Construct from a regular grammar such that Since is regular there is an NFA such that

59 59 Example

60 60 Convert to a right-linear grammar

61 61 In General For any transition: Add production: variableterminalvariable

62 62 For any final state: Add production:

63 63 Since is right-linear grammar is also a regular grammar with

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

65 65 Observation Regular grammars generate regular languages Examples

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

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

68 68 Context-Free Languages

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

70 70 Context-Free Grammars

71 71 A context-free grammar A derivation Example

72 72 A context-free grammar Another derivation

73 73

74 74 A context-free grammar A derivation Example

75 75 A context-free grammar Another derivation

76 76

77 77 A context-free grammar A derivation Example

78 78 A context-free grammar A derivation

79 79

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

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

82 82 Derivation Order Leftmost derivation

83 83 Derivation Order Rightmost derivation

84 84 Leftmost derivation

85 85 Rightmost derivation

86 86 Derivation Trees

87 87

88 88

89 89

90 90

91 91 Derivation Tree

92 92 yield Derivation Tree

93 93 Partial Derivation Trees Partial derivation tree

94 94 Partial derivation tree

95 95 Partial derivation tree sentential form yield

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

97 97 Ambiguity

98 98 leftmost derivation derivation (* denotes multiplication)

99 99 derivation leftmost derivation

100 100 Two derivation trees

101 101 The grammar is ambiguous! Stringhas two derivation trees

102 102 stringhas two leftmost derivations The grammar is ambiguous:

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

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

105 105 Why do we care about ambiguity?

106 106 Why do we care about ambiguity?

107 107 Correct result:

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

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

110 110

111 111 Unique derivation tree

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

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

114 114 The string has two derivation trees

115 115 Compilers

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

117 117 Lexical analyzer parser Compiler program machine code input output

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

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

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

121 121 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 *

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

123 123 Parsing

124 124 grammar Parser input string derivation

125 125 Example: Parser derivation input ?

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

127 127

128 128 Phase 2 Phase 1

129 129 Phase 2 Phase 3

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


Download ppt "1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005."

Similar presentations


Ads by Google