SlideShare ist ein Scribd-Unternehmen logo
1 von 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

Weitere ähnliche Inhalte

Was ist angesagt? (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
 

Ähnlich wie 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
 

Ähnlich wie 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
 

Mehr von 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
 

Kürzlich hochgeladen

FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...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
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 

Kürzlich hochgeladen (20)

FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
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...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
+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...
 
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
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 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, ...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

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