PARSING

18
PARSING Lecture # 14 Naveen Z Quazilbash

description

PARSING. Lecture # 14 Naveen Z Quazilbash. Overview. Parsing Simple grammars-if time permits Reading assignment Quiz no.2. Parsing. Given a string w and a grammar, determine whether the grammar generates w. Finding a derivation of w is called Parsing. E.g. S aSb|bSa|SS | ε - PowerPoint PPT Presentation

Transcript of PARSING

Page 1: PARSING

PARSING

Lecture # 14Naveen Z Quazilbash

Page 2: PARSING

Overview

• Parsing• Simple grammars-if time permits• Reading assignment• Quiz no.2

Page 3: PARSING

Parsing

• Given a string w and a grammar, determine whether the grammar generates w.

• Finding a derivation of w is called Parsing.• E.g.

SaSb|bSa|SS|εw=aabb

• Parsing: S=>aSb=>aaSbb=>aabb

Page 4: PARSING

Parsing

• Brute-force Technique (Exhaustive Search):– Determine everything that can be generated in

one production (first “round”).– In two productions (second “round”).– In three productions…..– Terminate after generating w

• We can discard intermediate results that do not match w.

Page 5: PARSING

• Example:• On board

Page 6: PARSING

Parsing

• Problems:– Very slow– May go in cycles: S=>SS=>S=>SS=>S=>….– If w is not in the language, may not terminate.

• To avoid cycles, get rid of the following types of productions:– Aε (empty string)– AB (replacing a variable with another variable)

Page 7: PARSING

Parsing

• If the language does not include empty string, then we can get rid of these production types.

• E.g. for the language above:– SaSb|bSa|SS|ab|ba – It no longer includes ε

Page 8: PARSING

Parsing

• Example without undesirable productions:SaC|bDCbC|aDbD|b

• If we use a grammar without undesirable productions, then at each step either:– The string gets longer or– We reduce the number of variables and the string

(still) does not get shorter

Instruction: Copy this grammar

Page 9: PARSING

Parsing

• Example:• S=>aC=>abC=>aba

• Thus, we cannot get into a cycle.

Longer string

Longer string

Fewer variables

Page 10: PARSING

Parsing

• Very slow• To avoid infinite search for a derivation of w:– Terminate when the string has more than |w|

characters.– If we have no undesirable productions, the string

does not get shorter, so it is a “safe termination”.

Page 11: PARSING

Parsing

• Example: w=aaa• On board• Can this be derived or not?

Page 12: PARSING

Parsing

• Remarks:– The time of this exhaustive search is exponential– There is a more efficient algorithm with running

time proportional to |w|2- still too slow.– Some special cases allow faster parsing. E.g.

simple grammars( aka s-grammars).

Page 13: PARSING

Simple Grammars-if time permits

• Lets first have a look at the assignment and quiz!!

Page 14: PARSING

Reading Assignment

• Explore 5.2, 5.2.1, 5.2.2 from Ullman.• Reference Book: • Peter Linz, An introduction to formal

languages and automata, Third edition• Motivation: the final term paper might

include terminologies and concepts from this assignment.

Page 15: PARSING

Assignment # 3-Redo

• Explore “Rightmost” and “Leftmost” derivations.

• Explore “Parse Trees”.• Give one example of each.• Total points=6

Page 16: PARSING

Quiz # 2

Q1: Consider the following grammar: (3 points)SSA | AAaAb | ε

• Show a left-most derivation, right-most derivation, and derivation tree for the string abaabb

Page 17: PARSING

• Q2: the following grammar generates the language of RE 0*1(0+1)*: (3 points)SA1BA0A|εB0B|1B| ε

• Give leftmost and rightmost derivations of the string 00101.

• Also draw parse trees for the derivation.

Page 18: PARSING

BONUS Qn: Design a context-free grammar for the following language:

The set {0n1n|n≥1}, that is, the set of all strings of one or more 0’s followed by an equal number of 1’s.

5 points