SlideShare uma empresa Scribd logo
1 de 4
Baixar para ler offline
Using the sample C Programming Language code in Section 4.2 (Lexical Analysis) of the
Textbook, create the C++ equivalent (front.cpp). Specifically, you need to convert the C
input and output statements to their C++ equivalents. Also, use the C++ string data type
instead of the C character array, wherever possible.
#include <stdio.h>
#include <ctype.h>
int charClass;
char lexeme [100];
char nextChar;
int lexLen;
int token;
int nextToken;
FILE *in_fp, *fopen();
//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
main() {
//Open the input data file
if ((in_fp = fopen ("front.in", "r")) == NULL)
printf("ERROR - cannot open front.in n");
else {
getChar();
do {
lex();
} while (nextToken!= EOF);
}
}
/**********************************/
/*lookup - a function to lookup*/
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
void addChar() {
if (lexLen <= 98) {
lexeme[lexLen++] = nextChar;
lexeme[lexLen] = 0;
}
else
printf("Error - lexeme is too long n");
}
/***********************/
//getChar - a function ton get the
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
void getNonBlank() {
while (isspace(nextChar))
getChar();
}
/*******************************/
//lex - a simple lexical analyzer
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
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT) {
addChar();
getChar();
}
nextToken = INT_LIT;
break;
//Parantheses 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

Mais conteúdo relacionado

Semelhante a Using the sample C Programming Language code in Section 4-2 (Lexical A.pdf

C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
I need help with implementing the priority queue data structure with a.docx
I need help with implementing the priority queue data structure with a.docxI need help with implementing the priority queue data structure with a.docx
I need help with implementing the priority queue data structure with a.docxhendriciraida
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.Russell Childs
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptSandipPradhan23
 
Java Programming Below are the lexerjava tokenjava and .pdf
Java Programming Below are the lexerjava tokenjava and .pdfJava Programming Below are the lexerjava tokenjava and .pdf
Java Programming Below are the lexerjava tokenjava and .pdfadinathassociates
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)ujihisa
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaonyash production
 
Welcome to Modern C++
Welcome to Modern C++Welcome to Modern C++
Welcome to Modern C++Seok-joon Yun
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...ssuserd6b1fd
 
JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervosoLuis Vendrame
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdfC++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdfrahulfancycorner21
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfjyothimuppasani1
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answerssheibansari
 
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdfWrite message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdfeyeonsecuritysystems
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
Add a 3rd field help that contains a short help string for each of t.pdf
Add a 3rd field help that contains a short help string for each of t.pdfAdd a 3rd field help that contains a short help string for each of t.pdf
Add a 3rd field help that contains a short help string for each of t.pdfinfo245627
 

Semelhante a Using the sample C Programming Language code in Section 4-2 (Lexical A.pdf (20)

C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Lecture 3 c++
Lecture 3 c++Lecture 3 c++
Lecture 3 c++
 
String Manipulation Function and Header File Functions
String Manipulation Function and Header File FunctionsString Manipulation Function and Header File Functions
String Manipulation Function and Header File Functions
 
I need help with implementing the priority queue data structure with a.docx
I need help with implementing the priority queue data structure with a.docxI need help with implementing the priority queue data structure with a.docx
I need help with implementing the priority queue data structure with a.docx
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
Java Programming Below are the lexerjava tokenjava and .pdf
Java Programming Below are the lexerjava tokenjava and .pdfJava Programming Below are the lexerjava tokenjava and .pdf
Java Programming Below are the lexerjava tokenjava and .pdf
 
Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)Hacking parse.y (RubyKansai38)
Hacking parse.y (RubyKansai38)
 
c++ practical Digvajiya collage Rajnandgaon
c++ practical  Digvajiya collage Rajnandgaonc++ practical  Digvajiya collage Rajnandgaon
c++ practical Digvajiya collage Rajnandgaon
 
Welcome to Modern C++
Welcome to Modern C++Welcome to Modern C++
Welcome to Modern C++
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
JavaScript - Agora nervoso
JavaScript - Agora nervosoJavaScript - Agora nervoso
JavaScript - Agora nervoso
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdfC++ code, please help! Troubleshooting and cannot for the life of me.pdf
C++ code, please help! Troubleshooting and cannot for the life of me.pdf
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
 
Overloading
OverloadingOverloading
Overloading
 
