FIRST AND FOLLOW Example



Similar documents
FIRST and FOLLOW sets a necessary preliminary to constructing the LL(1) parsing table

Logic in Computer Science: Logic Gates

Automata and Formal Languages

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

Bottom-Up Parsing. An Introductory Example

CS103B Handout 17 Winter 2007 February 26, 2007 Languages and Regular Expressions

Optimizations. Optimization Safety. Optimization Safety. Control Flow Graphs. Code transformations to improve program

Positional Numbering System

Fast string matching

CS143 Handout 08 Summer 2008 July 02, 2007 Bottom-Up Parsing

Database Design and Normal Forms

Python Loops and String Manipulation

Copyright 2013 wolfssl Inc. All rights reserved. 2

Turing Machines, Part I

Syntaktická analýza. Ján Šturc. Zima 208

Automata Theory. Şubat 2006 Tuğrul Yılmaz Ankara Üniversitesi

CSE 308. Coding Conventions. Reference

How To Program In Scheme (Prolog)

Negative Integer Exponents

A simple algorithm with no simple verication

1. Prove that the empty set is a subset of every set.

Functional Dependencies and Finding a Minimal Cover

15. Functions and Lists. Example 2: Sorting a List of Numbers. Example 1: Computing the Diameter of a Cloud of Points

Copyright 2014 Yahoo Japan Corporation. All Rights Reserved.

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

vector calculus 2 Learning outcomes

Sect Greatest Common Factor and Factoring by Grouping

Derivative Approximation by Finite Differences

A Systematic Approach to Factoring

STATUS REPORT ON MAUDE-NPA TOOL

THEORY of COMPUTATION

Reminder: Complexity (1) Parallel Complexity Theory. Reminder: Complexity (2) Complexity-new

Automata and Computability. Solutions to Exercises

SOLUTION Trial Test Grammar & Parsing Deficiency Course for the Master in Software Technology Programme Utrecht University

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Normalisation. Why normalise? To improve (simplify) database design in order to. Avoid update problems Avoid redundancy Simplify update operations

Lecture 2: Regular Languages [Fa 14]

Relational Database Design

Visual Logic Instructions and Assignments

Factoring - Greatest Common Factor

Regular Expressions and Automata using Haskell

ASSIGNMENT ONE SOLUTIONS MATH 4805 / COMP 4805 / MATH 5605

Applies to Version 6 Release 5 X12.6 Application Control Structure

LZ77. Example 2.10: Let T = badadadabaab and assume d max and l max are large. phrase b a d adadab aa b

MACM 101 Discrete Mathematics I

FACTORING ax 2 bx c. Factoring Trinomials with Leading Coefficient 1

STAFF ADDITIONAL COMPENSATION - REJECT RULES

Questions and Answers in Computer Science. By Yonas Tesfazghi Weldeselassie weldesel

RDB-MINER: A SQL-Based Algorithm for Mining True Relational Databases

TECHNOLOGY Computer Programming II Grade: 9-12 Standard 2: Technology and Society Interaction

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

The Halting Problem is Undecidable

Knuth-Morris-Pratt Algorithm

11 Multivariate Polynomials

Database Design and Normalization

CS 1133, LAB 2: FUNCTIONS AND TESTING

Reading 13 : Finite State Automata and Regular Expressions

Computer Programming I

Resolution. Informatics 1 School of Informatics, University of Edinburgh

Command Scripts Running scripts: include and commands

Mathematics for Computer Science/Software Engineering. Notes for the course MSM1F3 Dr. R. A. Wilson

SQL Server Table Design - Best Practices

Programming and Software Development CTAG Alignments

A CONSTRUCTION OF THE UNIVERSAL COVER AS A FIBER BUNDLE

Intermediate Math Circles March 7, 2012 Linear Diophantine Equations II

Boolean Algebra. Boolean Algebra. Boolean Algebra. Boolean Algebra

CSC 180 H1F Algorithm Runtime Analysis Lecture Notes Fall 2015

6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, Class 4 Nancy Lynch

#820 Computer Programming 1A

Why? A central concept in Computer Science. Algorithms are ubiquitous.

NP-Completeness and Cook s Theorem

CS101 Lecture 11: Number Systems and Binary Numbers. Aaron Stevens 14 February 2011

Table of Contents. Pointshop Betting. Installation

Unit 3 Boolean Algebra (Continued)

Lossless Data Compression Standard Applications and the MapReduce Web Computing Framework

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

1 Approximating Set Cover

How To Find Out What A Key Is In A Database Engine

Math 53 Worksheet Solutions- Minmax and Lagrange

On line construction of suffix trees 1

each college c i C has a capacity q i - the maximum number of students it will admit

AIMMS Function Reference - Set Functions

( 1) = 9 = 3 We would like to make the length 6. The only vectors in the same direction as v are those

Logic in general. Inference rules and theorem proving

Regular Expressions with Nested Levels of Back Referencing Form a Hierarchy

COLLEGE ALGEBRA. Paul Dawkins

Magit-Popup User Manual

VHDL Test Bench Tutorial

Moving from CS 61A Scheme to CS 61B Java

If-Then-Else Problem (a motivating example for LR grammars)

Computing Orthonormal Sets in 2D, 3D, and 4D

Lecture 2: Universality

Fundamentele Informatica II

Finite Automata. Reading: Chapter 2

New Techniques for Regular Expression Searching

Transcription:

FIRST AND FOLLOW Example CS 447 -- Stephen Watt University of Western Ontario Copyright 2007 Stephen M. Watt

FIRST/FOLLOW Main Ideas Look at all the grammar rules. Examine the possibilities of all substrings of RHS symbols being nullable. Nullable RHS prefixes imply adding to FIRST sets. Nullable RHS suffixes imply adding to FOLLOW sets. Nullable RHS interior substrings imply adding to FOLLOW sets.

Algorithm for FIRST, FOLLOW, nullable for each symbol X FIRST[X] := { }, FOLLOW[X] := { }, nullable[x] := false for each terminal symbol t FIRST[t] := {t} repeat for each production X Y1 Y2 Yk, if all Yi are nullable then nullable[x] := true if Y1..Yi-1 are nullable then FIRST[X] := FIRST[X] U FIRST[Yi] if Yi+1..Yk are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FOLLOW[X] if Yi+1..Yj-1 are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FIRST[Yj] until FIRST, FOLLOW, nullable do not change

Example Grammar G = (Σ, V, S, P), where Σ = { a, c, d} V = {X, Y, Z} S = Z P = { Z d, Y ε, X Y, Z X Y Z, Y c, X a }

Initialization The initialization loops give FIRST[X] := {} FOLLOW[X] := {} nullable[x] := false FIRST[Y] := {} FOLLOW[Y] := {} nullable[y] := false FIRST[Z] := {} FOLLOW[Z] := {} nullable[z] := false FIRST[a] := {a} FIRST[c] := {c} FIRST[d] := {d}

Rules That We Will Iterate for each production X Y1 Y2 Yk, if all Yi are nullable then nullable[x] := true if Y1..Yi-1 are nullable then FIRST[X] := FIRST[X] U FIRST[Yi] if Yi+1..Yk are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FOLLOW[X] if Yi+1..Yj-1 are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FIRST[Yj]

How to Interpret Sub-sequences Yi..Yk when i < k has k-i+1 elements Yi..Yk when i = k has k-i+1 = 1 elements Yi..Yk when i = k+1 has k-i+1 = 0 elements Otherwise, not defined. (Some applications actually do define i > k+1 by making a subsequence taking the original elements backwards, which can be interesting and powerful.)

Specialize for Z d Taking Z d as the production X Y1..Yk, we have X is Z and Y1 is d. There are no other Yi. The first rule if all Yi are nullable then nullable[x] := true is if d is nullable (it isn t) then nullable[z] := true.

Specialize for Z d The second rule if Y1..Yi-1 are nullable then FIRST[X] := FIRST[X] U FIRST[Yi] can be instantiated only for i=1. This gives if Y1..Y0 = ε is nullable (which it is) then FIRST[Z] := FIRST[Z] U { d }

Specialize for Z d The third rule if Yi+1..Yk are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FOLLOW[X] can be instantiated only for i=1. This gives if Y2..Y1= ε is nullable then FOLLOW[d] := FOLLOW[d] U FOLLOW[Z] We are not interested in computing FOLLOW of terminals, so we won t bother with this one.

Specialize for Z d The fourth rule if Yi+1..Yj-1 are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FIRST[Yj] has no valid values for i and j. The only Yi is Y1, so we would have to have i=j=1. But the subsequence Y2..Y0 does not exist.

Rules for X a, Y c, Z d The same logic applies for all of X a, Y c, Z d. This gives: FIRST[X] := FIRST[Z] U {a} FIRST[Y] := FIRST[Z] U {c} FIRST[Z] := FIRST[Z] U {d} There are also some rules to compute FOLLOW of terminals but we don t care about those.

Specialize for X Y Taking X Y as the production X Y1..Yk, we have X is X, Y1 is Y, and there are no other Yi. The first rule if all Yi are nullable then nullable[x] := true is if Y is nullable then nullable[x] := true.

Specialize for X Y The second rule if Y1..Yi-1 are nullable then FIRST[X] := FIRST[X] U FIRST[Yi] can be instantiated only for i=1. It becomes if Y1..Y0 is nullable then FIRST[X] := FIRST[X] U FIRST[Y] The condition is always satisfied as Y1..Y0 is empty. FIRST[X] := FIRST[X] U FIRST[Y]

Specialize for X Y The third rule if Yi+1..Yk are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FOLLOW[X] can be instantiated only for i=1. It becomes if Y2..Y1 is nullable then FOLLOW[Y] := FOLLOW[Y] U FOLLOW[X]. The test is true (the empty string is nullable).

Specialize for X Y The fourth rule if Yi+1..Yj-1 are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FIRST[Yj] has no instantiation. The only Y is Y1, so we would have to have i=j=1. But the subsequence Y2..Y0 is not defined.

