Presentation is loading. Please wait.

Presentation is loading. Please wait.

Regular Grammar - Finite Automaton

Similar presentations


Presentation on theme: "Regular Grammar - Finite Automaton"— Presentation transcript:

1 Regular Grammar - Finite Automaton

2 Regular Grammar - Finite Automaton
Collection of STATES and TRANSITION RULES

3 Regular Grammar - Finite Automaton
Collection of STATES and TRANSITION RULES STATE captures relevant information about the past

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 GRAMMAR E -> E + T E -> T T -> T * F T -> F F -> ( E ) F -> id SENTENCE id + id * id

15 id + id * id GRAMMAR E -> E + T E -> T T -> T * F T -> F
F -> ( E ) F -> id id + id * id

16 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

17 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 +

18 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 *

19 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

20 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

21 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

22 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

23 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

24 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

25 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

26 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.

27 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

28 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

29 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

30 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

31 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

32 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

33 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

34 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

35 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

36 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

37 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.

38 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

39 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

40 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

41 4 + F id * $ S RHS id + * ( ) $ E T F 4 - R id + id * id F id

42 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

43 2 + T id * $ S RHS id + * ( ) $ E T F 2 - R 9 id + id * id T F id

44 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

45 7 + E id * $ S RHS id + * ( ) $ E T F 7 - 8 A id + id * id E T F id

46 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

47 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

48 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

49 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

50 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

51 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

52 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 *

53 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

54 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

55 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

56 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

57 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

58 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

59 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


Download ppt "Regular Grammar - Finite Automaton"

Similar presentations


Ads by Google