C++aptitude questions and answers
C++aptitude questions and answersC++aptitude questions and answers
C++aptitude questions and answers
 
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdfWrite message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
Write message.cpp and priorityq.cpp. The code in message.cpp and pri.pdf
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Add a 3rd field help that contains a short help string for each of t.pdf
Add a 3rd field help that contains a short help string for each of t.pdfAdd a 3rd field help that contains a short help string for each of t.pdf
Add a 3rd field help that contains a short help string for each of t.pdf
 

Mais de OwenPBLRobertsv

void insertFixUp (Node node) This method recolors and rotates the tree.pdf
void insertFixUp (Node node) This method recolors and rotates the tree.pdfvoid insertFixUp (Node node) This method recolors and rotates the tree.pdf
void insertFixUp (Node node) This method recolors and rotates the tree.pdfOwenPBLRobertsv
 
Vitamins are organic compounds that you require in small amounts for i (1).pdf
Vitamins are organic compounds that you require in small amounts for i (1).pdfVitamins are organic compounds that you require in small amounts for i (1).pdf
Vitamins are organic compounds that you require in small amounts for i (1).pdfOwenPBLRobertsv
 
Valentino is a patient in a nursing home for 50 days of 2022- While in.pdf
Valentino is a patient in a nursing home for 50 days of 2022- While in.pdfValentino is a patient in a nursing home for 50 days of 2022- While in.pdf
Valentino is a patient in a nursing home for 50 days of 2022- While in.pdfOwenPBLRobertsv
 
Variable overhead cost- amounts as positive numbers- Leno Manufacturin.pdf
Variable overhead cost- amounts as positive numbers- Leno Manufacturin.pdfVariable overhead cost- amounts as positive numbers- Leno Manufacturin.pdf
Variable overhead cost- amounts as positive numbers- Leno Manufacturin.pdfOwenPBLRobertsv
 
Using the image below answer the following questions- a- Wh.pdf
Using the image below answer the following questions-            a- Wh.pdfUsing the image below answer the following questions-            a- Wh.pdf
Using the image below answer the following questions- a- Wh.pdfOwenPBLRobertsv
 
Using the information in the table below- what is the value of the M2.pdf
Using the information in the table below- what is the value of the M2.pdfUsing the information in the table below- what is the value of the M2.pdf
Using the information in the table below- what is the value of the M2.pdfOwenPBLRobertsv
 
What is an organophosphate pesticide- Why is this especially important.pdf
What is an organophosphate pesticide- Why is this especially important.pdfWhat is an organophosphate pesticide- Why is this especially important.pdf
What is an organophosphate pesticide- Why is this especially important.pdfOwenPBLRobertsv
 
What is a hyperspectral remote sensing system- What are the advantages.pdf
What is a hyperspectral remote sensing system- What are the advantages.pdfWhat is a hyperspectral remote sensing system- What are the advantages.pdf
What is a hyperspectral remote sensing system- What are the advantages.pdfOwenPBLRobertsv
 
What is a weakness of GDP as a metric- A- It doesn't measure non-monet.pdf
What is a weakness of GDP as a metric- A- It doesn't measure non-monet.pdfWhat is a weakness of GDP as a metric- A- It doesn't measure non-monet.pdf
What is a weakness of GDP as a metric- A- It doesn't measure non-monet.pdfOwenPBLRobertsv
 
what is Credit & Cash Collections-.pdf
what is Credit & Cash Collections-.pdfwhat is Credit & Cash Collections-.pdf
what is Credit & Cash Collections-.pdfOwenPBLRobertsv
 
What is a heartbeat protocol- Select one- a- A serious security bug in.pdf
What is a heartbeat protocol- Select one- a- A serious security bug in.pdfWhat is a heartbeat protocol- Select one- a- A serious security bug in.pdf
What is a heartbeat protocol- Select one- a- A serious security bug in.pdfOwenPBLRobertsv
 
What is 'limited liability-' a- Limited liability refers to the abilit.pdf
What is 'limited liability-' a- Limited liability refers to the abilit.pdfWhat is 'limited liability-' a- Limited liability refers to the abilit.pdf
What is 'limited liability-' a- Limited liability refers to the abilit.pdfOwenPBLRobertsv
 
What influences MNE to disperse or concentrate value activities- expla.pdf
What influences MNE to disperse or concentrate value activities- expla.pdfWhat influences MNE to disperse or concentrate value activities- expla.pdf
What influences MNE to disperse or concentrate value activities- expla.pdfOwenPBLRobertsv
 
What is a community- How may terrestrial and marine trophic pyramids d.pdf
What is a community- How may terrestrial and marine trophic pyramids d.pdfWhat is a community- How may terrestrial and marine trophic pyramids d.pdf
What is a community- How may terrestrial and marine trophic pyramids d.pdfOwenPBLRobertsv
 
