Presentation is loading. Please wait.

Presentation is loading. Please wait.

11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.

Similar presentations


Presentation on theme: "11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012."— Presentation transcript:

1 11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012

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

3 Midterm 1 Solution 3 http://www.idt.mdh.se/kurser/cd5560/12_11/examination/ Duggor/MIDTERM1-20121127-Solution.pdf

4 4 The Pumping Lemma for Regular Languages Pumping Lemma 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. http://www2.mat.ua.pt/rosalia/cadeiras/TC/pump.pdf A comment on the MIDTERM 1

5 5 Regular Languages Context-Free Languages Non-regular languages Time to take the next step: beyond Regular Languages

6 6 Automata theory: formal languages and formal grammars

7 7 Context-Free Languages Based on C Busch, RPI, Models of Computation

8 8 Context-Free Languages Pushdown Automata Context-Free Grammars

9 9 Context-Free Grammars

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

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

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

13 13 A regular grammar is either right or left linear, whereas context free* grammar is any combination of terminals and non-terminals. Hence regular grammars are a subset of context-free grammars. Grammar generating palindromes is not regular: Regular vs. Context-free Grammar *The name context-free grammar is explained by the property of productions that are independent of the surrounding symbols. There are also context-sensitive grammars where productions depend on the context (symbols that surround variables).

14 14 A context-free grammar A derivation Example 1:

15 15 A context-free grammar Another derivation

16 16

17 17 A context-free grammar A derivation Example 2:

18 18 A context-free grammar Another derivation

19 19

20 20 A context-free grammar A derivation Example 3:

21 21 A context-free grammar A derivation

22 22

23 Example 4:

24

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

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

27 27 Derivation Order Leftmost derivation

28 28 Derivation Order Rightmost derivation

29 29 Leftmost derivation

30 30 Rightmost derivation

31 31 Derivation Trees

32 32 Derivation can be represented in a tree form

33 33

34 34

35 35

36 36 Derivation Tree

37 37 yield Derivation Tree

38 38 Partial Derivation Trees Partial derivation tree

39 39 Partial derivation tree

40 40 Partial derivation tree sentential form yield

41 41 The same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:

42 42 Ambiguity

43 43 leftmost derivation derivation (* denotes multiplication)

44 44 derivation leftmost derivation

45 45

46 46 Two derivation trees

47 47 The grammar is ambiguous! Stringhas two derivation trees

48 48 has two leftmost derivations: The grammar is ambiguous as the string

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

50 50 Why do we care about ambiguity?

51 51 Why do we care about ambiguity?

52 52 Why do we care about ambiguity?

53 53 Correct result:

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

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

56 56

57 57 Unique derivation tree

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

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

60 60 The string has two derivation trees

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

62 62 Applications: Compilers

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

64 64 Lexical analyzer parser Compiler program machine code input output

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

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

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

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

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

70 70 Parsing examples

71 71 grammar Parser input string derivation

72 72 Example: Parser derivation input ?

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

74 74

75 75 Phase 2 Phase 1

76 76 Phase 2 Phase 3

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

78 78 Another use of context free grammars: Context Free Art http://www.contextfreeart.org/index.html

79 79 Context Free Art

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

81 81 Pushdown Automata PDAs

82 82 Pushdown Automaton - PDA Input String Stack States

83 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 a 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 84 The States Input symbol Pop old reading stack symbol Push new writing stack symbol

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

86 86 input Push top stack

87 87 input Pop top stack

88 88 input No Change top stack

89 89 Formal Definition Pushdown Automaton is defined as 7-tuple States Input alphabet Stack alphabet Transition function Final states Stack start symbol start state

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

91 91 Input Stack Example 3.7 Time 1

92 92 Input Stack Example 3.7 Time 2

93 93 Input Stack Example 3.7 Time 3

94 94 Input Stack Example 3.7 Time 4

95 95 Input Stack Example 3.7 Time 5

96 96 Input Stack Example 3.7 Time 6

97 97 Input Example 3.7 Time 7 Stack


Download ppt "11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012."

Similar presentations


Ads by Google