SlideShare a Scribd company logo
Lex and Yacc
COP - 3402
General Compiler Infra-structure
Scanner
(tokenizer)
Parser Semantic
Routines
Analysis/
Transformations/
optimizations
Code
Generator
Program source
(stream of
characters)
Tokens
Syntactic
Structure
IR: Intermediate
Representation (1)
Assembly code
IR: Intermediate
Representation (2)
Symbol and
Attribute Tables
Lex
Yacc
Lex & Yacc
β€’ Lex
– generates C code for the lexical analyzer (scanner)
– Token patterns specified by regular expressions
β€’ Yacc
– generates C code for a LR(1) syntax analyzer (parser)
– BNF rules for the grammar
Lex
ο‚— lex is a program (generator) that generates lexical analyzers,
(widely used on Unix).
ο‚— It is mostly used with Yacc parser generator.
ο‚— Written by Eric Schmidt and Mike Lesk.
ο‚— It reads the input stream (specifying the lexical analyzer ) and
outputs source code implementing the lexical analyzer in the C
programming language.
ο‚— Lex will read patterns (regular expressions); then produces C
code for a lexical analyzer that scans for identifiers.
Lex predefined variables and
functions
Lex – Pattern Matching
Primitives
Lex – Pattern Matching
Examples
Example: Simple Calculator
β€’ Computes the basic arithmetic operations
β€’ Allows declaration of variables
β€’ Enough to illustrate the basic structure of
Lex and Yacc programs
Lex program structure
… definitions …
%%
… rules …
%%
… subroutines …
%{
#include <stdio.h>
#include "y.tab.h"
int c;
extern int yylval;
%}
%%
" " ;
[a-z] { c = yytext[0]; yylval = c - 'a';
return(LETTER); }
[0-9]* { yylval = atoi(yytext);
return(NUMBER); }
[^a-z0-9b] { c = yytext[0]; return(c); }
Pattern Matching and Action
[a-z] { c = yytext[0]; yylval = c - 'a';
return(LETTER); }
[0-9]* { yylval = atoi(yytext);
return(NUMBER); }
Match a character
in the a-z range
Match a positive integer
(sequence of 0-9 digits)
Place the offset c – β€˜a’
In the stack
Place the integer value
In the stack
Buffer
Yacc
β€’ Grammars described by rules using a
variant of the Backus Naur Form (BNF)
– Context-free grammars
β€’ LALR(1) parse table is generated
automatically based on the rules
β€’ Actions are added to the rules and
executed after each reduction
Yacc Program Structure
%{
#include <stdio.h>
int regs[26];
int base;
%}
%token NUMBER LETTER
%left '+' '-β€˜
%left '*' '/β€˜
%%
list: | list stat 'n' |list error 'n' {yyerrok;} ;
stat: expr {printf("%dn",$1);}
| LETTER '=' expr {regs[$1] = $3;};
expr:
'(' expr ')' {$$ = $2;} |
expr '+' expr {$$ = $1 + $3;} |
LETTER {$$ = regs[$1];}
%%
main(){return(yyparse());}
yyerror(CHAR *s){fprintf(stderr, "%sn",s);}
yywrap(){ return(1);}
… definitions …
%%
… rules …
%%
… subroutines …
Rule Reduction and Action
stat: expr {printf("%dn",$1);}
| LETTER '=' expr {regs[$1] = $3;};
expr:
expr '+' expr {$$ = $1 + $3;} |
LETTER {$$ = regs[$1];}
Grammar rule Action
β€œor” operator:
For multiple RHS
Further reading…
οƒ˜
β€œA Compact Guide to Lex & Yacc”,
Thomas Niemann (recommended);
οƒ˜
β€œLex & Yacc”, Doug Brown (O’Reily);
οƒ˜
Lots of resources on the web
β€’ Check our website for some suggestions
Conclusions
β€’ Yacc and Lex are very helpful for building
the compiler front-end
β€’ A lot of time is saved when compared to
hand-implementation of parser and
scanner
β€’ They both work as a mixture of β€œrules” and
β€œC code”
β€’ C code is generated and is merged with
the rest of the compiler code

More Related Content

