SlideShare a Scribd company logo
1 of 36
Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
Position of a Parser in the Compiler Model Lexical Analyzer Parser and rest of front-end Source Program Token, tokenval Symbol Table Get next token Lexical error Syntax error Semantic error Intermediate representation
The Parser ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Syntax-Directed Translation ,[object Object],[object Object],[object Object],[object Object],[object Object]
Error Handling ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Viable-Prefix Property ,[object Object],[object Object],[object Object],… for (;) … … DO 10 I = 1;0 … Error is detected here Error is detected here Prefix Prefix
Error Recovery Strategies ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Grammars (Recap) ,[object Object],[object Object],[object Object],[object Object],[object Object]
Notational Conventions Used ,[object Object],[object Object],[object Object],[object Object],[object Object]
Derivations (Recap) ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Derivation (Example) Grammar  G =  ({ E }, { + , * , ( , ) , - , id },  P ,  E ) with productions  P = E      E   +   E E     E   *   E E      (   E   ) E      -   E E      id E      -   E     -   id E    *   E E    +   id * id + id E    rm   E  +  E   rm   E   +   id   rm  id + id Example derivations: E    *   id + id
Chomsky Hierarchy: Language Classification ,[object Object],[object Object],[object Object],[object Object],[object Object]
Chomsky Hierarchy L ( regular )     L ( context free )     L ( context sensitive )     L ( unrestricted ) Where  L ( T ) = {  L ( G ) |  G  is of type  T  } That is: the set of all languages generated by grammars  G  of type  T L 1   = {  a n b n  |  n     1 } is context free L 2   = {  a n b n c n  |  n     1 } is context sensitive Every  finite language  is regular! (construct a FSA for strings in  L ( G )) Examples:
Parsing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Top-Down Parsing ,[object Object],Grammar: E      T   +   T T      (   E   ) T      -   E T      id Leftmost derivation: E    lm   T  +  T    lm   id   +   T    lm  id + id E E T + T id id E T T + E T + T id
[object Object],[object Object],Left Recursion (Recap)
A General Systematic Left Recursion Elimination Method Input: Grammar G with no cycles or   - productions Arrange the nonterminals in some order  A 1 ,  A 2 , …,  A n for   i  = 1, …,  n   do for   j  = 1, …,  i -1  do replace each A i      A j    with A i       1     |   2     | … |   k    where A j       1  |   2  | … |   k enddo eliminate the  immediate left recursion  in  A i enddo
Immediate Left-Recursion Elimination Rewrite every left-recursive production A      A       |      |     |  A    into a right-recursive production: A         A R   |      A R A R         A R   |    A R   |   
Example Left Recursion Elim. A      B   C  |  a B      C A  |  A   b C      A B  |  C C  |  a Choose arrangement:  A ,  B ,  C i  = 1: nothing to do i  = 2,  j  = 1: B      C A  |  A   b  B      C A  |  B C   b  |  a  b  (imm) B      C A B R  |  a b   B R B R      C  b   B R  |     i  = 3,  j  = 1: C      A  B  |  C C  |  a  C      B C  B  |  a   B  |  C C  |  a i  = 3,  j  = 2: C      B  C B  |  a   B  |  C C  |  a  C      C A B R  C B  |  a b  B R   C B  |  a   B  |  C C  |  a  (imm) C      a b  B R  C B C R  |  a   B   C R  |  a  C R C R      A B R  C B C R  |  C C R  |  
Left Factoring ,[object Object],[object Object]
Predictive Parsing ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
FIRST (Revisited) ,[object Object]
FOLLOW ,[object Object]
LL(1) Grammar ,[object Object]
Non-LL(1) Examples Grammar Not LL(1) because: S     S   a  |  a Left recursive S     a   S  |  a FIRST( a   S )    FIRST( a )      S     a   R  |   R     S  |   For  R :  S   *     and      *    S     a   R   a R     S  |   For  R : FIRST( S )    FOLLOW( R )     
Recursive-Descent Parsing (Recap) ,[object Object],[object Object],[object Object]
Using FIRST and FOLLOW in a Recursive-Descent Parser expr      term rest  rest     +   term rest   |  -   term rest   |   term      id procedure  rest (); begin   if  lookahead  in  FIRST( +   term rest )   then   match (‘ + ’);  term ();  rest ()   else if  lookahead  in  FIRST( -   term rest )   then   match (‘ - ’);  term ();  rest ()   else if  lookahead  in  FOLLOW( rest )  then   return   else  error () end ; FIRST( +   term rest ) = {  +  } FIRST( -   term rest ) = {  -  } FOLLOW( rest ) = {  $  }
Non-Recursive Predictive Parsing: Table-Driven Parsing ,[object Object],Predictive parsing program (driver) Parsing table M stack input output a + b $ X Y Z $
Constructing an LL(1) Predictive Parsing Table for  each production  A         do for  each  a     FIRST(  )  do add  A        to  M [ A , a ] enddo if        FIRST(  )  then for  each  b     FOLLOW( A )  do add  A        to  M [ A , b ] enddo     endif enddo Mark each undefined entry in  M  error
Example Table E      T E R E R      +   T   E R  |     T      F T R T R      *   F   T R  |     F      (  E  )  |  id id + * ( ) $ E E      T E R E      T E R E R E R      +   T   E R E R       E R       T T      F T R T      F T R T R T R       T R      *   F   T R T R       T R       F F      id F      (  E  ) A       FIRST(  ) FOLLOW( A ) E      T E R ( id $ ) E R      +   T   E R + $ ) E R        $ ) T      F T R ( id + $ ) T R      *   F   T R * + $ ) T R        + $ ) F      (  E  ) ( * + $ ) F      id id * + $ )
LL(1) Grammars are Unambiguous Ambiguous grammar S      i  E  t  S S R  |  a S R      e   S  |     E     b Error: duplicate table entry a b e i t $ S S      a S      i  E  t  S S R S R S R         S R      e   S S R       E E      b A       FIRST(  ) FOLLOW( A ) S      i  E  t  S S R i e $ S      a a e $ S R      e   S e e $ S R        e $ E     b b t
Predictive Parsing Program (Driver) push( $ ) push( S ) a  :=  lookahead repeat X  := pop() if  X  is a terminal or  X  =  $   then match( X ) //  moves to next token and a  :=  lookahead else if  M [ X , a ] =  X      Y 1 Y 2 … Y k   then push( Y k ,  Y k -1 ,   …,  Y 2 ,   Y 1 ) //  such that Y 1  is on top …  invoke actions and/or produce IR output … else error() endif until   X  =  $
Example Table-Driven Parsing Stack $ E $ E R T $ E R T R F $ E R T R id $ E R T R $ E R $ E R T + $ E R T $ E R T R F $ E R T R id $ E R T R $ E R T R F * $ E R T R F $ E R T R id $ E R T R $ E R $ Input id +id*id$ id +id*id$ id +id*id$ id +id*id$ + id*id$ + id*id$ + id*id$ id *id$ id *id$ id *id$ * id$ * id$ id $ id $ $ $ $ Production applied E    T   E R T    F   T R F     id T R       E R      +   T   E R T    F   T R F     id T R      *   F   T R   F     id   T R      E R     
Panic Mode Recovery FOLLOW( E ) = {  )   $  } FOLLOW( E R ) = {  )   $  } FOLLOW( T ) = {  +   )   $  } FOLLOW( T R ) = {  +   )   $  } FOLLOW( F ) = {  +   * )   $  } Add synchronizing actions to undefined entries based on FOLLOW synch : the driver pops current nonterminal  A  and skips input tillsynch token or skips input until one of FIRST( A ) is found Pro: Can be automated Cons: Error messages are needed id + * ( ) $ E E      T E R E      T E R synch synch E R E R      +   T   E R E R       E R       T T      F T R synch T      F T R synch synch T R T R       T R      *   F   T R T R       T R       F F      id synch synch F      (  E  ) synch synch
Phrase-Level Recovery Change input stream by inserting missing tokens For example:  id id  is changed into  id * id insert  *: driver inserts missing  *  and retries the production Can then continue here Pro: Can be automated Cons: Recovery not always intuitive id + * ( ) $ E E      T E R E      T E R synch synch E R E R      +   T   E R E R       E R       T T      F T R synch T      F T R synch synch T R insert   * T R       T R      *   F   T R T R       T R       F F      id synch synch F      (  E  ) synch synch
Error Productions E      T E R E R      +   T   E R  |     T      F T R T R      *   F   T R  |     F      (  E  )  |  id Add “ error production” : T R      F T R to ignore missing  * , e.g.:  id id Pro: Powerful recovery method Cons: Cannot be automated id + * ( ) $ E E      T E R E      T E R synch synch E R E R      +   T   E R E R       E R       T T      F T R synch T      F T R synch synch T R T R     F T R T R       T R      *   F   T R T R       T R       F F      id synch synch F      (  E  ) synch synch

More Related Content

What's hot (20)

LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Top down parsing(sid) (1)
Top down parsing(sid) (1)Top down parsing(sid) (1)
Top down parsing(sid) (1)
 
Compiler: Syntax Analysis
Compiler: Syntax AnalysisCompiler: Syntax Analysis
Compiler: Syntax Analysis
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Top down and botttom up Parsing
Top down     and botttom up ParsingTop down     and botttom up Parsing
Top down and botttom up Parsing
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Bottomupparser
BottomupparserBottomupparser
Bottomupparser
 
Parsing
ParsingParsing
Parsing
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 
Parsing
ParsingParsing
Parsing
 
Parsing
ParsingParsing
Parsing
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Parsing (Automata)
Parsing (Automata)Parsing (Automata)
Parsing (Automata)
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 

Similar to Ch4a

Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptFamiDan
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptSheikhMuhammadSaad3
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTFutureTechnologies3
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manualNitesh Dubey
 
RegularExpressions.pdf
RegularExpressions.pdfRegularExpressions.pdf
RegularExpressions.pdfImranBhatti58
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniquesd72994185
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptxvenkatapranaykumarGa
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfeliasabdi2024
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdfkenilpatel65
 
A Dimension Abstraction Approach to Vectorization in Matlab
A Dimension Abstraction Approach to Vectorization in MatlabA Dimension Abstraction Approach to Vectorization in Matlab
A Dimension Abstraction Approach to Vectorization in MatlabaiQUANT
 
Cgo2007 P3 3 Birkbeck
Cgo2007 P3 3 BirkbeckCgo2007 P3 3 Birkbeck
Cgo2007 P3 3 BirkbeckaiQUANT
 

Similar to Ch4a (20)

Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.ppt
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Ch03
Ch03Ch03
Ch03
 
lect 06 top down p2.ppt
lect 06 top down p2.pptlect 06 top down p2.ppt
lect 06 top down p2.ppt
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
LL(1) Parsers
LL(1) ParsersLL(1) Parsers
LL(1) Parsers
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
 
RegularExpressions.pdf
RegularExpressions.pdfRegularExpressions.pdf
RegularExpressions.pdf
 
CS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design TechniquesCS17604_TOP Parser Compiler Design Techniques
CS17604_TOP Parser Compiler Design Techniques
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
 
Ch02
Ch02Ch02
Ch02
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
 
A Dimension Abstraction Approach to Vectorization in Matlab
A Dimension Abstraction Approach to Vectorization in MatlabA Dimension Abstraction Approach to Vectorization in Matlab
A Dimension Abstraction Approach to Vectorization in Matlab
 
Cgo2007 P3 3 Birkbeck
Cgo2007 P3 3 BirkbeckCgo2007 P3 3 Birkbeck
Cgo2007 P3 3 Birkbeck
 
UNIT_-_II.docx
UNIT_-_II.docxUNIT_-_II.docx
UNIT_-_II.docx
 

More from kinnarshah8888 (16)

Yuva Msp All
Yuva Msp AllYuva Msp All
Yuva Msp All
 
