Lexical Analysis

Write a program to get the following equation as input from a file and create token(s) on another file for the possible variable to get the value from the user. After get the value from the user, regenerate all the token(s) on another file for calculation . 

Equation:
x = (a+b)/2 + 4*b

14 comments:

  1. Name: Ibrahim Khalil
    ID: 201330475
    Batch: 44th

    #include
    #include
    #include
    #include

    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[] = "+-*/%=";
    FILE *fp;
    int i,j=0;

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

    if(fp == NULL){
    printf("error while opening the file\n");
    exit(0);
    }

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

    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 indentifier\n", buffer);
    }

    }

    fclose(fp);

    return 0;
    }

    input file: input.txt
    input file data: x = (a+b)/2 + 4*b

    Ouptup:
    x is indentifier
    = is operator
    + is operator
    / is operator
    ab2 is indentifier
    + is operator
    * is operator

    Process returned 0 (0x0) execution time : 1.610 s
    Press any key to continue.



    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Name : Firoj Ahmed
    ID : 201820222
    Batch-58th.

    #include
    #include
    #include

    char symTable[5][7] = { "int", "void", "float", "char", "string" };

    int main() {
    int i, j, k = 0, flag = 0;
    char string[7];
    char str[] = "int main(){printf(\"Hello\");return 0;}";
    char *ptr;
    printf("Splitting string \"%s\" into tokens:\n", str);
    ptr = strtok(str, " (){};""");
    printf("\n\n");
    while (ptr != NULL) {
    printf ("%s\n", ptr);

    for (i = k; i < 5; i++) {
    memset(&string[0], 0, sizeof(string));
    for (j = 0; j < 7; j++) {
    string[j] = symTable[i][j];
    }

    if (strcmp(ptr, string) == 0) {
    printf("Keyword\n\n");
    break;
    } else
    if (string[j] == 0 || string[j] == 1 || string[j] == 2 ||
    string[j] == 3 || string[j] == 4 || string[j] == 5 ||
    string[j] == 6 || string[j] == 7 || string[j] == 8 ||
    string[j] == 9) {
    printf("Constant\n\n");
    break;
    } else {
    printf("Identifier\n\n");
    break;
    }
    }
    ptr = strtok(NULL, " (){};""");
    k++;
    }
    _getch();
    return 0;
    }

    ReplyDelete
  4. ID:201820244
    Batch:58th
    Section:B
    #include
    #include
    #include

    void main()
    {

    char expr[100],operators[20];
    char variables[20][20];
    int constants[20],ascii[100]={0};
    int cnt,i,number=0;
    int j=-1,k=-1,l=-1,n=0;
    int pr[10]={0},m=0;

    printf("Welcome AUB LEXICAL ANALYZER by Rubel \n\n");

    printf("Enter the Equation: \n\n");
    scanf ("%[^\n]%*c", expr);

    cnt = strlen(expr);
    printf("\nString length is: %d\n", cnt);

    for (i = 0; i < cnt; ++i)
    {
    ascii[i] = (int)expr[i];
    }

    for(i=0;i<cnt;++i)

    {

    if( isdigit(expr[i]) )
    {
    while(isdigit(expr[i]))
    {
    number=10*number+ ascii[i] -'0';
    i++;
    }
    j++;
    constants[j]=number;
    number=0;
    }

    if( isalpha(expr[i]) )
    {
    while( isalpha(expr[i]) || isdigit(expr[i]) || expr[i]=='_' )
    {
    k++;
    variables[m][k]=expr[i];
    i++;
    }
    m++;
    pr[n]=k;
    n++;
    k=-1;
    }

    if(expr[i]=='+'||expr[i]=='-'||expr[i]=='/'|| expr[i]=='*'|| expr[i]=='='|| expr[i]=='^')
    l++;
    operators[l]= expr[i];
    }

    printf( "the are");


    for(i=0;i<=j;i++)
    {
    printf("\tlit%d\t%d \n",i+1,constants[i]);
    }

    printf("\nThe operators are \n\n");
    for(i=0;i<=l;i++)
    {
    printf("\top%d\t%c\n",i+1,operators[i]);
    }


    printf("\nThe variables are: \n\n");
    for(i=0;i<m;i++)
    {
    printf("\n\tid%d\t",i+1);
    for(j=0;j<=pr[i];j++)
    {
    printf("%c",variables[i][j]);
    }

    }

    printf("\n————– END OF LEXICAL ANALYZER ————\n\n");
    }

    ReplyDelete
  5. ID-201820298
    58 BATCH
    SECTION-B

    #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],b[30], logical_op[] = "><",math_op[]="+-*/=",numer[]=".0123456789",other[]=",;\(){}[]'':";
    ifstream fin("lexicalinput.txt");
    int mark[1000]={0};
    int i,j=0,kc=0,ic=0,lc=0,mc=0,nc=0,oc=0,aaa=0;
    vector < string > k;
    vectorid;
    vectorlo;
    vectorma;
    vectornu;
    vectorot;
    if(!fin.is_open()){
    cout<<"error while opening the file\n";
    exit(0);
    }

    while(!fin.eof()){
    ch = fin.get();
    for(i = 0; i < 12; ++i){
    if(ch == other[i]){
    int aa=ch;
    if(mark[aa]!=1){
    ot.push_back(ch);
    mark[aa]=1;
    ++oc;
    }
    }
    }

    for(i = 0; i < 5; ++i){
    if(ch == math_op[i]){
    int aa=ch;
    if(mark[aa]!=1){
    ma.push_back(ch);
    mark[aa]=1;
    ++mc;
    }
    }
    }
    for(i = 0; i < 2; ++i){
    if(ch == logical_op[i]){
    int aa=ch;
    if(mark[aa]!=1){
    lo.push_back(ch);
    mark[aa]=1;
    ++lc;
    }
    }

    }
    if(ch=='0' || ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5' || ch=='6' || ch=='7' || ch=='8' || ch=='9' || ch=='.' ||ch == ' ' || ch == '\n' || ch == ';'){

    if(ch=='0' || ch=='1' || ch=='2' || ch=='3' || ch=='4' || ch=='5' || ch=='6' || ch=='7' || ch=='8' || ch=='9' || ch=='.')b[aaa++]=ch;
    if((ch == ' ' || ch == '\n' || ch == ';') && (aaa != 0)){
    b[aaa] = '\0';
    aaa = 0;
    char arr[30];
    strcpy(arr,b);
    nu.push_back(arr);
    ++nc;

    }
    }


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

    if(isKeyword(buffer) == 1){

    k.push_back(buffer);
    ++kc;
    }
    else{



    if(buffer[0]>=97 && buffer[0]<=122) {
    if(mark[buffer[0]-'a']!=1){
    id.push_back(buffer[0]);
    ++ic;
    mark[buffer[0]-'a']=1;
    }

    }

    }

    }

    }

    fin.close();
    printf("Keywords: ");
    for(int f=0;f<kc;++f){
    if(f==kc-1){
    cout<<k[f]<<"\n";
    }
    else {
    cout<<k[f]<<", ";
    }
    }
    printf("Identifiers: ");
    for(int f=0;f<ic;++f){
    if(f==ic-1){
    cout<<id[f]<<"\n";
    }
    else {
    cout<<id[f]<<", ";
    }
    }
    printf("Math Operators: ");
    for(int f=0;f<mc;++f){
    if(f==mc-1){
    cout<<ma[f]<<"\n";
    }

    else {
    cout<<ma[f]<<", ";
    }
    }
    printf("Logical Operators: ");
    for(int f=0;f<lc;++f){
    if(f==lc-1){
    cout<<lo[f]<<"\n";
    }
    else {
    cout<<lo[f]<<", ";
    }

    }
    printf("Numerical Values: ");
    for(int f=0;f<nc;++f){
    if(f==nc-1){
    cout<<nu[f]<<"\n";
    }
    else {
    cout<<nu[f]<<", ";
    }

    }
    printf("Others: ");
    for(int f=0;f<oc;++f){
    if(f==oc-1){
    cout<<ot[f]<<"\n";
    }
    else {
    cout<<ot[f]<<" ";
    }

    }

    return 0;
    }

    ReplyDelete
  6. https://drive.google.com/open?id=1kM-7hQ6dWNv3tY0KghvWbr4daAuaxTKR

    ReplyDelete
  7. https://drive.google.com/open?id=1Y5gB9Mn9ZjT9P8xp3NrUE18jFNhCC_LU

    ReplyDelete
  8. id 201820153
    batch 58th
    sec b
    #include
    #include
    #include

    void main()
    {

    char expr[100],operators[20];
    char variables[20][20];
    int constants[20],ascii[100]={0};
    int cnt,i,number=0;
    int j=-1,k=-1,l=-1,n=0;
    int pr[10]={0},m=0;

    printf("Welcome AUB LEXICAL ANALYZER by Rubel \n\n");

    printf("Enter the Equation: \n\n");
    scanf ("%[^\n]%*c", expr);

    cnt = strlen(expr);
    printf("\nString length is: %d\n", cnt);

    for (i = 0; i < cnt; ++i)
    {
    ascii[i] = (int)expr[i];
    }

    for(i=0;i<cnt;++i)

    {

    if( isdigit(expr[i]) )
    {
    while(isdigit(expr[i]))
    {
    number=10*number+ ascii[i] -'0';
    i++;
    }
    j++;
    constants[j]=number;
    number=0;
    }

    if( isalpha(expr[i]) )
    {
    while( isalpha(expr[i]) || isdigit(expr[i]) || expr[i]=='_' )
    {
    k++;
    variables[m][k]=expr[i];
    i++;
    }
    m++;
    pr[n]=k;
    n++;
    k=-1;
    }

    if(expr[i]=='+'||expr[i]=='-'||expr[i]=='/'|| expr[i]=='*'|| expr[i]=='='|| expr[i]=='^')
    l++;
    operators[l]= expr[i];
    }

    printf( "the are");


    for(i=0;i<=j;i++)
    {
    printf("\tlit%d\t%d \n",i+1,constants[i]);
    }

    printf("\nThe operators are \n\n");
    for(i=0;i<=l;i++)
    {
    printf("\top%d\t%c\n",i+1,operators[i]);
    }


    printf("\nThe variables are: \n\n");
    for(i=0;i<m;i++)
    {
    printf("\n\tid%d\t",i+1);
    for(j=0;j<=pr[i];j++)
    {
    printf("%c",variables[i][j]);
    }

    }

    printf("\n————– END OF LEXICAL ANALYZER ————\n\n");
    }

    ReplyDelete
  9. id 201820153
    batch 58th
    sec b
    #include
    #include
    #include

    void main()
    {

    char expr[100],operators[20];
    char variables[20][20];
    int constants[20],ascii[100]={0};
    int cnt,i,number=0;
    int j=-1,k=-1,l=-1,n=0;
    int pr[10]={0},m=0;

    printf("Welcome AUB LEXICAL ANALYZER by Rubel \n\n");

    printf("Enter the Equation: \n\n");
    scanf ("%[^\n]%*c", expr);

    cnt = strlen(expr);
    printf("\nString length is: %d\n", cnt);

    for (i = 0; i < cnt; ++i)
    {
    ascii[i] = (int)expr[i];
    }

    for(i=0;i<cnt;++i)

    {

    if( isdigit(expr[i]) )
    {
    while(isdigit(expr[i]))
    {
    number=10*number+ ascii[i] -'0';
    i++;
    }
    j++;
    constants[j]=number;
    number=0;
    }

    if( isalpha(expr[i]) )
    {
    while( isalpha(expr[i]) || isdigit(expr[i]) || expr[i]=='_' )
    {
    k++;
    variables[m][k]=expr[i];
    i++;
    }
    m++;
    pr[n]=k;
    n++;
    k=-1;
    }

    if(expr[i]=='+'||expr[i]=='-'||expr[i]=='/'|| expr[i]=='*'|| expr[i]=='='|| expr[i]=='^')
    l++;
    operators[l]= expr[i];
    }

    printf( "the are");


    for(i=0;i<=j;i++)
    {
    printf("\tlit%d\t%d \n",i+1,constants[i]);
    }

    printf("\nThe operators are \n\n");
    for(i=0;i<=l;i++)
    {
    printf("\top%d\t%c\n",i+1,operators[i]);
    }


    printf("\nThe variables are: \n\n");
    for(i=0;i<m;i++)
    {
    printf("\n\tid%d\t",i+1);
    for(j=0;j<=pr[i];j++)
    {
    printf("%c",variables[i][j]);
    }

    }

    printf("\n————– END OF LEXICAL ANALYZER ————\n\n");
    }

    ReplyDelete
  10. https://drive.google.com/open?id=106uu9NZBOJ3CKjwGMSn-XY-ba9Xaef2I

    ReplyDelete
  11. https://drive.google.com/open?id=1dc6i1Lqfp5D6OZwhhwCqAp2vuEfr5-gd

    ReplyDelete
  12. Md. Aminul Islam Tushar
    ID#201710063

    Link : https://paste.ubuntu.com/p/R42rpbvWGH/

    ReplyDelete
  13. ID: 201710251
    Link: https://drive.google.com/open?id=1tEUZ8og0z8CG3JwzX70SwHZscHZdT5AF

    ReplyDelete
  14. #include
    #include
    #include
    #include
    using namespace std;
    #define DATA_SIZE 1000

    int main()
    {

    char data[DATA_SIZE], ch, file_name[DATA_SIZE], token_file[DATA_SIZE], test[DATA_SIZE];
    long long lenth=0;
    FILE * file1;
    FILE * token;
    FILE * fp;


    printf("Enter name of a file you wish to save\n");
    gets(file_name);
    file1 = fopen(file_name, "w");

    if(file1 == NULL)
    {
    /* File not created hence exit */
    printf("Unable to create file.\n");
    exit(EXIT_FAILURE);
    }


    /* Input contents from user to store in file */
    printf("Enter contents to store in file : \n");
    fgets(data, DATA_SIZE, stdin);


    /* Write data to file */

    fputs(data, file1);


    /* Close file to save file data */
    fclose(file1);


    /* Success message */
    printf("File created and saved successfully.\n");

    printf("Enter name of a file you wish to generate token\n");
    gets(token_file);
    token = fopen(token_file, "w+");

    if(token == NULL)
    {
    /* File not created hence exit */
    printf("Unable to create file.\n");
    exit(EXIT_FAILURE);
    }

    fp = fopen(file_name, "r"); // read mode

    if (fp == NULL)
    {
    perror("Error while opening the file.\n");
    exit(EXIT_FAILURE);
    }
    string tests="";
    while((ch=fgetc(fp)) != EOF){
    tests +=ch;
    tests += '\n';

    }
    char tab2[DATA_SIZE];
    strcpy(tab2, tests.c_str());
    fputs(tab2, token);

    fclose(fp);
    fclose(token);

    return 0;
    }

    ReplyDelete