SlideShare a Scribd company logo
1 of 9
Download to read offline
I have a c lexer program that outputs the specific type of token as a string using the expression
The existing code outputs identifiers and operators and parenthesis. What modifications need to
be made to the existing code to output keywords as well?
Example:
Next token is: KEYWORD, Next lexeme is int
Next token is: IDENT, Next lexeme is sum
Next token is: EQUAL_OP, Next lexeme is =
Next token is: INTEGER, Next lexeme is 3
Etc...
Code:
#include
#include
#include
/* 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();
int lookup(char ch);
/* 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
#define LESSER_OP 27
#define GREATER_OP 28
#define EQUAL_OP 29
#define NOT_EQUAL_OP 30
#define LESSER_EQUAL_OP 31
#define GREATER_EQUAL_OP 32
#define AND_OP 33
#define OR_OP 34
#define NOT_OP 35
#define IF_KEYWORD 36
#define ELSE_KEYWORD 37
#define WHILE_KEYWORD 38
#define DO_KEYWORD 39
#define FOR_KEYWORD 40
#define SWITCH_KEYWORD 41
#define CASE_KEYWORD 42
#define DEFAULT_KEYWORD 43
#define BREAK_KEYWORD 44
#define CONTINUE_KEYWORD 45
#define RETURN_KEYWORD 46
#define VOID_KEYWORD 47
#define INT_KEYWORD 48
#define FLOAT_KEYWORD 49
#define CHAR_KEYWORD 50
#define BOOL_KEYWORD 51
/* 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;
case '<':
addChar();
getChar();
if (nextChar == '=') {
addChar();
nextToken = LESSER_EQUAL_OP;
}
else if (nextChar == '>') {
addChar();
nextToken = NOT_EQUAL_OP;
}
else {
nextToken = LESSER_OP;
}
break;
case '>':
addChar();
getChar();
if (nextChar == '=') {
addChar();
nextToken = GREATER_EQUAL_OP;
}
else {
nextToken = GREATER_OP;
}
break;
case '=':
addChar();
getChar();
if (nextChar == '=') {
addChar();
nextToken = EQUAL_OP;
}
else {
nextToken = ASSIGN_OP;
}
break;
case '&':
addChar();
getChar();
if (nextChar == '&') {
addChar();
nextToken = AND_OP;
getChar();
}
else {
nextToken = UNKNOWN;
}
break;
case '|':
addChar();
getChar();
if (nextChar == '|') {
addChar();
nextToken = OR_OP;
getChar();
}
else {
nextToken = UNKNOWN;
}
break;
case '!':
addChar();
getChar();
if (nextChar == '=') {
addChar();
nextToken = NOT_EQUAL_OP;
getChar();
}
else {
nextToken = NOT_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();
}
}
int lex() {
lexLen = 0;
getNonBlank();
switch (charClass) {
/* Parse identifiers */
case LETTER:
addChar();
getChar();
while (charClass == LETTER || charClass == DIGIT) {
addChar();
getChar();
}
nextToken = IDENT;
printf("Next token is: IDENT, Next lexeme is %sn", lexeme);
break;
/* Parse integer literals */
case DIGIT:
addChar();
getChar();
while (charClass == DIGIT) {
addChar();
getChar();
}
nextToken = INT_LIT;
printf("Next token is: INT_LIT, Next lexeme is %sn", lexeme);
break;
/* Parentheses and operators */
case UNKNOWN:
lookup(nextChar);
getChar();
printf("Next token is: ");
switch (nextToken) {
case LEFT_PAREN:
printf("LEFT_PAREN");
break;
case RIGHT_PAREN:
printf("RIGHT_PAREN");
break;
case ADD_OP:
printf("ADD_OP");
break;
case SUB_OP:
printf("SUB_OP");
break;
case MULT_OP:
printf("MULT_OP");
break;
case DIV_OP:
printf("DIV_OP");
break;
case LESSER_OP:
printf("LESSER_OP");
break;
case GREATER_OP:
printf("GREATER_OP");
break;
case EQUAL_OP:
printf("EQUAL_OP");
break;
case NOT_EQUAL_OP:
printf("NOT_EQUAL_OP");
break;
case LESSER_EQUAL_OP:
printf("LESSER_EQUAL_OP");
break;
case GREATER_EQUAL_OP:
printf("GREATER_EQUAL_OP");
break;
case AND_OP:
printf("AND_OP");
break;
case OR_OP:
printf("OR_OP");
break;
case NOT_OP:
printf("NOT_OP");
break;
default:
printf("UNKNOWN");
break;
}
printf(", Next lexeme is %sn", lexeme);
break;
/* End of input */
case EOF:
nextToken = EOF;
lexeme[0] = 'E';
lexeme[1] = 'O';
lexeme[2] = 'F';
lexeme[3] = 0;
printf("Next token is: EOFn");
break;
}
return nextToken;
}

More Related Content

Similar to I have a c lexer program that outputs the specific type of token as .pdf

C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdfC++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdfrahulfancycorner21
 
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)Binary Studio
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisaujihisa
 
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
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Datagreenwop
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfezonesolutions
 
Back to basics - PHP_Codesniffer
Back to basics - PHP_CodesnifferBack to basics - PHP_Codesniffer
Back to basics - PHP_CodesnifferSebastian Marek
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxamrit47
 
C++ Course - Lesson 3
C++ Course - Lesson 3C++ Course - Lesson 3
C++ Course - Lesson 3Mohamed Ahmed
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012Sandeep Joshi
 
How to transfer bad PLSQL into good (AAAPEKS23)
How to transfer bad PLSQL into good (AAAPEKS23)How to transfer bad PLSQL into good (AAAPEKS23)
How to transfer bad PLSQL into good (AAAPEKS23)Maik Becker
 

Similar to I have a c lexer program that outputs the specific type of token as .pdf (18)

C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdfC++ code, please help! RESPOND W COMPLETED CODE PLEASE,  am using V.pdf
C++ code, please help! RESPOND W COMPLETED CODE PLEASE, am using V.pdf
 
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
Binary Studio Academy PRO: ANTLR course by Alexander Vasiltsov (lesson 4)
 
Antlr V3
Antlr V3Antlr V3
Antlr V3
 
Hacking Parse.y with ujihisa
Hacking Parse.y with ujihisaHacking Parse.y with ujihisa
Hacking Parse.y with ujihisa
 
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...
 
C to perl binding
C to perl bindingC to perl binding
C to perl binding
 
Category theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) DataCategory theory, Monads, and Duality in the world of (BIG) Data
Category theory, Monads, and Duality in the world of (BIG) Data
 
So I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdfSo I am writing a CS code for a project and I keep getting cannot .pdf
So I am writing a CS code for a project and I keep getting cannot .pdf
 
Php 2
Php 2Php 2
Php 2
 
Back to basics - PHP_Codesniffer
Back to basics - PHP_CodesnifferBack to basics - PHP_Codesniffer
Back to basics - PHP_Codesniffer
 
PL-SQL.pdf
PL-SQL.pdfPL-SQL.pdf
PL-SQL.pdf
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
C++ Course - Lesson 3
C++ Course - Lesson 3C++ Course - Lesson 3
C++ Course - Lesson 3
 
Plsql
PlsqlPlsql
Plsql
 
Ensure code quality with vs2012
Ensure code quality with vs2012Ensure code quality with vs2012
Ensure code quality with vs2012
 
Web Technology_10.ppt
Web Technology_10.pptWeb Technology_10.ppt
Web Technology_10.ppt
 
How to transfer bad PLSQL into good (AAAPEKS23)
How to transfer bad PLSQL into good (AAAPEKS23)How to transfer bad PLSQL into good (AAAPEKS23)
How to transfer bad PLSQL into good (AAAPEKS23)
 
SQL -PHP Tutorial
SQL -PHP TutorialSQL -PHP Tutorial
SQL -PHP Tutorial
 

More from allystraders

Supplemental knowledge conversion processes have been added to the E.pdf
Supplemental knowledge conversion processes have been added to the E.pdfSupplemental knowledge conversion processes have been added to the E.pdf
Supplemental knowledge conversion processes have been added to the E.pdfallystraders
 