What influences MNE to disperse or concentrate value activities- A- Su.pdf
What influences MNE to disperse or concentrate value activities- A- Su.pdfWhat influences MNE to disperse or concentrate value activities- A- Su.pdf
What influences MNE to disperse or concentrate value activities- A- Su.pdfOwenPBLRobertsv
 
What is a deficiency of the static planning budget- How does flexible.pdf
What is a deficiency of the static planning budget- How does flexible.pdfWhat is a deficiency of the static planning budget- How does flexible.pdf
What is a deficiency of the static planning budget- How does flexible.pdfOwenPBLRobertsv
 
What happens when the value of inventory is lower than its cost- 1- th.pdf
What happens when the value of inventory is lower than its cost- 1- th.pdfWhat happens when the value of inventory is lower than its cost- 1- th.pdf
What happens when the value of inventory is lower than its cost- 1- th.pdfOwenPBLRobertsv
 
What does the operator () stand for A-B in HDL- Intersection Complemen.pdf
What does the operator () stand for A-B in HDL- Intersection Complemen.pdfWhat does the operator () stand for A-B in HDL- Intersection Complemen.pdf
What does the operator () stand for A-B in HDL- Intersection Complemen.pdfOwenPBLRobertsv
 
What happens during isovolumetric contraction- AV and semilunar valves.pdf
What happens during isovolumetric contraction- AV and semilunar valves.pdfWhat happens during isovolumetric contraction- AV and semilunar valves.pdf
What happens during isovolumetric contraction- AV and semilunar valves.pdfOwenPBLRobertsv
 
What does it mean to say an organization is an open system- How is the.pdf
What does it mean to say an organization is an open system- How is the.pdfWhat does it mean to say an organization is an open system- How is the.pdf
What does it mean to say an organization is an open system- How is the.pdfOwenPBLRobertsv
 

Mais de OwenPBLRobertsv (20)

void insertFixUp (Node node) This method recolors and rotates the tree.pdf
void insertFixUp (Node node) This method recolors and rotates the tree.pdfvoid insertFixUp (Node node) This method recolors and rotates the tree.pdf
void insertFixUp (Node node) This method recolors and rotates the tree.pdf
 
Vitamins are organic compounds that you require in small amounts for i (1).pdf
Vitamins are organic compounds that you require in small amounts for i (1).pdfVitamins are organic compounds that you require in small amounts for i (1).pdf
Vitamins are organic compounds that you require in small amounts for i (1).pdf
 
Valentino is a patient in a nursing home for 50 days of 2022- While in.pdf
Valentino is a patient in a nursing home for 50 days of 2022- While in.pdfValentino is a patient in a nursing home for 50 days of 2022- While in.pdf
Valentino is a patient in a nursing home for 50 days of 2022- While in.pdf
 
Variable overhead cost- amounts as positive numbers- Leno Manufacturin.pdf
Variable overhead cost- amounts as positive numbers- Leno Manufacturin.pdfVariable overhead cost- amounts as positive numbers- Leno Manufacturin.pdf
Variable overhead cost- amounts as positive numbers- Leno Manufacturin.pdf
 
Using the image below answer the following questions- a- Wh.pdf
Using the image below answer the following questions-            a- Wh.pdfUsing the image below answer the following questions-            a- Wh.pdf
Using the image below answer the following questions- a- Wh.pdf
 
Using the information in the table below- what is the value of the M2.pdf
Using the information in the table below- what is the value of the M2.pdfUsing the information in the table below- what is the value of the M2.pdf
Using the information in the table below- what is the value of the M2.pdf
 
What is an organophosphate pesticide- Why is this especially important.pdf
What is an organophosphate pesticide- Why is this especially important.pdfWhat is an organophosphate pesticide- Why is this especially important.pdf
What is an organophosphate pesticide- Why is this especially important.pdf
 
What is a hyperspectral remote sensing system- What are the advantages.pdf
What is a hyperspectral remote sensing system- What are the advantages.pdfWhat is a hyperspectral remote sensing system- What are the advantages.pdf
What is a hyperspectral remote sensing system- What are the advantages.pdf
 
What is a weakness of GDP as a metric- A- It doesn't measure non-monet.pdf
What is a weakness of GDP as a metric- A- It doesn't measure non-monet.pdfWhat is a weakness of GDP as a metric- A- It doesn't measure non-monet.pdf
What is a weakness of GDP as a metric- A- It doesn't measure non-monet.pdf
 
what is Credit & Cash Collections-.pdf
what is Credit & Cash Collections-.pdfwhat is Credit & Cash Collections-.pdf
what is Credit & Cash Collections-.pdf
 
