Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » ScannerRSS Feeds

Program that performs SCANNING of the following program and finds the entire float,integer variables and keywords present in the program

Posted By: Adelmo Fischer     Category: C++ Programming     Views: 1884

Write a program that performs SCANNING of the following program and finds the entire float,integer variables and keywords present in the program.

Code for Program that performs SCANNING of the following program and finds the entire float,integer variables and keywords present in the program in C++ Programming

#include<header.h>
struct Symboltable
{
    char name[15];
    char type[10];
    long address;
};
void KeyWord();
void DisplayFile();
void Integerdata();
void Symbol_table();
void Floatdata();
void Chardata();
char Keyword [20][20]=
{
    "int",
    "float",
    "double",
    "long",
    "char",
    "void",
    "if",
    "else",
    "for",
    "while",
    "sizeof",
    "do",
    "goto",
    "static"
};

FILE *fp=NULL;
char Int[10][10];
char Float[10][10];
char Char[10][10];

char c;
char Word[200][200],*temp;
int i=0,j=0,k=0,I=0,p=0,q=0,r=0,s=0,t=0;
int intcount=0,floatcount=0,charcount=0;
char *VarStr;
char ch;
char testvalue[100];
void main()
{

    clrscr();
    fp=fopen("z:\\ss\\scanner\\data.txt","r");
//    fp=fopen("c:\\tc\\examples\\getopt.c","r");if(fp==NULL)
    {
        printf("\n\n\t\t File could not be open ");
        printf("\n\n\t\t Press any key to exit ...");
        getch();
        exit(1);
    }
    else
    {
        printf("\n\n\t\t Program for the Scanner ");
        printf("\n\n\t\t The data of the input file is as under ...\n\n");
        DisplayFile();
    }
    temp = (char *) malloc(sizeof(char) * 10);
    temp[0]='\0';
    fseek(fp,0L,0);
    while((c=getc(fp))!=EOF)
    {

        if(c==' ' || c=='\t')
        {
            temp[i]='\0';
            strcpy(Word[k],temp);
 //            printf("\n %s",Word[k]);//            delay(500);
            free(temp);
            i=0;
            temp=(char *) malloc(sizeof(char) * 10);
            k++;
        }
        else
        {
            temp[i]=c;
            i++;
        }
        if(feof(fp))
        {
            printf("\n\n End of file ");
            printf("\n\n Press any key to exit ...");
            break;
        }
    }
    KeyWord();
    Integerdata();
    Floatdata();
    Chardata();
  //Symbol_table();
    getch();
    fclose(fp);
    getch();
}

void DisplayFile() /* READING OF THE FILE CHARACTER BY CHARACTER */
{ while((c=getc(fp))!=EOF) { if(c=='\n') printf("\n"); else printf("%c",c); } } void Integerdata() /* FUNCTION FOR THE INTEGER VARIABLES */
{ /* READING OF THE STRING OF INTEGER VARIABLE FROM THE PROGRAM */
/* EXTRACTION OF THE VARIABLES FROM THE STRING OF VARIABLE */
printf("\n\n The list of Integer variables are as under ...\n\n"); for(i=0;i<k;i++) { if((strcmp(Word[i],"int"))==0) { strcpy(testvalue,Word[i+1]); q=p=s=0; VarStr = (char *) malloc(sizeof(char) * 10); VarStr[0]='\0'; for(q=0;q<strlen(testvalue);q++) { if(testvalue[q]==',' || testvalue[q]==';' ) { VarStr[s]='\0'; strcpy(Int[p],VarStr); printf("\n\t\t\t\t %s",Int[p]); intcount++; free(VarStr); s=0; VarStr=(char *) malloc(sizeof(char) * 10); VarStr[s]='\0'; p++; } else { VarStr[s]=testvalue[q]; s++; } } } } } void Floatdata() /* FUNCTION FOR THE FLOAT VARIABLES */
{ /* READING OF THE STRING OF FLOAT VARIABLE FROM THE PROGRAM */
/* EXTRACTION OF THE VARIABLES FROM THE STRING OF VARIABLE */
printf("\n\n The list of Float variables are as under ...\n\n"); for(i=0;i<k;i++) { if((strcmp(Word[i],"float"))==0) { strcpy(testvalue,Word[i+1]); q=p=s=0; VarStr = (char *) malloc(sizeof(char) * 10); VarStr[0]='\0'; for(q=0;q<strlen(testvalue);q++) { if(testvalue[q]==',' || testvalue[q]==';' ) { VarStr[s]='\0'; strcpy(Float[p],VarStr); printf("\n\t\t\t\t %s",Float[p]); floatcount++; free(VarStr); s=0; VarStr=(char *) malloc(sizeof(char) * 10); VarStr[s]='\0'; p++; } else { VarStr[s]=testvalue[q]; s++; } } } } } void Chardata() /* FUNCTION FOR THE CHAR VARIABLES */
{ /* READING OF THE STRING OF CHAR VARIABLE FROM THE PROGRAM */
/* EXTRACTION OF THE VARIABLES FROM THE STRING OF VARIABLE */
printf("\n\n The list of Char variables are as under ...\n\n"); for(i=0;i<k;i++) { if((strcmp(Word[i],"char"))==0) { strcpy(testvalue,Word[i+1]); q=p=s=0; VarStr = (char *) malloc(sizeof(char) * 10); VarStr[0]='\0'; for(q=0;q<strlen(testvalue);q++) { if(testvalue[q]==',' || testvalue[q]==';' ) { VarStr[s]='\0'; strcpy(Char[p],VarStr); printf("\n\t\t\t\t %s",Char[p]); charcount++; free(VarStr); s=0; VarStr=(char *) malloc(sizeof(char) * 10); VarStr[s]='\0'; p++; } else { VarStr[s]=testvalue[q]; s++; } } } } } void KeyWord() { printf("\n\n The list of keywords are as under ...\n\n"); for(i=0;i<k;i++) { for(j=0;j<14;j++) { if((strcmp(Word[i],Keyword[j]))==0) printf("\n\t\t\t\t %s",Word[i]); } } } void Symbol_table() { Symboltable a[10]; long tempaddress; i=j=k=0; /* Inserting integer variables in the symbol table */
for(i=0;i<intcount;i++) { strcpy(a[i].name,Int[i]); strcpy(a[i].type,"int"); tempaddress=long(&Int[i]); printf("\nADD: %u",Int[i]); a[i].address=tempaddress; } printf("\n\tName\tType\tAddress\n"); for(i=0;i<intcount;i++) { printf("\n\t%s\t%s\t%ld\n",a[i].name,a[i].type,a[i].address); } } /*

OUTPUT
------



Program for the Scanner

The data of the input file is as under ...

void main( )
{
int I,j,k;
float value;
char find;
value = j * I + k;
printf("%f",value);
}

The list of keywords are as under ...


void
int
float
char

The list of Integer variables are as under ...


I
j
k

The list of Float variables are as under ...


value

The list of Char variables are as under ...


find
*/
  
Share: 

 
 
 


Adelmo Fischer
Adelmo Fischer author of Program that performs SCANNING of the following program and finds the entire float,integer variables and keywords present in the program is from Frankfurt, Germany.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Umesh Singh from United States Comment on: Oct 03
how to write a program which would find number of user defined functions, where the input given is a .c file...

View All Comments