Making a lexical Analyzer

前端 未结 6 1340
独厮守ぢ
独厮守ぢ 2020-12-12 16:34

I\'m working with a Lexical Analyzer program right now and I\'m using Java. I\'ve been researching for answers on this problem but until now I failed to find any. Here\'s my

6条回答
  •  清歌不尽
    2020-12-12 16:55

    Write a program to make a simple lexical analyzer that will build a symbol table from given stream of chars. You will need to read a file named “input.txt” to collect all chars. For simplicity, input file will be a C/Java/Python program without headers and methods(body of the main progrm). Then you will identify all the numerical values, identifiers, keywords, math operators, logical operators and others[distinct]. See the example for more details. You can assume that, there will be a space after each keyword.

    #include
        #include
        #include
    
        int main(){
        /* By Ashik Rabbani
        Daffodil International University,CSE43 */
        keyword_check();
        identifier_check();
        math_operator_check();
        logical_operator_check();
        numerical_check();
        others_check();
    
    
            return 0;
        }
    
    
        void math_operator_check()
        {
    
            char ch, string_input[15], operators[] = "+-*/%";
            FILE *fp;
            char tr[20];
            int i,j=0;
    
            fp = fopen("input.txt","r");
    
            if(fp == NULL){
                printf("error while opening the file\n");
                exit(0);
            }
           printf("\nMath Operators : ");
            while((ch = fgetc(fp)) != EOF){
                   for(i = 0; i < 6; ++i){
                       if(ch == operators[i])
                           printf("%c ", ch);
    
                   }
                   }
                    printf("\n");
    
    
    
            fclose(fp);
        }
    
    
        void logical_operator_check()
        {
    
            char ch, string_input[15], operators[] = "&&||<>";
            FILE *fp;
            char tr[20];
            int i,j=0;
    
            fp = fopen("input.txt","r");
    
            if(fp == NULL){
                printf("error while opening the file\n");
                exit(0);
            }
           printf("\nLogical Operators : ");
            while((ch = fgetc(fp)) != EOF){
                   for(i = 0; i < 6; ++i){
                       if(ch == operators[i])
                           printf("%c ", ch);
    
                   }
                   }
                    printf("\n");
    
    
    
            fclose(fp);
        }
    
        void numerical_check()
        {
    
            char ch, string_input[15], operators[] ={'0','1','2','3','4','5','6','7','8','9'};
            FILE *fp;
    
            int i,j=0;
    
            fp = fopen("input.txt","r");
    
            if(fp == NULL){
                printf("error while opening the file\n");
                exit(0);
            }
           printf("\nNumerical Values : ");
            while((ch = fgetc(fp)) != EOF){
                   for(i = 0; i < 6; ++i){
                       if(ch == operators[i])
                           printf("%c ", ch);
    
                   }
                   }
                    printf("\n");
    
    
    
            fclose(fp);
        }
    
        void others_check()
        {
            char ch, string_input[15], symbols[] = "(){}[]";
            FILE *fp;
            char tr[20];
            int i,j=0;
    
            fp = fopen("input.txt","r");
    
            if(fp == NULL){
                printf("error while opening the file\n");
                exit(0);
            }
           printf("\nOthers : ");
            while((ch = fgetc(fp)) != EOF){
                   for(i = 0; i < 6; ++i){
                       if(ch == symbols[i])
                           printf("%c ", ch);
    
                   }
                   }
                   printf("\n");
    
    
    
            fclose(fp);
        }
    
        void identifier_check()
        {
            char ch, string_input[15];
            FILE *fp;
        char    operators[] ={'0','1','2','3','4','5','6','7','8','9'};
            int i,j=0;
    
            fp = fopen("input.txt","r");
    
            if(fp == NULL){
                printf("error while opening the file\n");
                exit(0);
            }
    
            printf("\nIdentifiers : ");
            while((ch = fgetc(fp)) != EOF){
    
                   if(isalnum(ch)){
                       string_input[j++] = ch;
                   }
                   else if((ch == ' ' || ch == '\n') && (j != 0)){
                           string_input[j] = '\0';
                           j = 0;
    
                           if(isKeyword(string_input) == 1)
                           {
    
                           }
    
                           else
                               printf("%s ", string_input);
                   }
    
                   }
    
                    printf("\n");
    
    
            fclose(fp);
        }
    
        int isKeyword(char string_input[]){
            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], string_input) == 0){
                    flag = 1;
                    break;
                }
            }
    
            return flag;
        }
    
        void keyword_check()
        {
    
            char ch, string_input[15], operators[] = "+-*/%=";
            FILE *fp;
            char tr[20];
            int i,j=0;
    
            printf(" Token Identification using C \n By Ashik-E-Rabbani \n 161-15-7093\n\n");
    
            fp = fopen("input.txt","r");
    
            if(fp == NULL){
                printf("error while opening the file\n");
                exit(0);
            }
    
            printf("\nKeywords : ");
            while((ch = fgetc(fp)) != EOF){
    
                   if(isalnum(ch)){
                       string_input[j++] = ch;
                   }
                   else if((ch == ' ' || ch == '\n') && (j != 0)){
                           string_input[j] = '\0';
                           j = 0;
    
                           if(isKeyword(string_input) == 1)
                               printf("%s ", string_input);
    
                   }
    
                   }
    
         printf("\n");
    
    
            fclose(fp);
        }
    

提交回复
热议问题