Rules for X Y All the rules together for X Y are if Yi is nullable then, nullable[z] := true. FIRST[X] := FIRST[X] U FIRST[Y] FOLLOW[Y] := FOLLOW[Y] U FOLLOW[X].

Specialize for Y ε Taking Y ε as the production X Y1..Yk, we have X is Y, and there are no Yi. The first rule if all Yi are nullable then nullable[x] := true is if all zero of the Y s are nullable (true) then nullable[y] := true.

Specialize for Y ε The second rule if Y1..Yi-1 are nullable then FIRST[X] := FIRST[X] U FIRST[Yi] cannot be instantiated as there are no Yi.

Specialize for Y ε The third rule if Yi+1..Yk are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FOLLOW[X] cannot be instantiated as there are no Yi.

Specialize for Y ε The fourth rule if Yi+1..Yj-1 are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FIRST[Yj] has no instantiation as there are no Yi.

Rules for Y ε All the rules together for Y ε are nullable[y] := true.

Specialize for Z X Y Z Taking Z X Y Z as the production X Y1..Yk, we have X is Z, Y1 is X, Y2 is Y, Y3 is Z. The first rule if all Yi are nullable then nullable[x] := true is if X, Y, Z are all nullable then nullable[z] := true.

Specialize for Z X Y Z The second rule if Y1..Yi-1 are nullable then FIRST[X] := FIRST[X] U FIRST[Yi] can be instantiated for i=1,2 or 3. These give if ε is nullable (which it is) then FIRST[Z] := FIRST[Z] union FIRST[X] if X is nullable then FIRST[Z] := FIRST[Z] union FIRST[Y] if XY is nullable then ou FIRST[Z] := FIRST[Z] union FIRST[Z]

Specialize for Z X Y Z The third rule if Yi+1..Yk are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FOLLOW[X] can be instantiated for i=1,2,3. These give if YZ is nullable then FOLLOW[X] := FOLLOW[X] union FOLLOW[Z] if Zisnullable then FOLLOW[Y] := FOLLOW[Y] union FOLLOW[Z] if ε is nullable then FOLLOW[Z] := FOLLOW[Z] union FOLLOW[Z]

Specialize for Z XYZ The fourth rule if Yi+1..Yj-1 are all nullable then FOLLOW[Yi] := FOLLOW[Yi] U FIRST[Yj] has instantiations for (i,j) = (1,2), (1,3), (2,3). if ε is nullable then FOLLOW[X] := FOLLOW[X] union FIRST[Y] if Y is nullable then FOLLOW[X] O := FOLLOW[X] O union FIRST[Z] if ε is nullable then FOLLOW[Y] := FOLLOW[Y] union FIRST[Z]

Rules for Z X Y Z if X, Y, Z all nullable then nullable[z] := true. if X is nullable then FIRST[Z] := FIRST[Z] union FIRST[Y] if XY is nullable then FIRST[Z] := FIRST[Z] union FIRST[Z] if YZ is nullable then FOLLOW[X] := FOLLOW[X] union FOLLOW[Z] if Z is nullable then FOLLOW[Y] := FOLLOW[Y] union FOLLOW[Z] if Yi is nullable then FOLLOW[X] := FOLLOW[X] union FIRST[Z] FIRST[Z] := FIRST[Z] union FIRST[X] FOLLOW[X] := FOLLOW[X] union FIRST[Y] FOLLOW[Y] := FOLLOW[Y] union FIRST[Z] FOLLOW[Z] := FOLLOW[Z] union FOLLOW[Z]

All the Rules We Need to Iterate nullable[x] := true, if Y is nullable nullable[y] := true. nullable[z] := true, if X, Y, Z all nullable FIRST[X] := FIRST[Z] U {a} FIRST[Y] := FIRST[Z] U {c} FIRST[Z] := FIRST[Z] U {d} FIRST[X] := FIRST[X] U FIRST[Y] FIRST[Z] := FIRST[Z] U FIRST[X] FIRST[Z] := FIRST[Z] U FIRST[Y], if X is nullable FIRST[Z] := FIRST[Z] U FIRST[Z], if XY is nullable // Trivial can drop it FOLLOW[X] := FOLLOW[X] U FIRST[Y] FOLLOW[X] := FOLLOW[X] U FIRST[Z], if Y is nullable FOLLOW[Y] := FOLLOW[Y] U FIRST[Z] FOLLOW[X] := FOLLOW[X] U FOLLOW[Z], if YZ is nullable FOLLOW[Y] := FOLLOW[Y] U FOLLOW[Z], if Z is nullable FOLLOW[Y] := FOLLOW[Y] U FOLLOW[X] FOLLOW[Z] := FOLLOW[Z] U FOLLOW[Z]

Now Can Iterate to Fill in the Table Z d Y ε X Y Z X Y Z Y c X a X Y Z nullable FIRST FOLLOW false false false nullable FIRST FOLLOW X false a c d Y true c d Z false d nullable FIRST FOLLOW X true a c a c d Y true c a c d Z false a c d