SlideShare a Scribd company logo
1 of 11
Download to read offline
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.
main.cpp
front.in
(sum + 47) / total
/**
* This the example lexical analyzer code in pages 173 - 177 of the
* textbook,
*
* Sebesta, R. W. (2012). Concepts of Programming Languages.
* Pearson, 10th edition.
*
*/
/* front.c - a lexical analyzer system for simple arithmetic expressions */
#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 */

More Related Content

Similar to Modify the Simple lexer program given in the link below to .pdf

Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functionsmussawir20
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.Russell Childs
 
Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfnitinarora01
 
仕事で使うF#
仕事で使うF#仕事で使うF#
仕事で使うF#bleis tift
 
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
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdfafgt2012
 
Main issues with the following code-Im not sure if its reading the fil.pdf
Main issues with the following code-Im not sure if its reading the fil.pdfMain issues with the following code-Im not sure if its reading the fil.pdf
Main issues with the following code-Im not sure if its reading the fil.pdfaonesalem
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxBrianGHiNewmanv
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfforladies
 
C++ Function
C++ FunctionC++ Function
C++ FunctionHajar
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonGenerating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonTristan Penman
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overviewstn_tkiller
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacksmaamir farooq
 

Similar to Modify the Simple lexer program given in the link below to .pdf (20)

CPP Assignment Help
CPP Assignment HelpCPP Assignment Help
CPP Assignment Help
 
Php Reusing Code And Writing Functions
Php Reusing Code And Writing FunctionsPhp Reusing Code And Writing Functions
Php Reusing Code And Writing Functions
 
Recursion to iteration automation.
Recursion to iteration automation.Recursion to iteration automation.
Recursion to iteration automation.
 
Please need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdfPlease need help on following program using c++ language. Please inc.pdf
Please need help on following program using c++ language. Please inc.pdf
 
仕事で使うF#
仕事で使うF#仕事で使うF#
仕事で使うF#
 
Programming Homework Help
Programming Homework Help Programming Homework Help
Programming Homework Help
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
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
 
Programming Assignment Help
Programming Assignment HelpProgramming Assignment Help
Programming Assignment Help
 
1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf1- The design of a singly-linked list below is a picture of the functi (1).pdf
1- The design of a singly-linked list below is a picture of the functi (1).pdf
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Main issues with the following code-Im not sure if its reading the fil.pdf
Main issues with the following code-Im not sure if its reading the fil.pdfMain issues with the following code-Im not sure if its reading the fil.pdf
Main issues with the following code-Im not sure if its reading the fil.pdf
 
C++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docxC++ Please write the whole code that is needed for this assignment- wr.docx
C++ Please write the whole code that is needed for this assignment- wr.docx
 
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdfI need to fill-in TODOs in .cpp file and in .h file Could some.pdf
I need to fill-in TODOs in .cpp file and in .h file Could some.pdf
 
Array Cont
Array ContArray Cont
Array Cont
 
C++ Function
C++ FunctionC++ Function
C++ Function
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonGenerating parsers using Ragel and Lemon
Generating parsers using Ragel and Lemon
 
Gift-VT Tools Development Overview
Gift-VT Tools Development OverviewGift-VT Tools Development Overview
Gift-VT Tools Development Overview
 
STACK Applications in DS
STACK Applications in DSSTACK Applications in DS
STACK Applications in DS
 
Data structures stacks
Data structures   stacksData structures   stacks
Data structures stacks
 

More from adityaenterprise32

Most plant leaves in rainforest are thick and with waxy surf.pdf
Most plant leaves in rainforest are thick and with waxy surf.pdfMost plant leaves in rainforest are thick and with waxy surf.pdf
Most plant leaves in rainforest are thick and with waxy surf.pdfadityaenterprise32
 
Most PCcompatible computer systems use a 20bit address cod.pdf
Most PCcompatible computer systems use a 20bit address cod.pdfMost PCcompatible computer systems use a 20bit address cod.pdf
Most PCcompatible computer systems use a 20bit address cod.pdfadityaenterprise32
 
