SlideShare a Scribd company logo
Compiler Design Lab
Suman Mia
Lecturer
Dept. Of CSE
Dhaka International University
Lex File Format
Definitions
%%
Rules
%%
Supplementary Code
 %{
 #include<stdio.h>
 #include<string.h>
 int i = 0;
 %}
 /* Rules Section*/
 %%
 ([a-zA-Z0-9])* {i++;}
 /* Rule for counting
 number of words*/

 "n" {printf("%dn", i); i = 0;}
 %%

 int yywrap(void){
 }
 int main()
 {
 // The function that starts the analysis
 yylex();
 return 0;
 }
2
Lex program check whether the given number is odd or even
Program:
%{
#include <stdio.h>
%}
%%
[0-9]+ {
int num = atoi(yytext);
if (num % 2 == 0) {
printf("%d is evenn", num);
} else {
printf("%d is oddn", num);
}
}
. {
// Ignore unrecognized characters
}
%%
int main()
{
yylex();
return 0;
}
int yywrap()
{
return 1;
}
3
Describe:
 The [0-9]+ pattern matches one or more digits.
 Inside the rule for [0-9]+, we convert the matched number (yytext)
to an integer using atoi(yytext).
 We then check if the number is divisible by 2 using the modulo
operator %. If the remainder is 0, the number is even; otherwise, it is
odd.
 The appropriate message is printed based on whether the number is
even or odd.
 The dot (.) rule ignores any unrecognized characters.
 In the main() function, yylex() is called to start the lexer.
 The yywrap() function is implemented to indicate the end of input.
4
Output
5
Lex program to recognize simple arithmetic expressions
Program:
%{
#include <stdio.h>
%}
%%
[0-9]+(.[0-9]+)? {
printf("Number: %sn", yytext);
}
[+-*/] {
printf("Operator: %sn", yytext);
}
[()] {
printf("Parenthesis: %sn", yytext);
}
. {
// Ignore other characters
}
%%
int main()
{
yylex();
return 0;
}
int yywrap()
{
return 1;
}
6
Describe:
1. [0-9]+(.[0-9]+)?:
 This rule matches numbers in the input text.
 [0-9]+ matches one or more digits.
 (.[0-9]+)? matches an optional decimal point followed by one
or more digits.
2. [+-*/]:
 This rule matches arithmetic operators (+, -, *, /) in the input
text.
3. [()]:
 This rule matches parentheses '(' and ')' in the input text.
7
Output
8
Lex program to identify Keywords and Identifiers
%{
#include<stdio.h>
int others = 0;
int keyword = 0;
int id = 0;
%}
/*Rules Section*/
%%
"int"|"double"|"float"|"char"|"do"|"while"|"mai
n"|"return"|"printf"|"scanf"|"include"|"stdio.
h" {
keyword++;
printf("'%s' is a keywordn",yytext);}
[a-zA-Z_][a-zA-Z0-9_]* {
id++;
printf("'%s' is an identifiern",yytext);
}
. {others++;}
%%
int main()
{
yyin = fopen("input.c","r");
yylex();
printf("Number of identifier is %dn", id);
return 0;
}
int yywrap()
{
return 1;
}
9
Describe:
1. %{ and %}: These symbols indicate the beginning and end
of the "C" code section. In this case, the code includes the
<stdio.h> header file and declares three variables: others,
keyword, and id.
2. The %% symbol separates the Lex rules section from the C
code section.
3. "int"|"double"|"float"|"char"|"do"|"while"|"main"|"return"|"print
f"|"scanf"|"include"|"stdio.h": This rule matches any of the
listed keywords. When a match is found, the associated
action increments the keyword count and prints the
keyword using printf.
4. [a-zA-Z_][a-zA-Z0-9_]*: This rule matches identifiers, which
consist of a letter or underscore followed by zero or more
letters, digits, or underscores. When a match is found, the
10
Describe:
5. “.” : This rule matches any character that doesn't match the
previous rules. It represents all other characters in the input
text. When a match is found, the associated action
increments the others count.
6. int main(): This is the entry point of the program. It opens
the "input.c" file for reading using fopen, then calls yylex()
to start the lexical analysis process based on the defined
Lex rules. After the analysis is complete, it prints the
number of identifiers (id) found using printf.
7. int yywrap(): This function is called by the Lex-generated
scanner when it reaches the end of input. It serves as the
termination condition for the scanning process and returns
1 to indicate the end of input. 11
Input. C File
#include<stdio.h>
//this is a comment
int main()
{
int number = 10;
double value = 1.67;
printf("The number is: %dn", number);
return 0;
}
/*
this is a multi line comment
*/
12
Output
13
14

More Related Content

Similar to Lexical Analysis and Parsing

Functions
FunctionsFunctions
Functions
PralhadKhanal1
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 
What is c
What is cWhat is c
What is c
Nitesh Saitwal
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
Icaii Infotech
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
amrit47
 
C important questions
C important questionsC important questions
C important questions
JYOTI RANJAN PAL
 
Programming egs
Programming egs Programming egs
Programming egs
Dr.Subha Krishna
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
psaravanan1985
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
HARSHSHARMA840
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
THE NORTHCAP UNIVERSITY
 
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
Computer experiments   1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branchComputer experiments   1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
SAKSHIGAWADE2
 
C function
C functionC function
C function
thirumalaikumar3
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
UMA PARAMESWARI
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
aluavi
 

Similar to Lexical Analysis and Parsing (20)

Functions
FunctionsFunctions
Functions
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Unit2 C
Unit2 CUnit2 C
Unit2 C
 
Unit2 C
Unit2 C Unit2 C
Unit2 C
 
What is c
What is cWhat is c
What is c
 
Assignment c programming
Assignment c programmingAssignment c programming
Assignment c programming
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
C important questions
C important questionsC important questions
C important questions
 
Programming egs
Programming egs Programming egs
Programming egs
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
Operators1.pptx
Operators1.pptxOperators1.pptx
Operators1.pptx
 
Fundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan KumariFundamentals of computer programming by Dr. A. Charan Kumari
Fundamentals of computer programming by Dr. A. Charan Kumari
 
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
Computer experiments   1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branchComputer experiments   1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
Computer experiments 1^j2^j3^j4^j8^j9. d24 ^j sakshi gawade cs branch
 
C function
C functionC function
C function
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Write a program to check a given number is prime or not
Write a program to check a given number is prime or notWrite a program to check a given number is prime or not
Write a program to check a given number is prime or not
 

More from Suman Mia

Computer Network Topology By Team_Initiator (Dept . of Sociology)
Computer Network Topology By Team_Initiator (Dept . of Sociology)Computer Network Topology By Team_Initiator (Dept . of Sociology)
Computer Network Topology By Team_Initiator (Dept . of Sociology)
Suman Mia
 
Computer Network Topology By Team_Diversity Detectives (Dept . of Sociology)
Computer Network Topology By Team_Diversity Detectives (Dept . of Sociology)Computer Network Topology By Team_Diversity Detectives (Dept . of Sociology)
Computer Network Topology By Team_Diversity Detectives (Dept . of Sociology)
Suman Mia
 
Computer Network Topology By Team_Societal Explorers (Dept . of Sociology)
Computer Network Topology By Team_Societal Explorers (Dept . of Sociology)Computer Network Topology By Team_Societal Explorers (Dept . of Sociology)
Computer Network Topology By Team_Societal Explorers (Dept . of Sociology)
Suman Mia
 
Computer Network Topology By Team_Social Dynamic Squad (Dept . of Sociology)
Computer Network Topology By Team_Social Dynamic Squad (Dept . of Sociology)Computer Network Topology By Team_Social Dynamic Squad (Dept . of Sociology)
Computer Network Topology By Team_Social Dynamic Squad (Dept . of Sociology)
Suman Mia
 
