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

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.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CS5371 Theory of Computation
Costas Busch - RPI1 Pushdown Automata PDAs. Costas Busch - RPI2 Pushdown Automaton -- PDA Input String Stack States.
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 Busch - RPI1 Pushdown Automata PDAs.
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.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
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 2006Costas Busch - RPI1 PDAs Accept 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 Pushdown Automata PDAs. 2 Pushdown Automaton -- PDA Input String Stack States.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2005Costas Busch - RPI1 Context-Free Languages.
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 7 Mälardalen University 2010.
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.
CS 461 – Sept. 19 Last word on finite automata… –Scanning tokens in a compiler –How do we implement a “state” ? Chapter 2 introduces the 2 nd model of.
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.
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 Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
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.
Context-Free Languages. Regular Languages Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Chapter 5 Context-Free Languages Regular language are effective in describing certain simple patterns. But it is not enough to describe programming 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.
1.Draw a parse tree for the following derivation: S  C A C  C A b b  b b A b b  b b B b b  b b a A a a b b  b b a b a a b b 2. Show on your parse.
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,
Costas Busch - LSU1 PDAs Accept Context-Free Languages.
Theory of Languages and Automata By: Mojtaba Khezrian.
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
Pushdown Automata PDAs
PDAs Accept Context-Free Languages
NPDAs Accept Context-Free Languages
Parsing Costas Busch - LSU.
Context-Free Languages
… NPDAs continued.
CSE 105 theory of computation
CSE 105 theory of computation
Presentation transcript:

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

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

Midterm 1 Solution 3 Duggor/MIDTERM Solution.pdf

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. A comment on the MIDTERM 1

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

6 Automata theory: formal languages and formal grammars

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

8 Context-Free Languages Pushdown Automata Context-Free Grammars

9 Context-Free Grammars

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

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

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

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 A context-free grammar A derivation Example 1:

15 A context-free grammar Another derivation

16

17 A context-free grammar A derivation Example 2:

18 A context-free grammar Another derivation

19

20 A context-free grammar A derivation Example 3:

21 A context-free grammar A derivation

22

Example 4:

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

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

27 Derivation Order Leftmost derivation

28 Derivation Order Rightmost derivation

29 Leftmost derivation

30 Rightmost derivation

31 Derivation Trees

32 Derivation can be represented in a tree form

33

34

35

36 Derivation Tree

37 yield Derivation Tree

38 Partial Derivation Trees Partial derivation tree

39 Partial derivation tree

40 Partial derivation tree sentential form yield

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

42 Ambiguity

43 leftmost derivation derivation (* denotes multiplication)

44 derivation leftmost derivation

45

46 Two derivation trees

47 The grammar is ambiguous! Stringhas two derivation trees

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

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

50 Why do we care about ambiguity?

51 Why do we care about ambiguity?

52 Why do we care about ambiguity?

53 Correct result:

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

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

56

57 Unique derivation tree

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

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

60 The string has two derivation trees

61 Regular Languages Context-Free Languages Non-regular languages

62 Applications: Compilers

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 Lexical analyzer parser Compiler program machine code input output

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

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

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

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

70 Parsing examples

71 grammar Parser input string derivation

72 Example: Parser derivation input ?

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

74

75 Phase 2 Phase 1

76 Phase 2 Phase 3

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

78 Another use of context free grammars: Context Free Art

79 Context Free Art

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 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 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 Formal Definition Pushdown Automaton is defined as 7-tuple States Input alphabet Stack alphabet Transition function Final states Stack start symbol start state

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

91 Input Stack Example 3.7 Time 1

92 Input Stack Example 3.7 Time 2

93 Input Stack Example 3.7 Time 3

94 Input Stack Example 3.7 Time 4

95 Input Stack Example 3.7 Time 5

96 Input Stack Example 3.7 Time 6

97 Input Example 3.7 Time 7 Stack