Most exoplanets found are much more massive than Earth and .pdf
Most exoplanets found are much more massive than Earth and .pdfMost exoplanets found are much more massive than Earth and .pdf
Most exoplanets found are much more massive than Earth and .pdfadityaenterprise32
 
Moodys Aaa irket tahvili getirisi ile 10 yllk ABD Hazine ta.pdf
Moodys Aaa irket tahvili getirisi ile 10 yllk ABD Hazine ta.pdfMoodys Aaa irket tahvili getirisi ile 10 yllk ABD Hazine ta.pdf
Moodys Aaa irket tahvili getirisi ile 10 yllk ABD Hazine ta.pdfadityaenterprise32
 
More effort needs to be put into the search and selection of.pdf
More effort needs to be put into the search and selection of.pdfMore effort needs to be put into the search and selection of.pdf
More effort needs to be put into the search and selection of.pdfadityaenterprise32
 
Morgan Barron is the best player on the Cornell University h.pdf
Morgan Barron is the best player on the Cornell University h.pdfMorgan Barron is the best player on the Cornell University h.pdf
Morgan Barron is the best player on the Cornell University h.pdfadityaenterprise32
 
Moosehead based in New Brunswick is the largest Canadiano.pdf
Moosehead based in New Brunswick is the largest Canadiano.pdfMoosehead based in New Brunswick is the largest Canadiano.pdf
Moosehead based in New Brunswick is the largest Canadiano.pdfadityaenterprise32
 
Month Based on this Climagraph from Columbia SC how would.pdf
Month Based on this Climagraph from Columbia SC  how would.pdfMonth Based on this Climagraph from Columbia SC  how would.pdf
Month Based on this Climagraph from Columbia SC how would.pdfadityaenterprise32
 
Monty Corp has 8000 shares of common stock outstanding It.pdf
Monty Corp has 8000 shares of common stock outstanding It.pdfMonty Corp has 8000 shares of common stock outstanding It.pdf
Monty Corp has 8000 shares of common stock outstanding It.pdfadityaenterprise32
 
Monstruos inc Mientras mira la pelcula busque elementos d.pdf
Monstruos inc  Mientras mira la pelcula busque elementos d.pdfMonstruos inc  Mientras mira la pelcula busque elementos d.pdf
Monstruos inc Mientras mira la pelcula busque elementos d.pdfadityaenterprise32
 
Mom were here Sorry were a little late Theyre more t.pdf
Mom were here Sorry were a little late Theyre more t.pdfMom were here Sorry were a little late Theyre more t.pdf
Mom were here Sorry were a little late Theyre more t.pdfadityaenterprise32
 
Money supply endogeneity and financial instability are two p.pdf
Money supply endogeneity and financial instability are two p.pdfMoney supply endogeneity and financial instability are two p.pdf
Money supply endogeneity and financial instability are two p.pdfadityaenterprise32
 
Monosodyum glutamat MSG glutamatn bir sodyum tuzu ve yayg.pdf
Monosodyum glutamat MSG glutamatn bir sodyum tuzu ve yayg.pdfMonosodyum glutamat MSG glutamatn bir sodyum tuzu ve yayg.pdf
Monosodyum glutamat MSG glutamatn bir sodyum tuzu ve yayg.pdfadityaenterprise32
 
Monopolistic competition is a market structure very much lik.pdf
Monopolistic competition is a market structure very much lik.pdfMonopolistic competition is a market structure very much lik.pdf
Monopolistic competition is a market structure very much lik.pdfadityaenterprise32
 
Moira has to be at the office from 93 but she gets to choo.pdf
Moira has to be at the office from 93 but she gets to choo.pdfMoira has to be at the office from 93 but she gets to choo.pdf
Moira has to be at the office from 93 but she gets to choo.pdfadityaenterprise32
 
