SlideShare a Scribd company logo
By
Dr. Prem Nath
Associate Professor
Computer Science & Engineering Department,
H N B Garhwal University
(A Central University)
Lex And Yacc
1
Outline
▪ Lex:
▪ Theory
▪ Execution
▪ Example
▪ Yacc:
▪ Theory
▪ Description
▪ Example
▪ Lex & Yacc linking.
2
Lex
 Lex is a Programming Language or Tool that generates lexical
analyzers, (widely used on Unix with vi editor).
 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.
3
4
LEX…
A Language for Specifying Lexical Analyzers
• Not only a table generator, but also allows “actions”
to associate with RE’s.
• Lex is widely used in the Unix community
• Lex is not efficient enough for production
compilers, however.
Lex…
Dr. Prem Nath 5
General Information:
• Input is stored in a file with *.l extension
• File consists of three main sections
• lex generates C function stored in lex.yy.c
Using Lex:
1) Specify words to be used as tokens (Extension of regular
expressions)
2) Run the lex utility on the source file to generate yylex( ),
a C function
3) Declares global variables char* yytext and int yyleng
6
Lex…
Lex
compiler
Lex
source
program
lex.l
lex.yy.c
C
compiler
lex.yy.c a.out
a.out
Input
stream
sequence
of tokens
Structure of Lex
Dr. Prem Nath 7
➢ Declaration (Variables, Constants, etc.)
➢ Translation Rules (Pattern and Action)
➢ Auxiliary Functions
/* Declaration
%%
/* Translation Rules
%%
Auxiliary Functions
Lex…
❑ Declaration
%{
#include<stdio.h>
int a, b;
%}
❑ Translation Rules
%%
Pattern1 {Action1}
Pattern2 {Action2}
%%
❑ Auxiliary Functions
int main()
{
…
yywrap();
} Dr. Prem Nath 8
Lex…
A simple pattern: letter(letter|digit)*
 Regular expressions are translated by Lex to a computer program that
mimics an FA.
 This pattern matches a string of characters that begins with a single
letter followed by zero or more letters or digits.
9
Lex…
 Some limitations, Lex cannot be used to recognize nested structures such
as parentheses, since it only has states and transitions between states.
 So, Lex is good at pattern matching, while Yacc is for more challenging
tasks.
10
Lex…
Pattern Matching Primitives
11
Lex…
Pattern Matching examples:
12
Lex…
Lex predefined variables:
13
Lex…
 Whitespace must separate the defining term and the associated expression.
 Code in the definitions section is simply copied as-is to the top of the generated C file and
must be bracketed with “%{“ and “%}” markers.
 substitutions in the rules section are surrounded by braces ({letter}) to distinguish them
from literals.
14
Lex…
/* Lex Program to Recognize Id, Keywords, and Number
%{ #include<stdio.h>
letter [a-z A-Z]
digit [0-9] %}
%%
id {letter}({letter}I{letter})*
number {digit}+(.{digit})+?(E[+, -]?{digit})+
%%
/* Auxiliary Functions
{id} {printf(“%s is an Identifier”, yytext);}
{if} {printf(“%s is a Keyword”, yytext);}
{number} {printf(“%s is a Number”, yytext);}
Dr. Prem Nath 15
Yacc
➢ Yacc reads the grammar and generate C code for a
parser .
➢ Parser Generator (Compiler to Compiler)
➢ Files are with .y extension
◦ Grammars written in Backus Naur Form (BNF)
◦ BNF grammar used to express context-free languages .
◦ e.g. to parse an expression , do reverse operation(
reducing the expression)
◦ LALR(1) Parser (Look Ahead Left to Right One)
◦ Using stack for storing (LIFO)
◦ Written by Stefen C Johnson
16
Yacc…
Input to Yacc is divided into three sections.
... definitions ...
%%
... rules ...
%%
... subroutines ...
17
Yacc…
 The definitions section consists of:
◦ token declarations .
◦ C code bracketed by “%{“ and “%}”.
◦ the rules section consists of:
 BNF grammar .
 the subroutines section consists of:
◦ user subroutines .
18
Linking lex and Yacc
19
CC: C Compiler
20
Linking lex and Yacc…
Yacc
Compiler
Lex
a.out
parse.y
y.tab.c
(yyparse)
scan.l
source
program
output
C
Compiler
lex.yy.c
a.out
Included
Linking lex and Yacc…
21
• yyval: Vallue Associated With Token
• yytext: Pointer to Input String
Linking lex and Yacc…
22
$$: First Symbol of the Body
$i: ith Symbol of the Body
Linking lex and Yacc…
❑Construct a Lexical Analyzer and Parser for
Language L={anbn: n ≥1}.
/* Lex Tool ab.l
%{
#include<y.tab.h>
%}
%%
[a] {return a;}
[b] {return b;}
[n] {return 0;}
%%
/* No Main Function
Dr. Prem Nath 23
Linking lex and Yacc…
/* Yacc Tool cd.y
%{
#include<stdio.h>
#include<stdlib.h>
%}
% token a b
%%
start: S ‘n’ {return 0;}
S: aSb {$S=$1$2;}
%%
Dr. Prem Nath 24
Linking lex and Yacc…
/* Yacc Tool CD.y
/* Main Function
int main( )
{
printf(“Enter a String:”);
if(yyparse= = 0)
printf(“Valid String”);
yyerror( )
{
printf(“Invalid String”);
exit(0);
}
int yywrap( )
{
return 1;
}
}
Dr. Prem Nath 25
26
Thanks

More Related Content

Similar to Lex and Yacc.pdf

Viva
VivaViva
Handout#02
Handout#02Handout#02
Handout#02
Sunita Milind Dol
 
Lex programming
Lex programmingLex programming
Lex programming
sureshmoharana2013
 
LANGUAGE PROCESSOR
LANGUAGE PROCESSORLANGUAGE PROCESSOR
LANGUAGE PROCESSOR
EZIOAUDITORE15070
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
Anujashejwal
 
11700220036.pdf
11700220036.pdf11700220036.pdf
11700220036.pdf
SouvikRoy149
 
Compiler design Project
Compiler design ProjectCompiler design Project
Compiler design Project
DushyantSharma146
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
Archana Gopinath
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyser
abhishek gupta
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabus
JK Knowledge
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
MashaelQ
 
CD UNIT-1.3 LEX PPT.pptx
CD UNIT-1.3 LEX PPT.pptxCD UNIT-1.3 LEX PPT.pptx
CD UNIT-1.3 LEX PPT.pptx
VamsiReddyHere
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
Ashwini Sonawane
 
Yacc lex
Yacc lexYacc lex
Yacc lex
915086731
 
Group 19 CD project
Group 19 CD projectGroup 19 CD project
Group 19 CD project
DushyantSharma146
 
Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
pssraikar
 

Similar to Lex and Yacc.pdf (20)

Viva
VivaViva
Viva
 
Viva
VivaViva
Viva
 
Handout#02
Handout#02Handout#02
Handout#02
 
Lex programming
Lex programmingLex programming
Lex programming
 
LANGUAGE PROCESSOR
LANGUAGE PROCESSORLANGUAGE PROCESSOR
LANGUAGE PROCESSOR
 
Lexical Analyzers and Parsers
Lexical Analyzers and ParsersLexical Analyzers and Parsers
Lexical Analyzers and Parsers
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
11700220036.pdf
11700220036.pdf11700220036.pdf
11700220036.pdf
 
Compiler design Project
Compiler design ProjectCompiler design Project
Compiler design Project
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
 
Compiler design and lexical analyser
Compiler design and lexical analyserCompiler design and lexical analyser
Compiler design and lexical analyser
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabus
 
Lexyacc
LexyaccLexyacc
Lexyacc
 
Lexyacc
LexyaccLexyacc
Lexyacc
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
CD UNIT-1.3 LEX PPT.pptx
CD UNIT-1.3 LEX PPT.pptxCD UNIT-1.3 LEX PPT.pptx
CD UNIT-1.3 LEX PPT.pptx
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Yacc lex
Yacc lexYacc lex
Yacc lex
 
Group 19 CD project
Group 19 CD projectGroup 19 CD project
Group 19 CD project
 
Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
 

Recently uploaded

Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
Pipe Restoration Solutions
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
Jayaprasanna4
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
Vijay Dialani, PhD
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
ongomchris
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
MLILAB
 

Recently uploaded (20)

Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
The Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdfThe Benefits and Techniques of Trenchless Pipe Repair.pdf
The Benefits and Techniques of Trenchless Pipe Repair.pdf
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
ethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.pptethical hacking-mobile hacking methods.ppt
ethical hacking-mobile hacking methods.ppt
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
ML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptxML for identifying fraud using open blockchain data.pptx
ML for identifying fraud using open blockchain data.pptx
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
space technology lecture notes on satellite
space technology lecture notes on satellitespace technology lecture notes on satellite
space technology lecture notes on satellite
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
H.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdfH.Seo,  ICLR 2024, MLILAB,  KAIST AI.pdf
H.Seo, ICLR 2024, MLILAB, KAIST AI.pdf
 

Lex and Yacc.pdf

  • 1. By Dr. Prem Nath Associate Professor Computer Science & Engineering Department, H N B Garhwal University (A Central University) Lex And Yacc 1
  • 2. Outline ▪ Lex: ▪ Theory ▪ Execution ▪ Example ▪ Yacc: ▪ Theory ▪ Description ▪ Example ▪ Lex & Yacc linking. 2
  • 3. Lex  Lex is a Programming Language or Tool that generates lexical analyzers, (widely used on Unix with vi editor).  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. 3
  • 4. 4 LEX… A Language for Specifying Lexical Analyzers • Not only a table generator, but also allows “actions” to associate with RE’s. • Lex is widely used in the Unix community • Lex is not efficient enough for production compilers, however.
  • 5. Lex… Dr. Prem Nath 5 General Information: • Input is stored in a file with *.l extension • File consists of three main sections • lex generates C function stored in lex.yy.c Using Lex: 1) Specify words to be used as tokens (Extension of regular expressions) 2) Run the lex utility on the source file to generate yylex( ), a C function 3) Declares global variables char* yytext and int yyleng
  • 7. Structure of Lex Dr. Prem Nath 7 ➢ Declaration (Variables, Constants, etc.) ➢ Translation Rules (Pattern and Action) ➢ Auxiliary Functions /* Declaration %% /* Translation Rules %% Auxiliary Functions
  • 8. Lex… ❑ Declaration %{ #include<stdio.h> int a, b; %} ❑ Translation Rules %% Pattern1 {Action1} Pattern2 {Action2} %% ❑ Auxiliary Functions int main() { … yywrap(); } Dr. Prem Nath 8
  • 9. Lex… A simple pattern: letter(letter|digit)*  Regular expressions are translated by Lex to a computer program that mimics an FA.  This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits. 9
  • 10. Lex…  Some limitations, Lex cannot be used to recognize nested structures such as parentheses, since it only has states and transitions between states.  So, Lex is good at pattern matching, while Yacc is for more challenging tasks. 10
  • 14. Lex…  Whitespace must separate the defining term and the associated expression.  Code in the definitions section is simply copied as-is to the top of the generated C file and must be bracketed with “%{“ and “%}” markers.  substitutions in the rules section are surrounded by braces ({letter}) to distinguish them from literals. 14
  • 15. Lex… /* Lex Program to Recognize Id, Keywords, and Number %{ #include<stdio.h> letter [a-z A-Z] digit [0-9] %} %% id {letter}({letter}I{letter})* number {digit}+(.{digit})+?(E[+, -]?{digit})+ %% /* Auxiliary Functions {id} {printf(“%s is an Identifier”, yytext);} {if} {printf(“%s is a Keyword”, yytext);} {number} {printf(“%s is a Number”, yytext);} Dr. Prem Nath 15
  • 16. Yacc ➢ Yacc reads the grammar and generate C code for a parser . ➢ Parser Generator (Compiler to Compiler) ➢ Files are with .y extension ◦ Grammars written in Backus Naur Form (BNF) ◦ BNF grammar used to express context-free languages . ◦ e.g. to parse an expression , do reverse operation( reducing the expression) ◦ LALR(1) Parser (Look Ahead Left to Right One) ◦ Using stack for storing (LIFO) ◦ Written by Stefen C Johnson 16
  • 17. Yacc… Input to Yacc is divided into three sections. ... definitions ... %% ... rules ... %% ... subroutines ... 17
  • 18. Yacc…  The definitions section consists of: ◦ token declarations . ◦ C code bracketed by “%{“ and “%}”. ◦ the rules section consists of:  BNF grammar .  the subroutines section consists of: ◦ user subroutines . 18
  • 19. Linking lex and Yacc 19 CC: C Compiler
  • 20. 20 Linking lex and Yacc… Yacc Compiler Lex a.out parse.y y.tab.c (yyparse) scan.l source program output C Compiler lex.yy.c a.out Included
  • 21. Linking lex and Yacc… 21 • yyval: Vallue Associated With Token • yytext: Pointer to Input String
  • 22. Linking lex and Yacc… 22 $$: First Symbol of the Body $i: ith Symbol of the Body
  • 23. Linking lex and Yacc… ❑Construct a Lexical Analyzer and Parser for Language L={anbn: n ≥1}. /* Lex Tool ab.l %{ #include<y.tab.h> %} %% [a] {return a;} [b] {return b;} [n] {return 0;} %% /* No Main Function Dr. Prem Nath 23
  • 24. Linking lex and Yacc… /* Yacc Tool cd.y %{ #include<stdio.h> #include<stdlib.h> %} % token a b %% start: S ‘n’ {return 0;} S: aSb {$S=$1$2;} %% Dr. Prem Nath 24
  • 25. Linking lex and Yacc… /* Yacc Tool CD.y /* Main Function int main( ) { printf(“Enter a String:”); if(yyparse= = 0) printf(“Valid String”); yyerror( ) { printf(“Invalid String”); exit(0); } int yywrap( ) { return 1; } } Dr. Prem Nath 25