Supongamos que Musashi, un economista de un programa de radio AM, y .pdf
Supongamos que Musashi, un economista de un programa de radio AM, y .pdfSupongamos que Musashi, un economista de un programa de radio AM, y .pdf
Supongamos que Musashi, un economista de un programa de radio AM, y .pdfallystraders
 
Suponga que las tasas de inter�s en los Estados Unidos, pero no aume.pdf
Suponga que las tasas de inter�s en los Estados Unidos, pero no aume.pdfSuponga que las tasas de inter�s en los Estados Unidos, pero no aume.pdf
Suponga que las tasas de inter�s en los Estados Unidos, pero no aume.pdfallystraders
 
Suponga que la ARN polimerasa estaba transcribiendo un gen eucari�ti.pdf
Suponga que la ARN polimerasa estaba transcribiendo un gen eucari�ti.pdfSuponga que la ARN polimerasa estaba transcribiendo un gen eucari�ti.pdf
Suponga que la ARN polimerasa estaba transcribiendo un gen eucari�ti.pdfallystraders
 
Suppose researchers are about to draw a sample of 1450 observations .pdf
Suppose researchers are about to draw a sample of 1450 observations .pdfSuppose researchers are about to draw a sample of 1450 observations .pdf
Suppose researchers are about to draw a sample of 1450 observations .pdfallystraders
 
Suponga que el Congreso est� considerando un proyecto de ley que imp.pdf
Suponga que el Congreso est� considerando un proyecto de ley que imp.pdfSuponga que el Congreso est� considerando un proyecto de ley que imp.pdf
Suponga que el Congreso est� considerando un proyecto de ley que imp.pdfallystraders
 
Suppose that there are two groups of people in the economy. In group.pdf
Suppose that there are two groups of people in the economy. In group.pdfSuppose that there are two groups of people in the economy. In group.pdf
Suppose that there are two groups of people in the economy. In group.pdfallystraders
 
Suppose that the Fed will increase the money supply. Which of the fo.pdf
Suppose that the Fed will increase the money supply. Which of the fo.pdfSuppose that the Fed will increase the money supply. Which of the fo.pdf
Suppose that the Fed will increase the money supply. Which of the fo.pdfallystraders
 
Suppose that the body weights of Roborovski dwarf hamsters are norma.pdf
Suppose that the body weights of Roborovski dwarf hamsters are norma.pdfSuppose that the body weights of Roborovski dwarf hamsters are norma.pdf
Suppose that the body weights of Roborovski dwarf hamsters are norma.pdfallystraders
 
Suppose that in a particular country, the TFR fell to zero and remai.pdf
Suppose that in a particular country, the TFR fell to zero and remai.pdfSuppose that in a particular country, the TFR fell to zero and remai.pdf
Suppose that in a particular country, the TFR fell to zero and remai.pdfallystraders
 
Suppose that disposable income, consumption, and saving in some coun.pdf
Suppose that disposable income, consumption, and saving in some coun.pdfSuppose that disposable income, consumption, and saving in some coun.pdf
Suppose that disposable income, consumption, and saving in some coun.pdfallystraders
 
Suppose that 60 of students at Kansas State University have listene.pdf
Suppose that 60 of students at Kansas State University have listene.pdfSuppose that 60 of students at Kansas State University have listene.pdf
Suppose that 60 of students at Kansas State University have listene.pdfallystraders
 
Suppose Maria and Jamal both face the following individual loss dist.pdf
Suppose Maria and Jamal both face the following individual loss dist.pdfSuppose Maria and Jamal both face the following individual loss dist.pdf
Suppose Maria and Jamal both face the following individual loss dist.pdfallystraders
 
Suppose a random variable X has the following probability distributi.pdf
Suppose a random variable X has the following probability distributi.pdfSuppose a random variable X has the following probability distributi.pdf
Suppose a random variable X has the following probability distributi.pdfallystraders
 
Suppose a country had a smaller increase in debt in 2011 than it had.pdf
Suppose a country had a smaller increase in debt in 2011 than it had.pdfSuppose a country had a smaller increase in debt in 2011 than it had.pdf
Suppose a country had a smaller increase in debt in 2011 than it had.pdfallystraders
 
