1 / 22

Predictive Parsing

Predictive Parsing. Lecture 9 Wed, Feb 9, 2005. Predictive Parsing Table. The parse table has one row for each nonterminal, one column for each terminal and $. $ is the end-of-file marker. Each cell in the table represents a combination ( A , a) of a nonterminal A and a terminal a.

khowes
Download Presentation

Predictive Parsing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Predictive Parsing Lecture 9 Wed, Feb 9, 2005

  2. Predictive Parsing Table • The parse table has • one row for each nonterminal, • one column for each terminal and $. • $ is the end-of-file marker. • Each cell in the table represents a combination (A, a) of a nonterminal A and a terminal a.

  3. Nullability and FIRST for Strings • We need to extend the definitions of nullable and FIRST to strings of grammar symbols. •  is nullable. • Let  = X1X2…Xn be a string of grammar symbols. •  is nullable if each Xi is nullable.

  4. Nullability and FIRST for Strings • By definition, FIRST() is the empty set. • For the string  = X1X2…Xn , let k be the largest integer for which X1, X2, …, and Xk are nullable. • Then FIRST() = FIRST(X1)  …  FIRST(Xk + 1). • We will not need FOLLOW for strings.

  5. Parse Table Entries • Each table entry is a production A . • Rules for entering A  in the table are • Write A  in the (A, a) cell, for every a  FIRST(). • If  is nullable, then write A  in the (A, a) cell for every symbol a in FOLLOW(A). • Write “error” in all cells that do not contain a production.

  6. Parse Table Entries • The interpretation of A  in cell (A, a) is that if A is on top of the stack and we encounter a in the input, then we should replace A by  on the stack. • Push the symbols of  onto the stack in reverse order, from right to left, so that the first (leftmost) symbol is on the top of the stack.

  7. Example: Parse Table • Let the grammar be ET E' E'+T E' | . TF T' T'*F T' | . F(E) | id | num

  8. Recall

  9. Example: Parse Table • Consider the production ET E'. • FIRST(TE') = FIRST(T) = {(, num, id}. • Therefore, enter ET E' in cells (E, ( ), (E, num), and (E, id). • T E' is not nullable.

  10. Example: Parse Table • Consider the production E'. • FIRST() = { }. •  is nullable and FOLLOW(E') = {$, )}. • Therefore, enter E' in cells (E', $) and (E', ) ).

  11. Example: Parse Table • Handle the other productions similarly. • In which cells do we enter E'+T E' ? • In which cells do we enter TF T' ? • In which cells do we enter T'*F T' ? • In which cells do we enter T'? • In which cells do we enter F(E) ? • In which cells do we enter Fid ? • In which cells do we enter Fnum ?

  12. Example: Parse Table • The parse table:

  13. Predictive Parsing • A grammar is called LL(1) if its predictive parse table does not contain any multiple entries. • A multiple entry would indicate that we couldn’t decide which production to apply.

  14. Predictive Parsing Algorithm • The predictive parsing algorithm uses • The parse table, • An input buffer containing a sequence of tokens, • A stack of grammar symbols. • Initially • The input buffer contains the input followed by $. • The stack contains $ and S, with S on the top.

  15. Predictive Parsing Algorithm • Consider the top stack symbol X. • There are three possibilities. • X is a terminal. • X is a nonterminal. • X is $.

  16. Predictive Parsing Algorithm • If X is a terminal, then • If X matches the current token, • Pop X from the stack. • Advance to the next token. • If X does not match the current token, then that is an error.

  17. Predictive Parsing Algorithm • If X is a nonterminal, then • Use X together with the current token to get the entry from the parse table. • If the entry is a production, • Pop X from the stack. • Push the symbols on the right-hand side of the production, from right to left, onto the stack. • If the entry is not a production, then that is an error.

  18. Predictive Parsing Algorithm • If X is $, then • If the current token is also $, • Accept the input. • If not, then that is an error.

  19. Example: Predictive Parsing • Parse the string (id + num)*id.

  20. Example: Predictive Parsing

  21. Example: Predictive Parsing

  22. Exercise • The grammar RRR | RR | R* | (R) | a | b generates all regular expressions on the alphabet {a, b}. • Using FIRST and FOLLOW from the exercise of the previous lecture, construct a parse table for this grammar. • Parse the expression ab*(a | ).

More Related