This is the end of the preview. Sign up
to
access the rest of the document.

Unformatted text preview: Top Down Parsing Top-Down Parsing The parse tree is created top to bottom. Top-down parser Recursive-Descent Parsing Backtracking is needed (If a choice of a production rule doe not work, we backtrack to try other alternatives.) It is a general parsing technique, but not widely used. Not efficient Predictive Parsing no backtracking Efficient needs a special form of grammars (LL(1) grammars). Non-Recursive (Table Driven) Predictive Parser is also known as LL(1) parser. Top-Down Parsing Begin with the start symbol at the root of the parse tree Build the parse tree from the top down Top-Down Parsing S aSbS | bSaS | S a S b S b S a S Parsing Decisions Which nonterminal in the parse tree should be expanded? Which of its grammar rules should be used to expand it? Nondeterministic Parser Expand any nonterminal. Expand it using a grammar rule that occurs in the derivation of the input string. Backtracking Parser Expand the leftmost nonterminal in the parse tree. Try a grammar rule for the nonterminal. If it does not work out, try another one. Backtracking Parser S aSa | bSb | a | b S b b b a S a Backtracking Parser S aSa | bSb | a | b S b b b b S b a S a Backtracking Parser S aSa | bSb | a | b S b b b b S b b S b a S a Backtracking Parser S aSa | bSb | a | b S b b b b S b b S b b S b Backtracking Parser S aSa | bSb | a | b S b b b b S b b S b a Backtracking Parser S aSa | bSb | a | b S b b b b S b b S b b Backtracking Parser S aSa | bSb | a | b S b b b b S b b Recursive Predictive Parsing Basic idea: Write a routine to recognize each lhs This produces a parser with mutually recursive routines. Good for hand-coded parsers. Ex: A aBb (This is only the production rule for A) proc A {- match the current token with a , and move to the next token;- call B ;- match the current token with b , and move to the next token;} Recursive Predictive Parsing ( cont.) When to apply -productions . A aA | bB | If all other productions fail, we should apply an -production. For example, if the current token is not a or b, we may apply the -production. Recursive Predictive Parsing (cont.) A aBb | bAB proc A { case of the current token { a : - match the current token with a , and move to the next token;- call B ;- match the current token with b , and move to the next token; b : - match the current token with b , and move to the next token;- call A;- call B ; }} A aB | b will correspond to A() { if (lookahead == 'a') match('a'); B(); else if (lookahead == 'b') match ('b'); else error(); } Recursive Descent Parser for a Simple Declaration Statement Decl_stmt-&gt; type idlist; Type-&gt; int|float Idlist-&gt;id | id ,idlist Proc declstmt() { Call type();...
View Full
Document