SlideShare une entreprise Scribd logo
1  sur  17
Systems Programming & Operating Systems
Unit – III
Case Study: Overview of LEX and YACC
Prof. Deptii Chaudhari
Assistant Professor
Department of Computer Engineering
International Institute of Information Technology, Pune
Department of Computer Engineering
1
LEX & YACC
• What is Lex?
• Lex is officially known as a "Lexical Analyser".
• It's main job is to break up an input stream into more usable elements.
• Or in, other words, to identify the "interesting bits" in a text file.
• What is Yacc?
• Yacc is officially known as a "parser".
• In the course of it's normal work, the parser also verifies that the input is
syntactically sound.
• YACC stands for "Yet Another Compiler Compiler". This is because this
kind of analysis of text files is normally associated with writing compilers.
2Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
3Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
LEX Program Structure
Definitions
%{
C global variables, prototype, Comments
%}
Production
Rules
%% ------------------------------------%%
User Subroutine
Section
(Optional)
4Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
• In the rules section, each rule is made up of two parts : a pattern and an action
separated by whitespace.
• The lexer that lex generates will execute the action when it recognizes the
pattern.
• The user subroutine section, consists of any legal C code.
• Lex copies it to the C file after the end of the lex generated code.
• Lex translates the Lex specification into C source file called lex.yy.c which
we compile and link with lex library –ll.
• Then we can execute the resulting program to check that it works as we
expected.
5Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
Example
%{
#include <stdio.h>
%}
%%
[0123456789]+ printf("NUMBERn");
[a-zA-Z][a-zA-Z0-9]* printf("WORDn");
%%
• Running the Program
$ lex example_lex.l
gcc lex.yy.c –ll
./a.out
6Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
Pattern Matching Primitives
Metacharacter Matches
. any character except newline
n newline
* zero or more copies of the preceding expression
+ one or more copies of the preceding expression
? zero or one copy of the preceding expression
^ beginning of line
$ end of line
a|b a or b
(ab)+ one or more copies of ab (grouping)
"a+b" literal "a+b" (C escapes still work)
[] character class
7Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
Pattern Matching Examples
Expression Matches
abc abc
abc* ab abc abcc abccc ...
abc+ abc, abcc, abccc, abcccc, ...
a(bc)+ abc, abcbc, abcbcbc, ...
a(bc)? a, abc
[abc] one of: a, b, c
[a-z] any letter, a through z
[a-z] one of: a, -, z
[-az] one of: - a z
[A-Za-z0-9]+ one or more alphanumeric characters
[ tn]+ whitespace
[^ab] anything except: a, b
[a^b] a, ^, b
[a|b] a, |, b
a|b a, b
8Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
Operation of yylex()
•When lex compiles the input specification, it generates the
C file lex.yy.c that contains the routine int yylex(void).
•This routine reads the input string trying to match it with
any of the token patterns specified in the rules section.
•On a match associated action is executed.
•When we call yylex() function, it starts the process of
pattern matching.
•Lex keeps the matched string into the address pointed by
pointer yytext.
•Matched string's length is kept in yyleng while value of
token is kept in variable yylval.
9Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
%{
int com=0;
%}
%%
"/*"[^n]+"*/" {com++;fprintf(yyout, " ");}
%%
int main()
{
printf("Write a C programn");
yyout=fopen("output", "w");
yylex();
printf("Comment=%dn",com);
return 0;
}
$ cc lex.yy.c -ll
$ ./a.out
Write a C program
#include<stdio.h>
int main()
{
int a, b;
/*float c;*/
printf(“Hi”);
/*printf(“Hello”);*/
}
Comment=2
$ cat output
#include<stdio.h>
int main()
{
int a, b;
printf(“Hi”);
}
10Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
Lex Predefined Variables
11Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
YACC
•YACC is a parser generator that takes an input file with
an attribute-enriched BNF (Backus – Naur Form) grammar
specification.
•It generates the output C file y.tab.c containing the
function int yyparse(void) that implements its parser.
•This function automatically invokes yylex() everytime it
needs a token to continue parsing.
12Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
13Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
Structure of YACC Program
Definitions
%{
C global variables, prototype, Comments
%}
Context free grammar
& action for each
production
%% ------------------------------------%%
Subroutines/Functions
14Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
Arithmatic.l
%{
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[t] ;
[n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;}
How To Run:
$yacc -d arithmatic.y
$lex arithmatic.l
$gcc lex.yy.c y.tab.c
$./a.out
15Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
References
• https://www.epaperpress.com/lexandyacc/
• John. R. Levine, Tony Mason and Doug Brown - Lex and Yacc‖, O'Reilly
16Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
17
THANK YOU
For further details, please contact
Deptii Chaudhari
deptiic@isquareit.edu.in
Department of Computer Engineering
Hope Foundation’s
International Institute of Information Technology, I²IT
P-14,Rajiv Gandhi Infotech Park
MIDC Phase 1, Hinjawadi, Pune – 411057
Tel - +91 20 22933441/2/3
www.isquareit.edu.in | info@isquareit.edu.in

Contenu connexe

Tendances

Tendances (20)

FUSION - Pattern Recognition, Classification, Classifier Fusion
FUSION - Pattern Recognition, Classification, Classifier Fusion FUSION - Pattern Recognition, Classification, Classifier Fusion
FUSION - Pattern Recognition, Classification, Classifier Fusion
 
Artificial Intelligence - Introduction
Artificial Intelligence - IntroductionArtificial Intelligence - Introduction
Artificial Intelligence - Introduction
 
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
Difference Between AI(Artificial Intelligence), ML(Machine Learning), DL (Dee...
 
What Is High Performance-Computing?
What Is High Performance-Computing?What Is High Performance-Computing?
What Is High Performance-Computing?
 
Conformal Mapping - Introduction & Examples
Conformal Mapping - Introduction & ExamplesConformal Mapping - Introduction & Examples
Conformal Mapping - Introduction & Examples
 
Introduction To Fog Computing
Introduction To Fog ComputingIntroduction To Fog Computing
Introduction To Fog Computing
 
Usability Heuristics - Principles & Examples
Usability Heuristics - Principles & ExamplesUsability Heuristics - Principles & Examples
Usability Heuristics - Principles & Examples
 
Introduction To Design Pattern
Introduction To Design PatternIntroduction To Design Pattern
Introduction To Design Pattern
 
Cloud Computing & Virtual Infrastructure
Cloud Computing & Virtual InfrastructureCloud Computing & Virtual Infrastructure
Cloud Computing & Virtual Infrastructure
 
Introduction To Assembly Language Programming
Introduction To Assembly Language ProgrammingIntroduction To Assembly Language Programming
Introduction To Assembly Language Programming
 
What Is Cascading Style Sheet?
What Is Cascading Style Sheet?What Is Cascading Style Sheet?
What Is Cascading Style Sheet?
 
Newton's Laws of Motion
Newton's Laws of MotionNewton's Laws of Motion
Newton's Laws of Motion
 
Introduction to Big Data, HADOOP: HDFS, MapReduce
Introduction to Big Data,  HADOOP: HDFS, MapReduceIntroduction to Big Data,  HADOOP: HDFS, MapReduce
Introduction to Big Data, HADOOP: HDFS, MapReduce
 
Addition of Two Polynomials
Addition of Two PolynomialsAddition of Two Polynomials
Addition of Two Polynomials
 
Introduction to Wireless Sensor Networks (WSN)
Introduction to Wireless Sensor Networks (WSN)Introduction to Wireless Sensor Networks (WSN)
Introduction to Wireless Sensor Networks (WSN)
 
Operating System Scheduling Algorithms
Operating System Scheduling AlgorithmsOperating System Scheduling Algorithms
Operating System Scheduling Algorithms
 
Interrupt Handling with LPC2148
Interrupt Handling with LPC2148Interrupt Handling with LPC2148
Interrupt Handling with LPC2148
 
Data Structure - Linked List
Data Structure - Linked ListData Structure - Linked List
Data Structure - Linked List
 
Document Type Definition (DTD)
Document Type Definition (DTD)Document Type Definition (DTD)
Document Type Definition (DTD)
 
What are the real differences between a wireframe, storyboard and a prototype?
What are the real differences between a wireframe, storyboard and a prototype?What are the real differences between a wireframe, storyboard and a prototype?
What are the real differences between a wireframe, storyboard and a prototype?
 

Similaire à Systems Programming & Operating Systems - Overview of LEX-and-YACC

Similaire à Systems Programming & Operating Systems - Overview of LEX-and-YACC (20)

DAA Introduction to Algorithms & Application
DAA Introduction to Algorithms & ApplicationDAA Introduction to Algorithms & Application
DAA Introduction to Algorithms & Application
 
What is DevOps?
What is DevOps?What is DevOps?
What is DevOps?
 
Fundamentals of Computer Networks
Fundamentals of Computer NetworksFundamentals of Computer Networks
Fundamentals of Computer Networks
 
Database Query Optimization
Database Query OptimizationDatabase Query Optimization
Database Query Optimization
 
NP-Complete Problem
NP-Complete Problem NP-Complete Problem
NP-Complete Problem
 
Supervised Learning in Cybersecurity
Supervised Learning in CybersecuritySupervised Learning in Cybersecurity
Supervised Learning in Cybersecurity
 
Big Data Technologies
Big Data TechnologiesBig Data Technologies
Big Data Technologies
 
Basics of Digital Electronics
Basics of Digital ElectronicsBasics of Digital Electronics
Basics of Digital Electronics
 
Introduction to TCP Protocol Suite
Introduction to TCP Protocol SuiteIntroduction to TCP Protocol Suite
Introduction to TCP Protocol Suite
 
DESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy PatternsDESIGN PATTERNS: Strategy Patterns
DESIGN PATTERNS: Strategy Patterns
 
Adapter Pattern: Introduction & Implementation (with examples)
Adapter Pattern: Introduction & Implementation (with examples)Adapter Pattern: Introduction & Implementation (with examples)
Adapter Pattern: Introduction & Implementation (with examples)
 
Java as Object Oriented Programming Language
Java as Object Oriented Programming LanguageJava as Object Oriented Programming Language
Java as Object Oriented Programming Language
 
IRJET- Review on Intelligent System for College
IRJET- Review on Intelligent System for CollegeIRJET- Review on Intelligent System for College
IRJET- Review on Intelligent System for College
 
IRJET- Review on Intelligent System for College
IRJET- Review on Intelligent System for CollegeIRJET- Review on Intelligent System for College
IRJET- Review on Intelligent System for College
 
Digital Certificate Verification using Blockchain
Digital Certificate Verification using BlockchainDigital Certificate Verification using Blockchain
Digital Certificate Verification using Blockchain
 
IRJET- Intelligent Laboratory Management System based on Internet of Thin...
IRJET-  	  Intelligent Laboratory Management System based on Internet of Thin...IRJET-  	  Intelligent Laboratory Management System based on Internet of Thin...
IRJET- Intelligent Laboratory Management System based on Internet of Thin...
 
Engineering Mathematics | Maxima and Minima
Engineering Mathematics | Maxima and MinimaEngineering Mathematics | Maxima and Minima
Engineering Mathematics | Maxima and Minima
 
akash_cv
akash_cvakash_cv
akash_cv
 
Asp .Net Website on E Learning
Asp .Net Website on E LearningAsp .Net Website on E Learning
Asp .Net Website on E Learning
 
AN ANDROID APPLICATION FOR CAMPUS INFORMATION SYSTEM
AN ANDROID APPLICATION FOR CAMPUS INFORMATION SYSTEMAN ANDROID APPLICATION FOR CAMPUS INFORMATION SYSTEM
AN ANDROID APPLICATION FOR CAMPUS INFORMATION SYSTEM
 

Plus de International Institute of Information Technology (I²IT)

Plus de International Institute of Information Technology (I²IT) (20)

Minimization of DFA
Minimization of DFAMinimization of DFA
Minimization of DFA
 
Understanding Natural Language Processing
Understanding Natural Language ProcessingUnderstanding Natural Language Processing
Understanding Natural Language Processing
 
What Is Smart Computing?
What Is Smart Computing?What Is Smart Computing?
What Is Smart Computing?
 
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
Professional Ethics & Etiquette: What Are They & How Do I Get Them?Professional Ethics & Etiquette: What Are They & How Do I Get Them?
Professional Ethics & Etiquette: What Are They & How Do I Get Them?
 
Writing Skills: Importance of Writing Skills
Writing Skills: Importance of Writing SkillsWriting Skills: Importance of Writing Skills
Writing Skills: Importance of Writing Skills
 
Professional Communication | Introducing Oneself
Professional Communication | Introducing Oneself Professional Communication | Introducing Oneself
Professional Communication | Introducing Oneself
 
Servlet: A Server-side Technology
Servlet: A Server-side TechnologyServlet: A Server-side Technology
Servlet: A Server-side Technology
 
What Is Jenkins? Features and How It Works
What Is Jenkins? Features and How It WorksWhat Is Jenkins? Features and How It Works
What Is Jenkins? Features and How It Works
 
Cloud Computing
Cloud ComputingCloud Computing
Cloud Computing
 
Hypothesis-Testing
Hypothesis-TestingHypothesis-Testing
Hypothesis-Testing
 
Data Science, Big Data, Data Analytics
Data Science, Big Data, Data AnalyticsData Science, Big Data, Data Analytics
Data Science, Big Data, Data Analytics
 
Types of Artificial Intelligence
Types of Artificial Intelligence Types of Artificial Intelligence
Types of Artificial Intelligence
 
Sentiment Analysis in Machine Learning
Sentiment Analysis in  Machine LearningSentiment Analysis in  Machine Learning
Sentiment Analysis in Machine Learning
 
What Is Cloud Computing?
What Is Cloud Computing?What Is Cloud Computing?
What Is Cloud Computing?
 
Data Visualization - How to connect Microsoft Forms to Power BI
Data Visualization - How to connect Microsoft Forms to Power BIData Visualization - How to connect Microsoft Forms to Power BI
Data Visualization - How to connect Microsoft Forms to Power BI
 
AVL Tree Explained
AVL Tree ExplainedAVL Tree Explained
AVL Tree Explained
 
Yoga To Fight & Win Against COVID-19
Yoga To Fight & Win Against COVID-19Yoga To Fight & Win Against COVID-19
Yoga To Fight & Win Against COVID-19
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
LR(0) PARSER
 
Land Pollution - Causes, Effects & Solution
Land Pollution - Causes, Effects & SolutionLand Pollution - Causes, Effects & Solution
Land Pollution - Causes, Effects & Solution
 
Sampling Theorem and Band Limited Signals
Sampling Theorem and Band Limited SignalsSampling Theorem and Band Limited Signals
Sampling Theorem and Band Limited Signals
 

Dernier

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Christo Ananth
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Dr.Costas Sachpazis
 

Dernier (20)

Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTINGMANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
MANUFACTURING PROCESS-II UNIT-1 THEORY OF METAL CUTTING
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Introduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptxIntroduction and different types of Ethernet.pptx
Introduction and different types of Ethernet.pptx
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 

Systems Programming & Operating Systems - Overview of LEX-and-YACC

  • 1. Systems Programming & Operating Systems Unit – III Case Study: Overview of LEX and YACC Prof. Deptii Chaudhari Assistant Professor Department of Computer Engineering International Institute of Information Technology, Pune Department of Computer Engineering 1
  • 2. LEX & YACC • What is Lex? • Lex is officially known as a "Lexical Analyser". • It's main job is to break up an input stream into more usable elements. • Or in, other words, to identify the "interesting bits" in a text file. • What is Yacc? • Yacc is officially known as a "parser". • In the course of it's normal work, the parser also verifies that the input is syntactically sound. • YACC stands for "Yet Another Compiler Compiler". This is because this kind of analysis of text files is normally associated with writing compilers. 2Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 3. 3Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 4. LEX Program Structure Definitions %{ C global variables, prototype, Comments %} Production Rules %% ------------------------------------%% User Subroutine Section (Optional) 4Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 5. • In the rules section, each rule is made up of two parts : a pattern and an action separated by whitespace. • The lexer that lex generates will execute the action when it recognizes the pattern. • The user subroutine section, consists of any legal C code. • Lex copies it to the C file after the end of the lex generated code. • Lex translates the Lex specification into C source file called lex.yy.c which we compile and link with lex library –ll. • Then we can execute the resulting program to check that it works as we expected. 5Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 6. Example %{ #include <stdio.h> %} %% [0123456789]+ printf("NUMBERn"); [a-zA-Z][a-zA-Z0-9]* printf("WORDn"); %% • Running the Program $ lex example_lex.l gcc lex.yy.c –ll ./a.out 6Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 7. Pattern Matching Primitives Metacharacter Matches . any character except newline n newline * zero or more copies of the preceding expression + one or more copies of the preceding expression ? zero or one copy of the preceding expression ^ beginning of line $ end of line a|b a or b (ab)+ one or more copies of ab (grouping) "a+b" literal "a+b" (C escapes still work) [] character class 7Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 8. Pattern Matching Examples Expression Matches abc abc abc* ab abc abcc abccc ... abc+ abc, abcc, abccc, abcccc, ... a(bc)+ abc, abcbc, abcbcbc, ... a(bc)? a, abc [abc] one of: a, b, c [a-z] any letter, a through z [a-z] one of: a, -, z [-az] one of: - a z [A-Za-z0-9]+ one or more alphanumeric characters [ tn]+ whitespace [^ab] anything except: a, b [a^b] a, ^, b [a|b] a, |, b a|b a, b 8Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 9. Operation of yylex() •When lex compiles the input specification, it generates the C file lex.yy.c that contains the routine int yylex(void). •This routine reads the input string trying to match it with any of the token patterns specified in the rules section. •On a match associated action is executed. •When we call yylex() function, it starts the process of pattern matching. •Lex keeps the matched string into the address pointed by pointer yytext. •Matched string's length is kept in yyleng while value of token is kept in variable yylval. 9Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 10. %{ int com=0; %} %% "/*"[^n]+"*/" {com++;fprintf(yyout, " ");} %% int main() { printf("Write a C programn"); yyout=fopen("output", "w"); yylex(); printf("Comment=%dn",com); return 0; } $ cc lex.yy.c -ll $ ./a.out Write a C program #include<stdio.h> int main() { int a, b; /*float c;*/ printf(“Hi”); /*printf(“Hello”);*/ } Comment=2 $ cat output #include<stdio.h> int main() { int a, b; printf(“Hi”); } 10Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 11. Lex Predefined Variables 11Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 12. YACC •YACC is a parser generator that takes an input file with an attribute-enriched BNF (Backus – Naur Form) grammar specification. •It generates the output C file y.tab.c containing the function int yyparse(void) that implements its parser. •This function automatically invokes yylex() everytime it needs a token to continue parsing. 12Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 13. 13Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 14. Structure of YACC Program Definitions %{ C global variables, prototype, Comments %} Context free grammar & action for each production %% ------------------------------------%% Subroutines/Functions 14Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 15. Arithmatic.l %{ #include<stdio.h> #include "y.tab.h" extern int yylval; %} %% [0-9]+ { yylval=atoi(yytext); return NUMBER; } [t] ; [n] return 0; . return yytext[0]; %% int yywrap() { return 1;} How To Run: $yacc -d arithmatic.y $lex arithmatic.l $gcc lex.yy.c y.tab.c $./a.out 15Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 16. References • https://www.epaperpress.com/lexandyacc/ • John. R. Levine, Tony Mason and Doug Brown - Lex and Yacc‖, O'Reilly 16Deptii Chaudhari, Dept of Computer Engineering, Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 | www.isquareit.edu.in | info@isquareit.edu.in
  • 17. 17 THANK YOU For further details, please contact Deptii Chaudhari deptiic@isquareit.edu.in Department of Computer Engineering Hope Foundation’s International Institute of Information Technology, I²IT P-14,Rajiv Gandhi Infotech Park MIDC Phase 1, Hinjawadi, Pune – 411057 Tel - +91 20 22933441/2/3 www.isquareit.edu.in | info@isquareit.edu.in