Yuva Msp Intro
Yuva Msp IntroYuva Msp Intro
Yuva Msp Intro
 
Ch6
Ch6Ch6
Ch6
 
Ch9c
Ch9cCh9c
Ch9c
 
Ch8a
Ch8aCh8a
Ch8a
 
Ch5a
Ch5aCh5a
Ch5a
 
Ch9b
Ch9bCh9b
Ch9b
 
Ch9a
Ch9aCh9a
Ch9a
 
Ch10
Ch10Ch10
Ch10
 
Ch7
Ch7Ch7
Ch7
 
Ch2
Ch2Ch2
Ch2
 
Ch4b
Ch4bCh4b
Ch4b
 
Ch8b
Ch8bCh8b
Ch8b
 
Ch5b
Ch5bCh5b
Ch5b
 
Ch4c
Ch4cCh4c
Ch4c
 
Ch1
Ch1Ch1
Ch1
 

Recently uploaded

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 

Recently uploaded (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Ch4a

  • 1. Syntax Analysis Part I Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
  • 2. Position of a Parser in the Compiler Model Lexical Analyzer Parser and rest of front-end Source Program Token, tokenval Symbol Table Get next token Lexical error Syntax error Semantic error Intermediate representation
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11. Derivation (Example) Grammar G = ({ E }, { + , * , ( , ) , - , id }, P , E ) with productions P = E  E + E E  E * E E  ( E ) E  - E E  id E  - E  - id E  * E E  + id * id + id E  rm E + E  rm E + id  rm id + id Example derivations: E  * id + id
  • 12.
  • 13. Chomsky Hierarchy L ( regular )  L ( context free )  L ( context sensitive )  L ( unrestricted ) Where L ( T ) = { L ( G ) | G is of type T } That is: the set of all languages generated by grammars G of type T L 1 = { a n b n | n  1 } is context free L 2 = { a n b n c n | n  1 } is context sensitive Every finite language is regular! (construct a FSA for strings in L ( G )) Examples:
  • 14.
  • 15.
  • 16.
  • 17. A General Systematic Left Recursion Elimination Method Input: Grammar G with no cycles or  - productions Arrange the nonterminals in some order A 1 , A 2 , …, A n for i = 1, …, n do for j = 1, …, i -1 do replace each A i  A j  with A i   1  |  2  | … |  k  where A j   1 |  2 | … |  k enddo eliminate the immediate left recursion in A i enddo
  • 18. Immediate Left-Recursion Elimination Rewrite every left-recursive production A  A  |  |  | A  into a right-recursive production: A   A R |  A R A R   A R |  A R | 
  • 19. Example Left Recursion Elim. A  B C | a B  C A | A b C  A B | C C | a Choose arrangement: A , B , C i = 1: nothing to do i = 2, j = 1: B  C A | A b  B  C A | B C b | a b  (imm) B  C A B R | a b B R B R  C b B R |  i = 3, j = 1: C  A B | C C | a  C  B C B | a B | C C | a i = 3, j = 2: C  B C B | a B | C C | a  C  C A B R C B | a b B R C B | a B | C C | a  (imm) C  a b B R C B C R | a B C R | a C R C R  A B R C B C R | C C R | 
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25. Non-LL(1) Examples Grammar Not LL(1) because: S  S a | a Left recursive S  a S | a FIRST( a S )  FIRST( a )   S  a R |  R  S |  For R : S  *  and   *  S  a R a R  S |  For R : FIRST( S )  FOLLOW( R )  
  • 26.
  • 27. Using FIRST and FOLLOW in a Recursive-Descent Parser expr  term rest rest  + term rest | - term rest |  term  id procedure rest (); begin if lookahead in FIRST( + term rest ) then match (‘ + ’); term (); rest () else if lookahead in FIRST( - term rest ) then match (‘ - ’); term (); rest () else if lookahead in FOLLOW( rest ) then return else error () end ; FIRST( + term rest ) = { + } FIRST( - term rest ) = { - } FOLLOW( rest ) = { $ }
  • 28.
  • 29. Constructing an LL(1) Predictive Parsing Table for each production A   do for each a  FIRST(  ) do add A   to M [ A , a ] enddo if   FIRST(  ) then for each b  FOLLOW( A ) do add A   to M [ A , b ] enddo endif enddo Mark each undefined entry in M error
  • 30. Example Table E  T E R E R  + T E R |  T  F T R T R  * F T R |  F  ( E ) | id id + * ( ) $ E E  T E R E  T E R E R E R  + T E R E R   E R   T T  F T R T  F T R T R T R   T R  * F T R T R   T R   F F  id F  ( E ) A   FIRST(  ) FOLLOW( A ) E  T E R ( id $ ) E R  + T E R + $ ) E R    $ ) T  F T R ( id + $ ) T R  * F T R * + $ ) T R    + $ ) F  ( E ) ( * + $ ) F  id id * + $ )
  • 31. LL(1) Grammars are Unambiguous Ambiguous grammar S  i E t S S R | a S R  e S |  E  b Error: duplicate table entry a b e i t $ S S  a S  i E t S S R S R S R   S R  e S S R   E E  b A   FIRST(  ) FOLLOW( A ) S  i E t S S R i e $ S  a a e $ S R  e S e e $ S R    e $ E  b b t
  • 32. Predictive Parsing Program (Driver) push( $ ) push( S ) a := lookahead repeat X := pop() if X is a terminal or X = $ then match( X ) // moves to next token and a := lookahead else if M [ X , a ] = X  Y 1 Y 2 … Y k then push( Y k , Y k -1 , …, Y 2 , Y 1 ) // such that Y 1 is on top … invoke actions and/or produce IR output … else error() endif until X = $
  • 33. Example Table-Driven Parsing Stack $ E $ E R T $ E R T R F $ E R T R id $ E R T R $ E R $ E R T + $ E R T $ E R T R F $ E R T R id $ E R T R $ E R T R F * $ E R T R F $ E R T R id $ E R T R $ E R $ Input id +id*id$ id +id*id$ id +id*id$ id +id*id$ + id*id$ + id*id$ + id*id$ id *id$ id *id$ id *id$ * id$ * id$ id $ id $ $ $ $ Production applied E  T E R T  F T R F  id T R   E R  + T E R T  F T R F  id T R  * F T R F  id T R   E R  
  • 34. Panic Mode Recovery FOLLOW( E ) = { ) $ } FOLLOW( E R ) = { ) $ } FOLLOW( T ) = { + ) $ } FOLLOW( T R ) = { + ) $ } FOLLOW( F ) = { + * ) $ } Add synchronizing actions to undefined entries based on FOLLOW synch : the driver pops current nonterminal A and skips input tillsynch token or skips input until one of FIRST( A ) is found Pro: Can be automated Cons: Error messages are needed id + * ( ) $ E E  T E R E  T E R synch synch E R E R  + T E R E R   E R   T T  F T R synch T  F T R synch synch T R T R   T R  * F T R T R   T R   F F  id synch synch F  ( E ) synch synch
  • 35. Phrase-Level Recovery Change input stream by inserting missing tokens For example: id id is changed into id * id insert *: driver inserts missing * and retries the production Can then continue here Pro: Can be automated Cons: Recovery not always intuitive id + * ( ) $ E E  T E R E  T E R synch synch E R E R  + T E R E R   E R   T T  F T R synch T  F T R synch synch T R insert * T R   T R  * F T R T R   T R   F F  id synch synch F  ( E ) synch synch
  • 36. Error Productions E  T E R E R  + T E R |  T  F T R T R  * F T R |  F  ( E ) | id Add “ error production” : T R  F T R to ignore missing * , e.g.: id id Pro: Powerful recovery method Cons: Cannot be automated id + * ( ) $ E E  T E R E  T E R synch synch E R E R  + T E R E R   E R   T T  F T R synch T  F T R synch synch T R T R  F T R T R   T R  * F T R T R   T R   F F  id synch synch F  ( E ) synch synch