What is a heartbeat protocol- Select one- a- A serious security bug in.pdf
What is a heartbeat protocol- Select one- a- A serious security bug in.pdfWhat is a heartbeat protocol- Select one- a- A serious security bug in.pdf
What is a heartbeat protocol- Select one- a- A serious security bug in.pdf
 
What is 'limited liability-' a- Limited liability refers to the abilit.pdf
What is 'limited liability-' a- Limited liability refers to the abilit.pdfWhat is 'limited liability-' a- Limited liability refers to the abilit.pdf
What is 'limited liability-' a- Limited liability refers to the abilit.pdf
 
What influences MNE to disperse or concentrate value activities- expla.pdf
What influences MNE to disperse or concentrate value activities- expla.pdfWhat influences MNE to disperse or concentrate value activities- expla.pdf
What influences MNE to disperse or concentrate value activities- expla.pdf
 
What is a community- How may terrestrial and marine trophic pyramids d.pdf
What is a community- How may terrestrial and marine trophic pyramids d.pdfWhat is a community- How may terrestrial and marine trophic pyramids d.pdf
What is a community- How may terrestrial and marine trophic pyramids d.pdf
 
What influences MNE to disperse or concentrate value activities- A- Su.pdf
What influences MNE to disperse or concentrate value activities- A- Su.pdfWhat influences MNE to disperse or concentrate value activities- A- Su.pdf
What influences MNE to disperse or concentrate value activities- A- Su.pdf
 
What is a deficiency of the static planning budget- How does flexible.pdf
What is a deficiency of the static planning budget- How does flexible.pdfWhat is a deficiency of the static planning budget- How does flexible.pdf
What is a deficiency of the static planning budget- How does flexible.pdf
 
What happens when the value of inventory is lower than its cost- 1- th.pdf
What happens when the value of inventory is lower than its cost- 1- th.pdfWhat happens when the value of inventory is lower than its cost- 1- th.pdf
What happens when the value of inventory is lower than its cost- 1- th.pdf
 
What does the operator () stand for A-B in HDL- Intersection Complemen.pdf
What does the operator () stand for A-B in HDL- Intersection Complemen.pdfWhat does the operator () stand for A-B in HDL- Intersection Complemen.pdf
What does the operator () stand for A-B in HDL- Intersection Complemen.pdf
 
What happens during isovolumetric contraction- AV and semilunar valves.pdf
What happens during isovolumetric contraction- AV and semilunar valves.pdfWhat happens during isovolumetric contraction- AV and semilunar valves.pdf
What happens during isovolumetric contraction- AV and semilunar valves.pdf
 
What does it mean to say an organization is an open system- How is the.pdf
What does it mean to say an organization is an open system- How is the.pdfWhat does it mean to say an organization is an open system- How is the.pdf
What does it mean to say an organization is an open system- How is the.pdf
 

Último

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
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.pptxMaritesTamaniVerdade
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
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.pdfAdmir Softic
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
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...pradhanghanshyam7136
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
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.christianmathematics
 
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.pdfNirmal Dwivedi
 

Último (20)

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).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
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
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
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
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...
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
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.
 
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
 

Using the sample C Programming Language code in Section 4-2 (Lexical A.pdf

  • 1. Using the sample C Programming Language code in Section 4.2 (Lexical Analysis) of the Textbook, create the C++ equivalent (front.cpp). Specifically, you need to convert the C input and output statements to their C++ equivalents. Also, use the C++ string data type instead of the C character array, wherever possible. #include <stdio.h> #include <ctype.h> int charClass; char lexeme [100]; char nextChar; int lexLen; int token; int nextToken; FILE *in_fp, *fopen(); //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 main() { //Open the input data file if ((in_fp = fopen ("front.in", "r")) == NULL) printf("ERROR - cannot open front.in n"); else { getChar(); do { lex(); } while (nextToken!= EOF);
  • 2. } } /**********************************/ /*lookup - a function to lookup*/ 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 void addChar() {
  • 3. if (lexLen <= 98) { lexeme[lexLen++] = nextChar; lexeme[lexLen] = 0; } else printf("Error - lexeme is too long n"); } /***********************/ //getChar - a function ton get the 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 void getNonBlank() { while (isspace(nextChar)) getChar(); } /*******************************/ //lex - a simple lexical analyzer 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 case DIGIT: addChar();
  • 4. getChar(); while (charClass == DIGIT) { addChar(); getChar(); } nextToken = INT_LIT; break; //Parantheses 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