简体   繁体   English

C语言中的词法分析器代码

[英]Lexical Analyzer code in C

im using visual studio 2010 for C++ i implemented a code for a lexical analyzer for C programming but i got an error says "missing type specifier - int assumed. Note: C++ does not support default-int" is there something wrong with my code? 即时通讯使用Visual Studio 2010 for C ++,我为C编程实现了词法分析器的代码,但出现错误,提示“缺少类型说明符-假定为int。注意:C ++不支持default-int”我的代码有问题吗? or i just have to change the compiler? 还是我只需要更改编译器? here is the code 这是代码

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

    //Global variables
     int charClass;
    char lexeme [100];
    char nextChar;
    int lexLen;
    int token;
    int nextToken;
    FILE *in_fp, *fopen();

   // Function Declarations
   void addChar();
   void getChar();
   void getNonBlank();
   int lex();

   //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

   //Main
       main(){
        if ((in_fp = fopen("front.in", "r")) == NULL)
            printf("ERROR - cannot open front.in \n");
        else{
            getChar();
            do {
                lex();
            }while (nextToken != EOF);
          }
       }

       // lookUp

       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;

       default:
           addChar();
           nextToken = EOF;
           break;
       }
       return nextToken;
       }
       //AddChar

       void addChar(){
           if (lexLen <= 98) {
               lexeme[lexLen++] = nextChar;
               lexeme[lexLen] = 0;
           }
           else
               printf("Error - lexeme is too long \n");
       }

       //getChar
       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
       void getNonBlank(){
           while (isspace(nextChar))
               getChar();
       }
       //lex
       int lex(){
           lexLen = 0;
           getNonBlank();
           switch (charClass){
           case LETTER:
                   addChar();
                   getChar();
                   while (charClass == LETTER || charClass ==                    DIGIT){
                       addChar();
                       getChar();
                   }
                   nextToken = IDENT;
                   break;
                   // parse ints lits
               case DIGIT:
                   addChar();
                   getChar();
                   while (charClass == DIGIT){
                       addChar();
                       getChar();
                   }
                   nextToken = INT_LIT;
                   break;

                //pares and ops
               case UNKNOWN:
                   lookup(nextChar);
                   getChar();
                   break;

                   //EOF
               case EOF:
                   nextToken = EOF;
                   lexeme[0] = 'E';
                   lexeme[1] = 'O';
                   lexeme[2] = 'F';
                   lexeme[3] = 0;
                   break;
           }
        // end of switch
           printf("Next toke is: %d, next lexeme is %s\n",nextToken, lexeme);
           return nextToken;
       }

so for this example ( sum + 47 ) / total this is suppose to be the output 因此,对于此示例(sum + 47)/ total,假定为输出

Next token is: 25 Next lexeme is ( Next token is: 11 Next lexeme is sum Next token is: 21 Next lexeme is + Next token is: 10 Next lexeme is 47 Next token is: 26 Next lexeme is ) Next token is: 24 Next lexeme is / Next token is: 11 Next lexeme is total Next token is: -1 Next lexeme is EOF 下一个标记是:25下一个词素是(下一个标记是:11下一个词素是总和下一个标记是:21下一个词素是+下一个标记是:10下一个词素是47下一个标记是:26下一个词素是)下一个标记是:24下一个词素是/下一个标记是:11下一个词素是总计下一个标记是:-1下一个词素是EOF

   main(){
    if ((in_fp = fopen("front.in", "r")) == NULL)
        printf("ERROR - cannot open front.in \n");
    else{
        getChar();
        do {
            lex();
        }while (nextToken != EOF);
      }
   }

should be changed like this : 应该这样改变:

 //  return type of main should be explicitly int.
   int  main(){
    if ((in_fp = fopen("front.in", "r")) == NULL)
        printf("ERROR - cannot open front.in \n");
    else{
        getChar();
        do {
            lex();
        }while (nextToken != EOF);
      }
   }

Main is incorrectly declared. Main声明错误。

Either it takes command line parameters: 它需要命令行参数:

int main(int argc, char *argv[])
{

    return 0;
}

or it doesn't: 或没有:

int main(void)
{
    return 0;
}

Answer pinched from here 从这里捏答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM