241-437 Compilers: syntax/4 1 Compiler Structures Objective – –describe general syntax analysis, grammars, parse trees, FIRST and FOLLOW sets 241-437,

Slides:



Advertisements
Similar presentations
Grammars, Languages and Parse Trees. Language Let V be an alphabet or vocabulary V* is set of all strings over V A language L is a subset of V*, i.e.,
Advertisements

By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Chapter 3 Describing Syntax and Semantics Sections 1-3.
Context-Free Grammars Lecture 7
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
Chapter 3: Formal Translation Models
COP4020 Programming Languages
1 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice Grammars and Parsing.
Chapter 3 Chang Chi-Chung Parse tree intermediate representation The Role of the Parser Lexical Analyzer Parser Source Program Token Symbol.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
1 Syntax and Semantics The Purpose of Syntax Problem of Describing Syntax Formal Methods of Describing Syntax Derivations and Parse Trees Sebesta Chapter.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Joey Paquet, 2000, 2002, Lecture 3 Syntactic Analysis Part I.
Introduction Syntax: form of a sentence (is it valid) Semantics: meaning of a sentence Valid: the frog writes neatly Invalid: swims quickly mathematics.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Parsing III (Top-down parsing: recursive descent & LL(1) )
CS Describing Syntax CS 3360 Spring 2012 Sec Adapted from Addison Wesley’s lecture notes (Copyright © 2004 Pearson Addison Wesley)
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 4 Top-Down Parsing Dr. Frank Lee. Parsing and Parsers Once we have described the syntax of our programming language using a context-free grammar,
1 Syntax In Text: Chapter 3. 2 Chapter 3: Syntax and Semantics Outline Syntax: Recognizer vs. generator BNF EBNF.
CSI 3120, Syntactic analysis, page 1 Syntactic Analysis and Parsing Based on A. V. Aho, R. Sethi and J. D. Ullman Compilers: Principles, Techniques and.
1 Chapter 4 Grammars and Parsing. 2 Context-Free Grammars: Concepts and Notation A context-free grammar G = (Vt, Vn, S, P) –A finite terminal vocabulary.
11 Chapter 4 Grammars and Parsing Grammar Grammars, or more precisely, context-free grammars, are the formalism for describing the structure of.
Parsing Introduction Syntactic Analysis I. Parsing Introduction 2 The Role of the Parser The Syntactic Analyzer, or Parser, is the heart of the front.
Syntax Analysis CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
CPS 506 Comparative Programming Languages Syntax Specification.
Chap. 4, Formal Grammars and Parsing J. H. Wang Oct. 19, 2015.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
Parsing Top-Down.
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Lecture 3: Parsing CS 540 George Mason University.
1 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
Top-Down Predictive Parsing We will look at two different ways to implement a non- backtracking top-down parser called a predictive parser. A predictive.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered.
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
COMP 3438 – Part II - Lecture 4 Syntax Analysis I Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
EXAMPLES: FIRST, FOLLOW, LL PARSING SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Parsing COMP 3002 School of Computer Science. 2 The Structure of a Compiler syntactic analyzer code generator program text interm. rep. machine code tokenizer.
Chapter 3 – Describing Syntax
Compiler Construction
FIRST and FOLLOW Lecture 8 Mon, Feb 7, 2005.
Top-Down Parsing.
CS 363 Comparative Programming Languages
CS 540 George Mason University
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Lecture 7: Introduction to Parsing (Syntax Analysis)
Chapter 3 Syntactic Analysis I.
Compiler Structures 4. Syntax Analysis Objectives
Discrete Maths 13. Grammars Objectives
Presentation transcript:

Compilers: syntax/4 1 Compiler Structures Objective – –describe general syntax analysis, grammars, parse trees, FIRST and FOLLOW sets , Semester 1, Syntax Analysis

Compilers: syntax/4 2 Overview 1. What is a Syntax Analyzer? 2. What is a Grammar? 3. Parse Trees 4. Types of CFG Parsing 5. Syntax Analysis Sets

Compilers: syntax/4 3 In this lecture Source Program Target Lang. Prog. Semantic Analyzer Syntax Analyzer Lexical Analyzer Front End Code Optimizer Target Code Generator Back End Int. Code Generator Intermediate Code

Compilers: syntax/ What is a Syntax Analyzer? Lexical Analyzer if (a == 0) a = b; if(a==0)a=b; Syntax Analyzer builds a parse tree IF EQASSIGN a0ab

Compilers: syntax/4 5 Syntax Analyses that we do IgaveJim cardthe pronounverb proper noun noun phrase articlenoun - Identify the function of each word - Recognize if a sentence is grammatically correct sentence (subject) (action)(object) verb phrase (indirect object) grammar types / categories

Compilers: syntax/4 6 Languages We use a natural language to communicate – –its grammar rules are very complex – –the rules don’t cover important things We use a formal language to define a programming language – –its grammar rules are fairly simple – –the rules cover almost everything

Compilers: syntax/ What is a Grammar? A grammar is a notation for defining a language, and is made from 4 parts: – –the terminal symbols – –the syntactic categories (nonterminal symbols) e.g. statement, expression, noun, verb – –the grammar rules (productions) e,g, A => B1 B2... Bn – –the starting nonterminal the top-most syntactic category for this grammar continued

Compilers: syntax/4 8 We define a grammar G as a 4-tuple: G = (T, N, P, S) – –T = terminal symbols – –N = nonterminal symbols – –P = productions/rules – –S = starting nonterminal

Compilers: syntax/ Example 1 Consider the grammar: T = {0, 1} N = {S, R} P = {S => 0 S => 0 R R => 1 S } S is the starting nonterminal the right hand sides of productions usually use a mix of terminals and nonterminals

Compilers: syntax/4 10 Is “01010” in the language? Start with a S rule: – –RuleString Generated --S S => 0 R0 R R => 1 S0 1 S S => 0 R0 1 0 R R => 1 S S S => No more rules can be applied since there are no more nonterminals left in the string. Yes, it is in the language.

Compilers: syntax/4 11 Example 2 Consider the grammar: T = {a, b, c, d, z} N = {S, R, U, V} P = {S => R U z | z R => a | b R U => d V U | c V => b | c } S is the starting nonterminal

Compilers: syntax/4 12 The notation: X => Y | Z is shorthand for the two rules: X => Y X => Z Read ‘|’ as ‘or’.

Compilers: syntax/4 13 Is “adbdbcz” in the language? RuleString Generated --S S => R U zR U z R => aa U z U => d V Ua d V U z V => ba d b U z U => d V Ua d b d V U z V => ba d b d b U z U => ca d b d b c z Yes! This grammar has choices about how to rewrite the string.

Compilers: syntax/4 14 Example 3: Sums The grammar: T = {+, -, 0, 1, 2, 3,..., 9} N = {L, D} P = {L => L + D | L – D | D D => 0 | 1 | 2 |... | 9 } L is the starting nonterminal e.g

Compilers: syntax/4 15 Example 4: Brackets The grammar: T = { '(', ')' } N = {L} P = {L => '(' L ')' L L => ε } L is the starting nonterminal ε means 'nothing'

Compilers: syntax/ Derivations A sequence of the form: w 0  w 1  …  w n is a derivation of w n from w 0 (or w 0  * w n ) Example: Lrule L => ( L ) L    ( L ) Lrule L =>     ( ) Lrule L =>     ( ) L  * ( ) This means that the sentence ( ) is a derivation of L

Compilers: syntax/4 17 L rule L => ( L ) L   ( L ) L rule L => ( L ) L   ( L ) ( L ) L rule L =>   ( L ) ( L ) rule L => ( L ) L   (( L ) L ) ( L ) rule L =>    (( ) L ) ( L ) rule L =>   ( ( ) L ) ( ) rule L =>   ( ( ) ) ( ) so L  * (( )) ( )

Compilers: syntax/ Kinds of Grammars There are 4 main kinds of grammar, of increasing expressive power: – –regular (type 3) grammars – –context-free (type 2) grammars – –context-sensitive (type 1) grammars – –unrestricted (type 0) grammars They vary in the kinds of productions they allow.

Compilers: syntax/4 19 Regular Grammars Every production is of the form: A => a | a B |  – –A, B are nonterminals, a is a terminal These are sometimes called right linear rules because if a nonterminal appears in the rule body, then it must appear last. Regular grammars are equivalent to REs. S => wT T => xT T => a

Compilers: syntax/4 20 Example Integer => + UInt | - UInt | 0 Digits | 1 Digits |... | 9 Digits UInt => 0 Digits | 1 Digits |... | 9 Digits Digits => 0 Digits | 1 Digits |... | 9 Digits | 

Compilers: syntax/4 21 Context-Free Grammars (CFGs) Every production is of the form: A =>  – –A is a nonterminal,  can be any number of nonterminals or terminals The Syntax Analyzer uses CFGs. A => a A => aBcd B => ae

Compilers: syntax/ REs for Syntax Analysis? Why not use REs to describe the syntax of a programming language? – –they don’t have enough power Examples: – –nested blocks, if statements, balanced braces We need the ability to 'count', which can be implemented with CFGs but not REs.

Compilers: syntax/ Parse Trees A parse tree is a graphical way of showing how productions are used to generate a string. The syntax analyzer creates a parse tree to store information about the program being compiled.

Compilers: syntax/4 24 Example The grammar: T = { a, b } N = { S } P = { S => S S | a S b | a b | b a } S is the starting nonterminal

Compilers: syntax/4 25 Parse Tree for “aabbba” The root of the tree is the start symbol S: S Expand using S => S S S S S Expand using S => a S b continued expand the symbol in the circle

Compilers: syntax/4 26 S S S S a b Expand using S => a b S S S S a b ab Expand using S => b a continued

Compilers: syntax/4 27 S S S a b ab S ba Stop when there are no more nonterminals in leaf positions. Read off the string by reading the leaves left to right.

Compilers: syntax/ Ambiguity Two (or more) parse trees for the same string E => E + E E => E – E E => 0 | … | 9 E E + E E - E E + E E - E E – or

Compilers: syntax/4 29 The two derivations: E   E + E E   E – E   E – E + E   2 – E   2 – E + E   2 – E + E   2 – 3 + E   2 – 3 + E   2 –   2 – 3 + 4

Compilers: syntax/4 30 Fixing Ambiguity An ambiguous grammar can sometimes be made unambiguous: E => E + T | E – T | T T => 0 | … | 9 We'll look at some techniques in chapter 5.

Compilers: syntax/ Types of CFG Parsing Top-down (chapter 5) – –recursive descent (predictive) parsing – –LL methods Bottom-up (chapter 6) – –operator precedence parsing – –LR methods – –SLR, canonical LR, LALR

Compilers: syntax/ A Statement Block Grammar The grammar: T = {begin, end, simplestmt, ;} N = {B, SS, S} P = {B => begin SS end SS => S ; SS | ε S => simplestmt | begin SS end } B is the starting nonterminal

Compilers: syntax/4 33 Parse Tree begin simplestmt ; simplestmt ; end S S SS  B B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end begin simplestmt ; simplestmt ; end

Compilers: syntax/ Top Down (LL) Parsing begin simplestmt ; simplestmt ; end SS B B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end continued

Compilers: syntax/4 35 begin simplestmt ; simplestmt ; end S SS B B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end continued

Compilers: syntax/4 36 begin simplestmt ; simplestmt ; end S SS B B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end continued

Compilers: syntax/4 37 begin simplestmt ; simplestmt ; end S S SS B B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end continued

Compilers: syntax/4 38 begin simplestmt ; simplestmt ; end S S SS B B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end continued

Compilers: syntax/4 39 begin simplestmt ; simplestmt ; end S S SS B  6 B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end

Compilers: syntax/ Bottomup (LR) Parsing begin simplestmt ; simplestmt ; end S B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end continued

Compilers: syntax/4 41 begin simplestmt ; simplestmt ; end S S B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end continued

Compilers: syntax/4 42 begin simplestmt ; simplestmt ; end S S SS  B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end continued

Compilers: syntax/4 43 begin simplestmt ; simplestmt ; end S S SS  B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end continued

Compilers: syntax/4 44 begin simplestmt ; simplestmt ; end S S SS  B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end continued

Compilers: syntax/4 45 begin simplestmt ; simplestmt ; end S S SS B  3 B => begin SS end SS => S ; SS SS =>  S => simplestmt S => begin SS end

Compilers: syntax/ Syntax Analysis Sets Syntax analyzers for top-down (LL) and bottom-up (LR) parsing utilize two types of sets: – –FIRST sets – –FOLLOW sets These sets are generated from the programming language CFG.

Compilers: syntax/ The FIRST Sets FIRST( ) = set of all terminals that start productions for that non-terminal Example: S => ping S => begin S end FIRST(S) = { ping, begin }

Compilers: syntax/4 48 More Mathematically A is a non-terminal. FIRST(A) = – –{ c | A =>* c , c is a terminal }  {  } if A =>*   is the rest of the terminals and nonterminals after 'c'

Compilers: syntax/4 49 Building FIRST Sets For each non-terminal A, FIRST(A) = FIRST_SEQ(  )  FIRST_SEQ(  ) ... for all productions A => , A => ,... – – ,  are the bodies of the productions

Compilers: syntax/4 50 FIRST_SEQ() FIRST_SEQ(  ) = {  } FIRST_SEQ(c  ) = { c }, if c is a terminal FIRST_SEQ(A  ) = FIRST(A), if   FIRST(A) = (FIRST(A) – {  })  FIRST_SEQ(  ), if   FIRST(A) – –  is a sequence of terminals and non-terminals, and possibly empty

Compilers: syntax/4 51 FIRST() Example 1 S => a S e S => B B => b B e B => C C => c C e C => d FIRST(C) = {c,d} FIRST(C) = {c,d} FIRST(B) = FIRST(B) = FIRST(S) = FIRST(S) = Start with FIRST(C) since its rules only start with terminals continued

Compilers: syntax/4 52 FIRST(C) = {c,d} FIRST(B) = {b,c,d} FIRST(S) = do FIRST(B) now that we know FIRST(C) S => a S e S => a S e S => B S => B B => b B e B => b B e B => C B => C C => c C e C => c C e C => d C => d continued

Compilers: syntax/4 53 FIRST(C) = {c,d} FIRST(B) = {b,c,d} FIRST(S) = {a,b,c,d} S => a S e S => a S e S => B S => B B => b B e B => b B e B => C B => C C => c C e C => c C e C => d C => d do FIRST(S) now that we know FIRST(B)

Compilers: syntax/4 54 FIRST() Example 2 P => i | c | n T S Q => P | a S | b S c S T R => b |  S => c | R n |  T => R S q FIRST(P) = {i,c,n} FIRST(P) = {i,c,n} FIRST(Q) = FIRST(Q) = FIRST(R) = {b,  } FIRST(R) = {b,  } FIRST(S) = FIRST(S) = FIRST(T) = FIRST(T) = continued Start with P and R since their rules only start with terminals or 

Compilers: syntax/4 55 FIRST(P) = {i,c,n} FIRST(Q) = {i,c,n,a,b} FIRST(R) = {b,  } FIRST(S) = FIRST(T) = P => i | c | n T S P => i | c | n T S Q => P | a S | b S c S T Q => P | a S | b S c S T R => b |  R => b |  S => c | R n |  S => c | R n |  T => R S q T => R S q continued do FIRST(Q) now that we know FIRST(P)

Compilers: syntax/4 56 FIRST(P) = {i,c,n} FIRST(Q) = {i,c,n,a,b} FIRST(R) = {b,  } FIRST(S) = {c,b,n,  } FIRST(T) = do FIRST(S) now that we know FIRST(R) Note: S  R n  n because R  *  P => i | c | n T S P => i | c | n T S Q => P | a S | b S c S T Q => P | a S | b S c S T R => b |  R => b |  S => c | R n |  S => c | R n |  T => R S q T => R S q continued

Compilers: syntax/4 57 FIRST(P) = {i,c,n} FIRST(Q) = {i,c,n,a,b} FIRST(R) = {b,  } FIRST(S) = {c,b,n,  } FIRST(T) = {b,c,n,q} do FIRST(T) now that we know FIRST(R) and FIRST(S) Note: T  R S q  S q  q because both R and S  *  P => i | c | n T S P => i | c | n T S Q => P | a S | b S c S T Q => P | a S | b S c S T R => b |  R => b |  S => c | R n |  S => c | R n |  T => R S q T => R S q

Compilers: syntax/4 58 FIRST() Example 3 S => a S e | S T S T => R S e | Q R => r S r |  Q => S T |  FIRST(S) = {a} FIRST(S) = {a} FIRST(T) = {r, a,  } FIRST(T) = {r, a,  } FIRST(R) = {r,  } FIRST(R) = {r,  } FIRST(Q) = {a,  } FIRST(Q) = {a,  } Order 1) R, S 2) Q 3) T

Compilers: syntax/ The FOLLOW Sets FOLLOW( ) = – –set of all the terminals that follow in productions – –the set includes $ if nothing follows

Compilers: syntax/4 60 Example: S => bing A bong | ping A pong | zing A A => ha FOLLOW(A) = { bong, pong, $ }

Compilers: syntax/4 61 More Mathematically A is a non-terminal. FOLLOW(A) = { c in terminals | S => +... A c... }  { $ } if S => +...   is a sequence of terminals and non-terminals => + is any number of => expansions

Compilers: syntax/4 62 Building FOLLOW() Sets To make the FOLLOW(A) set, apply rules 1-4: 1. for all productions (B =>... A  ) add FIRST_SEQ(  )-{  } 2. for all (B =>... A  ) and   FIRST_SEQ(  ) add FOLLOW(B) 3. for all (B =>... A) add FOLLOW(B) 4. if A is the start symbol then add { $ }  is a sequence of termminals and non-terminals

Compilers: syntax/4 63 What is in FOLLOW(A) for the productions: B => A C C => s FOLLOW(A) gets FIRST_SEQ(C) == FIRST(C) == { s } – –uses rule 1 continued Small Examples

Compilers: syntax/4 64 What is in FOLLOW(A) for the productions: C => B r B => t A FOLLOW(A) gets FOLLOW(B) == { r } – –uses rule 3

Compilers: syntax/4 65 FOLLOW() Example 1 S => a S e | B B => b B C f | C C => c C g | d |  FIRST(C) = {c,d,  } FIRST(B) = {b,c,d,  } FIRST(S) = {a,b,c,d,  } FOLLOW(C) = FOLLOW(C) = FOLLOW(B) = FOLLOW(B) = FOLLOW(S) = {$, e} FOLLOW(S) = {$, e} S is the start symbol continued

Compilers: syntax/4 66 S => a S e | B B => b B C f | C C => c C g | d |  FIRST(C) = {c,d,  } FIRST(B) = {b,c,d,  } FIRST(S) = {a,b,c,d,  } FOLLOW(C) = {f,g}  follow(B) FOLLOW(C) = {f,g}  follow(B) FOLLOW(B) = FIRST_SEQ(C f) -{  }  FOLLOW(S) = {c, d, f, $, e} FOLLOW(B) = FIRST_SEQ(C f) -{  }  FOLLOW(S) = {c, d, f, $, e} FOLLOW(S) = {$,e} FOLLOW(S) = {$,e} continued

Compilers: syntax/4 67 S => a S e | B B => b B C f | C C => c C g | d |  FIRST(C) = {c,d,  } FIRST(B) = {b,c,d,  } FIRST(S) = {a,b,c,d,  } FOLLOW(C) = {f,g,c,d,$,e} FOLLOW(C) = {f,g,c,d,$,e} FOLLOW(B) = {c, d, f, $, e} FOLLOW(B) = {c, d, f, $, e} FOLLOW(S) = {$,e} FOLLOW(S) = {$,e}

Compilers: syntax/4 68 FOLLOW() Example 2 S => ( A ) |  A => T E E => & T E |  T => ( A ) | a | b | c FIRST(T) = {(,a,b,c} FIRST(E) = {&,  } FIRST(A) = {(,a,b,c} FIRST(S) = {(,  } FOLLOW(S) = {$} FOLLOW(S) = {$} FOLLOW(A) = {)} FOLLOW(A) = {)} FOLLOW(E) = FOLLOW(E) = FOLLOW(T) = FOLLOW(T) = continued

Compilers: syntax/4 69 S => ( A ) |  A => T E E => & T E |  T => ( A ) | a | b | c FIRST(T) = {(,a,b,c} FIRST(E) = {&,  } FIRST(A) = {(,a,b,c} FIRST(S) = {(,  } FOLLOW(S) = { $ } FOLLOW(S) = { $ } FOLLOW(A) = { ) } FOLLOW(A) = { ) } FOLLOW(E) = FOLLOW(E) = FOLLOW(A)  FOLLOW(E) = { ) } FOLLOW(A)  FOLLOW(E) = { ) } FOLLOW(T) = FOLLOW(T) = (FIRST_SEQ(E) – {  })  FOLLOW(A)  FOLLOW(E) = {&, )} (FIRST_SEQ(E) – {  })  FOLLOW(A)  FOLLOW(E) = {&, )}

Compilers: syntax/4 70 FOLLOW() Example 3 S => T E1 E1 => + T E1 |  T => F T1 T1 => * F T1 |  F => ( S ) | id FIRST(F) = FIRST(T) = FIRST(S) = {(,id} FIRST(T1) = {*,  } FIRST(E1) = {+,  } FOLLOW(S) = {$,)} FOLLOW(S) = {$,)} FOLLOW(E1) = FOLLOW(E1) = FOLLOW(T) = FOLLOW(T) = FOLLOW(T1) = FOLLOW(T1) = FOLLOW(F) = FOLLOW(F) = continued

Compilers: syntax/4 71 S => T E1 E1 => + T E1 |  T => F T1 T1 => * F T1 |  F => ( S ) | id FIRST(F) = FIRST(T) = FIRST(S) = {(,id} FIRST(T1) = {*,  } FIRST(E1) = {+,  } FOLLOW(S) = {$,)} FOLLOW(S) = {$,)} FOLLOW(E1) = FOLLOW(S)  Follow(E1) = {$,)} FOLLOW(E1) = FOLLOW(S)  Follow(E1) = {$,)} FOLLOW(T) = FIRST(E1)  FOLLOW(S)  FOLLOW(E1) = {+,$,)} FOLLOW(T) = FIRST(E1)  FOLLOW(S)  FOLLOW(E1) = {+,$,)} FOLLOW(T1) = FOLLOW(T) = {+,$,)} FOLLOW(T1) = FOLLOW(T) = {+,$,)} FOLLOW(F) = FIRST(T1)  FOLLOW(T)  FOLLOW(T1) = {*,+,$,)} FOLLOW(F) = FIRST(T1)  FOLLOW(T)  FOLLOW(T1) = {*,+,$,)}

Compilers: syntax/4 72 FOLLOW() Example 4 S => A B C | A D A => a | a A B => b | c |  C => D a C D => b b | c c FIRST(D) = FIRST(C) = {b,c} FIRST(B) = {b,c  FIRST(A) = FIRST(S) = {a} FOLLOW(S) = {$} FOLLOW(S) = {$} FOLLOW(D) = {a,$} FOLLOW(D) = {a,$} FOLLOW(A) = FOLLOW(A) = FOLLOW(B) = FOLLOW(B) = FOLLOW(C) = FOLLOW(C) = continued

Compilers: syntax/4 73 S => A B C | A D A => a | a A B => b | c |  C => D a C D => b b | c c FIRST(D) = FIRST(C) = {b,c} FIRST(B) = {b,c  FIRST(A) = FIRST(S) = {a} FOLLOW(S) = {$} FOLLOW(S) = {$} FOLLOW(D) = {a,$} FOLLOW(D) = {a,$} FOLLOW(A) = {b,c} FOLLOW(A) = {b,c} FOLLOW(B) = {b,c} FOLLOW(B) = {b,c} FOLLOW(C) = {$} FOLLOW(C) = {$}