Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

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

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

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

5 5 Automata theory: formal languages and formal grammars

6 6 Context-Free Languages

7 7 Pushdown Automata Context-Free Grammars

8 8 Context-Free Grammars

9 9 A context-free grammar A derivation Example

10 10 A context-free grammar Another derivation

11 11

12 12 A context-free grammar A derivation Example

13 13 A context-free grammar Another derivation

14 14

15 15 A context-free grammar A derivation Example

16 16 A context-free grammar A derivation

17 17

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

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

20 20 Derivation Order Leftmost derivation

21 21 Derivation Order Rightmost derivation

22 22 Leftmost derivation

23 23 Rightmost derivation

24 24 Derivation Trees

25 25

26 26

27 27

28 28

29 29 Derivation Tree

30 30 yield Derivation Tree

31 31 Partial Derivation Trees Partial derivation tree

32 32 Partial derivation tree

33 33 Partial derivation tree sentential form yield

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

35 35 Ambiguity

36 36 leftmost derivation derivation (* denotes multiplication)

37 37 derivation leftmost derivation

38 38

39 39 Two derivation trees

40 40 The grammar is ambiguous! Stringhas two derivation trees

41 41 stringhas two leftmost derivations The grammar is ambiguous:

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

43 43 Why do we care about ambiguity?

44 44 Why do we care about ambiguity?

45 45 Why do we care about ambiguity?

46 46 Correct result:

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

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

49 49

50 50 Unique derivation tree

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

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

53 53 The string has two derivation trees

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

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

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

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

58 58 Pushdown Automata PDAs

59 59 Pushdown Automaton - PDA Input String Stack States

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

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

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

63 63 input Push top stack

64 64 input Pop top stack

65 65 input No Change top stack

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

67 67 Input Stack Example 3.7 Time 1

68 68 Input Stack Example 3.7 Time 2

69 69 Input Stack Example 3.7 Time 3

70 70 Input Stack Example 3.7 Time 4

71 71 Input Stack Example 3.7 Time 5

72 72 Input Stack Example 3.7 Time 6

73 73 Input Example 3.7 Time 7 Stack

74 74 Applications: Compilers

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

76 76 Lexical analyzer parser Compiler program machine code input output

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

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

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

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

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

82 82 Parsing

83 83 grammar Parser input string derivation

84 84 Example: Parser derivation input ?

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

86 86

87 87 Phase 2 Phase 1

88 88 Phase 2 Phase 3

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

90 90 Context Free Art http://www.contextfreeart.org/ http://www.contextfreeart.org/

91 91 Context Free Art http://www.contextfreeart.org/ http://www.contextfreeart.org/


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

Similar presentations


Ads by Google