SlideShare uma empresa Scribd logo
1 de 5
Baixar para ler offline
Project description: Modify the Simple lexer program given in the link below to: Add tokens so
that all the tokens in the program from Activity 1 of this module are represented (75 points). The
output of your program should specify the type of token in words (Identifier, Keyword ...etc) not
a number (25 points). Print each token on a separate line.
Code:
main.cpp file
#include <stdio.h>
#include <ctype.h>
/* Global declarations */
/* Variables */
int charClass;
char lexeme [100];
char nextChar;
int lexLen;
int nextToken;
FILE *in_fp;
/* Function declarations */
void addChar();
void getChar();
void getNonBlank();
int lex();
/* Character classes */
#define LETTER 0
#define DIGIT 1
#define UNKNOWN 99
/* Token codes */
#define INT_LIT 10
#define IDENT 11
#define ASSIGN_OP 20
#define ADD_OP 21
#define SUB_OP 22
#define MULT_OP 23
#define DIV_OP 24
#define LEFT_PAREN 25
#define RIGHT_PAREN 26
/******************************************************/
/* main driver */
int main()
{
/* Open the input data file and process its contents */
if ((in_fp = fopen("front.in", "r")) == NULL) {
printf("ERROR - cannot open front.in n");
} else {
getChar();
do {
lex();
} while (nextToken != EOF);
}
return 0;
}
/*****************************************************/
/* lookup - a function to lookup operators and parentheses and return the
* token */
int lookup(char ch) {
switch (ch) {
case '(':
addChar();
nextToken = LEFT_PAREN;
break;
case ')':
addChar();
nextToken = RIGHT_PAREN;
break;
case '+':
addChar();
nextToken = ADD_OP;
break;
case '-':
addChar();
nextToken = SUB_OP;
break;
case '*':
addChar();
nextToken = MULT_OP;
break;
case '/':
addChar();
nextToken = DIV_OP;
break;
default:
addChar();
nextToken = EOF;
break;
}
return nextToken;
}
/*****************************************************/
/* addChar - a function to add nextChar to lexeme */
void addChar() {
if (lexLen <= 98) {
lexeme[lexLen++] = nextChar;
lexeme[lexLen] = 0;
} else {
printf("Error - lexeme is too long n");
}
}
/*****************************************************/
/* getChar - a function to get the next character of input and determine its
* character class */
void getChar() {
if ((nextChar = getc(in_fp)) != EOF) {
if (isalpha(nextChar))
charClass = LETTER;
else if (isdigit(nextChar))
charClass = DIGIT;
else charClass = UNKNOWN;
} else {
charClass = EOF;
}
}
/*****************************************************/
/* getNonBlank - a function to call getChar until it returns a non-whitespace
* character */
void getNonBlank() {
while (isspace(nextChar)) getChar();
}
/*****************************************************/
/* lex - a simple lexical analyzer for arithmetic expressions */
int lex() {
lexLen = 0;
getNonBlank();
switch (charClass) {
/* Parse identifiers */
case LETTER:
addChar();
getChar();
while (charClass == LETTER || charClass == DIGIT) {
addChar();
getChar();
}
nextToken = IDENT;
break;
/* Parse integer literals */
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT) {
addChar();
getChar();
}
nextToken = INT_LIT;
break;
/* Parentheses and operators */
case UNKNOWN:
lookup(nextChar);
getChar();
break;
/* EOF */
case EOF:
nextToken = EOF;
lexeme[0] = 'E';
lexeme[1] = 'O';
lexeme[2] = 'F';
lexeme[3] = 0;
break;
} /* End of switch */
printf("Next token is: %d, Next lexeme is %sn", nextToken, lexeme);
return nextToken;
} /* End of function lex */
front.in file
(sum + 47)/total
Project description- Modify the Simple lexer program given in the link.pdf

Mais conteúdo relacionado

Semelhante a Project description- Modify the Simple lexer program given in the link.pdf

finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
ChereCheek752
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
stn_tkiller
 
Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2
Max Kleiner
 
GEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkGEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions Framework
Alexey Smirnov
 
Part APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docxPart APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docx
dewhirstichabod
 

Semelhante a Project description- Modify the Simple lexer program given in the link.pdf (20)

Base de-datos
Base de-datosBase de-datos
Base de-datos
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
14 operator overloading
14 operator overloading14 operator overloading
14 operator overloading
 
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the bfinalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
finalprojtemplatev5finalprojtemplate.gitignore# Ignore the b
 
Flex lexer.h -c++-- flexlexer.h -- define interfaces f
Flex lexer.h  -c++-- flexlexer.h -- define interfaces fFlex lexer.h  -c++-- flexlexer.h -- define interfaces f
Flex lexer.h -c++-- flexlexer.h -- define interfaces f
 
Lex programming
Lex programmingLex programming
Lex programming
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
 
CP 04.pptx
CP 04.pptxCP 04.pptx
CP 04.pptx
 
Array Cont
Array ContArray Cont
Array Cont
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabus
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
Function C++
Function C++ Function C++
Function C++
 
Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2Pascal script maxbox_ekon_14_2
Pascal script maxbox_ekon_14_2
 
GEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions FrameworkGEM - GNU C Compiler Extensions Framework
GEM - GNU C Compiler Extensions Framework
 
Part APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docxPart APurposeThis laboratory provides some experience work.docx
Part APurposeThis laboratory provides some experience work.docx
 
System programmin practical file
System programmin practical fileSystem programmin practical file
System programmin practical file
 
Qe Reference
Qe ReferenceQe Reference
Qe Reference
 
Clean code slide
Clean code slideClean code slide
Clean code slide
 
Clean code _v2003
 Clean code _v2003 Clean code _v2003
Clean code _v2003
 

Mais de 3rdeyesolutions

public class EllipsoidList { private String list- private Array.pdf
public class EllipsoidList {     private String list-    private Array.pdfpublic class EllipsoidList {     private String list-    private Array.pdf
public class EllipsoidList { private String list- private Array.pdf
3rdeyesolutions
 
public class ImpPythTripEx public static double---- triples - new d.pdf
public class ImpPythTripEx    public static double---- triples - new d.pdfpublic class ImpPythTripEx    public static double---- triples - new d.pdf
public class ImpPythTripEx public static double---- triples - new d.pdf
3rdeyesolutions
 

Mais de 3rdeyesolutions (20)

Python Use Request library only- Use the proper widgets - fonts and ba.pdf
Python Use Request library only- Use the proper widgets - fonts and ba.pdfPython Use Request library only- Use the proper widgets - fonts and ba.pdf
Python Use Request library only- Use the proper widgets - fonts and ba.pdf
 
Python Questions (pictured) G-1 What is the impact of the PATH environ.pdf
Python Questions (pictured) G-1 What is the impact of the PATH environ.pdfPython Questions (pictured) G-1 What is the impact of the PATH environ.pdf
Python Questions (pictured) G-1 What is the impact of the PATH environ.pdf
 
Python Questions (pictured) E-1 How can you replace the current proces.pdf
Python Questions (pictured) E-1 How can you replace the current proces.pdfPython Questions (pictured) E-1 How can you replace the current proces.pdf
Python Questions (pictured) E-1 How can you replace the current proces.pdf
 
Python Questions (pictured) F-1 Compiling the program in Step 1- how.pdf
Python Questions (pictured)   F-1 Compiling the program in Step 1- how.pdfPython Questions (pictured)   F-1 Compiling the program in Step 1- how.pdf
Python Questions (pictured) F-1 Compiling the program in Step 1- how.pdf
 
Python Questions (pictured) D-1 What happens to the file -etc-zzz when.pdf
Python Questions (pictured) D-1 What happens to the file -etc-zzz when.pdfPython Questions (pictured) D-1 What happens to the file -etc-zzz when.pdf
Python Questions (pictured) D-1 What happens to the file -etc-zzz when.pdf
 
python program ile Handling- Collection - given the file currency-csv.pdf
python program ile Handling- Collection - given the file currency-csv.pdfpython program ile Handling- Collection - given the file currency-csv.pdf
python program ile Handling- Collection - given the file currency-csv.pdf
 
Python is a popular programming language used in a wide range of field.pdf
Python is a popular programming language used in a wide range of field.pdfPython is a popular programming language used in a wide range of field.pdf
Python is a popular programming language used in a wide range of field.pdf
 
PYTHON CODING I have to create a Red vs Blue game in python with a GUI.pdf
PYTHON CODING I have to create a Red vs Blue game in python with a GUI.pdfPYTHON CODING I have to create a Red vs Blue game in python with a GUI.pdf
PYTHON CODING I have to create a Red vs Blue game in python with a GUI.pdf
 
python code plz Task 2 1- Create a list with the digits in your studen.pdf
python code plz Task 2 1- Create a list with the digits in your studen.pdfpython code plz Task 2 1- Create a list with the digits in your studen.pdf
python code plz Task 2 1- Create a list with the digits in your studen.pdf
 
PYTHON - time and space complexity for the next function- def some_fun.pdf
PYTHON - time and space complexity for the next function- def some_fun.pdfPYTHON - time and space complexity for the next function- def some_fun.pdf
PYTHON - time and space complexity for the next function- def some_fun.pdf
 
Python - Explain the concept- and why you found it interesting- How w.pdf
Python -  Explain the concept- and why you found it interesting- How w.pdfPython -  Explain the concept- and why you found it interesting- How w.pdf
Python - Explain the concept- and why you found it interesting- How w.pdf
 
Put the steps of endochondral ossification in the correct order- Your.pdf
Put the steps of endochondral ossification in the correct order- Your.pdfPut the steps of endochondral ossification in the correct order- Your.pdf
Put the steps of endochondral ossification in the correct order- Your.pdf
 
Put the events of HIV infection in the correct order- HIV fuses to the.pdf
Put the events of HIV infection in the correct order- HIV fuses to the.pdfPut the events of HIV infection in the correct order- HIV fuses to the.pdf
Put the events of HIV infection in the correct order- HIV fuses to the.pdf
 
Put the following events in the proper order of occurrence when RIK pa.pdf
Put the following events in the proper order of occurrence when RIK pa.pdfPut the following events in the proper order of occurrence when RIK pa.pdf
Put the following events in the proper order of occurrence when RIK pa.pdf
 
Put an I if the term refers to Innate Immunity Put an A if the term re.pdf
Put an I if the term refers to Innate Immunity Put an A if the term re.pdfPut an I if the term refers to Innate Immunity Put an A if the term re.pdf
Put an I if the term refers to Innate Immunity Put an A if the term re.pdf
 
Pure tone averages (PTAs) give us information about- how well a person.pdf
Pure tone averages (PTAs) give us information about- how well a person.pdfPure tone averages (PTAs) give us information about- how well a person.pdf
Pure tone averages (PTAs) give us information about- how well a person.pdf
 
public class EllipsoidList { private String list- private Array.pdf
public class EllipsoidList {     private String list-    private Array.pdfpublic class EllipsoidList {     private String list-    private Array.pdf
public class EllipsoidList { private String list- private Array.pdf
 
Public Administration -what kind of challenges does the Department of.pdf
Public Administration -what kind of challenges does the Department of.pdfPublic Administration -what kind of challenges does the Department of.pdf
Public Administration -what kind of challenges does the Department of.pdf
 
public class ImpPythTripEx public static double---- triples - new d.pdf
public class ImpPythTripEx    public static double---- triples - new d.pdfpublic class ImpPythTripEx    public static double---- triples - new d.pdf
public class ImpPythTripEx public static double---- triples - new d.pdf
 
public class Integenthalysis public static doumle analyreints(iet max-.pdf
public class Integenthalysis public static doumle analyreints(iet max-.pdfpublic class Integenthalysis public static doumle analyreints(iet max-.pdf
public class Integenthalysis public static doumle analyreints(iet max-.pdf
 

Último

Último (20)

Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 

Project description- Modify the Simple lexer program given in the link.pdf

  • 1. Project description: Modify the Simple lexer program given in the link below to: Add tokens so that all the tokens in the program from Activity 1 of this module are represented (75 points). The output of your program should specify the type of token in words (Identifier, Keyword ...etc) not a number (25 points). Print each token on a separate line. Code: main.cpp file #include <stdio.h> #include <ctype.h> /* Global declarations */ /* Variables */ int charClass; char lexeme [100]; char nextChar; int lexLen; int nextToken; FILE *in_fp; /* Function declarations */ void addChar(); void getChar(); void getNonBlank(); int lex(); /* Character classes */ #define LETTER 0 #define DIGIT 1 #define UNKNOWN 99 /* Token codes */ #define INT_LIT 10 #define IDENT 11 #define ASSIGN_OP 20 #define ADD_OP 21 #define SUB_OP 22 #define MULT_OP 23 #define DIV_OP 24 #define LEFT_PAREN 25 #define RIGHT_PAREN 26 /******************************************************/ /* main driver */ int main()
  • 2. { /* Open the input data file and process its contents */ if ((in_fp = fopen("front.in", "r")) == NULL) { printf("ERROR - cannot open front.in n"); } else { getChar(); do { lex(); } while (nextToken != EOF); } return 0; } /*****************************************************/ /* lookup - a function to lookup operators and parentheses and return the * token */ int lookup(char ch) { switch (ch) { case '(': addChar(); nextToken = LEFT_PAREN; break; case ')': addChar(); nextToken = RIGHT_PAREN; break; case '+': addChar(); nextToken = ADD_OP; break; case '-': addChar(); nextToken = SUB_OP; break; case '*': addChar(); nextToken = MULT_OP; break; case '/': addChar(); nextToken = DIV_OP; break; default: addChar(); nextToken = EOF;
  • 3. break; } return nextToken; } /*****************************************************/ /* addChar - a function to add nextChar to lexeme */ void addChar() { if (lexLen <= 98) { lexeme[lexLen++] = nextChar; lexeme[lexLen] = 0; } else { printf("Error - lexeme is too long n"); } } /*****************************************************/ /* getChar - a function to get the next character of input and determine its * character class */ void getChar() { if ((nextChar = getc(in_fp)) != EOF) { if (isalpha(nextChar)) charClass = LETTER; else if (isdigit(nextChar)) charClass = DIGIT; else charClass = UNKNOWN; } else { charClass = EOF; } } /*****************************************************/ /* getNonBlank - a function to call getChar until it returns a non-whitespace * character */ void getNonBlank() { while (isspace(nextChar)) getChar(); } /*****************************************************/ /* lex - a simple lexical analyzer for arithmetic expressions */ int lex() { lexLen = 0; getNonBlank(); switch (charClass) { /* Parse identifiers */
  • 4. case LETTER: addChar(); getChar(); while (charClass == LETTER || charClass == DIGIT) { addChar(); getChar(); } nextToken = IDENT; break; /* Parse integer literals */ case DIGIT: addChar(); getChar(); while (charClass == DIGIT) { addChar(); getChar(); } nextToken = INT_LIT; break; /* Parentheses and operators */ case UNKNOWN: lookup(nextChar); getChar(); break; /* EOF */ case EOF: nextToken = EOF; lexeme[0] = 'E'; lexeme[1] = 'O'; lexeme[2] = 'F'; lexeme[3] = 0; break; } /* End of switch */ printf("Next token is: %d, Next lexeme is %sn", nextToken, lexeme); return nextToken; } /* End of function lex */ front.in file (sum + 47)/total