SlideShare una empresa de Scribd logo
1 de 13
Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
ANTLR, Yacc, and Bison ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
Creating an LALR(1) Parser with Yacc/Bison Yacc or Bison compiler yacc specification yacc.y y.tab.c input stream C compiler a.out output stream y.tab.c a.out
Yacc Specification ,[object Object],[object Object]
Writing a Grammar in Yacc ,[object Object],[object Object],[object Object]
Synthesized Attributes ,[object Object],[object Object],[object Object],[object Object],factor .val= x expr .val= x ) ( $$=$2
Example 1 %{ #include <ctype.h> %} %token DIGIT %% line : expr ‘’ { printf(“%d”, $1); } ; expr : expr ‘+’ term { $$ = $1 + $3; } | term { $$ = $1; } ; term : term ‘*’ factor { $$ = $1 * $3; } | factor { $$ = $1; } ; factor : ‘(’ expr ‘)’ { $$ = $2; } | DIGIT { $$ = $1; } ; %% int yylex() { int c = getchar();   if (isdigit(c))   { yylval = c-’0’;   return DIGIT;   }   return c; } Also results in definition of #define DIGIT xxx Attribute of token (stored in  yylval ) Attribute of term  (parent) Attribute of  factor  (child) Example of a very crude lexical analyzer invoked by the parser
Dealing With Ambiguous Grammars ,[object Object],[object Object]
Example 2 %{ #include <ctype.h> #include <stdio.h> #define YYSTYPE double %} %token NUMBER %left ‘+’ ‘-’ %left ‘*’ ‘/’ %right UMINUS %% lines : lines expr ‘’  { printf(“%g”, $2); } | lines ‘’ | /* empty */ ; expr : expr ‘+’ expr  { $$ = $1 + $3; } | expr ‘-’ expr  { $$ = $1 - $3; } | expr ‘*’ expr  { $$ = $1 * $3; } | expr ‘/’ expr  { $$ = $1 / $3; } | ‘(’ expr ‘)’  { $$ = $2; } | ‘-’ expr %prec UMINUS { $$ = -$2; } | NUMBER ; %% Double type for attributes and  yylval
Example 2 (cont’d) %% int yylex() { int c;   while ((c = getchar()) == ‘ ‘)   ;   if ((c == ‘.’) || isdigit(c))   { ungetc(c, stdin);   scanf(“%lf”, &yylval);   return NUMBER;   }   return c; } int main() { if (yyparse() != 0) fprintf(stderr, “Abnormal exit”); return 0; } int yyerror(char *s) { fprintf(stderr, “Error: %s”, s); } Run the parser Crude lexical analyzer for fp doubles and arithmetic operators Invoked by parser to report parse errors
Combining Lex/Flex with Yacc/Bison Yacc or Bison compiler yacc specification yacc.y lex.yy.c y.tab.c input stream C compiler a.out output stream y.tab.c y.tab.h a.out Lex or Flex compiler Lex specification lex.l and token definitions y.tab.h lex.yy.c
Lex Specification for Example 2 %option noyywrap %{ #include “y.tab.h” extern double yylval; %} number [0-9]+?|[0-9]*[0-9]+ %% [ ] { /* skip blanks */ } {number} { sscanf(yytext, “%lf”, &yylval);   return NUMBER; } |. { return yytext[0]; } Generated by Yacc, contains #define NUMBER xxx yacc -d example2.y lex example2.l gcc y.tab.c lex.yy.c ./a.out bison -d -y example2.y flex example2.l gcc y.tab.c lex.yy.c ./a.out Defined in  y.tab.c
Error Recovery in Yacc %{ … %} … %% lines : lines expr ‘’ { printf(“%g”, $2; } | lines ‘’ | /* empty */ | error ‘’ { yyerror(“reenter last line: ”);   yyerrok; } ; … Reset parser to normal mode Error production: set error mode and skip input until newline

Más contenido relacionado

La actualidad más candente

Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc pptpssraikar
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manualSami Said
 
More on Lex
More on LexMore on Lex
More on LexTech_MX
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabusJK Knowledge
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lexAnusuya123
 
system software
system software system software
system software randhirlpu
 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationEelco Visser
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesEelco Visser
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysisraosir123
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming LanguageSteve Johnson
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)bolovv
 
Compiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor ServicesCompiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor ServicesEelco Visser
 
Programming languages
Programming languagesProgramming languages
Programming languagesEelco Visser
 

La actualidad más candente (20)

LEX & YACC TOOL
LEX & YACC TOOLLEX & YACC TOOL
LEX & YACC TOOL
 
Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
 
Lexyacc
LexyaccLexyacc
Lexyacc
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
More on Lex
More on LexMore on Lex
More on Lex
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabus
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
 
Lex & yacc
Lex & yaccLex & yacc
Lex & yacc
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
system software
system software system software
system software
 
Yacc
YaccYacc
Yacc
 
Compiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code GenerationCompiler Construction | Lecture 13 | Code Generation
Compiler Construction | Lecture 13 | Code Generation
 
Lex
LexLex
Lex
 
Compiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual MachinesCompiler Construction | Lecture 12 | Virtual Machines
Compiler Construction | Lecture 12 | Virtual Machines
 
02. chapter 3 lexical analysis
02. chapter 3   lexical analysis02. chapter 3   lexical analysis
02. chapter 3 lexical analysis
 
Learn c++ Programming Language
Learn c++ Programming LanguageLearn c++ Programming Language
Learn c++ Programming Language
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
Compiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor ServicesCompiler Construction | Lecture 3 | Syntactic Editor Services
Compiler Construction | Lecture 3 | Syntactic Editor Services
 
C++
C++C++
C++
 
Programming languages
Programming languagesProgramming languages
Programming languages
 

Destacado (6)

Flex y Bison
Flex y BisonFlex y Bison
Flex y Bison
 
Cimplementation
CimplementationCimplementation
Cimplementation
 
Chapter Five(2)
Chapter Five(2)Chapter Five(2)
Chapter Five(2)
 
System Programming Unit IV
System Programming Unit IVSystem Programming Unit IV
System Programming Unit IV
 
Parsing example
Parsing exampleParsing example
Parsing example
 
Compiler design lab programs
Compiler design lab programs Compiler design lab programs
Compiler design lab programs
 

Similar a Ch4c (20)

Ch4c.ppt
Ch4c.pptCh4c.ppt
Ch4c.ppt
 
Compiler Design Lab File
Compiler Design Lab FileCompiler Design Lab File
Compiler Design Lab File
 
2. operator
2. operator2. operator
2. operator
 
Funções, Scanf, EOF
Funções, Scanf, EOFFunções, Scanf, EOF
Funções, Scanf, EOF
 
Intro to c chapter cover 1 4
Intro to c chapter cover 1 4Intro to c chapter cover 1 4
Intro to c chapter cover 1 4
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
module 4.pptx
module 4.pptxmodule 4.pptx
module 4.pptx
 
Functions
FunctionsFunctions
Functions
 
Basics of c
Basics of cBasics of c
Basics of c
 
Assignment2
Assignment2Assignment2
Assignment2
 
Lecture 3 and 4.pptx
Lecture 3 and 4.pptxLecture 3 and 4.pptx
Lecture 3 and 4.pptx
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Functions
FunctionsFunctions
Functions
 
functions
functionsfunctions
functions
 
introduction to c programming and C History.pptx
introduction to c programming and C History.pptxintroduction to c programming and C History.pptx
introduction to c programming and C History.pptx
 
C tutorial
C tutorialC tutorial
C tutorial
 
C Programming Example
C Programming Example C Programming Example
C Programming Example
 
Functions
FunctionsFunctions
Functions
 
Unit2 C
Unit2 C Unit2 C
Unit2 C
 

Más de kinnarshah8888 (17)

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
 
Ch3
Ch3Ch3
Ch3
 
Ch2
Ch2Ch2
Ch2
 
Ch4b
Ch4bCh4b
Ch4b
 
Ch4a
Ch4aCh4a
Ch4a
 
Ch8b
Ch8bCh8b
Ch8b
 
Ch5b
Ch5bCh5b
Ch5b
 
Ch1
Ch1Ch1
Ch1
 

Último

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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
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
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
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
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 

Último (20)

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
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
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
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
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
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 

Ch4c

  • 1. Syntax Analysis Part III Chapter 4 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009
  • 2.
  • 3. Creating an LALR(1) Parser with Yacc/Bison Yacc or Bison compiler yacc specification yacc.y y.tab.c input stream C compiler a.out output stream y.tab.c a.out
  • 4.
  • 5.
  • 6.
  • 7. Example 1 %{ #include <ctype.h> %} %token DIGIT %% line : expr ‘’ { printf(“%d”, $1); } ; expr : expr ‘+’ term { $$ = $1 + $3; } | term { $$ = $1; } ; term : term ‘*’ factor { $$ = $1 * $3; } | factor { $$ = $1; } ; factor : ‘(’ expr ‘)’ { $$ = $2; } | DIGIT { $$ = $1; } ; %% int yylex() { int c = getchar(); if (isdigit(c)) { yylval = c-’0’; return DIGIT; } return c; } Also results in definition of #define DIGIT xxx Attribute of token (stored in yylval ) Attribute of term (parent) Attribute of factor (child) Example of a very crude lexical analyzer invoked by the parser
  • 8.
  • 9. Example 2 %{ #include <ctype.h> #include <stdio.h> #define YYSTYPE double %} %token NUMBER %left ‘+’ ‘-’ %left ‘*’ ‘/’ %right UMINUS %% lines : lines expr ‘’ { printf(“%g”, $2); } | lines ‘’ | /* empty */ ; expr : expr ‘+’ expr { $$ = $1 + $3; } | expr ‘-’ expr { $$ = $1 - $3; } | expr ‘*’ expr { $$ = $1 * $3; } | expr ‘/’ expr { $$ = $1 / $3; } | ‘(’ expr ‘)’ { $$ = $2; } | ‘-’ expr %prec UMINUS { $$ = -$2; } | NUMBER ; %% Double type for attributes and yylval
  • 10. Example 2 (cont’d) %% int yylex() { int c; while ((c = getchar()) == ‘ ‘) ; if ((c == ‘.’) || isdigit(c)) { ungetc(c, stdin); scanf(“%lf”, &yylval); return NUMBER; } return c; } int main() { if (yyparse() != 0) fprintf(stderr, “Abnormal exit”); return 0; } int yyerror(char *s) { fprintf(stderr, “Error: %s”, s); } Run the parser Crude lexical analyzer for fp doubles and arithmetic operators Invoked by parser to report parse errors
  • 11. Combining Lex/Flex with Yacc/Bison Yacc or Bison compiler yacc specification yacc.y lex.yy.c y.tab.c input stream C compiler a.out output stream y.tab.c y.tab.h a.out Lex or Flex compiler Lex specification lex.l and token definitions y.tab.h lex.yy.c
  • 12. Lex Specification for Example 2 %option noyywrap %{ #include “y.tab.h” extern double yylval; %} number [0-9]+?|[0-9]*[0-9]+ %% [ ] { /* skip blanks */ } {number} { sscanf(yytext, “%lf”, &yylval); return NUMBER; } |. { return yytext[0]; } Generated by Yacc, contains #define NUMBER xxx yacc -d example2.y lex example2.l gcc y.tab.c lex.yy.c ./a.out bison -d -y example2.y flex example2.l gcc y.tab.c lex.yy.c ./a.out Defined in y.tab.c
  • 13. Error Recovery in Yacc %{ … %} … %% lines : lines expr ‘’ { printf(“%g”, $2; } | lines ‘’ | /* empty */ | error ‘’ { yyerror(“reenter last line: ”); yyerrok; } ; … Reset parser to normal mode Error production: set error mode and skip input until newline