PPT
yacc installation & sample program exe.ppt
PPT
Module4 lex and yacc.ppt
PPT
Lex and Yacc ppt
PPT
Lex and Yacc Tool M1.ppt
PDF
Compilers Design
PPT
Yacc lex
PPTX
Lexical Analyzer Implementation
PDF
Handout#03
yacc installation & sample program exe.ppt
Module4 lex and yacc.ppt
Lex and Yacc ppt
Lex and Yacc Tool M1.ppt
Compilers Design
Yacc lex
Lexical Analyzer Implementation
Handout#03

Similar to Compiler Design Brief in Computer Application (20)

PPT
compiler Design laboratory lex and yacc tutorial
PDF
Handout#02
PPT
Lexical analyzer
PPTX
Compiler Design_Syntax Analyzer_Yaac Tool.pptx
PPTX
module 4.pptx
PPT
LEX lexical analyzer for compiler theory.ppt
PPT
Compiler Design Tutorial
PPT
Chapter-2-lexical-analyser and its property lecture note.ppt
PPTX
CD U1-5.pptx
PPTX
Lex & yacc
PPT
Unit1.ppt
PPT
LexicalAnalysis in Compiler design .pt
PPT
system software
DOCX
Yacc topic beyond syllabus
PPTX
Lexical Analyser PPTs for Third Lease Computer Sc. and Engineering
PPT
Cpcs302 1
PDF
Lexical
PDF
Lex and Yacc.pdf
PPTX
Ch 2.pptx
Β 
compiler Design laboratory lex and yacc tutorial
Handout#02
Lexical analyzer
Compiler Design_Syntax Analyzer_Yaac Tool.pptx
module 4.pptx
LEX lexical analyzer for compiler theory.ppt
Compiler Design Tutorial
Chapter-2-lexical-analyser and its property lecture note.ppt
CD U1-5.pptx
Lex & yacc
Unit1.ppt
LexicalAnalysis in Compiler design .pt
system software
Yacc topic beyond syllabus
Lexical Analyser PPTs for Third Lease Computer Sc. and Engineering
Cpcs302 1
Lexical
Lex and Yacc.pdf
Ch 2.pptx
Β 
Ad

Recently uploaded (20)

PDF
Tenda Login Guide: Access Your Router in 5 Easy Steps
PPT
Design_with_Watersergyerge45hrbgre4top (1).ppt
PDF
Introduction to the IoT system, how the IoT system works
PDF
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
PDF
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
PPTX
Internet___Basics___Styled_ presentation
PPT
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
PDF
An introduction to the IFRS (ISSB) Stndards.pdf
PDF
Exploring VPS Hosting Trends for SMBs in 2025
PPTX
E -tech empowerment technologies PowerPoint
PDF
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
PPTX
presentation_pfe-universite-molay-seltan.pptx
DOCX
Unit-3 cyber security network security of internet system
PPTX
Power Point - Lesson 3_2.pptx grad school presentation
Β 
PDF
SASE Traffic Flow - ZTNA Connector-1.pdf
PPT
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
PPTX
Job_Card_System_Styled_lorem_ipsum_.pptx
PPTX
artificial intelligence overview of it and more
PPTX
INTERNET------BASICS-------UPDATED PPT PRESENTATION
PDF
Sims 4 Historia para lo sims 4 para jugar
Tenda Login Guide: Access Your Router in 5 Easy Steps
Design_with_Watersergyerge45hrbgre4top (1).ppt
Introduction to the IoT system, how the IoT system works
πŸ’° π”πŠπ“πˆ πŠπ„πŒπ„ππ€ππ†π€π πŠπˆππ„π‘πŸ’πƒ π‡π€π‘πˆ 𝐈𝐍𝐈 πŸπŸŽπŸπŸ“ πŸ’°
Β 
FINAL CALL-6th International Conference on Networks & IOT (NeTIOT 2025)
Internet___Basics___Styled_ presentation
isotopes_sddsadsaadasdasdasdasdsa1213.ppt
An introduction to the IFRS (ISSB) Stndards.pdf
Exploring VPS Hosting Trends for SMBs in 2025
E -tech empowerment technologies PowerPoint
Automated vs Manual WooCommerce to Shopify Migration_ Pros & Cons.pdf
presentation_pfe-universite-molay-seltan.pptx
Unit-3 cyber security network security of internet system
Power Point - Lesson 3_2.pptx grad school presentation
Β 
SASE Traffic Flow - ZTNA Connector-1.pdf
FIRE PREVENTION AND CONTROL PLAN- LUS.FM.MQ.OM.UTM.PLN.00014.ppt
Job_Card_System_Styled_lorem_ipsum_.pptx
artificial intelligence overview of it and more
INTERNET------BASICS-------UPDATED PPT PRESENTATION
Sims 4 Historia para lo sims 4 para jugar
Ad

Compiler Design Brief in Computer Application

  • 2. General Compiler Infra-structure Scanner (tokenizer) Parser Semantic Routines Analysis/ Transformations/ optimizations Code Generator Program source (stream of characters) Tokens Syntactic Structure IR: Intermediate Representation (1) Assembly code IR: Intermediate Representation (2) Symbol and Attribute Tables Lex Yacc
  • 3. Lex & Yacc β€’ Lex – generates C code for the lexical analyzer (scanner) – Token patterns specified by regular expressions β€’ Yacc – generates C code for a LR(1) syntax analyzer (parser) – BNF rules for the grammar
  • 4. Lex ο‚— lex is a program (generator) that generates lexical analyzers, (widely used on Unix). ο‚— It is mostly used with Yacc parser generator. ο‚— Written by Eric Schmidt and Mike Lesk. ο‚— It reads the input stream (specifying the lexical analyzer ) and outputs source code implementing the lexical analyzer in the C programming language. ο‚— Lex will read patterns (regular expressions); then produces C code for a lexical analyzer that scans for identifiers.
  • 5. Lex predefined variables and functions
  • 6. Lex – Pattern Matching Primitives
  • 7. Lex – Pattern Matching Examples
  • 8. Example: Simple Calculator β€’ Computes the basic arithmetic operations β€’ Allows declaration of variables β€’ Enough to illustrate the basic structure of Lex and Yacc programs
  • 9. Lex program structure … definitions … %% … rules … %% … subroutines … %{ #include <stdio.h> #include "y.tab.h" int c; extern int yylval; %} %% " " ; [a-z] { c = yytext[0]; yylval = c - 'a'; return(LETTER); } [0-9]* { yylval = atoi(yytext); return(NUMBER); } [^a-z0-9b] { c = yytext[0]; return(c); }
  • 10. Pattern Matching and Action [a-z] { c = yytext[0]; yylval = c - 'a'; return(LETTER); } [0-9]* { yylval = atoi(yytext); return(NUMBER); } Match a character in the a-z range Match a positive integer (sequence of 0-9 digits) Place the offset c – β€˜a’ In the stack Place the integer value In the stack Buffer
  • 11. Yacc β€’ Grammars described by rules using a variant of the Backus Naur Form (BNF) – Context-free grammars β€’ LALR(1) parse table is generated automatically based on the rules β€’ Actions are added to the rules and executed after each reduction
  • 12. Yacc Program Structure %{ #include <stdio.h> int regs[26]; int base; %} %token NUMBER LETTER %left '+' '-β€˜ %left '*' '/β€˜ %% list: | list stat 'n' |list error 'n' {yyerrok;} ; stat: expr {printf("%dn",$1);} | LETTER '=' expr {regs[$1] = $3;}; expr: '(' expr ')' {$$ = $2;} | expr '+' expr {$$ = $1 + $3;} | LETTER {$$ = regs[$1];} %% main(){return(yyparse());} yyerror(CHAR *s){fprintf(stderr, "%sn",s);} yywrap(){ return(1);} … definitions … %% … rules … %% … subroutines …
  • 13. Rule Reduction and Action stat: expr {printf("%dn",$1);} | LETTER '=' expr {regs[$1] = $3;}; expr: expr '+' expr {$$ = $1 + $3;} | LETTER {$$ = regs[$1];} Grammar rule Action β€œor” operator: For multiple RHS
  • 14. Further reading… οƒ˜ β€œA Compact Guide to Lex & Yacc”, Thomas Niemann (recommended); οƒ˜ β€œLex & Yacc”, Doug Brown (O’Reily); οƒ˜ Lots of resources on the web β€’ Check our website for some suggestions
  • 15. Conclusions β€’ Yacc and Lex are very helpful for building the compiler front-end β€’ A lot of time is saved when compared to hand-implementation of parser and scanner β€’ They both work as a mixture of β€œrules” and β€œC code” β€’ C code is generated and is merged with the rest of the compiler code