Moira has to be at the office from 93 but she gets to choos.pdf
Moira has to be at the office from 93 but she gets to choos.pdfMoira has to be at the office from 93 but she gets to choos.pdf
Moira has to be at the office from 93 but she gets to choos.pdfadityaenterprise32
 
Module 1 test 101316 10 What are 3 types of nase lsted in .pdf
Module 1 test 101316 10 What are 3 types of nase lsted in .pdfModule 1 test 101316 10 What are 3 types of nase lsted in .pdf
Module 1 test 101316 10 What are 3 types of nase lsted in .pdfadityaenterprise32
 
Modify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdfModify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdfadityaenterprise32
 
Module 5 Case study Reas the foliewing Case study and answer.pdf
Module 5 Case study Reas the foliewing Case study and answer.pdfModule 5 Case study Reas the foliewing Case study and answer.pdf
Module 5 Case study Reas the foliewing Case study and answer.pdfadityaenterprise32
 
Modify Exercise 1adding an interface called SidedObject that.pdf
Modify Exercise 1adding an interface called SidedObject that.pdfModify Exercise 1adding an interface called SidedObject that.pdf
Modify Exercise 1adding an interface called SidedObject that.pdfadityaenterprise32
 

More from adityaenterprise32 (20)

Most plant leaves in rainforest are thick and with waxy surf.pdf
Most plant leaves in rainforest are thick and with waxy surf.pdfMost plant leaves in rainforest are thick and with waxy surf.pdf
Most plant leaves in rainforest are thick and with waxy surf.pdf
 
Most PCcompatible computer systems use a 20bit address cod.pdf
Most PCcompatible computer systems use a 20bit address cod.pdfMost PCcompatible computer systems use a 20bit address cod.pdf
Most PCcompatible computer systems use a 20bit address cod.pdf
 
Most exoplanets found are much more massive than Earth and .pdf
Most exoplanets found are much more massive than Earth and .pdfMost exoplanets found are much more massive than Earth and .pdf
Most exoplanets found are much more massive than Earth and .pdf
 
Moodys Aaa irket tahvili getirisi ile 10 yllk ABD Hazine ta.pdf
Moodys Aaa irket tahvili getirisi ile 10 yllk ABD Hazine ta.pdfMoodys Aaa irket tahvili getirisi ile 10 yllk ABD Hazine ta.pdf
Moodys Aaa irket tahvili getirisi ile 10 yllk ABD Hazine ta.pdf
 
More effort needs to be put into the search and selection of.pdf
More effort needs to be put into the search and selection of.pdfMore effort needs to be put into the search and selection of.pdf
More effort needs to be put into the search and selection of.pdf
 
Morgan Barron is the best player on the Cornell University h.pdf
Morgan Barron is the best player on the Cornell University h.pdfMorgan Barron is the best player on the Cornell University h.pdf
Morgan Barron is the best player on the Cornell University h.pdf
 
Moosehead based in New Brunswick is the largest Canadiano.pdf
Moosehead based in New Brunswick is the largest Canadiano.pdfMoosehead based in New Brunswick is the largest Canadiano.pdf
Moosehead based in New Brunswick is the largest Canadiano.pdf
 
Month Based on this Climagraph from Columbia SC how would.pdf
Month Based on this Climagraph from Columbia SC  how would.pdfMonth Based on this Climagraph from Columbia SC  how would.pdf
Month Based on this Climagraph from Columbia SC how would.pdf
 
Monty Corp has 8000 shares of common stock outstanding It.pdf
Monty Corp has 8000 shares of common stock outstanding It.pdfMonty Corp has 8000 shares of common stock outstanding It.pdf
Monty Corp has 8000 shares of common stock outstanding It.pdf
 
Monstruos inc Mientras mira la pelcula busque elementos d.pdf
Monstruos inc  Mientras mira la pelcula busque elementos d.pdfMonstruos inc  Mientras mira la pelcula busque elementos d.pdf
Monstruos inc Mientras mira la pelcula busque elementos d.pdf
 
