Question

In: Computer Science

PLEASE COMMENT THE FOLLOWING LEXICAL ANALYSER PROGRAM. Thank you. #include #include #include #include #include using namespace...

PLEASE COMMENT THE FOLLOWING LEXICAL ANALYSER PROGRAM. Thank you.

#include

#include

#include

#include

#include

using namespace std;

int isKeyword(char buffer[]){

char keywords[32][10] = {"auto","break","case","char","const","continue","default",

"do","double","else","enum","extern","float","for","goto",

"if","int","long","register","return","short","signed",

"sizeof","static","struct","switch","typedef","union",

"unsigned","void","volatile","while"};

int i, flag = 0;

for(i = 0; i < 32; ++i){

if(strcmp(keywords[i], buffer) == 0){

flag = 1;

break;

}

}

return flag;

}

int main(){

char ch, buffer[15], operators[] = "+-*/%=";

ifstream fin("Text.txt");

int i,j=0;

if(!fin.is_open()){

cout<<"error while opening the file\n";

exit(0);

}

while(!fin.eof()){

ch = fin.get();

  

for(i = 0; i < 6; ++i){

if(ch == operators[i])

cout<

}

  

if(isalnum(ch)){

buffer[j++] = ch;

}

else if((ch == ' ' || ch == '\n') && (j != 0)){

buffer[j] = '\0';

j = 0;

if(isKeyword(buffer) == 1)

cout<

else

cout<

}

}

fin.close();

return 0;

}

Solutions

Expert Solution

Working code implemented in C and appropriate comments provided for better understanding.

Source Code for main.c:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

int isKeyword(char buffer[]){
char keywords[35][30] = {"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float","for","goto",
"if","int","long","register","return","short","signed",
"sizeof","static","struct","switch","typedef","union",
"unsigned","void","volatile","while","main","includestdioh","stdio"};
int i, flag = 0;

for(i = 0; i < 35; ++i){
if(strcmp(keywords[i], buffer) == 0){
flag = 1;
break;
}
}

return flag;
}

int main(){
char ch, buffer[15], operators[] = "+-*/%=";
FILE *fp;
int i,j=0;

fp = fopen("text.txt","r");

if(fp == NULL){
printf("error while opening the file\n");
// <stdlib.h> is used for exit
exit(0);
}

while((ch = fgetc(fp)) != EOF){
for(i = 0; i < 6; ++i){
if(ch == operators[i])
printf("%c is operator\n", ch);
}

// isalnum need ctype library import
if(isalnum(ch)){
buffer[j++] = ch;
}
  
else if((ch == ' ' || ch == '\n') && (j != 0)){
buffer[j] = '\0';
j = 0;

if(isKeyword(buffer) == 1)
printf("%s is keyword\n", buffer);
else
printf("%s is identifier\n", buffer);
}

}

fclose(fp);

return 0;
}

Sample Output Screenshots:


Related Solutions

What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std;...
What is the flowchart for this code. Thank You! #include<iostream> #include<iomanip> #include<string> #include<cmath> using namespace std; float series(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum += r[i];    return sum; } float parallel(float r[], int n) {    float sum = 0;    int i;    for (i = 0; i < n; i++)        sum = sum + (1 / r[i]);...
Write the following program in java please. a. Include a good comment when you write the...
Write the following program in java please. a. Include a good comment when you write the method described below: Method method1 receives an integer, n, and a real number, d, and returns a real number. If the integer is positive it returns the square root of the real number added to the integer. If the integer is negative or zero it returns the absolute value of the integer multiplied by the real number. b. Write the statements from the main...
Take the following C++ program and translate it into assembly language( pep9 ) #include using namespace...
Take the following C++ program and translate it into assembly language( pep9 ) #include using namespace std; char ch; int main() {    cin >> ch;    cout << "You inputted " << ch << endl;    ch++;    cout << "Next character is " << ch << endl; if (ch <= ‘Z’)         cout << “Could be luppercase\n”;    return 0; }
what am I doing wrong here? thank you! #include <iostream> #include <string> using namespace std; class...
what am I doing wrong here? thank you! #include <iostream> #include <string> using namespace std; class DivSales { private:    int quarterSales[4];    static double totalSales; public:    void add(int, int, int, int);    int sales(int);    static double getValue()    {        return totalSales;    };    void DivSales::add(int s1, int s2, int s3, int s4)    {        quarterSales[0] = s1;        quarterSales[1] = s2;        quarterSales[2] = s3;        quarterSales[3] = s4;...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must...
Writing a squareroot program in C++ using only: #include <iostream> using namespace std; The program must be very basic. Please don't use math sqrt. Assume that the user does not input anything less than 0. For example: the integer square root of 16 is 4 because 4 squared is 16. The integer square root of 18 is 5 because 4 squared is 16 and 5 squared is 25, so 18 is bigger than 16 but less than 25.  
Writing an nth root program in C++ using only: #include using namespace std; The program must...
Writing an nth root program in C++ using only: #include using namespace std; The program must be very basic. Please don't use math sqrt or pow . For example, the 4th root of 16 is 2 because 2 * 2 * 2 * 2 = 16. The 4th root of 20 is 2 because 2 * 2 * 2 * 2 = 16 and 16 is less than 20, and 3 * 3 * 3 * 3 = 81, which...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std;...
Please write variables and program plan(pseudocode) of this C++ programming code: #include <iostream> using namespace std; void leapYear(int x); int main() { int x; cout << "Enter a year: "; cin >> x; leapYear (x);   return 0; } void leapYear(int x ) {    if (x % 400 == 0)    {    cout << "This is a leap Year";}    else if    ((x % 4 == 0) && (x % 100 != 0))    {    cout <<...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std;...
Take the following program and translate it into PEP/9 assembly language: #include <iostream> using namespace std; int theArray[] = { 5, 11, -29, 45, 9, -1}; void sumPos(int ary[], int len, int &sum) {    sum = 0;    for (int i = 0; i < len; i++)            if (ary[i] > 0)                sum = sum + ary[i]; } int main() {    int total;    sumPos(theArray, 6, total);    for (int k=0; k < 6; k++)      cout...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int...
Take the following program and translate it into PEP/9 assembly language: #include using namespace std; int fib(int n) { int temp; if (n <= 0) return 0; else if (n <= 2) return 1; else { temp = fib(n – 1); return temp + fib(n-2); } } int main() { int num; cout << "Which fibonacci number? "; cin >> num; cout << fib(num) << endl; return 0; } You must use equates to access the stack and follow the...
Take the following C++ program and translate it into Pep/9 assembly language #include using namespace std;...
Take the following C++ program and translate it into Pep/9 assembly language #include using namespace std; int age; char first, last; int main() {    cin >> age;    cin >> first >> last;    cout << "Your age " << age << endl;    cout << "Initials " << first << last << endl;    if (age >= 30)        cout << “Cannot trust\n”;    return 0; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT