Chapter 5 Grammars and Parsers

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

Compiler Construction
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.
Chapter 3 Syntax Analysis
Exercise: Balanced Parentheses
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
YANGYANG 1 Chap 5 LL(1) Parsing LL(1) left-to-right scanning leftmost derivation 1-token lookahead parser generator: Parsing becomes the easiest! Modifying.
Lexical and Syntactic Analysis Here, we look at two of the tasks involved in the compilation process –Given source code, we need to first break it into.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
Chapter 2 A Simple Compiler
Compiler construction in4020 – lecture 3 Koen Langendoen Delft University of Technology The Netherlands.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
1 Chapter 2 A Simple Compiler. 2 Outlines 2.1 The Structure of a Micro Compiler 2.2 A Micro Scanner 2.3 The Syntax of Micro 2.4 Recursive Descent Parsing.
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
Chapter 5 Top-Down Parsing.
C Chuen-Liang Chen, NTUCS&IE / 77 TOP-DOWN PARSING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
C Chuen-Liang Chen, NTUCS&IE / 51 CONTEXT-FREE GRAMMARS Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
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.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Bernd Fischer RW713: Compiler and Software Language Engineering.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Announcements/Reading
Chapter 3 – Describing Syntax
Parsing #1 Leonidas Fegaras.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Programming Languages Translator
CS510 Compiler Lecture 4.
Compiler Baojian Hua LR Parsing Compiler Baojian Hua
Lecture #12 Parsing Types.
Introduction to Parsing (adapted from CS 164 at Berkeley)
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
Parsing IV Bottom-up Parsing
Chapter 3 – Describing Syntax
Table-driven parsing Parsing performed by a finite state machine.
Compiler design Bottom-up parsing: Canonical LR and LALR
Chapter 4 Top-Down Parsing Part-1 September 8, 2018
Bottom-Up Syntax Analysis
4 (c) parsing.
CS 3304 Comparative Languages
Top-Down Parsing.
Syntax Analysis Sections :.
Syntax-Directed Translation
Top-Down Parsing CS 671 January 29, 2008.
CS 540 George Mason University
4d Bottom Up Parsing.
CSE 3302 Programming Languages
COP4020 Programming Languages
Compiler Design 7. Top-Down Table-Driven Parsing
Bottom Up Parsing.
R.Rajkumar Asst.Professor CSE
Programming Language Syntax 5
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Bottom-Up Parsing “Shift-Reduce” Parsing
LL and Recursive-Descent Parsing
Syntax Analysis - Parsing
Kanat Bolazar February 16, 2010
BNF 9-Apr-19.
Nonrecursive Predictive Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Compiler design Bottom-up parsing: Canonical LR and LALR
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Chapter 5 Grammars and Parsers Prof Chung.

Outlines The LL(1) Predict Function The LL(1) Parse Table Building Recursive Descent Parsers from LL(1) Tables An LL(1) Parser Driver LL(1) Action Symbols Making Grammars LL(1) The If-Then-Else Problem in LL(1) Parsing

The LL(1) Predict Function Given the productions A1 A2 … An During a (leftmost) derivation … A …  … 1 … or … 2 … or … n … Deciding which production to match Using lookahead symbols

The LL(1) Predict Function (1) More Detail Operation at Next Page… The LL(1) Predict Function (1) The limitation of LL(1) LL(1) contains exactly those grammars that have disjoint predict sets for productions that share a common left-hand side Single Symbol Lookahead Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm)

The LL(1) Predict Function (2) More Detail Operation at Next Page… The LL(1) Predict Function (2) An LL(1) parse table T : Vn x Vt  P  {Error} The definition of T T[A][t] = AX1Xm if t  Prediction (AX1Xm ); T[A][t] = Error otherwise

The LL(1) Parse Table (1) More Detail Operation at Next Page… First Sets for Micro Nonterminal First Set <program> {begin} <statement list> {ID, read, write} <statement> <statement tail> {ID, read, write, } <expression> {ID, INTLIT,( } <id list> {ID} <expr list> {ID,INTLIT,( } <id tail> {COMMA, } <expr tail> <primary> {ID,INTLIT, ( } <primary tail> {+, -, } <add op> {+, -} <system goal> The LL(1) Parse Table (1) Grammar <program>  begin<statement list>end <statement list> <statement> <statement tail> <statement tail>  <statement> ID := <expression>; read ( <id list> ); write ( <expr list> ); <id list> ID <id tail> <id tail> ,ID <id tail> <expr list> <expression><expr tail> <expr tail> , <expression><expr tail> <expression> <primary> <primary tail> <primary tail> <add op><primary tail> <primary> (<experssion>) ID INTLIT <add op> ┼ ─ <system goal> <program>$ Follow Sets for Micro Nonterminal Follow Set <program> {$} <statement list> {end} <statement> {ID, read, write, end} <statement tail> <expression> {COMMA, SEMICOLON, ) } <id list> { )} <expr list> <id tail> <expr tail> <primary> {COMMA, SEMICOLON, +, -, ) } <primary tail> <add op> {ID, INTLIT, ( } <system goal> {}

The LL(1) Parse Table (2) More Detail Operation at Next Page… Prod Predict Set 1 First (begin <statement list> end) = First (begin) = {begin} 2 First (<statement> <statement tail>) = First ( <statement> )= {ID, read, write} 3 4 ( First (λ) – λ)∪Follow(<statement tail))= Follow (<statement tail>) = {end} 5 First (ID := <expression> ;) = First (ID) = {ID} 6 First (read (<id list>);) = First ( read ) = { read } 7 First (write (<expr list>);) = First ( write ) = {write} 8 First (ID<id tail>)= First(ID)= {id} 9 First (,ID<id tail>)= First(,)= {,} 10 First (λ)-λ) ∪Follow(<id tail>)= Follow(<id tail>)= {)} 11 First(<expression><expr tail>)= First(<expression>)= {ID,INTLIT,(} 12 First(,<expression><expr tail>)= 13 First (λ)-λ) ∪Follow(<expr tail>)= Follow(<expr tail>)= 14 First(<primary><primary tail>)= First(<primary>)= 15 First(<add op><primary><primary tail>)= First(<add op>)= {+,-} 16 First (λ)-λ) ∪Follow(<primary tail>)= Follow(<primary tail>)= {COMMA,;,)} 17 First((<expression>))= First(()= {(} 18 First (ID)= 19 First (INTLIT)= {INTLIT} 20 First(+)= {+} 21 First(-)= {-} 22 First(<program>)$)= First(<program>)= 7

The LL(1) Table for Micro More Detail Operation at Next Page… The LL(1) Parse Table (3) The LL(1) Table for Micro ID INTLIT := , ; + - ( ) begin end read write $ <program> 1 <statement list> 2 <statement> 5 6 7 <statement tail> 3 4 <expression> 14 <id list> 8 <expr list> 11 <id tail> 9 10 <expr tail> 12 13 <primary> 18 19 17 <primary tail> 16 15 <add op> 20 21 <system goal> 22

Prod Predict Set – (1) 1 9 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (1) <program>  begin<statement list>end <statement list> <statement> <statement tail> <statement tail>  First Sets for Micro – (1) Nonterminal First Set <program> {begin} <statement list> {ID, read, write} <statement> <statement tail> {ID, read, write, } Follow Sets for Micro – (1) Nonterminal Follow Set <program> {$} <statement list> {end} <statement> {ID, read, write, end} <statement tail> Prod Predict Set – (1) 1 First (begin <statement list> end) = First (begin) = {begin} 2 First (<statement> <statement tail>) = First ( <statement> )= {ID, read, write} 3 4 ( First (λ) – λ)∪Follow(<statement tail))= Follow (<statement tail>) = {end} The LL(1) Table for Micro - (1) ID INTLIT := , ; + - ( ) begin end read write $ <program> <statement list> <statement> <statement tail> 1 9

Prod Predict Set – (1) 2 2 2 10 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (1) <program>  begin<statement list>end <statement list> <statement> <statement tail> <statement tail>  First Sets for Micro – (1) Nonterminal First Set <program> {begin} <statement list> {ID, read, write} <statement> <statement tail> {ID, read, write, } Follow Sets for Micro – (1) Nonterminal Follow Set <program> {$} <statement list> {end} <statement> {ID, read, write, end} <statement tail> Prod Predict Set – (1) 1 First (begin <statement list> end) = First (begin) = {begin} 2 First (<statement> <statement tail>) = First ( <statement> )= {ID, read, write} 3 4 ( First (λ) – λ)∪Follow(<statement tail))= Follow (<statement tail>) = {end} The LL(1) Table for Micro - (1) ID INTLIT := , ; + - ( ) begin end read write $ <program> 1 <statement list> <statement> <statement tail> 2 2 2 10

Prod Predict Set – (1) 3 3 3 11 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (1) <program>  begin<statement list>end <statement list> <statement> <statement tail> <statement tail>  First Sets for Micro – (1) Nonterminal First Set <program> {begin} <statement list> {ID, read, write} <statement> <statement tail> {ID, read, write, } Follow Sets for Micro – (1) Nonterminal Follow Set <program> {$} <statement list> {end} <statement> {ID, read, write, end} <statement tail> Prod Predict Set – (1) 1 First (begin <statement list> end) = First (begin) = {begin} 2 First (<statement> <statement tail>) = First ( <statement> )= {ID, read, write} 3 4 ( First (λ) – λ)∪Follow(<statement tail))= Follow (<statement tail>) = {end} The LL(1) Table for Micro - (1) ID INTLIT := , ; + - ( ) begin end read write $ <program> 1 <statement list> 2 <statement> <statement tail> 3 3 3 11

Prod Predict Set – (1) 4 12 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (1) <program>  begin<statement list>end <statement list> <statement> <statement tail> <statement tail>  First Sets for Micro – (1) Nonterminal First Set <program> {begin} <statement list> {ID, read, write} <statement> <statement tail> {ID, read, write, } Follow Sets for Micro – (1) Nonterminal Follow Set <program> {$} <statement list> {end} <statement> {ID, read, write, end} <statement tail> Prod Predict Set – (1) 1 First (begin <statement list> end) = First (begin) = {begin} 2 First (<statement> <statement tail>) = First ( <statement> )= {ID, read, write} 3 4 ( First (λ) – λ)∪Follow(<statement tail))= Follow (<statement tail>) = {end} The LL(1) Table for Micro - (1) ID INTLIT := , ; + - ( ) begin end read write $ <program> 1 <statement list> 2 <statement> <statement tail> 3 4 12

Prod Predict Set – (2) 5 13 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (2) <statement>  ID := <expression>; read ( <id list> ); write ( <expr list> ); First Sets for Micro – (2) Nonterminal First Set Follow Sets for Micro – (2) Nonterminal Follow Set Prod Predict Set – (2) 5 First (ID := <expression> ;) = First (ID) = {ID} 6 First (read (<id list>);) = First ( read ) = { read } 7 First (write (<expr list>);) = First ( write ) = {write} The LL(1) Table for Micro - (2) ID INTLIT := , ; + - ( ) begin end read write $ <program> 1 <statement list> 2 <statement> <statement tail> 3 4 5 13

Prod Predict Set – (2) 6 14 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (2) <statement>  ID := <expression>; read ( <id list> ); write ( <expr list> ); First Sets for Micro – (2) Nonterminal First Set Follow Sets for Micro – (2) Nonterminal Follow Set Prod Predict Set – (2) 5 First (ID := <expression> ;) = First (ID) = {ID} 6 First (read (<id list>);) = First ( read ) = { read } 7 First (write (<expr list>);) = First ( write ) = {write} The LL(1) Table for Micro - (2) ID INTLIT := , ; + - ( ) begin end read write $ <program> 1 <statement list> 2 <statement> 5 <statement tail> 3 4 6 14

Prod Predict Set – (2) 7 15 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (2) <statement>  ID := <expression>; read ( <id list> ); write ( <expr list> ); First Sets for Micro – (2) Nonterminal First Set Follow Sets for Micro – (2) Nonterminal Follow Set Prod Predict Set – (2) 5 First (ID := <expression> ;) = First (ID) = {ID} 6 First (read (<id list>);) = First ( read ) = { read } 7 First (write (<expr list>);) = First ( write ) = {write} The LL(1) Table for Micro - (2) ID INTLIT := , ; + - ( ) begin end read write $ <program> 1 <statement list> 2 <statement> 5 6 <statement tail> 3 4 7 15

Prod Predict Set – (3) 8 16 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (3) <id list>  ID <id tail> <id tail> ,ID <id tail>  <expr list> <expression><expr tail> First Sets for Micro – (3) Nonterminal First Set <expression> {ID, INTLIT,( } <id tail> {COMMA, } Follow Sets for Micro – (3) Nonterminal Follow Set <expression> {COMMA, SEMICOLON, ) } <id tail> { )} Prod Predict Set – (3) 8 First (ID<id tail>)= First(ID)= {id} 9 First (,ID<id tail>)= First(,)= {,} 10 First (λ)-λ) ∪Follow(<id tail>)= Follow(<id tail>)= {)} 11 First(<expression><expr tail>)= First(<expression>)= {ID,INTLIT,(} The LL(1) Table for Micro - (3) ID INTLIT := , ; + - ( ) begin end read write $ <id list> <expr list> <id tail> 8 16

Prod Predict Set – (3) 9 17 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (3) <id list>  ID <id tail> <id tail> ,ID <id tail>  <expr list> <expression><expr tail> First Sets for Micro – (3) Nonterminal First Set <expression> {ID, INTLIT,( } <id tail> {COMMA, } Follow Sets for Micro – (3) Nonterminal Follow Set <expression> {COMMA, SEMICOLON, ) } <id tail> { )} Prod Predict Set – (3) 8 First (ID<id tail>)= First(ID)= {id} 9 First (,ID<id tail>)= First(,)= {,} 10 First (λ)-λ) ∪Follow(<id tail>)= Follow(<id tail>)= {)} 11 First(<expression><expr tail>)= First(<expression>)= {ID,INTLIT,(} The LL(1) Table for Micro - (3) ID INTLIT := , ; + - ( ) begin end read write $ <id list> 8 <expr list> <id tail> 9 17

Prod Predict Set – (3) 10 18 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (3) <id list>  ID <id tail> <id tail> ,ID <id tail>  <expr list> <expression><expr tail> First Sets for Micro – (3) Nonterminal First Set <expression> {ID, INTLIT,( } <id tail> {COMMA, } Follow Sets for Micro – (3) Nonterminal Follow Set <expression> {COMMA, SEMICOLON, ) } <id tail> { )} Prod Predict Set – (3) 8 First (ID<id tail>)= First(ID)= {id} 9 First (,ID<id tail>)= First(,)= {,} 10 First (λ)-λ) ∪Follow(<id tail>)= Follow(<id tail>)= {)} 11 First(<expression><expr tail>)= First(<expression>)= {ID,INTLIT,(} The LL(1) Table for Micro - (3) ID INTLIT := , ; + - ( ) begin end read write $ <id list> 8 <expr list> <id tail> 9 10 18

Prod Predict Set – (3) 11 11 11 19 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (3) <id list>  ID <id tail> <id tail> ,ID <id tail>  <expr list> <expression><expr tail> First Sets for Micro – (3) Nonterminal First Set <expression> {ID, INTLIT,( } <id tail> {COMMA, } Follow Sets for Micro – (3) Nonterminal Follow Set <expression> {COMMA, SEMICOLON, ) } <id tail> { )} Prod Predict Set – (3) 8 First (ID<id tail>)= First(ID)= {id} 9 First (,ID<id tail>)= First(,)= {,} 10 First (λ)-λ) ∪Follow(<id tail>)= Follow(<id tail>)= {)} 11 First(<expression><expr tail>)= First(<expression>)= {ID,INTLIT,(} The LL(1) Table for Micro - (3) ID INTLIT := , ; + - ( ) begin end read write $ <id list> 8 <expr list> <id tail> 9 10 11 11 11 19

Prod Predict Set – (4) 12 20 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (4) <expr tail>  , <expression><expr tail>  <expression> <primary> <primary tail> <primary tail> <add op><primary tail> First Sets for Micro – (4) Nonterminal First Set <expr tail> {COMMA, } <primary> {ID,INTLIT, ( } <primary tail> {+, -, } <add op> {+, -} Follow Sets for Micro – (4) Nonterminal Follow Set <expr tail> { )} <primary> {COMMA, SEMICOLON, +, -, ) } <primary tail> {COMMA, SEMICOLON, ) } <add op> {ID, INTLIT, ( } Prod Predict Set – (4) 12 First(,<expression><expr tail>)= First(,)= {,} 13 First (λ)-λ) ∪Follow(<expr tail>)= Follow(<expr tail>)= {)} 14 First(<primary><primary tail>)= First(<primary>)= {ID,INTLIT,(} 15 First(<add op><primary><primary tail>)= First(<add op>)= {+,-} 16 First (λ)-λ) ∪Follow(<primary tail>)= Follow(<primary tail>)= {COMMA,;,)} The LL(1) Table for Micro - (4) ID INTLIT := , ; + - ( ) begin end read write $ <expression> <expr tail> <primary tail> 12 20

Prod Predict Set – (4) 13 21 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (4) <expr tail>  , <expression><expr tail>  <expression> <primary> <primary tail> <primary tail> <add op><primary tail> First Sets for Micro – (4) Nonterminal First Set <expr tail> {COMMA, } <primary> {ID,INTLIT, ( } <primary tail> {+, -, } <add op> {+, -} Follow Sets for Micro – (4) Nonterminal Follow Set <expr tail> { )} <primary> {COMMA, SEMICOLON, +, -, ) } <primary tail> {COMMA, SEMICOLON, ) } <add op> {ID, INTLIT, ( } Prod Predict Set – (4) 12 First(,<expression><expr tail>)= First(,)= {,} 13 First (λ)-λ) ∪Follow(<expr tail>)= Follow(<expr tail>)= {)} 14 First(<primary><primary tail>)= First(<primary>)= {ID,INTLIT,(} 15 First(<add op><primary><primary tail>)= First(<add op>)= {+,-} 16 First (λ)-λ) ∪Follow(<primary tail>)= Follow(<primary tail>)= {COMMA,;,)} The LL(1) Table for Micro - (4) ID INTLIT := , ; + - ( ) begin end read write $ <expression> <expr tail> 12 <primary tail> 13 21

Prod Predict Set – (4) 14 22 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (4) <expr tail>  , <expression><expr tail>  <expression> <primary> <primary tail> <primary tail> <add op><primary tail> First Sets for Micro – (4) Nonterminal First Set <expr tail> {COMMA, } <primary> {ID,INTLIT, ( } <primary tail> {+, -, } <add op> {+, -} Follow Sets for Micro – (4) Nonterminal Follow Set <expr tail> { )} <primary> {COMMA, SEMICOLON, +, -, ) } <primary tail> {COMMA, SEMICOLON, ) } <add op> {ID, INTLIT, ( } Prod Predict Set – (4) 12 First(,<expression><expr tail>)= First(,)= {,} 13 First (λ)-λ) ∪Follow(<expr tail>)= Follow(<expr tail>)= {)} 14 First(<primary><primary tail>)= First(<primary>)= {ID,INTLIT,(} 15 First(<add op><primary><primary tail>)= First(<add op>)= {+,-} 16 First (λ)-λ) ∪Follow(<primary tail>)= Follow(<primary tail>)= {COMMA,;,)} The LL(1) Table for Micro - (4) ID INTLIT := , ; + - ( ) begin end read write $ <expression> <expr tail> 12 13 <primary tail> 14 22

Prod Predict Set – (4) 15 15 23 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (4) <expr tail>  , <expression><expr tail>  <expression> <primary> <primary tail> <primary tail> <add op><primary tail> First Sets for Micro – (4) Nonterminal First Set <expr tail> {COMMA, } <primary> {ID,INTLIT, ( } <primary tail> {+, -, } <add op> {+, -} Follow Sets for Micro – (4) Nonterminal Follow Set <expr tail> { )} <primary> {COMMA, SEMICOLON, +, -, ) } <primary tail> {COMMA, SEMICOLON, ) } <add op> {ID, INTLIT, ( } Prod Predict Set – (4) 12 First(,<expression><expr tail>)= First(,)= {,} 13 First (λ)-λ) ∪Follow(<expr tail>)= Follow(<expr tail>)= {)} 14 First(<primary><primary tail>)= First(<primary>)= {ID,INTLIT,(} 15 First(<add op><primary><primary tail>)= First(<add op>)= {+,-} 16 First (λ)-λ) ∪Follow(<primary tail>)= Follow(<primary tail>)= {COMMA,;,)} The LL(1) Table for Micro - (4) ID INTLIT := , ; + - ( ) begin end read write $ <expression> 14 <expr tail> 12 13 <primary tail> 15 15 23

Prod Predict Set – (4) 16 16 16 24 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (4) <expr tail>  , <expression><expr tail>  <expression> <primary> <primary tail> <primary tail> <add op><primary tail> First Sets for Micro – (4) Nonterminal First Set <expr tail> {COMMA, } <primary> {ID,INTLIT, ( } <primary tail> {+, -, } <add op> {+, -} Follow Sets for Micro – (4) Nonterminal Follow Set <expr tail> { )} <primary> {COMMA, SEMICOLON, +, -, ) } <primary tail> {COMMA, SEMICOLON, ) } <add op> {ID, INTLIT, ( } Prod Predict Set – (4) 12 First(,<expression><expr tail>)= First(,)= {,} 13 First (λ)-λ) ∪Follow(<expr tail>)= Follow(<expr tail>)= {)} 14 First(<primary><primary tail>)= First(<primary>)= {ID,INTLIT,(} 15 First(<add op><primary><primary tail>)= First(<add op>)= {+,-} 16 First (λ)-λ) ∪Follow(<primary tail>)= Follow(<primary tail>)= {COMMA,;,)} The LL(1) Table for Micro - (4) ID INTLIT := , ; + - ( ) begin end read write $ <expression> 14 <expr tail> 12 13 <primary tail> 15 16 16 16 24

Prod Predict Set – (5) 17 25 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (5) <primary>  (<experssion>) ID INTLIT <add op> ┼ ─ <system goal> <program>$ First Sets for Micro – (5) Nonterminal First Set <program> {begin} Follow Sets for Micro – (5) Nonterminal Follow Set <program> {$} Prod Predict Set – (5) 17 First((<expression>))= First(()= {(} 18 First (ID)= {ID} 19 First (INTLIT)= {INTLIT} 20 First(+)= {+} 21 First(-)= {-} 22 First(<program>)$)= First(<program>)= {begin} The LL(1) Table for Micro - (5) ID INTLIT := , ; + - ( ) begin end read write $ <primary> <add op> <system goal> 17 25

Prod Predict Set – (5) 18 26 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (5) <primary>  (<experssion>) ID INTLIT <add op> ┼ ─ <system goal> <program>$ First Sets for Micro – (5) Nonterminal First Set <program> {begin} Follow Sets for Micro – (5) Nonterminal Follow Set <program> {$} Prod Predict Set – (5) 17 First((<expression>))= First(()= {(} 18 First (ID)= {ID} 19 First (INTLIT)= {INTLIT} 20 First(+)= {+} 21 First(-)= {-} 22 First(<program>)$)= First(<program>)= {begin} The LL(1) Table for Micro - (5) ID INTLIT := , ; + - ( ) begin end read write $ <primary> 17 <add op> <system goal> 18 26

Prod Predict Set – (5) 19 27 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (5) <primary>  (<experssion>) ID INTLIT <add op> ┼ ─ <system goal> <program>$ First Sets for Micro – (5) Nonterminal First Set <program> {begin} Follow Sets for Micro – (5) Nonterminal Follow Set <program> {$} Prod Predict Set – (5) 17 First((<expression>))= First(()= {(} 18 First (ID)= {ID} 19 First (INTLIT)= {INTLIT} 20 First(+)= {+} 21 First(-)= {-} 22 First(<program>)$)= First(<program>)= {begin} The LL(1) Table for Micro - (5) ID INTLIT := , ; + - ( ) begin end read write $ <primary> 18 17 <add op> <system goal> 19 27

Prod Predict Set – (5) 20 28 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (5) <primary>  (<experssion>) ID INTLIT <add op> ┼ ─ <system goal> <program>$ First Sets for Micro – (5) Nonterminal First Set <program> {begin} Follow Sets for Micro – (5) Nonterminal Follow Set <program> {$} Prod Predict Set – (5) 17 First((<expression>))= First(()= {(} 18 First (ID)= {ID} 19 First (INTLIT)= {INTLIT} 20 First(+)= {+} 21 First(-)= {-} 22 First(<program>)$)= First(<program>)= {begin} The LL(1) Table for Micro - (5) ID INTLIT := , ; + - ( ) begin end read write $ <primary> 18 19 17 <add op> <system goal> 20 28

Prod Predict Set – (5) 21 29 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (5) <primary>  (<experssion>) ID INTLIT <add op> ┼ ─ <system goal> <program>$ First Sets for Micro – (5) Nonterminal First Set <program> {begin} Follow Sets for Micro – (5) Nonterminal Follow Set <program> {$} Prod Predict Set – (5) 17 First((<expression>))= First(()= {(} 18 First (ID)= {ID} 19 First (INTLIT)= {INTLIT} 20 First(+)= {+} 21 First(-)= {-} 22 First(<program>)$)= First(<program>)= {begin} The LL(1) Table for Micro - (5) ID INTLIT := , ; + - ( ) begin end read write $ <primary> 18 19 17 <add op> 20 <system goal> 21 29

Prod Predict Set – (5) 22 30 Predict (AX1 …Xm ) =  If  ∈ First (X1…Xm) ( First (X1…Xm) -  ) ∪ Follow (A)  else First (X1…Xm) Grammar - (5) <primary>  (<experssion>) ID INTLIT <add op> ┼ ─ <system goal> <program>$ First Sets for Micro – (5) Nonterminal First Set <program> {begin} Follow Sets for Micro – (5) Nonterminal Follow Set <program> {$} Prod Predict Set – (5) 17 First((<expression>))= First(()= {(} 18 First (ID)= {ID} 19 First (INTLIT)= {INTLIT} 20 First(+)= {+} 21 First(-)= {-} 22 First(<program>)$)= First(<program>)= {begin} The LL(1) Table for Micro - (5) ID INTLIT := , ; + - ( ) begin end read write $ <primary> 18 19 17 <add op> 20 21 <system goal> 22 30

The LL(1) Table for Micro The LL(1) Parse Table (3) The LL(1) Table for Micro ID INTLIT := , ; + - ( ) begin end read write $ <program> 1 <statement list> 2 <statement> 5 6 7 <statement tail> 3 4 <expression> 14 <id list> 8 <expr list> 11 <id tail> 9 10 <expr tail> 12 13 <primary> 18 19 17 <primary tail> 16 15 <add op> 20 21 <system goal> 22

Building Recursive Descent Parsers from LL(1) Tables (1) Similar the implementation of a scanner Build in The parsing decisions recorded in LL(1) tables can be hardwired into the parsing procedures used by recursive descent parsers Table-driven

Building Recursive Descent Parsers from LL(1) Tables (2) The form of parsing procedure: non_term() [Function Code] It is the name of the nonterminal the parsing procedure handles. case TERMINAL_LIST …… default statement () [Function Code] An algorithm that automatically creates parsing procedures case ID case READ case WRITE

Building Recursive Descent Parsers from LL(1) Tables (3) Describing grammars Data Structure [Function Code] Setup Describing grammars Data Structure. gen_actions() [Function Code] [Example] Takes the grammar symbols and generates the actions necessary to match them in a recursive descent parse make_parsing_proc() [Function Code] Generae recursive descent parsing procedure for A. for each production where A is the LHS for each terminal in the grammar

An LL(1) Parser Driver (1) Rather than using the LL(1) table to build parsing procedures it is possible to use the table in conjunction with a driver program to form an LL(1) parser Smaller and faster than a corresponding recursive descent parser Changing a grammar and building a new parser is easy LL(1) driver [Function Code] computed and substituted for the old tables

LL(1) Action Symbols (1) During parsing, the appearance of an action symbol in a production will serve to initiate the corresponding semantic action – a call to the corresponding semantic routine. The semantic routine calls pass no explicit parameters LL(1) driver Action Symbols [Function Code] Necessary parameters are transmitted through a semantic stack Semantic stack  parse stack [Compare. @ Next page] Semantic stack is a stack of semantic records Action symbols are pushed to the parse stack

LL(1) Action Symbols (2) Compare Parse Stack Semantic Stack In recursive descent parsers, the parse stack is “hidden” in the procedure call stack of the running compiler. Semantic Stack “Parse Stack” extended. The semantic stack can be hidden that way as well, by having each parsing routine return a semantic record.

Making Grammars LL(1) Not all grammars are LL(1). LR LR(1) SLR(1) Some non-LL(1) grammars can be made LL(1) by simple modifications.

Making Grammars LL(1) When a grammar is not LL(1) ? ID This is called a Conflict, which means we do not know which production to use when <stmt> is on stack top and ID is the next input token. <stmt> ID 2,5 39

Making Grammars LL(1) Major LL(1) prediction conflicts Common prefixes Left recursion <stmt>  if <exp> then <stmt> <stmt>  if <exp> then <stmt> else <stmt> Solution: factoring transform See Next Page.

Making Grammars LL(1) void factor (grammar *G) {   while (G has 2 or more productions with the same LHS and a common prefix) {     Let S = { Aαβ, …, Aαξ } be the set of productions with the same left-hand side, A, and a common prefix, α;     Create a new nonterminal, N;    Replace S with the production set SET_OF (AαN, Nβ, …, Nξ);   } } <stmt>  if <exp> then <stmt list> <if suffix> <if suffix>  end if; <if suffix>  else <stmt list> end if;

Making Grammars LL(1) Grammars with left-recursive production can never be LL(1) A  A  Why? A will be the top stack symbol, and hence the same production would be predicted forever

Making Grammars LL(1) Solution: void remove_left_recursion (grammar *G) {  while ( G contains a left-recursive nonterminal) {   Let S = {AA ,A , …, Aζ } be the set of production with the same left-hand side, A, where A is left-recursive.   cerate two new nonterminals, T and N;   Replace S with the production set SET_OF (AN T, Nβ, …, Nξ, T  T, Tλ); } ANT N … N  T T T  AA A … A

Making Grammars LL(1) Other transformation may be needed No common prefixes, no left recursion <stmt>  <label> <unlabeled stmt> <label>  ID : <label>   <unlabeled stmt>  ID := <exp> ; <stmt> ID 2,3

Making Grammars LL(1) Example Grammar: <stmt>  ID <suffix> <suffix>  : <unlabeled stmt> <suffix>  := <exp> ; <unlabeled stmt>  ID := <exp> ;

Making Grammars LL(1) In Ada, we may declare arrays as A: array(I .. J, BOOLEAN) A straightforward grammar for array bound <array bound>  <expr> .. <expr> <array bound>  ID Solution <array bound>  <expr> <bound tail> <bound tail>  .. <expr> <bound tail>  

Making Grammars LL(1) Greibach Normal Form Given a grammar G, we can Every production is of the form Aa a is a terminal and  is a string (possible empty) of symbols Every context-free language L without  can be generated by a grammar in Greibach Normal Form Factoring of common prefixes is easy Given a grammar G, we can G  GNF  No common prefixes, no left recursion (but still may not be LL(1))

The If-Then-Else Problem in LL(1) Parsing “Dangling else” problem in Algo60, Pascal, and C else clause is optional BL={[i]j | ij 0} [  if <expr> then <stmt> ]  else <stmt> BL is not LL(1) and in fact not LL(k) for any k

The If-Then-Else Problem in LL(1) Parsing First try G1: S  [ S CL S   CL  ] CL   G1 is ambiguous: E.g., [[] S [ S CL  ] S [ S CL  ]

The If-Then-Else Problem in LL(1) Parsing Second try G2: S  [S S  S1 S1  [S1] S1   G2 is not ambiguous: E.g., [[] The problem is [  First([S) and [  First(S1) [[  First2([S) and [[  First2 (S1) G2 is not LL(1), nor is it LL(k) for any k. [ S1 [ S1 ] S 

The If-Then-Else Problem in LL(1) Parsing Solution : conflicts + special rules G3: G  S; S  if S E S  Other E  else S E   G3 is ambiguous We can enforce that T[E, else] = 4th rule. This essentially forces “else “ to be matched with the nearest unpaired “ if “. If else Other ; S Predict 2 Predict 3 E Predict 4 Predict 5 Predict 5 G Predict 1

The If-Then-Else Problem in LL(1) Parsing If all if statements are terminated with an end if, or some equivalent symbol, the problem disappears. S  if S E S  Other E  else S end if E  end if An alternative solution Change the language

The form of parsing procedure(1) void non_term(void) { token tok; token tok = next_token(); switch (tok) { case TERMINAL_LIST: parsing_actions(); break;      …… default : syntax_error(tok);   } } Represents a list of terminal symbol Re[resents a sequence of parsing actions: calls to parsing procedures to match nonterminals and calls to match() to match terminal symbol. Is called if next_token() predicts no valid prodyction

The form of parsing procedure(2) void statement (void) {   token tok;   tok = next_token();   switch (tok) { case ID : match (ID) ; match (ASSIGNOP); expression (); match (SEMICOLON); break;    case READ : match (READ) ; match (LPAREN); id_list(); match (RPAREN); match (SEMICOLON);    case WRITE:      match (WRITE) ; match (LPAREN); expr_list(); default : syntax_error(tok); }

for describing grammars (1) The data structure typedef int symbol; /* a symbol in the grammar */ #define VOCABULARY ( NUM_NONTERMINALS + NUM_TERMNALS ) typedef struct gram { symbol terminals [NUM_TERMINALS]; symbol nonterminals [NUM_NONTERMINALS]; symbol start_symbol; int num_productions; struct prod { symbol lhs; int rhs_length; symbol rhs [MAX_RHS_LENGTH]; } productions [NUM_PRODUCTIONS]; symbol vocabulary [VOCABULARY]; char *names [VOCABULARY]; } grammar; typedef struct prod production; typedef symbol terminal; typedef symbol nonterminal; 55

for describing grammars (2) extern char *make_id (char *); void gen_actions (symbol x[], int x_length) {   int i;   char *id;   /* Generate recursive descent actions needs to match x. */   if ( x_length ==0)     printf (“; /* null */\n”);   else {     for (i=0; i<x_length; i++) {       id = make_id ( g.names [ x[i] ]);       if ( is_terminal (x[i] ))         printf(“]t]tmatch(%s);\n”, id);       else         printf(\t\t%s();\n”, id);     }   } } 56

for describing grammars (3) gen_action(“ID:=<expression> #gen_assign;”) match(ID); match(ASSIGN); exp(); assign(); match(semicolon); ID := <expression> #gen_assign;

for describing grammars (4) void make_parsing_proc (const nonterminal A,const lltableT) {   /* Generae recursive descent parsing procedure for A. */   extern grammar g;   production p;   terminal x;   int I,j;   printf (“void %s(void)\n{\n”, make_id (g.names[A]));   printf(“ttoken tok = next_token()\n”);   printf(“\tswitch (tok) { \n”);   /* for each production where A is the LHS */   for (i=0; i< g.num_productions; i++) {     if ( g.productions[i].lhs != A )       continue;     p = g.productions[i];     /* for each terminal in the grammar */     for (j=0; j<NUM_TERMINALS; j++) {       x = g.terminals[j];       if ( T[A][x] ==I ) /* this production */         printf(“\tcase %s:\n”, make_id(g.names[x]));     }     gen_actions(p.rhs, p.rhs_length);     printf(“\t\tbreak;\n”);   }   printf(“\tdefault:\n\t syntax_error(tok);\n\t break;\n\t }”); } 58

<statement list> Void lldriver() (1) void lldriver (void) {    /* Push the Start Symbol onto an empty stack */   push (s);   while (!stack_empty()) {     /* Let X be the top stack symbol */     /* Let a be the current input token. */     if (is_nonterminal (X) && T[X][a] == XY1 … Ym ) {       /* Expand non-terminal */       pop(1);       push Ym, Y,m-1, … Y1 onto the sack;     } else if ( X == a) { /* X in terminals */       pop(1);  /* Match of X worked */       scanner (& a);  /* Get next token */     } else       /* Process syntax error. */   } } 【GOAL】:It stack smbols that are to be matched or expanded. begin <statement list> <program> end s <program>    begin<statement list>end <statement list>  <statement> <statement tail>

<statement list> Void lldriver() (1) void lldriver (void) {    /* Push the Start Symbol onto an empty stack */   push (s);   while (!stack_empty()) {     /* Let X be the top stack symbol */     /* Let a be the current input token. */     if (is_nonterminal (X) && T[X][a] == XY1 … Ym ) {       /* Expand non-terminal */       pop(1);       push Ym, Y,m-1, … Y1 onto the sack;     } else if ( X == a) { /* X in terminals */       pop(1);  /* Match of X worked */       scanner (& a);  /* Get next token */     } else       /* Process syntax error. */   } } 【GOAL】:It stack smbols that are to be matched or expanded. begin <statement list> end s <program>    begin<statement list>end <statement list>  <statement> <statement tail>

<statement tail> <statement list> Void lldriver() (1) void lldriver (void) {    /* Push the Start Symbol onto an empty stack */   push (s);   while (!stack_empty()) {     /* Let X be the top stack symbol */     /* Let a be the current input token. */     if (is_nonterminal (X) && T[X][a] == XY1 … Ym ) {       /* Expand non-terminal */       pop(1);       push Ym, Y,m-1, … Y1 onto the sack;     } else if ( X == a) { /* X in terminals */       pop(1);  /* Match of X worked */       scanner (& a);  /* Get next token */     } else       /* Process syntax error. */   } } 【GOAL】:It stack smbols that are to be matched or expanded. <statement> <statement tail> <statement list> end s <program>    begin<statement list>end <statement list>  <statement> <statement tail>

lldriver -- Action Symbols void lldriver(void) {   /* Push the Start Symbol onto an empty stack */   push (s);   while ( !stack_empty() ) {     /* Let X be the top stack symbol; */     /* Let a be the current input token */     if ( is_nonterminal(X) && T[X][a] == XY1 … Ym ) {       /* Expand nonterminal */       Replace X with Y1 … Ym on the stack;     } else if ( is_terminal (x) && X == a ) {       pop (1);  /* Match of X worked */       scanner ( &a); /* Get of X worked */     } else if ( is_action_symbol (X) ) {       pop(1);       Call Semantic Routine corresponding to X;     } else       /* Process syntax error */   } } 62