Mom were here Sorry were a little late Theyre more t.pdf
Mom were here Sorry were a little late Theyre more t.pdfMom were here Sorry were a little late Theyre more t.pdf
Mom were here Sorry were a little late Theyre more t.pdf
 
Money supply endogeneity and financial instability are two p.pdf
Money supply endogeneity and financial instability are two p.pdfMoney supply endogeneity and financial instability are two p.pdf
Money supply endogeneity and financial instability are two p.pdf
 
Monosodyum glutamat MSG glutamatn bir sodyum tuzu ve yayg.pdf
Monosodyum glutamat MSG glutamatn bir sodyum tuzu ve yayg.pdfMonosodyum glutamat MSG glutamatn bir sodyum tuzu ve yayg.pdf
Monosodyum glutamat MSG glutamatn bir sodyum tuzu ve yayg.pdf
 
Monopolistic competition is a market structure very much lik.pdf
Monopolistic competition is a market structure very much lik.pdfMonopolistic competition is a market structure very much lik.pdf
Monopolistic competition is a market structure very much lik.pdf
 
Moira has to be at the office from 93 but she gets to choo.pdf
Moira has to be at the office from 93 but she gets to choo.pdfMoira has to be at the office from 93 but she gets to choo.pdf
Moira has to be at the office from 93 but she gets to choo.pdf
 
Moira has to be at the office from 93 but she gets to choos.pdf
Moira has to be at the office from 93 but she gets to choos.pdfMoira has to be at the office from 93 but she gets to choos.pdf
Moira has to be at the office from 93 but she gets to choos.pdf
 
Module 1 test 101316 10 What are 3 types of nase lsted in .pdf
Module 1 test 101316 10 What are 3 types of nase lsted in .pdfModule 1 test 101316 10 What are 3 types of nase lsted in .pdf
Module 1 test 101316 10 What are 3 types of nase lsted in .pdf
 
Modify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdfModify this code to change the underlying data structure to .pdf
Modify this code to change the underlying data structure to .pdf
 
Module 5 Case study Reas the foliewing Case study and answer.pdf
Module 5 Case study Reas the foliewing Case study and answer.pdfModule 5 Case study Reas the foliewing Case study and answer.pdf
Module 5 Case study Reas the foliewing Case study and answer.pdf
 
Modify Exercise 1adding an interface called SidedObject that.pdf
Modify Exercise 1adding an interface called SidedObject that.pdfModify Exercise 1adding an interface called SidedObject that.pdf
Modify Exercise 1adding an interface called SidedObject that.pdf
 

Recently uploaded

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxLigayaBacuel1
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 

Recently uploaded (20)

MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Planning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptxPlanning a health career 4th Quarter.pptx
Planning a health career 4th Quarter.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 

Modify the Simple lexer program given in the link below to .pdf

  • 1. 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. main.cpp front.in (sum + 47) / total /** * This the example lexical analyzer code in pages 173 - 177 of the * textbook, * * Sebesta, R. W. (2012). Concepts of Programming Languages. * Pearson, 10th edition. * */ /* front.c - a lexical analyzer system for simple arithmetic expressions */ #include <stdio.h> #include <ctype.h> /* Global declarations */
  • 2. /* 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
  • 3. /* 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 */
  • 4. 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 '(':
  • 5. addChar(); nextToken = LEFT_PAREN; break; case ')': addChar(); nextToken = RIGHT_PAREN; break; case '+': addChar(); nextToken = ADD_OP; break; case '-': addChar(); nextToken = SUB_OP; break;
  • 6. case '*': addChar(); nextToken = MULT_OP; break; case '/': addChar(); nextToken = DIV_OP; break; default: addChar(); nextToken = EOF; break; } return nextToken; } /*****************************************************/
  • 7. /* 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))
  • 8. 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 */
  • 9. 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 */
  • 10. 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:
  • 11. 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 */