55
C O N T E X T - F R E E LANGUAGES (use a grammar to describe a language) 1

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language)

  • Upload
    pegeen

  • View
    27

  • Download
    0

Embed Size (px)

DESCRIPTION

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language). Context-free grammars. Ch.1 introduced two different, though equivalent, methods of describing languages: finite automata and regular expressions . - PowerPoint PPT Presentation

Citation preview

Page 1: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

1

C O N T E X T - F R E ELANGUAGES

(use a grammar to describe a language)

Page 2: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

2

Page 3: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

3

Context-free grammars• Ch.1 introduced two different, though equivalent, methods of

describing languages: finite automata and regular expressions. • Languages can be described in this way but that some simple

languages, such as (0n 1n | n > 0}, cannot.

CONTEXT-FREE GRAMMARS (C.F.L.)

• powerful method of describing languages.• used in the study of human languages.• understanding the relationship of terms such as noun, verb,

and preposition.• An important application of context-free grammars occurs in

the specification and compilation of programming languages.

Page 4: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

4

Context-free grammars (cont.)

• A grammar for a programming language often appears as a reference for people trying to learn the language syntax.

• Designers of compilers and interpreters for programming languages often start by obtaining a grammar for the language.

• Most compilers and interpreters contain a component called a parser that extracts the meaning of a program prior to generating the compiled code or performing the interpreted execution.

Page 5: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

5

Context free languages

• The collection of languages associated with context-free grammars are called the context-free languages.

Page 6: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

6

Objectives

• To give a formal definition of context-free grammars and study the properties of context-free languages.

• To introduce pushdown automata, a class of machines recognizing the context-free languages.

Page 7: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

7

CONTEXT-FREE GRAMMARS

• The following is an example of a context-free grammar, which we call G1:

• A grammar consists of a collection of substitution rules, also called productions.

• Each rule appears as a line in the grammar, comprising a symbol and a string separated by an arrow.

Page 8: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

8

A → β

• The symbol is called a variable. • The string consists of variables and other symbols

called terminals.• The variable symbols often are represented by

capital letters.• The terminals are (input alphabet) often represented

by lowercase letters, numbers, or special symbols.• One variable is designated as the start variable (left-

hand side of the topmost rule.).

Page 9: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

9

Rule

• Any production of the form A → β. β can therefore be any string of terminal and non-terminal elements.

• Example: A → BC A → a

start variable terminals

variable symbols

Page 10: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

10

Example Example grammar G1:

• Grammar G1 contains three rules.• G1 's variables are A and B, where A is the start

variable. • Its terminals are 0, 1, and #.

Page 11: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

11

Describing a language

• a grammar is used to describe a language by generating each string of that language in the following manner:

1. Write down the start variable. (left-hand side of the top rule, unless specified otherwise).

2. Find a variable that is written down and a rule that starts with that variable. Replace the written down variable with the right-hand side of that rule.

3. Repeat step 2 until no variables remain.

Page 12: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

12

Abbreviation

• abbreviate several rules with the same left-hand variable, such as A 0A1 and A B, into a single lineA 0A1 I B, using the symbol " I " as an "or.“A 0A1 or B.

Page 13: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

13

Derivation• The sequence of substitutions to obtain a string is called a

derivation.• A derivation of string 000#111 in grammar G1 is:

parse tree

Page 14: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

14

• Grammar G2 has ?? – rules?? - variables ?? - terminals??.

Page 15: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

15

Grammar G2

• Grammar G2 has:

• 10 variables (the capitalized grammatical terms written inside brackets);

• 27 terminals (the standard English alphabet plus a space character);

• 18 rules.

Page 16: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

16

Derivation

• Each of these strings has a derivation in grammar G2. The following is a derivation of the first string on this list.

Page 17: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

17

FORMAL DEFINITION OF A CONTEXT-FREE GRAMMAR

Page 18: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

18

Example Grammar G1

In grammar G2

Page 19: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

19

Ambiguity

• The generation of a sentence by a context-free grammar can be represented by a tree diagram.

• Not only one way in which a sentence can be derived.

Page 20: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

20

Example

Let G be a context-free grammar with the following productions:1. S → AB 5. Β → Sd2. S → CD 6. C → aS3. S → bc 7. D → d4. A → a

The sentence abcd can be derived from this grammar??????????????????

Page 21: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

21

solution & Derivation tree The sentence abcd can be derived from this grammar as follows: 1. S AB⇒ ⇒ aB aSd abcd.⇒ ⇒2. S AB ASd Abcd abcd,⇒ ⇒ ⇒ ⇒3. S CD aSD abcD abcd. (⇒ ⇒ ⇒ ⇒ Alternative)

Derivation tree for the sentence abcd -1&2Derivation tree for the sentence abcd -3 true or false?????

c

Page 22: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

22

Ambiguity (cont.)

• If a grammar generates the same string in several different ways, we say that the string is derived ambiguously in that grammar.

• If a grammar generates some string ambiguously we say that the grammar is ambiguous.

Page 23: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

23

ExampleFor example, consider grammar G5:

