DOC PREVIEW
Berkeley COMPSCI 164 - Lecture 7: General and Bottom-Up Parsing

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 7: General and Bottom-Up ParsingFixing Recursive DescentMaking a Deterministic AlgorithmEarley's Algorithm: IChart ParsingExampleExample, completedAdding Semantic ActionsAmbiguityLecture 7: General and Bottom-Up ParsingAdministrivia• Project #1 posted. Due 27 Feb.• HW #3 posted, due next Monday. HW #4 goes back to Fridayschedule.• Test #1: March 10 (in class).Last modified: Wed Mar 11 19:41:44 2009 CS164: Lecture #7 1Fixing Recursive Descent• Can formulate top-down parsing analogously to NFAs.parse (A, S):"""Assuming A is a nonterminal and S = c1c2. . . cnis a string,return integer k such that A can derive the string c1. . . ck."""Choose production ‘A: α1α2· · · αm’ for A (nondeterministically)k = 0for x in α1, α2, · · · , αm:if x is a terminal:if x == ck+1:k += 1else:GIVE UPelse:k += parse (x, ck+1· · · cn)return k• Assume that the grammar contains one production for the startsymbol: p: γ⊣.• We’ll say that a call to parse returns a value ifsomeset of choicesfor productions (the blue step) would return a value (just like NFA).• Then if parse(p, S) returns a value, S must be in the language.Last modified: Wed Mar 11 19:41:44 2009 CS164: Lecture #7 2Making a Deterministic Algorithm• If we had an infinite supply of processors, could just spawn new onesat each “Choose” line.• Some would give up, some loop forever, but on correct programs, atleast one process would get through.• To do this for real (say with one processor), need to keep track ofall possibilities systematically.• This is the idea behind Earley’s algorithm:– Handles any context-free grammar.– Finds all parses of any string.– Runs in O(N3) time for ambiguous grammars, O(N2) time for “non-deterministic grammars”, or O(N ) time for deterministic gram-mars (such as accepted by Bison).Last modified: Wed Mar 11 19:41:44 2009 CS164: Lecture #7 3Earley’s Algorithm: I• First, reformulate to ditch the loop. Assume the string S = c1· · · cnis fixed.parse (A: α • β, s, k):"""Assumes A: αβ is a production in thegrammar, 0 <= s <= k <= n, and α can produce the stringcs+1· · · ck. Returns integer j such that β canproduce ck+1· · · cj."""if β is empty:return kAssume β has the form yδif y is a terminal:if y == ck+1:return parse(A: αy • δ, s, k+1)elseGIVE UPelse:Choose production ‘y: κ’ for y (nondeterministically)j = parse(y: •κ, k, k)return parse (A: αy • δ, s, j)• Now do all possible choices that result in such a way as to avoidredundant work (“nondeterministic memoization”).Last modified: Wed Mar 11 19:41:44 2009 CS164: Lecture #7 4Chart Parsing• Idea is to build up a table (known as achartof all calls to parse thathave been made.• Only one entry in chart for each distinct triple of arguments (A: α • β, s, k).• We’ll organize table in columns numbered by the k parameter, sothat column k represents all calls that are looking at ck+1in theinput.• Each column contains entries with the other two parameters: [A: α • β, s],which is called anitem.• The columns, therefore, areitem sets.Last modified: Wed Mar 11 19:41:44 2009 CS164: Lecture #7 5ExampleGrammarp : e ’⊣’e : s I | e ’+’ es : ’-’ |Input String- I + I ⊣Chart. Headings are values of k and ck+1(raised symbols).0-1I2+3Ia.p: •e ’⊣’, 0e.s: ’-’•, 0g.e: s I•, 0i.e: e ’+’ •e, 0b.e: •e ’+’ e, 0f.e: s•I, 0h.e: e •’+’ e, 0j.e: •s I, 3c.e: •s I, 0k.s: •, 3d.s: •’-’, 0l.e: s •I, 34⊣5m.e: s I•, 3p.p: e ’⊣’ •, 0n.e: e ’+’ e•, 0o.p: e•’⊣’, 0Last modified: Wed Mar 11 19:41:44 2009 CS164: Lecture #7 6Example, completed• Last slide showed only those items that survive and get used. Algo-rithm actually computes dead ends as well (unlettered, in red).0-1I2+3Ia.p: •e ’⊣’, 0e.s: ’-’•, 0g.e: s I•, 0i.e: e ’+’ •e, 0b.e: •e ’+’ e, 0f.e: s•I, 0h.e: e •’+’ e, 0j.e: •s I, 3c.e: •s I, 0k.s: •, 3d.s: •’-’, 0l.e: s •I, 3s: •, 0 s: •’-’, 3e: •e ’+’ e, 34⊣5m.e: s I•, 3p.p: e ’⊣’ •, 0n.e: e ’+’ e•, 0o.p: e•’⊣’, 0e: e •’+’ e, 3Last modified: Wed Mar 11 19:41:44 2009 CS164: Lecture #7 7Adding Semantic Actions• Pretty much like recursive descent. The call parse(A: α • β, s, k)can return, in addition to j, the semantic value of the A that matchescharacters cs+1· · · cj.• This value is actually computed during calls of the form parse(A: α′•,s, k) (i.e., where the β part is empty).• Assume that we have attached these values to the nonterminals inα, so that they are available when computing the value for A.Last modified: Wed Mar 11 19:41:44 2009 CS164: Lecture #7 8Ambiguity• Ambiguity only important here when computing semantic actions.• Rather than being satisfied with a single path through the chart, welook atallpaths.• And we attach thesetof possible results of parse(Y: •κ, s, k)to the nonterminal Y in the algorithm.Last modified: Wed Mar 11 19:41:44 2009 CS164: Lecture #7


View Full Document

Berkeley COMPSCI 164 - Lecture 7: General and Bottom-Up Parsing

Documents in this Course
Lecture 8

Lecture 8

40 pages

Load more
Download Lecture 7: General and Bottom-Up Parsing
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture 7: General and Bottom-Up Parsing and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 7: General and Bottom-Up Parsing 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?