Regular Grammar - Finite Automaton

Slides:



Advertisements
Similar presentations
Parsing V: Bottom-up Parsing
Advertisements

A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Lesson 8 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Bottom up Parsing Bottom up parsing trys to transform the input string into the start symbol. Moves through a sequence of sentential forms (sequence of.
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Predictive Parsing l Find derivation for an input string, l Build a abstract syntax tree (AST) –a representation of the parsed program l Build a symbol.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Bottom-Up Syntax Analysis Mooly Sagiv Textbook:Modern Compiler Design Chapter (modified)
Bottom-Up Syntax Analysis Mooly Sagiv html:// Textbook:Modern Compiler Design Chapter
Formal Aspects Term 2, Week4 LECTURE: LR “Shift-Reduce” Parsers: The JavaCup Parser-Generator CREATES LR “Shift-Reduce” Parsers, they are very commonly.
Lecture #8, Feb. 7, 2007 Shift-reduce parsing,
Prof. Fateman CS 164 Lecture 91 Bottom-Up Parsing Lecture 9.
Bottom-Up Syntax Analysis Mooly Sagiv & Greta Yorsh Textbook:Modern Compiler Design Chapter (modified)
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Joey Paquet, 2000, 2002, 2012, Lecture 6 Bottom-Up Parsing.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
CS 321 Programming Languages and Compilers Bottom Up Parsing.
Review 1.Lexical Analysis 2.Syntax Analysis 3.Semantic Analysis 4.Code Generation 5.Code Optimization.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
CS3230R. What is a parser? What is an LR parser? A bottom-up parser that efficiently handles deterministic context-free languages in guaranteed linear.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
Syntax and Semantics Structure of programming languages.
Formal Languages, Automata and Models of Computation
Parsing #1 Leonidas Fegaras.
Chapter 4 - Parsing CSCE 343.
Parsing Bottom Up CMPS 450 J. Moloney CMPS 450.
Programming Languages Translator
Bottom-up parsing Goal of parser : build a derivation
Compiler design Bottom-up parsing Concepts
Parsing and Parser Parsing methods: top-down & bottom-up
Unit-3 Bottom-Up-Parsing.
UNIT - 3 SYNTAX ANALYSIS - II
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
CS 404 Introduction to Compiler Design
Bottom-Up Syntax Analysis
Top-Down Parsing.
Subject Name:COMPILER DESIGN Subject Code:10CS63
Lexical and Syntax Analysis
Syntax-Directed Translation
Top-Down Parsing CS 671 January 29, 2008.
Syntax Analysis source program lexical analyzer tokens syntax analyzer
4d Bottom Up Parsing.
Parsing #2 Leonidas Fegaras.
BOTTOM UP PARSING Lecture 16.
Lecture 8 Bottom Up Parsing
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
R.Rajkumar Asst.Professor CSE
فصل دوم Context-Free Languages
Compiler Construction
Parsing IV Bottom-up Parsing
LR Parsing. Parser Generators.
Parsing #2 Leonidas Fegaras.
4d Bottom Up Parsing.
Chapter 2 Context-Free Language - 01
4d Bottom Up Parsing.
4d Bottom Up Parsing.
Bottom-up parsing is also known as shift-reduce parsing
4d Bottom Up Parsing.
Parsing Bottom-Up Introduction.
4d Bottom Up Parsing.
Normal Forms for Context-free Grammars
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 2, 09/04/2003 Prof. Roy Levow.
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

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