This grammar generates the string a+axa ambiguously?????????????? Yes (two different parse trees)

Page 24: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

24

DEFINITION 2.7

• A string w is derived ambiguously in context-free grammar G if it has two or more different leftmost derivations.

• Grammar G is ambiguous if it generates some string ambiguously.

Page 25: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

25

Chomsky normal form

Page 26: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

26

Page 27: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

27

EXAMPLE 2.10

• Let G6 be the following CFG and convert it to Chomsky normal form by using the conversion procedure just given. The series of grammars presented illustrates the steps in the conversion. Rules shown in bold have just been added. Rules shown in gray have just been removed.

Page 28: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

28

Page 29: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

29

Page 30: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

30

Page 31: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

31

Context-Free Languages{0n1n I n > 0} is not regular???

Example: G1A → 0A1A → B

B# →

A 0A1 00A11 000A111 000B111 000#111⇒ ⇒ ⇒ ⇒ ⇒

L(G1) = {0n#1n | n ≥ 0 }parse tree

Can the following grammar generate the subsequent language? 000#111

Page 32: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

32

A Grammar for Arithmetic Expressions

Let :X = {E, T, F, id, + , - ,*,/,(,), a, b, c} T = {a, b, c, + , - ,*,/,(,)}. The start symbol S is E and the productions are as follows

Write a derivation of string (a + b)*c

Page 33: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

33

Write a derivation of string (a + b)*c

The derivation of string (a + b)*c:

Page 34: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

34

PUSHDOWN AUTOMATA

• It’s a new type of computational model called pushdown automata.

• These automata are like nondeterministic finite automata but have an extra component called a stack.

• The stack provides additional memory beyond the finite amount available in the control.

Page 35: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

35

The following figure is a schematic representation of a finite automaton.The control represents the states and transition function, the tape contains the input string, and the arrow represents the input head, pointing at the next input symbol to be read.

Schematic of a finite automaton

Page 36: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

36

Schematic of a pushdown automaton

With the addition of a stack component we obtain a schematic representation of a pushdown automaton, as shown in the following figure

Page 37: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

37

Page 38: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

38

PUSHDOWN AUTOMATA• A pushdown automaton (PDA) can write symbols on the

stack and read them back later.

• Writing a symbol "pushes down" all the other symbols on the stack.

• At any time the symbol on the top of the stack can be read and removed.

• Writing a symbol on the stack is often referred to as pushing the symbol, and removing a symbol is referred to as popping it.

Page 39: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

39

PUSHDOWN AUTOMATA

• a stack is a "last in, first out" storage device.

• If certain information is written on the stack and additional information is written afterward, the earlier information becomes inaccessible until the later information is removed.

• A stack is valuable because it can hold an unlimited amount of information.

Page 40: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

40

PUSHDOWN AUTOMATA

• Recall that a finite automaton is unable to recognize the language {0n1n I n > 0} because it cannot store very large numbers in its finite memory.

• A PDA is able to recognize this language because it can use its stack to store the number of Os it has seen.

• Thus the unlimited nature of a stack allows the PDA to store numbers of unbounded size.

Page 41: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

41

Informal description• The following informal description shows how the

automaton for this language works:

– Read symbols from the input. As each 0 is read, push it onto the stack.

– Pop a 0 off the stack for each 1 read. – If reading the input is finished exactly when the stack

becomes empty of 0s, accept the input.– If the stack becomes empty while is remain or if the is

are finished while the stack still contains 0s or if any 0s appear in the input following is, reject the input.

Page 42: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

42

FORMAL DEFINITION OF A PUSHDOWN AUTOMATON

Page 43: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

43

Page 44: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

44

Page 45: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

45

Page 46: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

46

Example aaabba = reject??

Page 47: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

47

Example ab#ab = accept??

• Note that it merely records a's and b's on the stack until it reaches the marker (#) and then checks them off against the remainder of the input.

Page 48: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

48

Let P be defined through X = {a, b, c}, Z = {zA = z1, z2, z3}, S = {SA, S, }, ZF = {z3} and, finally the state transitions

Page 49: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

49

Example a3bc3 = aaabccc??

((a, z1, SA(,)z1, SSA))((a, z1, S(,)z1, SS ))

((b, z1, S(,)z2,λ ))((c, z2, S(,)z2, λ ))((c, z2, SA(,)z3, λ ))

Page 51: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

51

Page 52: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

52

PDA in Figure - test empty stack by initially placing a special symbol, $, on the

stack

If ever it sees $ again on the stack, it knows that the stack is effectively empty.

Page 53: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

53

Page 54: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

54

THEOREM 2.20 • A language is context free if and only if some

pushdown automaton recognizes it.

LEMMA 2.21

• If a language is context free, then some pushdown automaton recognizes it.

Page 55: C  O N T E X T - F R E E LANGUAGES  ( use  a grammar to describe a  language)

55

Pages 115 – 122 read only“EQUIVALENCE WITH CONTEXT-FREE

GRAMMARS”

Home work pages 123-127NON-CONTEXT-FREE LANGUAGES