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

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
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.
Pushdown Automata Part II: PDAs and CFG Chapter 12.
CS5371 Theory of Computation
1 … NPDAs continued. 2 Pushing Strings Input symbol Pop symbol Push string.
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 Buch - RPI1 Simplifications of Context-Free Grammars.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
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 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
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 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.
1 PDAs Accept Context-Free Languages. 2 Context-Free Languages (Grammars) Languages Accepted by PDAs Theorem:
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)
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.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Bernd Fischer RW713: Compiler and Software Language Engineering.
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.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.2: Pushdown Automata) Prof. Karen Daniels, Fall 2010 with acknowledgement.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
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.
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,
COMP 3438 – Part II - Lecture 4 Syntax Analysis I Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Theory of Languages and Automata By: Mojtaba Khezrian.
5. Context-Free Grammars and Languages
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
PDAs Accept Context-Free Languages
NPDAs Accept Context-Free Languages
فصل دوم Context-Free Languages
Parsing Costas Busch - LSU.
Context-Free Languages
… NPDAs continued.
CSE 105 theory of computation
Normal Forms for Context-free Grammars
CSE 105 theory of computation
Presentation transcript:

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

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

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

4 Regular Languages Context-Free Languages Non-regular languages

5 Automata theory: formal languages and formal grammars

6 Context-Free Languages

7 Pushdown Automata Context-Free Grammars

8 Context-Free Grammars

9 A context-free grammar A derivation Example

10 A context-free grammar Another derivation

11

12 A context-free grammar A derivation Example

13 A context-free grammar Another derivation

14

15 A context-free grammar A derivation Example

16 A context-free grammar A derivation

17

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

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

20 Derivation Order Leftmost derivation

21 Derivation Order Rightmost derivation

22 Leftmost derivation

23 Rightmost derivation

24 Derivation Trees

25

26

27

28

29 Derivation Tree

30 yield Derivation Tree

31 Partial Derivation Trees Partial derivation tree

32 Partial derivation tree

33 Partial derivation tree sentential form yield

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

35 Ambiguity

36 leftmost derivation derivation (* denotes multiplication)

37 derivation leftmost derivation

38

39 Two derivation trees

40 The grammar is ambiguous! Stringhas two derivation trees

41 stringhas two leftmost derivations The grammar is ambiguous:

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

43 Why do we care about ambiguity?

44 Why do we care about ambiguity?

45 Why do we care about ambiguity?

46 Correct result:

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

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

49

50 Unique derivation tree

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

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

53 The string has two derivation trees

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

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

56 Regular Languages Context-Free Languages Non-regular languages

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

58 Pushdown Automata PDAs

59 Pushdown Automaton - PDA Input String Stack States

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

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

63 input Push top stack

64 input Pop top stack

65 input No Change top stack

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

67 Input Stack Example 3.7 Time 1

68 Input Stack Example 3.7 Time 2

69 Input Stack Example 3.7 Time 3

70 Input Stack Example 3.7 Time 4

71 Input Stack Example 3.7 Time 5

72 Input Stack Example 3.7 Time 6

73 Input Example 3.7 Time 7 Stack

74 Applications: Compilers

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

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

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

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

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

82 Parsing

83 grammar Parser input string derivation

84 Example: Parser derivation input ?

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

86

87 Phase 2 Phase 1

88 Phase 2 Phase 3

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

90 Context Free Art

91 Context Free Art