SlideShare a Scribd company logo
20DCE096 CE442: DESIGN OF LANGUAGE PROCESSOR 5
PRACTICAL - 2
Aim: Implement a lexical analyser using C.
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Define token types
enum TokenType {
KEYWORD,
IDENTIFIER,
LITERAL,
OPERATOR,
PUNCTUATION,
PREPROCESSOR_DIRECTIVE
};
// Function to classify and print tokens
void printToken(char* lexeme, enum TokenType type) {
char* tokenTypeStr;
switch (type) {
case KEYWORD:
tokenTypeStr = "Keyword";
break;
case IDENTIFIER:
tokenTypeStr = "Identifier";
break;
case LITERAL:
tokenTypeStr = "Literal";
break;
case OPERATOR:
tokenTypeStr = "Operator";
break;
case PUNCTUATION:
tokenTypeStr = "Punctuation";
break;
case PREPROCESSOR_DIRECTIVE:
tokenTypeStr = "Preprocessor Directive";
break;
default:
tokenTypeStr = "Unknown";
}
printf("Type: %s, Lexeme: %sn", tokenTypeStr, lexeme);
20DCE096 CE442: DESIGN OF LANGUAGE PROCESSOR 6
}
int main() {
FILE *file = fopen("input.txt", "r"); // Open the file in read mode
if (file == NULL) {
perror("Error opening file");
return 1;
}
char input[1000];
char lexeme[1000];
int i = 0;
int j = 0;
while (fgets(input, sizeof(input), file) != NULL) {
for (i = 0; i < strlen(input); i++) {
// Check for keywords
if (isalpha(input[i])) {
j = 0;
while (isalpha(input[i]) || isdigit(input[i])) {
lexeme[j++] = input[i++];
}
lexeme[j] = '0';
// You would have a list of keywords to compare against
if (strcmp(lexeme, "if") == 0 || strcmp(lexeme, "while") == 0 || strcmp(lexeme, "for") == 0)
{
printToken(lexeme, KEYWORD);
} else {
printToken(lexeme, IDENTIFIER);
}
i--;
}
// Check for numeric literals
else if (isdigit(input[i])) {
j = 0;
while (isdigit(input[i]) || input[i] == '.') {
lexeme[j++] = input[i++];
}
lexeme[j] = '0';
printToken(lexeme, LITERAL);
i--;
}
20DCE096 CE442: DESIGN OF LANGUAGE PROCESSOR 7
// Check for operators and punctuation
else if (strchr("+-*/=<>,;(){}", input[i]) != NULL) {
lexeme[0] = input[i];
lexeme[1] = '0';
printToken(lexeme, OPERATOR);
}
// Check for preprocessor directives
else if (input[i] == '#') {
j = 0;
while (input[i] != '0' && input[i] != 'n') {
lexeme[j++] = input[i++];
}
lexeme[j] = '0';
printToken(lexeme, PREPROCESSOR_DIRECTIVE);
}
}
}
fclose(file); // Close the file
return 0;
}
Output:
Conclusion: In this practical we implement lexical analyzer, which read content of text file and
analyze it.
Grade:
Signature:
Remarks:

More Related Content

More from ShivangPithadiya1

20DCE096_dlp_prac2.pdf
20DCE096_dlp_prac2.pdf20DCE096_dlp_prac2.pdf
20DCE096_dlp_prac2.pdf
ShivangPithadiya1
 
20DCE096_dlp_prac5.pdf
20DCE096_dlp_prac5.pdf20DCE096_dlp_prac5.pdf
20DCE096_dlp_prac5.pdf
ShivangPithadiya1
 

More from ShivangPithadiya1 (6)

20DCE096_prac1.pdf
20DCE096_prac1.pdf20DCE096_prac1.pdf
20DCE096_prac1.pdf
 
20DCE096_prac6.pdf
20DCE096_prac6.pdf20DCE096_prac6.pdf
20DCE096_prac6.pdf
 
20DCE096_prac10.pdf
20DCE096_prac10.pdf20DCE096_prac10.pdf
20DCE096_prac10.pdf
 
20DCE096_dlp_prac2.pdf
20DCE096_dlp_prac2.pdf20DCE096_dlp_prac2.pdf
20DCE096_dlp_prac2.pdf
 
20DCE096_prac8.pdf
20DCE096_prac8.pdf20DCE096_prac8.pdf
20DCE096_prac8.pdf
 
20DCE096_dlp_prac5.pdf
20DCE096_dlp_prac5.pdf20DCE096_dlp_prac5.pdf
20DCE096_dlp_prac5.pdf
 

