Regular Grammar - Finite Automaton
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES STATE captures relevant information about the past
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES STATE captures relevant information about the past TRANSITION RULE and input tells which STATE to go to
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES STATE captures relevant information about the past TRANSITION RULE and input tells which STATE to go to Context-Free Grammar – Pushdown Automaton
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES STATE captures relevant information about the past TRANSITION RULE and input tells which STATE to go to Context-Free Grammar – Pushdown Automaton Collection of STATES, TRANSITION RULES, and STACK
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES STATE captures relevant information about the past TRANSITION RULE and input tells which STATE to go to Context-Free Grammar – Pushdown Automaton Collection of STATES, TRANSITION RULES, and STACK STATE captures relevant information about the near past
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES STATE captures relevant information about the past TRANSITION RULE and input tells which STATE to go to Context-Free Grammar – Pushdown Automaton Collection of STATES, TRANSITION RULES, and STACK STATE captures relevant information about the near past STACK captures relevant information about the far past
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES STATE captures relevant information about the past TRANSITION RULE and input tells which STATE to go to Context-Free Grammar – Pushdown Automaton Collection of STATES, TRANSITION RULES, and STACK STATE captures relevant information about the near past STACK captures relevant information about the far past TRANSITION RULE and input tells which STATE to go to and whether to PUSH/POP anything to/from the STACK
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES STATE captures relevant information about the past TRANSITION RULE and input tells which STATE to go to Context-Free Grammar – Pushdown Automaton Collection of STATES, TRANSITION RULES, and STACK STATE captures relevant information about the near past STACK captures relevant information about the far past TRANSITION RULE and input tells which STATE to go to and whether to PUSH/POP anything to/from the STACK Goal of LR(1) Parser
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES STATE captures relevant information about the past TRANSITION RULE and input tells which STATE to go to Context-Free Grammar – Pushdown Automaton Collection of STATES, TRANSITION RULES, and STACK STATE captures relevant information about the near past STACK captures relevant information about the far past TRANSITION RULE and input tells which STATE to go to and whether to PUSH/POP anything to/from the STACK Goal of LR(1) Parser Process input stream left-to-right one token at a time
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES STATE captures relevant information about the past TRANSITION RULE and input tells which STATE to go to Context-Free Grammar – Pushdown Automaton Collection of STATES, TRANSITION RULES, and STACK STATE captures relevant information about the near past STACK captures relevant information about the far past TRANSITION RULE and input tells which STATE to go to and whether to PUSH/POP anything to/from the STACK Goal of LR(1) Parser Process input stream left-to-right one token at a time Produce a right-most derivation of the input stream
Regular Grammar - Finite Automaton Collection of STATES and TRANSITION RULES STATE captures relevant information about the past TRANSITION RULE and input tells which STATE to go to Context-Free Grammar – Pushdown Automaton Collection of STATES, TRANSITION RULES, and STACK STATE captures relevant information about the near past STACK captures relevant information about the far past TRANSITION RULE and input tells which STATE to go to and whether to PUSH/POP anything to/from the STACK Goal of LR(1) Parser Process input stream left-to-right one token at a time Produce a right-most derivation of the input stream End up in the Start State at the end of the input stream
GRAMMAR E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id SENTENCE id + id * id
id + id * id GRAMMAR E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id id + id * id
E id + id * id RM DERIVATION E GRAMMAR E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id id + id * id E RM DERIVATION E
E E T + id + id * id RM DERIVATION E E + T GRAMMAR E -> E + T T -> T * F T -> F F -> ( E ) F -> id id + id * id E E T RM DERIVATION E E + T +
E E T T F + * id + id * id RM DERIVATION E E + T E + T * F GRAMMAR F -> ( E ) F -> id id + id * id E E T RM DERIVATION E E + T E + T * F T F + *
E E T T F + * id id + id * id RM DERIVATION E E + T E + T * F GRAMMAR E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id id + id * id E E T RM DERIVATION E E + T E + T * F E + T * id T F + * id
E E T T F F + * id id + id * id RM DERIVATION E E + T E + T * F GRAMMAR E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id id + id * id E E T RM DERIVATION E E + T E + T * F E + T * id E + F * id T F F + * id
E E T T F F + id * id id + id * id RM DERIVATION E E + T E + T * F GRAMMAR E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id id + id * id E E T RM DERIVATION E E + T E + T * F E + T * id E + F * id E + id * id T F F + id * id
E E T T T F F + id * id id + id * id RM DERIVATION E E + T E + T * F GRAMMAR E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id id + id * id E E T RM DERIVATION E E + T E + T * F E + T * id E + F * id E + id * id T + id * id T T F F + id * id
E E T T T F F F + id * id id + id * id RM DERIVATION E E + T E + T * F GRAMMAR E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id id + id * id E E T RM DERIVATION E E + T E + T * F E + T * id E + F * id E + id * id T + id * id F + id * id T T F F F + id * id
E E T T T F F F id + id * id id + id * id RM DERIVATION E E + T GRAMMAR E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id id + id * id E E T RM DERIVATION E E + T E + T * F E + T * id E + F * id E + id * id T + id * id F + id * id id + id * id T T F F F id + id * id
E E T T T F F F id + id * id id + id * id RM DERIVATION E E + T GRAMMAR E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id id + id * id E E T RM DERIVATION E E + T E + T * F E + T * id E + F * id E + id * id T + id * id F + id * id id + id * id T T F F F id + id * id
E E T T T F F F id + id * id id + id * id Root node: Node that has no parent. Leaf node: Node that has no children. Internal node: Node that has children. Phrase: Sub-tree rooted at an internal node. Simple phrase: Sub-tree that has only a root and leaf nodes. Handle: Left-most simple phrase. Reduction: Replace handle with its root. id + id * id E E T T T F F F id + id * id Two types of transition rules: Shift and Reduce. Shift – Push the next non-terminal onto the stack. Reduce – Pop the leaves off the stack and Push the root onto the stack. All pushes also push current state second and all pops remove it.
E E T T T F F F id + id * id id + id * id Parser Methodology Identify the leftRoot node: Node that has no parent. Leaf node: Node that has no children. Internal node: Node that has children. Phrase: Sub-tree rooted at an internal node. Simple phrase: Sub-tree that has only a root and leaf nodes. Reduction: Replace simple phrase with its root. id + id * id E E T T T F F F id + id * id
S RHS id + * ( ) $ E T F null 1 E+T 2 3 T*F 4 5 (E) 6 7 8 E+ 9 T* 10 11 (E One Start State One state for each production rule One state for each production rule prefix
S RHS id + * ( ) $ E T F null 1 E+T 2 3 T*F 4 5 (E) 6 7 8 E+ 9 T* 10 11 (E One column for each token One column for the END token One column for each non-terminal
S RHS id + * ( ) $ E T F null 1 E+T 2 3 T*F 4 5 (E) 6 7 A 8 E+ 9 T* 10 11 (E END token Start Symbol Accepting State
Add transitions that EXTEND an existing RHS prefix id + * ( ) $ E T F null 1 E+T 9 2 3 T*F 4 5 (E) 6 7 8 A E+ T* 10 11 (E
Add transitions that START a new RHS prefix id + * ( ) $ E T F null 6 10 7 2 4 1 E+T 9 3 T*F 5 (E) 8 A E+ T* 11 (E
REDUCE all remaining Production Rule states RHS id + * ( ) $ E T F null 6 10 7 2 4 1 E+T R 9 3 T*F 5 (E) 8 A E+ T* 11 (E
ALL remaining transitions are Syntax Errors RHS id + * ( ) $ E T F null 6 - 10 7 2 4 1 E+T R 9 3 T*F 5 (E) 8 A E+ T* 11 (E
OPTIONAL – Identify and remove known Syntax Errors RHS id + * ( ) $ E T F null 6 - 10 7 2 4 1 E+T R 9 3 T*F 5 (E) 8 A E+ T* 11 (E
Final Parse Transition Table RHS id + * ( ) $ E T F null 6 - 10 7 2 4 1 E+T R 9 3 T*F 5 (E) 8 A E+ T* 11 (E
id + * $ S RHS id + * ( ) $ E T F Initialize STACK pushing the Start State onto the STACK (the Start State is NOT the same as the Start Symbol). id + id * id Initialize input stack by pushing END token ($) and entire sentence to be parsed from back to front. In practice, the input stream serves as the input stack and the EOF serves as the END token.
id + * $ S RHS id + * ( ) $ E T F null 6 - 10 7 2 4 id + id * id SHIFT id from input to STACK PUSH State 6 onto STACK
id 6 + id * $ S RHS id + * ( ) $ E T F 6 - R id + id * id * $ S RHS id + * ( ) $ E T F 6 - R id + id * id REDUCE by first popping the RHS tokens off STACK id
F id + id * $ S RHS id + * ( ) $ E T F 6 - R null 10 7 2 4 + id * $ S RHS id + * ( ) $ E T F 6 - R null 10 7 2 4 id + id * id Finish the Reduce: PUSH the LHS terminal and new state onto STACK You can think of this process as POP the RHS off the STACK PUSH the LHS onto the INPUT STREAM Perform the resulting SHIFT operation like any other F id
4 + F id * $ S RHS id + * ( ) $ E T F 4 - R id + id * id F id
T F id + id * $ S RHS id + * ( ) $ E T F 4 - R null 6 10 7 2 + id * $ S RHS id + * ( ) $ E T F 4 - R null 6 10 7 2 id + id * id T F id
2 + T id * $ S RHS id + * ( ) $ E T F 2 - R 9 id + id * id T F id
E T F id + id * $ S RHS id + * ( ) $ E T F 2 - R 9 null 6 10 7 4 + id * $ S RHS id + * ( ) $ E T F 2 - R 9 null 6 10 7 4 id + id * id E T F id
7 + E id * $ S RHS id + * ( ) $ E T F 7 - 8 A id + id * id E T F id
E T F id + 8 id + * 7 E $ S RHS id + * ( ) $ E T F 8 E+ 6 - 10 1 4 S RHS id + * ( ) $ E T F 8 E+ 6 - 10 1 4 id + id * id E T F id +
E T F id + id 6 * id 8 $ + 7 E S RHS id + * ( ) $ E T F 6 - R S RHS id + * ( ) $ E T F 6 - R id + id * id E T F id + id
E T F F F id + id 8 * + id 7 $ E S RHS id + * ( ) $ E T F 6 - R 8 E+ S RHS id + * ( ) $ E T F 6 - R 8 E+ 10 1 4 id + id * id E T F F F id + id
E T F F F id + id 4 * F id 8 $ + 7 E S RHS id + * ( ) $ E T F 4 - R S RHS id + * ( ) $ E T F 4 - R id + id * id E T F F F id + id
E T T F F F id + id 8 * + id 7 $ E S RHS id + * ( ) $ E T F 4 - R 8 E+ S RHS id + * ( ) $ E T F 4 - R 8 E+ 6 10 1 id + id * id E T T F F F id + id
E T T F F F id + id 1 * T id 8 $ + 7 E S RHS id + * ( ) $ E T F 1 E+T S RHS id + * ( ) $ E T F 1 E+T - R 9 id + id * id E T T F F F id + id
E T T F F F id + id * 9 id * $ 1 T 8 + 7 E S RHS id + * ( ) $ E T F 9 S RHS id + * ( ) $ E T F 9 T* 6 - 10 3 id + id * id E T T F F F id + id *
E T T F F F id + id * id 6 $ id 9 * 1 T 8 + 7 E S RHS id + * ( ) $ E T S RHS id + * ( ) $ E T F 6 - R id + id * id E T T F F F id + id * id
E T T F F F F id + id * id 9 $ * 1 T 8 + 7 E S RHS id + * ( ) $ E T F S RHS id + * ( ) $ E T F 6 - R 9 T* 10 3 id + id * id E T T F F F F id + id * id
E T T F F F F id + id * id 3 $ F 9 * 1 T 8 + 7 E S RHS id + * ( ) $ E S RHS id + * ( ) $ E T F 3 T*F - R id + id * id Pop 3 tokens from STACK E T T F F F F id + id * id
E T E T T F F F F id + id * id 8 $ + 7 E S RHS id + * ( ) $ E T F 3 S RHS id + * ( ) $ E T F 3 T*F - R 8 E+ 6 10 1 4 id + id * id E T E T T F F F F id + id * id
E T E T T F F F F id + id * id 1 $ T 8 + 7 E S RHS id + * ( ) $ E T F S RHS id + * ( ) $ E T F 1 E+T - R 9 id + id * id E T E T T F F F F id + id * id
E E T T T F F F id + id * id $ S RHS id + * ( ) $ E T F 1 E+T - R 9 $ S RHS id + * ( ) $ E T F 1 E+T - R 9 null 6 x 10 7 2 4 id + id * id E E T T T F F F id + id * id
E E T T T F F F id + id * id 7 $ E S RHS id + * ( ) $ E T F 7 - 8 A S RHS id + * ( ) $ E T F 7 - 8 A id + id * id E Accepting State – Parse is complete E T T T F F F id + id * id