2010-04-18 53 views
0
#include<stdio.h> 

/* this is a lexer which recognizes constants , variables ,symbols, identifiers , functions , comments and also header files . It stores the lexemes in 3 different files . One file contains all the headers and the comments . Another file will contain all the variables , another will contain all the symbols. */ 

int main() 
{ 
    int i=0,j; 
    char a,b[20],c[30]; 
    FILE *fp1,*fp2; 
    c[0]='"if"; 
    c[1]="then"; 
    c[2]="else"; 
    c[3]="switch"; 
    c[4]="printf"; 
    c[5]="scanf"; 
    c[6]="NULL"; 
    c[7]="int"; 
    c[8]="char"; 
    c[9]="float"; 
    c[10]="long"; 
    c[11]="double"; 
    c[12]="char"; 
    c[13]="const"; 
    c[14]="continue"; 
    c[15]="break"; 
    c[16]="for"; 
    c[17]="size of"; 
    c[18]="register"; 
    c[19]="short"; 
    c[20]="auto"; 
    c[21]="while"; 
    c[22]="do"; 
    c[23]="case"; 
    fp1=fopen("source.txt","r"); //the source file is opened in read only mode which will passed through the lexer 
    fp2=fopen("lext.txt","w"); 
    //now lets remove all the white spaces and store the rest of the words in a file 


    if(fp1==NULL) 
    { 
     perror("failed to open source.txt"); 
     //return EXIT_FAILURE; 
    } 
    i=0; 
    while(!feof(fp1)) 
    { 


     a=fgetc(fp1); 

     if(a!=' ') 
     { 
      b[i]=a; 

     } 
     else 
     { 
      for (j=0;j<23;j++) 
     { 
      if(c[j]==b) 
      { 
       fprintf(fp2, "%.20s\n", c[j]); 
       continue ; 
         } 
      b[i]='\0'; 
      fprintf(fp2, "%.20s\n", b); 
      i=0; 
      continue; 
     } 
    //else if 
    //{ 

     i=i+1;     

     /*Switch(a) 
     { 
      case EOF :return eof; 
      case '+':sym=sym+1; 

      case '-':sym=sym+1; 

      case '*':sym=sym+1; 

      case '/':sym=sym+1; 

      case '%':sym=sym+1; 

      case ' 
     */ 
    } 
fclose(fp1); 
fclose(fp2); 
return 0; 
} 

這是詞法分析我的C代碼..它給予警告,也不寫任何東西到LEXT文件..「分配使得整數指針不進行強制轉換」,在C警告

+1

僵化的語法突出顯示,這段代碼應該給出一個語法錯誤,而不是警告('c [0] ='後面有一個單引號不應該出現)。 – sepp2k 2010-04-18 16:50:16

+0

什麼行給你警告? – shosti 2010-04-18 16:51:59

+0

從第10行到第33行和第60行和第94行 – Hick 2010-04-18 16:52:59

回答

7

char c[30];的聲明30個char的數組,即30個字節長的存儲器塊。所以像c[0] = "if";這樣的任務會嘗試將一個指針放入一個大小爲char的整數。

你可能想要的是char* c[30]; - 一個30 指針的數組。

+0

如果我把它指向它會給我分段錯誤.. – Hick 2010-04-18 16:55:01

+0

當你使用指針時,你分配了內存嗎? – 2010-04-18 16:57:08

0

你也是比較字符串爲:

if(c[j]==b) 

你應該使用strcmp此爲:

if(! strcmp(c[j],b)) 

它傷心,你不跟着任何的建議,您以前question

2

C不支持數組賦值 - 你不能說這樣的話:

c[0]='"if"; 
在C.

而且似乎是在你的代碼的外來報價。

今天下午你在這裏發表的所有帖子都是基本的東西。你在使用哪種C語言教科書?

+0

究竟是什麼讓你覺得他有使用教科書的機會? ;-) – 2010-04-18 17:00:19

+0

我沒有使用任何書..只是從我的錯誤中學習.. – Hick 2010-04-18 17:05:53

+0

@Steve Jessop - 顯然,自從你看了教科書以來,它已經有一段時間了(儘管,我還沒有看到過那麼糟糕)。 – 2010-04-18 17:07:00

0

正如我已經說過here(你的另一個問題),

cchar*,而c[0], c[1], c[2], ...char。 你要做的是將char*(例如「if」)分配給char(例如c [0])。