cs314: formal languages and automata theory · pda can read and write into the stack by popping the...

25
Chapter 2: Context Free Grammar CS314: FORMAL LANGUAGES AND AUTOMATA THEORY L. NADA ALZABEN

Upload: others

Post on 18-Jan-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

Chapter 2: Context Free Grammar

CS314: FORMAL LANGUAGES

AND AUTOMATA THEORY L. NADA ALZABEN

Page 2: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

Quick Note

Cs314pnu.wordpress.com

Computer Science Department

2

Page 3: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

2.1 Context Free Grammar (CFG)

Useful in App’s that use recursion feature also it occurs in the specification and compilation of a programming language.

Also used in the study of human languages

Parsers in compilers extracts the meaning of a program before generating the compiled code. That is done when CFG is available.

The collection of languages associated with the CFG is called Context Free Language (CFL).

The machine that recognizes the CFL is called pushdown automata (PDA).

Computer Science Department

3

Page 4: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

2.1 Context Free Grammar (CFG)

E.g. G1: (Each part is called substitution rule or

production.)

Or “|” can be used to simplify the production.

Strings can be generated as follows:

A 0A1

A B

B #

Start variable terminal

Computer Science Department

4

Page 5: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

The sequence of substitutions to obtain a string is

called derivation. A derivation is:

Parse tree:

2.1 Context Free Grammar (CFG)

Computer Science Department

5

Page 6: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

E.g. G2:

2.1 Context Free Grammar (CFG)

Computer Science Department

6

Page 7: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

Formal definition of CFG

When we say Aw as rule in the grammar this

means uAv is uwv.

When we say u drives v, u v this means u=v

Computer Science Department

7

Page 8: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

E.g. G3:

E.g. G4: define if the string is generated by the G4

grammar

1. a + a x a 2. ( a + a) x a

2.1 Context Free Grammar (CFG)

Computer Science Department

8

Page 9: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

2.1 Context Free Grammar (CFG)

Computer Science Department

9

Page 10: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

A compiler translates the code to another form to

be executed. To do so the compiler extracts the

meaning of the code by the Parsing process as we

did in parse tree.

2.1 Context Free Grammar (CFG)

Computer Science Department

10

Page 11: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

Designing CFG

Designing CFG require creativity same as the finite

automata.

First method: is to break the CFL to smaller ones then

combine them with “|” operator.

E.g. design the CFG for

Computer Science Department

11

Page 12: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

Second method: if the language is regular we can construct the CFG by first giving the DFA of the language

Third method: if the CFG contains strings with two substrings that are linked which need to remember an unbounded amount of information about one of the substrings to verify that it corresponds to the other substrings.

Fourth method: for complex languages with strings of many structures that are recursively appearing as part of other or the same structure (mostly happens in the grammar that generate arithmetic expression. )

Designing CFG

Computer Science Department

12

Page 13: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

Ambiguity

When a grammar generates the same string in several

different ways (different parse trees) we say the string is

derived ambiguously in that grammar.

We call that grammar ambiguous grammar.

E.g. G5

test a + a x a

Computer Science Department

13

Page 14: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

TEST YOUR SELF

Use G2 to show the two different derivation of:

Submit on Thursday's lecture

Computer Science Department

14

Page 15: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

Some languages can be generated by ambiguous

grammars and also unambiguous grammar BUT for

those who only are generated by the ambiguous

grammar is called inherently ambiguous

Ambiguity

Computer Science Department

15

Page 16: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

CHOMSKY normal form

Simplifying the CFG is by using CHOMSKY normal form rules.

Any CFL is generated by a CFG in the CHOMSKY normal form.

Computer Science Department

16

Page 17: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

E.g. convert the G6 into CHOMSKY normal form:

CHOMSKY normal form

Step1: make new start

variable

S ASA | aB

A B|S

B b|ᵋ

𝑺𝟎 S

S ASA | aB

A B|S

B b|ᵋ

Step2: Remove ᵋ

𝑺𝟎 S

S ASA | aB|a

A B|S |ᵋ

B b|ᵋ

𝑺𝟎 S

S ASA | aB|a |AS|SA|S

A B|S |ᵋ

B b

Step3: Remove unit rule

𝑺𝟎 S| ASA | aB|a |AS|SA

S ASA | aB|a |AS|SA|S

A B|S

B b

𝑺𝟎 ASA | aB|a |AS|SA

S ASA | aB|a |AS|SA

A B|S|b|ASA|aB|a |AS|SA

B b Computer Science Department

17

Page 18: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

E.g. convert the G6 into CHOMSKY normal form:

CHOMSKY normal form

𝑺𝟎 ASA | aB|a |AS|SA

S ASA | aB|a |AS|SA

A b|ASA|aB|a |AS|SA

B b

Computer Science Department

18

Step4: convert it to proper form

𝑺𝟎 AA1 | UB|a |AS|SA

S AA1 | UB|a |AS|SA

A b|AA1|UB|a |AS|SA

B b

A1 SA

U a

Page 19: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

2.2 Pushdown Automata (PDA)

PDA is a generalization of NFA.

PDA structure

NFA part

(state diagram)

W string

Input tape (unbounded) Stack

(unbounded)

Read Head Read /write

Head

Computer Science Department

19

Page 20: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing.

The accept status is reached when the input tape and the stack are empty

Reading ᵋ from input tape or stack means ignoring the reading part.

2.2 Pushdown Automata (PDA)

Formal Definition of PDA

Computer Science Department

20

Page 21: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

Formal Definition of PDA

Computer Science Department

21

Page 22: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

E.g. the formal description for PDA machine

recognizing the language {0𝑛1𝑛 𝑛 ≥ 0 . Let

M1=( 𝑄, , 𝛤 , 𝛿, 𝑞1, 𝐹 where:

Q={q1,q2,q3,q4}

= 0,1 , 𝛤={0,$}

F={q1,q4} , 𝛿 is givin by the table:

2.2 Pushdown Automata (PDA)

input 0 1 ᵋ

Stack 0 $ ᵋ 0 $ ᵋ

0 $ ᵋ Q1 {(q2,$)}

Q2 {(q2,0)} {(q3,ᵋ)}

Q3 {(q3,ᵋ)} {(q4,ᵋ)}

Q4 Computer Science Department

22

Page 23: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

2.2 Pushdown Automata (PDA)

Computer Science Department

23

Page 24: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

E.g. M2 recognizes the language

{𝑎𝑖𝑏𝑗𝑐𝑘 𝑖, 𝑗, 𝑘 ≥ 0 𝑎𝑛𝑑 𝑖 = 𝑗 𝑜𝑟 𝑖 = 𝑘

2.2 Pushdown Automata (PDA)

Computer Science Department

24

Page 25: CS314: FORMAL LANGUAGES AND AUTOMATA THEORY · PDA can read and write into the stack by popping the symbol in reading and pushing another symbol in writing. The accept status is reached

2.2 Pushdown Automata (PDA)

Computer Science Department

25