180 likes | 440 Views
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 | ε
E N D
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. SaSb|bSa|SS|ε w=aabb • Parsing: S=>aSb=>aaSbb=>aabb
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.
Example: • On board
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)
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 ε
Parsing • Example without undesirable productions: SaC|bD CbC|a DbD|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
Parsing • Example: • S=>aC=>abC=>aba • Thus, we cannot get into a cycle. Longer string Longer string Fewer variables
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”.
Parsing • Example: w=aaa • On board • Can this be derived or not?
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).
Simple Grammars-if time permits • Lets first have a look at the assignment and quiz!! •
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.
Assignment # 3-Redo • Explore “Rightmost” and “Leftmost” derivations. • Explore “Parse Trees”. • Give one example of each. • Total points=6
Quiz # 2 Q1: Consider the following grammar: (3 points) SSA | A AaAb | ε • Show a left-most derivation, right-most derivation, and derivation tree for the string abaabb
Q2: the following grammar generates the language of RE 0*1(0+1)*: (3 points) SA1B A0A|ε B0B|1B| ε • Give leftmost and rightmost derivations of the string 00101. • Also draw parse trees for the derivation.
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