Computer Network Topology By Team_Inclusion Inquiry Initiative (Dept . of Soc...
Computer Network Topology By Team_Inclusion Inquiry Initiative (Dept . of Soc...Computer Network Topology By Team_Inclusion Inquiry Initiative (Dept . of Soc...
Computer Network Topology By Team_Inclusion Inquiry Initiative (Dept . of Soc...
Suman Mia
 
Computer Network Topology By Team_Culture Crusade (Dept . of Sociology)
Computer Network Topology By Team_Culture Crusade (Dept . of Sociology)Computer Network Topology By Team_Culture Crusade (Dept . of Sociology)
Computer Network Topology By Team_Culture Crusade (Dept . of Sociology)
Suman Mia
 
Computer Network Topology By Team_Community Connectors (Dept . of Sociology)
Computer Network Topology By Team_Community Connectors (Dept . of Sociology)Computer Network Topology By Team_Community Connectors (Dept . of Sociology)
Computer Network Topology By Team_Community Connectors (Dept . of Sociology)
Suman Mia
 
Computer Network Topology By Team_Empowerment Ensemble (Dept . of Sociology)
Computer Network Topology By Team_Empowerment Ensemble (Dept . of Sociology)Computer Network Topology By Team_Empowerment Ensemble (Dept . of Sociology)
Computer Network Topology By Team_Empowerment Ensemble (Dept . of Sociology)
Suman Mia
 
Computer Network Topology By Team_Venus(Dept. English)
Computer Network Topology By Team_Venus(Dept. English)Computer Network Topology By Team_Venus(Dept. English)
Computer Network Topology By Team_Venus(Dept. English)
Suman Mia
 
Computer Network Topology By Team_Triangle(Dept. English)
Computer Network Topology By Team_Triangle(Dept. English)Computer Network Topology By Team_Triangle(Dept. English)
Computer Network Topology By Team_Triangle(Dept. English)
Suman Mia
 
Computer Network Topology By Team_Royal (Dept. English)
Computer Network Topology By Team_Royal (Dept. English)Computer Network Topology By Team_Royal (Dept. English)
Computer Network Topology By Team_Royal (Dept. English)
Suman Mia
 
Computer Network Topology By Team_Sanam (Dept. English)
Computer Network Topology By Team_Sanam (Dept. English)Computer Network Topology By Team_Sanam (Dept. English)
Computer Network Topology By Team_Sanam (Dept. English)
Suman Mia
 
Computer Network Topology By Team_Purple (Dept. English)
Computer Network Topology By Team_Purple (Dept. English)Computer Network Topology By Team_Purple (Dept. English)
Computer Network Topology By Team_Purple (Dept. English)
Suman Mia
 
Computer Network Topology By Team_ Paramount (Dept. English)
Computer Network Topology By Team_ Paramount (Dept. English)Computer Network Topology By Team_ Paramount (Dept. English)
Computer Network Topology By Team_ Paramount (Dept. English)
Suman Mia
 
Computer Network Topology By Team_Metrolife(Dept. English)
Computer Network Topology By Team_Metrolife(Dept. English)Computer Network Topology By Team_Metrolife(Dept. English)
Computer Network Topology By Team_Metrolife(Dept. English)
Suman Mia
 
Computer Network Topology By Team_Meghna (Dept. English)
Computer Network Topology By Team_Meghna (Dept. English)Computer Network Topology By Team_Meghna (Dept. English)
Computer Network Topology By Team_Meghna (Dept. English)
Suman Mia
 
Computer Network Topology By Team_Jamuna (Dept. English)
Computer Network Topology By Team_Jamuna (Dept. English)Computer Network Topology By Team_Jamuna (Dept. English)
Computer Network Topology By Team_Jamuna (Dept. English)
Suman Mia
 
Computer Network Topology By Team_CSK (Dept. English)
Computer Network Topology By Team_CSK (Dept. English)Computer Network Topology By Team_CSK (Dept. English)
Computer Network Topology By Team_CSK (Dept. English)
Suman Mia
 
COmputer Network Topology By Team Gemini
COmputer Network Topology By Team GeminiCOmputer Network Topology By Team Gemini
COmputer Network Topology By Team Gemini
Suman Mia
 
Software Development Life Cycle(SDLC) By Team Hunter (Dept. of Pharmacy)
Software Development Life Cycle(SDLC) By Team Hunter (Dept. of Pharmacy)Software Development Life Cycle(SDLC) By Team Hunter (Dept. of Pharmacy)
Software Development Life Cycle(SDLC) By Team Hunter (Dept. of Pharmacy)
Suman Mia
 

More from Suman Mia (20)

Computer Network Topology By Team_Initiator (Dept . of Sociology)
Computer Network Topology By Team_Initiator (Dept . of Sociology)Computer Network Topology By Team_Initiator (Dept . of Sociology)
Computer Network Topology By Team_Initiator (Dept . of Sociology)
 
Computer Network Topology By Team_Diversity Detectives (Dept . of Sociology)
Computer Network Topology By Team_Diversity Detectives (Dept . of Sociology)Computer Network Topology By Team_Diversity Detectives (Dept . of Sociology)
Computer Network Topology By Team_Diversity Detectives (Dept . of Sociology)
 
Computer Network Topology By Team_Societal Explorers (Dept . of Sociology)
Computer Network Topology By Team_Societal Explorers (Dept . of Sociology)Computer Network Topology By Team_Societal Explorers (Dept . of Sociology)
Computer Network Topology By Team_Societal Explorers (Dept . of Sociology)
 
Computer Network Topology By Team_Social Dynamic Squad (Dept . of Sociology)
Computer Network Topology By Team_Social Dynamic Squad (Dept . of Sociology)Computer Network Topology By Team_Social Dynamic Squad (Dept . of Sociology)
Computer Network Topology By Team_Social Dynamic Squad (Dept . of Sociology)
 
Computer Network Topology By Team_Inclusion Inquiry Initiative (Dept . of Soc...
Computer Network Topology By Team_Inclusion Inquiry Initiative (Dept . of Soc...Computer Network Topology By Team_Inclusion Inquiry Initiative (Dept . of Soc...
Computer Network Topology By Team_Inclusion Inquiry Initiative (Dept . of Soc...
 
Computer Network Topology By Team_Culture Crusade (Dept . of Sociology)
Computer Network Topology By Team_Culture Crusade (Dept . of Sociology)Computer Network Topology By Team_Culture Crusade (Dept . of Sociology)
Computer Network Topology By Team_Culture Crusade (Dept . of Sociology)
 
Computer Network Topology By Team_Community Connectors (Dept . of Sociology)
Computer Network Topology By Team_Community Connectors (Dept . of Sociology)Computer Network Topology By Team_Community Connectors (Dept . of Sociology)
Computer Network Topology By Team_Community Connectors (Dept . of Sociology)
 
Computer Network Topology By Team_Empowerment Ensemble (Dept . of Sociology)
Computer Network Topology By Team_Empowerment Ensemble (Dept . of Sociology)Computer Network Topology By Team_Empowerment Ensemble (Dept . of Sociology)
Computer Network Topology By Team_Empowerment Ensemble (Dept . of Sociology)
 
Computer Network Topology By Team_Venus(Dept. English)
Computer Network Topology By Team_Venus(Dept. English)Computer Network Topology By Team_Venus(Dept. English)
Computer Network Topology By Team_Venus(Dept. English)
 
Computer Network Topology By Team_Triangle(Dept. English)
Computer Network Topology By Team_Triangle(Dept. English)Computer Network Topology By Team_Triangle(Dept. English)
Computer Network Topology By Team_Triangle(Dept. English)
 
Computer Network Topology By Team_Royal (Dept. English)
Computer Network Topology By Team_Royal (Dept. English)Computer Network Topology By Team_Royal (Dept. English)
Computer Network Topology By Team_Royal (Dept. English)
 
Computer Network Topology By Team_Sanam (Dept. English)
Computer Network Topology By Team_Sanam (Dept. English)Computer Network Topology By Team_Sanam (Dept. English)
Computer Network Topology By Team_Sanam (Dept. English)
 
Computer Network Topology By Team_Purple (Dept. English)
Computer Network Topology By Team_Purple (Dept. English)Computer Network Topology By Team_Purple (Dept. English)
Computer Network Topology By Team_Purple (Dept. English)
 
Computer Network Topology By Team_ Paramount (Dept. English)
Computer Network Topology By Team_ Paramount (Dept. English)Computer Network Topology By Team_ Paramount (Dept. English)
Computer Network Topology By Team_ Paramount (Dept. English)
 
Computer Network Topology By Team_Metrolife(Dept. English)
Computer Network Topology By Team_Metrolife(Dept. English)Computer Network Topology By Team_Metrolife(Dept. English)
Computer Network Topology By Team_Metrolife(Dept. English)
 
Computer Network Topology By Team_Meghna (Dept. English)
Computer Network Topology By Team_Meghna (Dept. English)Computer Network Topology By Team_Meghna (Dept. English)
Computer Network Topology By Team_Meghna (Dept. English)
 
Computer Network Topology By Team_Jamuna (Dept. English)
Computer Network Topology By Team_Jamuna (Dept. English)Computer Network Topology By Team_Jamuna (Dept. English)
Computer Network Topology By Team_Jamuna (Dept. English)
 
Computer Network Topology By Team_CSK (Dept. English)
Computer Network Topology By Team_CSK (Dept. English)Computer Network Topology By Team_CSK (Dept. English)
Computer Network Topology By Team_CSK (Dept. English)
 
COmputer Network Topology By Team Gemini
COmputer Network Topology By Team GeminiCOmputer Network Topology By Team Gemini
COmputer Network Topology By Team Gemini
 
Software Development Life Cycle(SDLC) By Team Hunter (Dept. of Pharmacy)
Software Development Life Cycle(SDLC) By Team Hunter (Dept. of Pharmacy)Software Development Life Cycle(SDLC) By Team Hunter (Dept. of Pharmacy)
Software Development Life Cycle(SDLC) By Team Hunter (Dept. of Pharmacy)
 

Recently uploaded

PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
anoopmanoharan2
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
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
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
Dr Ramhari Poudyal
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
ssuser7dcef0
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
zwunae
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
obonagu
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
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
 

Recently uploaded (20)

PPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testingPPT on GRP pipes manufacturing and testing
PPT on GRP pipes manufacturing and testing
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.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
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
Literature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptxLiterature Review Basics and Understanding Reference Management.pptx
Literature Review Basics and Understanding Reference Management.pptx
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
NUMERICAL SIMULATIONS OF HEAT AND MASS TRANSFER IN CONDENSING HEAT EXCHANGERS...
 
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
一比一原版(UMich毕业证)密歇根大学|安娜堡分校毕业证成绩单专业办理
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
在线办理(ANU毕业证书)澳洲国立大学毕业证录取通知书一模一样
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
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
 

Lexical Analysis and Parsing

  • 1. Compiler Design Lab Suman Mia Lecturer Dept. Of CSE Dhaka International University
  • 2. Lex File Format Definitions %% Rules %% Supplementary Code  %{  #include<stdio.h>  #include<string.h>  int i = 0;  %}  /* Rules Section*/  %%  ([a-zA-Z0-9])* {i++;}  /* Rule for counting  number of words*/   "n" {printf("%dn", i); i = 0;}  %%   int yywrap(void){  }  int main()  {  // The function that starts the analysis  yylex();  return 0;  } 2
  • 3. Lex program check whether the given number is odd or even Program: %{ #include <stdio.h> %} %% [0-9]+ { int num = atoi(yytext); if (num % 2 == 0) { printf("%d is evenn", num); } else { printf("%d is oddn", num); } } . { // Ignore unrecognized characters } %% int main() { yylex(); return 0; } int yywrap() { return 1; } 3
  • 4. Describe:  The [0-9]+ pattern matches one or more digits.  Inside the rule for [0-9]+, we convert the matched number (yytext) to an integer using atoi(yytext).  We then check if the number is divisible by 2 using the modulo operator %. If the remainder is 0, the number is even; otherwise, it is odd.  The appropriate message is printed based on whether the number is even or odd.  The dot (.) rule ignores any unrecognized characters.  In the main() function, yylex() is called to start the lexer.  The yywrap() function is implemented to indicate the end of input. 4
  • 6. Lex program to recognize simple arithmetic expressions Program: %{ #include <stdio.h> %} %% [0-9]+(.[0-9]+)? { printf("Number: %sn", yytext); } [+-*/] { printf("Operator: %sn", yytext); } [()] { printf("Parenthesis: %sn", yytext); } . { // Ignore other characters } %% int main() { yylex(); return 0; } int yywrap() { return 1; } 6
  • 7. Describe: 1. [0-9]+(.[0-9]+)?:  This rule matches numbers in the input text.  [0-9]+ matches one or more digits.  (.[0-9]+)? matches an optional decimal point followed by one or more digits. 2. [+-*/]:  This rule matches arithmetic operators (+, -, *, /) in the input text. 3. [()]:  This rule matches parentheses '(' and ')' in the input text. 7
  • 9. Lex program to identify Keywords and Identifiers %{ #include<stdio.h> int others = 0; int keyword = 0; int id = 0; %} /*Rules Section*/ %% "int"|"double"|"float"|"char"|"do"|"while"|"mai n"|"return"|"printf"|"scanf"|"include"|"stdio. h" { keyword++; printf("'%s' is a keywordn",yytext);} [a-zA-Z_][a-zA-Z0-9_]* { id++; printf("'%s' is an identifiern",yytext); } . {others++;} %% int main() { yyin = fopen("input.c","r"); yylex(); printf("Number of identifier is %dn", id); return 0; } int yywrap() { return 1; } 9
  • 10. Describe: 1. %{ and %}: These symbols indicate the beginning and end of the "C" code section. In this case, the code includes the <stdio.h> header file and declares three variables: others, keyword, and id. 2. The %% symbol separates the Lex rules section from the C code section. 3. "int"|"double"|"float"|"char"|"do"|"while"|"main"|"return"|"print f"|"scanf"|"include"|"stdio.h": This rule matches any of the listed keywords. When a match is found, the associated action increments the keyword count and prints the keyword using printf. 4. [a-zA-Z_][a-zA-Z0-9_]*: This rule matches identifiers, which consist of a letter or underscore followed by zero or more letters, digits, or underscores. When a match is found, the 10
  • 11. Describe: 5. “.” : This rule matches any character that doesn't match the previous rules. It represents all other characters in the input text. When a match is found, the associated action increments the others count. 6. int main(): This is the entry point of the program. It opens the "input.c" file for reading using fopen, then calls yylex() to start the lexical analysis process based on the defined Lex rules. After the analysis is complete, it prints the number of identifiers (id) found using printf. 7. int yywrap(): This function is called by the Lex-generated scanner when it reaches the end of input. It serves as the termination condition for the scanning process and returns 1 to indicate the end of input. 11
  • 12. Input. C File #include<stdio.h> //this is a comment int main() { int number = 10; double value = 1.67; printf("The number is: %dn", number); return 0; } /* this is a multi line comment */ 12
  • 14. 14