1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.

Slides:



Advertisements
Similar presentations
Context-Free Languages
Advertisements

CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
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.
ISBN Chapter 3 Describing Syntax and Semantics.
CFGs and PDAs Sipser 2 (pages ). Last time…
Costas Busch - RPI1 Grammars. Costas Busch - RPI2 Grammars Grammars express languages Example: the English language.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Courtesy Costas Busch - RPI
1 Grammars. 2 Grammars express languages Example: the English language.
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 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.
Chapter 3: Formal Translation Models
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
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 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.
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
1 Regular Expressions. 2 Regular expressions describe regular languages Example: describes the language.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
Grammars CPSC 5135.
Context-free Languages
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CS 3240: Languages and Computation Context-Free Languages.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
LESSON 04.
Context Free Grammars 1. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
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 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.
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.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Chapter 5 Context-Free Languages Regular language are effective in describing certain simple patterns. But it is not enough to describe programming languages.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
1 Context Free Grammars Xiaoyin Wang CS 5363 Spring 2016.
Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered.
Theory of Languages and Automata By: Mojtaba Khezrian.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Chapter 3: Describing Syntax and Semantics
Chapter 3 – Describing Syntax
Context-Free Languages
Formal Language & Automata Theory
G. Pullaiah College of Engineering and Technology
Chapter 3 – Describing Syntax
Syntax versus Semantics
Syntax Analysis Sections :.
CSE322 LEFT & RIGHT LINEAR REGULAR GRAMMAR
CS21 Decidability and Tractability
Lecture 7: Introduction to Parsing (Syntax Analysis)
فصل دوم Context-Free Languages
Context-Free Languages
Chapter 2 Context-Free Language - 01
Theory of Computation Lecture #
… NPDAs continued.
Presentation transcript:

1 Context-Free Languages

2 Regular Languages

3 Context-Free Languages

4 Pushdown Automata Context-Free Grammars stack automaton

5 Context-Free Grammars

6 Grammars Grammars express languages Example: the English language

7

8 A derivation of “the dog walks”:

9 A derivation of “a cat runs”:

10 Language of the grammar: L = { “a cat runs”, “a cat walks”, “the cat runs”, “the cat walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” }

11 Notation VariableTerminal Production Rules

12 Another Example Grammar: Derivation of sentence :

13 Grammar: Derivation of sentence :

14 Other derivations:

15 Language of the grammar

16 More Notation Grammar Set of variables Set of terminal symbols Start variable Set of Production rules

17 Example Grammar :

18 More Notation Sentential Form: A sentence that contains variables and terminals Example: Sentential Formssentence

19 We write: Instead of:

20 In general we write: If:

21 By default:

22 Example Grammar Derivations

23 Grammar Example Derivations

24 Another Grammar Example Grammar : Derivations:

25 More Derivations

26 Language of a Grammar For a grammar with start variable : String of terminals

27 Example For grammar : Since:

28 A Convenient Notation

29 Example A context-free grammar : A derivation:

30 A context-free grammar : Another derivation:

31 (((( )))) ‏ Describes parentheses:

32 A context-free grammar : A derivation: Example

33 A context-free grammar : Another derivation:

34

35 A context-free grammar : A derivation: Example

36 A context-free grammar : A derivation:

37 () ((( ))) (( )) ‏ Describes matched parentheses:

38 Definition: Context-Free Grammars Grammar Productions of the form: String of variables and terminals VariablesTerminal symbols Start variable Variable

39

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

41 Derivation Order Leftmost derivation: Rightmost derivation:

42 Leftmost derivation: Rightmost derivation:

43 Derivation Trees

44

45

46

47

48 Derivation Tree

49 yield Derivation Tree

50 Ambiguity

51 leftmost derivation

52 leftmost derivation

53 Two derivation trees

54 The grammar is ambiguous: stringhas two derivation trees

55 stringhas two leftmost derivations The grammar is ambiguous:

56 Definition: A context-free grammar is ambiguous if some string has: two or more derivation trees

57 In other words: A context-free grammar is ambiguous if some string has: two or more leftmost derivations (or rightmost) ‏

58 Why do we care about ambiguity? take

59

60

61 Correct result:

62 We want to remove ambiguity Ambiguity is bad for programming languages

63 Another Ambiguous Grammar IF_STMTif EXPR then STMT if EXPR then STMT else STMT

64 If expr1 then if expr2 then stmt1 else stmt2 IF_STMT expr1then elseifexpr2then STMT stmt1 if IF_STMT expr1thenelse ifexpr2then STMTstmt2 if stmt1 stmt2

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

66 The string has two derivation trees