how would implement empowerment techniques within a service Choose .pdf
how would implement empowerment techniques within a service Choose .pdfhow would implement empowerment techniques within a service Choose .pdf
how would implement empowerment techniques within a service Choose .pdfallystraders
 
How were the management controls at Siemens prior to the Bribery sca.pdf
How were the management controls at Siemens prior to the Bribery sca.pdfHow were the management controls at Siemens prior to the Bribery sca.pdf
How were the management controls at Siemens prior to the Bribery sca.pdfallystraders
 
How would I create this diagramRentTool Viewpoints � Technology .pdf
How would I create this diagramRentTool Viewpoints � Technology .pdfHow would I create this diagramRentTool Viewpoints � Technology .pdf
How would I create this diagramRentTool Viewpoints � Technology .pdfallystraders
 
How well did Thaldorf interact with each member of the DMUOn what.pdf
How well did Thaldorf interact with each member of the DMUOn what.pdfHow well did Thaldorf interact with each member of the DMUOn what.pdf
How well did Thaldorf interact with each member of the DMUOn what.pdfallystraders
 
How do increasing atmospheric CO2 emissions threaten sea creatures, .pdf
How do increasing atmospheric CO2 emissions threaten sea creatures, .pdfHow do increasing atmospheric CO2 emissions threaten sea creatures, .pdf
How do increasing atmospheric CO2 emissions threaten sea creatures, .pdfallystraders
 

More from allystraders (20)

Supplemental knowledge conversion processes have been added to the E.pdf
Supplemental knowledge conversion processes have been added to the E.pdfSupplemental knowledge conversion processes have been added to the E.pdf
Supplemental knowledge conversion processes have been added to the E.pdf
 
Supongamos que Musashi, un economista de un programa de radio AM, y .pdf
Supongamos que Musashi, un economista de un programa de radio AM, y .pdfSupongamos que Musashi, un economista de un programa de radio AM, y .pdf
Supongamos que Musashi, un economista de un programa de radio AM, y .pdf
 
Suponga que las tasas de inter�s en los Estados Unidos, pero no aume.pdf
Suponga que las tasas de inter�s en los Estados Unidos, pero no aume.pdfSuponga que las tasas de inter�s en los Estados Unidos, pero no aume.pdf
Suponga que las tasas de inter�s en los Estados Unidos, pero no aume.pdf
 
Suponga que la ARN polimerasa estaba transcribiendo un gen eucari�ti.pdf
Suponga que la ARN polimerasa estaba transcribiendo un gen eucari�ti.pdfSuponga que la ARN polimerasa estaba transcribiendo un gen eucari�ti.pdf
Suponga que la ARN polimerasa estaba transcribiendo un gen eucari�ti.pdf
 
Suppose researchers are about to draw a sample of 1450 observations .pdf
Suppose researchers are about to draw a sample of 1450 observations .pdfSuppose researchers are about to draw a sample of 1450 observations .pdf
Suppose researchers are about to draw a sample of 1450 observations .pdf
 
Suponga que el Congreso est� considerando un proyecto de ley que imp.pdf
Suponga que el Congreso est� considerando un proyecto de ley que imp.pdfSuponga que el Congreso est� considerando un proyecto de ley que imp.pdf
Suponga que el Congreso est� considerando un proyecto de ley que imp.pdf
 
Suppose that there are two groups of people in the economy. In group.pdf
Suppose that there are two groups of people in the economy. In group.pdfSuppose that there are two groups of people in the economy. In group.pdf
Suppose that there are two groups of people in the economy. In group.pdf
 
Suppose that the Fed will increase the money supply. Which of the fo.pdf
Suppose that the Fed will increase the money supply. Which of the fo.pdfSuppose that the Fed will increase the money supply. Which of the fo.pdf
Suppose that the Fed will increase the money supply. Which of the fo.pdf
 
Suppose that the body weights of Roborovski dwarf hamsters are norma.pdf
Suppose that the body weights of Roborovski dwarf hamsters are norma.pdfSuppose that the body weights of Roborovski dwarf hamsters are norma.pdf
Suppose that the body weights of Roborovski dwarf hamsters are norma.pdf
 
Suppose that in a particular country, the TFR fell to zero and remai.pdf
Suppose that in a particular country, the TFR fell to zero and remai.pdfSuppose that in a particular country, the TFR fell to zero and remai.pdf
Suppose that in a particular country, the TFR fell to zero and remai.pdf
 
Suppose that disposable income, consumption, and saving in some coun.pdf
Suppose that disposable income, consumption, and saving in some coun.pdfSuppose that disposable income, consumption, and saving in some coun.pdf
Suppose that disposable income, consumption, and saving in some coun.pdf
 
Suppose that 60 of students at Kansas State University have listene.pdf
Suppose that 60 of students at Kansas State University have listene.pdfSuppose that 60 of students at Kansas State University have listene.pdf
Suppose that 60 of students at Kansas State University have listene.pdf
 
Suppose Maria and Jamal both face the following individual loss dist.pdf
Suppose Maria and Jamal both face the following individual loss dist.pdfSuppose Maria and Jamal both face the following individual loss dist.pdf
Suppose Maria and Jamal both face the following individual loss dist.pdf
 
Suppose a random variable X has the following probability distributi.pdf
Suppose a random variable X has the following probability distributi.pdfSuppose a random variable X has the following probability distributi.pdf
Suppose a random variable X has the following probability distributi.pdf
 
Suppose a country had a smaller increase in debt in 2011 than it had.pdf
Suppose a country had a smaller increase in debt in 2011 than it had.pdfSuppose a country had a smaller increase in debt in 2011 than it had.pdf
Suppose a country had a smaller increase in debt in 2011 than it had.pdf
 
how would implement empowerment techniques within a service Choose .pdf
how would implement empowerment techniques within a service Choose .pdfhow would implement empowerment techniques within a service Choose .pdf
how would implement empowerment techniques within a service Choose .pdf
 
How were the management controls at Siemens prior to the Bribery sca.pdf
How were the management controls at Siemens prior to the Bribery sca.pdfHow were the management controls at Siemens prior to the Bribery sca.pdf
How were the management controls at Siemens prior to the Bribery sca.pdf
 
How would I create this diagramRentTool Viewpoints � Technology .pdf
How would I create this diagramRentTool Viewpoints � Technology .pdfHow would I create this diagramRentTool Viewpoints � Technology .pdf
How would I create this diagramRentTool Viewpoints � Technology .pdf
 
How well did Thaldorf interact with each member of the DMUOn what.pdf
How well did Thaldorf interact with each member of the DMUOn what.pdfHow well did Thaldorf interact with each member of the DMUOn what.pdf
How well did Thaldorf interact with each member of the DMUOn what.pdf
 
How do increasing atmospheric CO2 emissions threaten sea creatures, .pdf
How do increasing atmospheric CO2 emissions threaten sea creatures, .pdfHow do increasing atmospheric CO2 emissions threaten sea creatures, .pdf
How do increasing atmospheric CO2 emissions threaten sea creatures, .pdf
 

Recently uploaded

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
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
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
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
 
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
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.arsicmarija21
 
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
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
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
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
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
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 

Recently uploaded (20)

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
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Ă...
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
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
 
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
 
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
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.AmericanHighSchoolsprezentacijaoskolama.
AmericanHighSchoolsprezentacijaoskolama.
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.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
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.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)
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
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...
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 

I have a c lexer program that outputs the specific type of token as .pdf

  • 1. I have a c lexer program that outputs the specific type of token as a string using the expression The existing code outputs identifiers and operators and parenthesis. What modifications need to be made to the existing code to output keywords as well? Example: Next token is: KEYWORD, Next lexeme is int Next token is: IDENT, Next lexeme is sum Next token is: EQUAL_OP, Next lexeme is = Next token is: INTEGER, Next lexeme is 3 Etc... Code: #include #include #include /* 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(); int lookup(char ch); /* Character classes */ #define LETTER 0 #define DIGIT 1 #define UNKNOWN 99 /* Token codes */ #define INT_LIT 10 #define IDENT 11 #define ASSIGN_OP 20
  • 2. #define ADD_OP 21 #define SUB_OP 22 #define MULT_OP 23 #define DIV_OP 24 #define LEFT_PAREN 25 #define RIGHT_PAREN 26 #define LESSER_OP 27 #define GREATER_OP 28 #define EQUAL_OP 29 #define NOT_EQUAL_OP 30 #define LESSER_EQUAL_OP 31 #define GREATER_EQUAL_OP 32 #define AND_OP 33 #define OR_OP 34 #define NOT_OP 35 #define IF_KEYWORD 36 #define ELSE_KEYWORD 37 #define WHILE_KEYWORD 38 #define DO_KEYWORD 39 #define FOR_KEYWORD 40 #define SWITCH_KEYWORD 41 #define CASE_KEYWORD 42 #define DEFAULT_KEYWORD 43 #define BREAK_KEYWORD 44 #define CONTINUE_KEYWORD 45 #define RETURN_KEYWORD 46 #define VOID_KEYWORD 47 #define INT_KEYWORD 48 #define FLOAT_KEYWORD 49 #define CHAR_KEYWORD 50 #define BOOL_KEYWORD 51 /* 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");
  • 3. } 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;
  • 4. case '<': addChar(); getChar(); if (nextChar == '=') { addChar(); nextToken = LESSER_EQUAL_OP; } else if (nextChar == '>') { addChar(); nextToken = NOT_EQUAL_OP; } else { nextToken = LESSER_OP; } break; case '>': addChar(); getChar(); if (nextChar == '=') { addChar(); nextToken = GREATER_EQUAL_OP; } else { nextToken = GREATER_OP; } break; case '=': addChar(); getChar(); if (nextChar == '=') { addChar(); nextToken = EQUAL_OP; } else { nextToken = ASSIGN_OP; }
  • 5. break; case '&': addChar(); getChar(); if (nextChar == '&') { addChar(); nextToken = AND_OP; getChar(); } else { nextToken = UNKNOWN; } break; case '|': addChar(); getChar(); if (nextChar == '|') { addChar(); nextToken = OR_OP; getChar(); } else { nextToken = UNKNOWN; } break; case '!': addChar(); getChar(); if (nextChar == '=') { addChar(); nextToken = NOT_EQUAL_OP; getChar(); } else { nextToken = NOT_OP; }
  • 6. 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 */
  • 7. void getNonBlank() { while (isspace(nextChar)) { getChar(); } } int lex() { lexLen = 0; getNonBlank(); switch (charClass) { /* Parse identifiers */ case LETTER: addChar(); getChar(); while (charClass == LETTER || charClass == DIGIT) { addChar(); getChar(); } nextToken = IDENT; printf("Next token is: IDENT, Next lexeme is %sn", lexeme); break; /* Parse integer literals */ case DIGIT: addChar(); getChar(); while (charClass == DIGIT) { addChar(); getChar(); } nextToken = INT_LIT; printf("Next token is: INT_LIT, Next lexeme is %sn", lexeme); break; /* Parentheses and operators */ case UNKNOWN: lookup(nextChar);
  • 8. getChar(); printf("Next token is: "); switch (nextToken) { case LEFT_PAREN: printf("LEFT_PAREN"); break; case RIGHT_PAREN: printf("RIGHT_PAREN"); break; case ADD_OP: printf("ADD_OP"); break; case SUB_OP: printf("SUB_OP"); break; case MULT_OP: printf("MULT_OP"); break; case DIV_OP: printf("DIV_OP"); break; case LESSER_OP: printf("LESSER_OP"); break; case GREATER_OP: printf("GREATER_OP"); break; case EQUAL_OP: printf("EQUAL_OP"); break; case NOT_EQUAL_OP: printf("NOT_EQUAL_OP"); break; case LESSER_EQUAL_OP: printf("LESSER_EQUAL_OP"); break;
  • 9. case GREATER_EQUAL_OP: printf("GREATER_EQUAL_OP"); break; case AND_OP: printf("AND_OP"); break; case OR_OP: printf("OR_OP"); break; case NOT_OP: printf("NOT_OP"); break; default: printf("UNKNOWN"); break; } printf(", Next lexeme is %sn", lexeme); break; /* End of input */ case EOF: nextToken = EOF; lexeme[0] = 'E'; lexeme[1] = 'O'; lexeme[2] = 'F'; lexeme[3] = 0; printf("Next token is: EOFn"); break; } return nextToken; }