SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais de 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
 

Último

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 
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
 
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
 
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
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
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
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 

Último (20)

presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
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, ...
 
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
 
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
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 

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