Recently uploaded

Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
Krisztián Száraz
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
DhatriParmar
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Atul Kumar Singh
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
vaibhavrinwa19
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
EduSkills OECD
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
Kartik Tiwari
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
Nguyen Thanh Tu Collection
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
Atul Kumar Singh
 

Recently uploaded (20)

Advantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO PerspectiveAdvantages and Disadvantages of CMS from an SEO Perspective
Advantages and Disadvantages of CMS from an SEO Perspective
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.Biological Screening of Herbal Drugs in detailed.
Biological Screening of Herbal Drugs in detailed.
 
Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
The Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptxThe Diamond Necklace by Guy De Maupassant.pptx
The Diamond Necklace by Guy De Maupassant.pptx
 
Guidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th SemesterGuidance_and_Counselling.pdf B.Ed. 4th Semester
Guidance_and_Counselling.pdf B.Ed. 4th Semester
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
Acetabularia Information For Class 9 .docx
Acetabularia Information For Class 9  .docxAcetabularia Information For Class 9  .docx
Acetabularia Information For Class 9 .docx
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Francesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptxFrancesca Gottschalk - How can education support child empowerment.pptx
Francesca Gottschalk - How can education support child empowerment.pptx
 
Chapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdfChapter -12, Antibiotics (One Page Notes).pdf
Chapter -12, Antibiotics (One Page Notes).pdf
 
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
BÀI TẬP BỔ TRỢ TIẾNG ANH GLOBAL SUCCESS LỚP 3 - CẢ NĂM (CÓ FILE NGHE VÀ ĐÁP Á...
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
Language Across the Curriculm LAC B.Ed.
Language Across the  Curriculm LAC B.Ed.Language Across the  Curriculm LAC B.Ed.
Language Across the Curriculm LAC B.Ed.
 

20DCE096_dlp_prac2.pdf.

  • 1. 20DCE096 CE442: DESIGN OF LANGUAGE PROCESSOR 5 PRACTICAL - 2 Aim: Implement a lexical analyser using C. Code: #include <stdio.h> #include <string.h> #include <ctype.h> // Define token types enum TokenType { KEYWORD, IDENTIFIER, LITERAL, OPERATOR, PUNCTUATION, PREPROCESSOR_DIRECTIVE }; // Function to classify and print tokens void printToken(char* lexeme, enum TokenType type) { char* tokenTypeStr; switch (type) { case KEYWORD: tokenTypeStr = "Keyword"; break; case IDENTIFIER: tokenTypeStr = "Identifier"; break; case LITERAL: tokenTypeStr = "Literal"; break; case OPERATOR: tokenTypeStr = "Operator"; break; case PUNCTUATION: tokenTypeStr = "Punctuation"; break; case PREPROCESSOR_DIRECTIVE: tokenTypeStr = "Preprocessor Directive"; break; default: tokenTypeStr = "Unknown"; } printf("Type: %s, Lexeme: %sn", tokenTypeStr, lexeme);
  • 2. 20DCE096 CE442: DESIGN OF LANGUAGE PROCESSOR 6 } int main() { FILE *file = fopen("input.txt", "r"); // Open the file in read mode if (file == NULL) { perror("Error opening file"); return 1; } char input[1000]; char lexeme[1000]; int i = 0; int j = 0; while (fgets(input, sizeof(input), file) != NULL) { for (i = 0; i < strlen(input); i++) { // Check for keywords if (isalpha(input[i])) { j = 0; while (isalpha(input[i]) || isdigit(input[i])) { lexeme[j++] = input[i++]; } lexeme[j] = '0'; // You would have a list of keywords to compare against if (strcmp(lexeme, "if") == 0 || strcmp(lexeme, "while") == 0 || strcmp(lexeme, "for") == 0) { printToken(lexeme, KEYWORD); } else { printToken(lexeme, IDENTIFIER); } i--; } // Check for numeric literals else if (isdigit(input[i])) { j = 0; while (isdigit(input[i]) || input[i] == '.') { lexeme[j++] = input[i++]; } lexeme[j] = '0'; printToken(lexeme, LITERAL); i--; }
  • 3. 20DCE096 CE442: DESIGN OF LANGUAGE PROCESSOR 7 // Check for operators and punctuation else if (strchr("+-*/=<>,;(){}", input[i]) != NULL) { lexeme[0] = input[i]; lexeme[1] = '0'; printToken(lexeme, OPERATOR); } // Check for preprocessor directives else if (input[i] == '#') { j = 0; while (input[i] != '0' && input[i] != 'n') { lexeme[j++] = input[i++]; } lexeme[j] = '0'; printToken(lexeme, PREPROCESSOR_DIRECTIVE); } } } fclose(file); // Close the file return 0; } Output: Conclusion: In this practical we implement lexical analyzer, which read content of text file and analyze it. Grade: Signature: Remarks: