2014-03-31 42 views
1

我想在C中實現簡單的詞法分析器,而我的問題是關於字符和字符串。通常在我的鏈表中插入我給char作爲參數。但在關鍵字的情況下,因爲它們是字符串,而打印它們,我有問題:以簡單的詞法分析器中的字符串作爲字符

#define _CRT_SECURE_NO_DEPRECATE 
#include<stdio.h> 
#include<ctype.h> 
#include<string.h> 
#include<stdlib.h> 
#define MAX 50 

char token[MAX]; 
char ch, str[25]; 

//Structure definition for lexemes 
struct lexeme{ 
    char lexemes; 
    char tokenclass[MAX]; 
    struct lexeme *next; 
}; 

typedef struct lexeme lexeme; 

lexeme *firstPtr = NULL; 
lexeme *lastPtr = NULL; 

//This method is for inserting the values into linked list. 
void insert(char s, char *t){ 

    lexeme *np; 
    np = malloc(sizeof(lexeme)); 
    np->lexemes = s; 
    strcpy(np->tokenclass, t); 
    np->next = NULL; 

    if (firstPtr == NULL){ 
     firstPtr = np; 
    } 
    else{ 
     lastPtr->next = np; 
    } 
    lastPtr = np; 
} 
/*void insert_key(char *kyw, char *t){ 
    lexeme *kp; 
    kp = malloc(sizeof(lexeme)); 
    kp->lexemes 

}*/ 

void keyw(char *p); 
int i = 0; 

//Array of keywords 
char keys[12][10] = { "break", "char", "continue", 
"double", "else", "end", "for", "if", "int", "return", "void", "while" }; 

int main() { 


    char seps[13] = " \n,;(){}[]\""; 
    char oper[] = "!%^&*-+=~|.<>/?"; 
    int j; 
    //char fname[200]; 
    FILE *f1; 
    //clrscr(); 
    fopen_s(&f1, "input.txt", "r"); 

    if (f1 == NULL) 
    { 
     printf("file not found"); 
    } 

    while ((ch = fgetc(f1)) != EOF) 
    { 

     for (j = 0; j <= 14; j++) 
     { 
      if (ch == oper[j]) 
      { 
       printf("%c is an operator\n", ch); 
       strcpy(token, "operator"); 
       insert(ch, token); 
       str[i] = '\0'; 
       keyw(str); 
      } 
     } 
     for (j = 0; j <= 12; j++) 
     { 
      /* if(i==-1) 
      break;*/ 
      if (ch == seps[j]) 
      { 
       // if(strcmp(ch,"==") || strcmp(ch,"<=") || strcmp(ch,">=") || strcmp(ch,"<")|| strcmp(ch,">") || strcmp(ch,"?=")) 
       // printf("%s is a logical operator",ch); 

       str[i] = '\0'; 
       keyw(str); 
      } 
     } 
     if (i != -1) 
     { 
      str[i] = ch; 
      i++; 
     } 
     else 
      i = 0; 
    } 
    printf("("); 
    while (firstPtr != NULL){ 

     printf("%c,", firstPtr->lexemes); 
     printf("%s |", firstPtr->tokenclass); 
    //printf("---- %c,%s ---- \n", firstPtr->next->lexemes, firstPtr->next->tokenclass); 
    firstPtr = firstPtr->next; 
    } 
    printf(")"); 
    printf("\n"); 
    printf("\n"); 

    system("pause"); 
    return 1; 

} 

void keyw(char *p) 
{ 
    int k, flag = 0; 
    for (k = 0; k <= 11; k++) 
    { 
     if (strcmp(keys[k], p) == 0) 
     { 
      printf("%s is a keyword\n", p); 
      strcpy(token, "keyword"); 
      insert(p[0], token); 
      flag = 1; 
      break; 
     } 
    } 
    if (flag == 0) 
    { 
     if (isdigit(p[0])) 
     { 
      printf("%s is a number\n", p); 
      strcpy(token, "number"); 
      insert(p[0], token); 
     } 
     else 
     { 

      if (p[0] != '\0') 
      { 
       printf("%s is an identifier\n", p); 
       strcpy(token, "id"); 
       insert(p[0], token); 
      } 
     } 
    } 
    i = -1; 
} 

當我輸入的是:

int a=5; 
int b=3; 
int c; 
if(a>b){ 
c=7; 
b=c+a; 
end 
} 

通常我得到這樣我的輸出:

<i,keyword |=,operator |>,operator |a,id |5,number |i,keyword |=,operator |b,id |3,number |i, keyword |c,id | .... and so on. 

我知道在關鍵字的情況下我不應該給p [0]。我還檢查了我的結構定義,並將我的char詞位設置爲char lexemes []但我遇到了一些錯誤。我試圖找到適當的C類,但我不能。 我想我的輸出像:

(int,keyword) (i,keyword) instead 

那麼你有什麼建議?我該怎麼做才能做到呢?

+0

次要:在當'char'不在範圍0-127和微妙的情況下'char'是一個'簽署char' ,'isdigit(p [0])'是一個問題。要修復,請使用'isdigit((unsigned char)p [0])'。 – chux

+0

建議'char seps [] =「\ n,;(){} [] \」「;'和'for(j = 0; seps [j]; j ++)'。'seps'從11開始非空'char'。另外'for(j = 0; oper [j]; j ++)' – chux

回答

1

我的建議:關鍵字另存爲數字。

登記部分

if (strcmp(keys[k], p) == 0) 
    { 
     printf("%s is a keyword\n", p); 
     strcpy(token, "keyword"); 
     insert(k, token);//insert(p[0], token); 
     flag = 1; 
     break; 
    } 

打印部分

if(firstPtr->lexemes < 12) 
     printf("%s,", keys[firstPtr->lexemes]); 
    else 
     printf("%c,", firstPtr->lexemes); 
    printf("%s |", firstPtr->tokenclass); 
+0

它工作的很好;)非常感謝很多人 – Mertcan

+0

@Mertcan你可以在這裏粘貼你的工作代碼嗎?即使我在搜索它。 – Priyanka

相關問題