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

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)
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 4 Mälardalen University 2010.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 4 School of Innovation, Design and Engineering Mälardalen University 2012.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Costas Busch - RPI1 Grammars. Costas Busch - RPI2 Grammars Grammars express languages Example: the English language.
Courtesy Costas Busch - RPI
1 Grammars. 2 Grammars express languages Example: the English language.
1 Normal Forms for Context-free Grammars. 2 Chomsky Normal Form All productions have form: variable and terminal.
Fall 2004COMP 3351 NPDA’s Accept Context-Free Languages.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
1 Regular Grammars Generate Regular Languages. 2 Theorem Regular grammars generate exactly the class of regular languages: If is a regular grammar then.
Courtesy Costas Busch - RPI1 Context-Free Languages.
1 Reverse of a Regular Language. 2 Theorem: The reverse of a regular language is a regular language Proof idea: Construct NFA that accepts : invert the.
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.
COP4020 Programming Languages
Prof. Busch - LSU1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
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.
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.
Fall 2003Costas Busch - RPI1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
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.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2010.
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
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.
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.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Overview of Previous Lesson(s) Over View  In our compiler model, the parser obtains a string of tokens from the lexical analyzer & verifies that the.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
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.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Regular Grammars Reading: 3.3. What we know so far…  FSA = Regular Language  Regular Expression describes a Regular Language  Every Regular Language.
Chapter 5 Context-Free Languages Regular language are effective in describing certain simple patterns. But it is not enough to describe programming languages.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2007.
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 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
Costas Busch - LSU1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2006.
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.
1 Linear Grammars Grammars with at most one variable at the right side of a production Examples:
Chapter 3 – Describing Syntax
Context-Free Languages
G. Pullaiah College of Engineering and Technology
Simplifications of Context-Free Grammars
CSE322 LEFT & RIGHT LINEAR REGULAR GRAMMAR
Parsing Costas Busch - LSU.
… NPDAs continued.
Normal Forms for Context-free Grammars
Presentation transcript:

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

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 Grammars

4 Grammars express languages Example: the English language

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

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

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

8 The language of the grammar:

9 Notation Non-terminal (Variable) Terminal Production rule

10 Example Derivation of sentence: Grammar:

11 Grammar: Derivation of sentence

12 Other derivations

13 The language of the grammar

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

15 Example Grammar

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

17 We write: Instead of:

18 In general we write if By default ()

19 Example Grammar Derivations

20 baaaaaSbbbbaaSbb   S   Grammar Example Derivations

21 Another Grammar Example Derivations Grammar

22 More Derivations

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

24 Example For grammar Since

25 Notation

26 Linear Grammars

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

28 A Non-Linear Grammar Grammar

29 Another Linear Grammar Grammar

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

31 Left-Linear Grammars All productions have form or Example

32 Regular Grammars

33 Regular Grammars Generate Regular Languages

34 Theorem Languages Generated by Regular Grammars Regular Languages

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

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

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

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 Grammar is right-linear Example

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

41 Add edges for each production:

42

43

44

45

46

47 Grammar NFA

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

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

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

51 Example

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 Since is left-linear grammar the productions look like:

54 Construct right-linear grammar In :

55 Construct right-linear grammar In :

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

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

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 Example

60 Convert to a right-linear grammar

61 In General For any transition: Add production: variableterminalvariable

62 For any final state: Add production:

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

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

65 Observation Regular grammars generate regular languages Examples

66 Regular Languages Context-Free Languages Non-regular languages

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

68 Context-Free Languages

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

70 Context-Free Grammars

71 A context-free grammar A derivation Example

72 A context-free grammar Another derivation

73

74 A context-free grammar A derivation Example

75 A context-free grammar Another derivation

76

77 A context-free grammar A derivation Example

78 A context-free grammar A derivation

79

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

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

82 Derivation Order Leftmost derivation

83 Derivation Order Rightmost derivation

84 Leftmost derivation

85 Rightmost derivation

86 Derivation Trees

87

88

89

90

91 Derivation Tree

92 yield Derivation Tree

93 Partial Derivation Trees Partial derivation tree

94 Partial derivation tree

95 Partial derivation tree sentential form yield

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

97 Ambiguity

98 leftmost derivation derivation (* denotes multiplication)

99 derivation leftmost derivation

100 Two derivation trees

101 The grammar is ambiguous! Stringhas two derivation trees

102 stringhas two leftmost derivations The grammar is ambiguous:

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

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

105 Why do we care about ambiguity?

106 Why do we care about ambiguity?

107 Correct result:

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

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

110

111 Unique derivation tree

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

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

114 The string has two derivation trees

115 Compilers

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

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

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

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

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

123 Parsing

124 grammar Parser input string derivation

125 Example: Parser derivation input ?

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

127

128 Phase 2 Phase 1

129 Phase 2 Phase 3

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