Understanding LR(0) Parser: States, Construction, and Tables

lr 0 parser n.w
1 / 9
Embed
Share

Dive into the LR(0) parser in the context of deterministic grammars, exploring the states of the automaton, the process of building LR(0) states, adding states, constructing a predictive parsing table, and more. Learn how LR(0) parsing works and its practical applications in information technology.

  • LR Parser
  • Deterministic Grammars
  • Parsing Techniques
  • Predictive Parsing
  • Information Technology

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. LR(0) PARSER Mr.Sameer Prembabu Mamadapure Information Technology Department International Institute of Information Technology, I IT www.isquareit.edu.in

  2. LR(k) Parsers Left-to-right, rightmost derivation with k-token lookahead. Most general parsing technique for deterministic grammars. In general, not practical: tables too large (10^6 states for C++, Ada). Common subsets: SLR, LALR (1).

  3. The states of the LR(0) automaton An item is a point within a production, indicating that part of the production has been recognized: A . B , seen the expansion of , expect to see expansion of B A state is a set of items Transition within states are determined by terminals and non-terminals Parsing tables are built from automaton: action: shift / reduce depending on next symbol goto: change state depending on synthesized non-terminal

  4. Building LR (0) states If a state includes: A .B it also includes every state that is the start of B: B . X Y Z Informally: if I expect to see B next, I expect to see anything that B can start with, and so on: X . G H I States are built by closure from individual items.

  5. A grammar of expressions: initial state E E E E + T | T; -- left-recursion ok here. T T * F | F; F id | (E) S0 = { E .E, E .E + T, E .T, F .id, F . ( E ) , T .T * F, T .F }

  6. Adding states If a state has item A .a , and the next symbol in the input is a, we shift a on the stack and enter a state that contains item A a. (as well as all other items brought in by closure) if a state has as item A . , this indicates the end of a production: reduce action. If a state has an item A .N , then after a reduction that find an N, go to a state with A N.

  7. Construction of a predictive parsing table The following rules are used to construct the predictive parsing table: 1. for each terminal a in FIRST( ), add A to matrix M[A,a] 2. if is in FIRST( ), then for each terminal b in FOLLOW(A), add A to matrix M[A,b]

  8. The LR (0) states for expressions S1 = { E E., E E. + T } S2 = { E T., T T. * F } S3 = { T F. } S4 = { F (. E), } + S0 (by closure) S5 = { F id. } S6 = { E E +. T, T .T * F, T .F, F .id, F .(E)} S7 = { T T *. F, F .id, F .(E)} S8 = { F (E.), E E.+ T} S9 = { E E + T., T T.* F} S10 = { T T * F.}, S11 = {F (E).}

  9. LR(0) Automaton

More Related Content