2013-11-26 1 views
1

현재 Lex 유틸리티를 사용하여 C 프로그램을 읽으려고하고 예약 된 C 단어가 아닌 모든 단어의 줄 항목을 표시하려고합니다. 나는 'yyin' 'yyout' 'yylex' 'yy_create_buffer'등의 다중 정의를 말하는 거대한 오류 목록을 실행 중입니다 ...Lex를 사용하여 문자열 발생 수 계산, 메이크 파일의 가능한 문제

현재 내 makefile에 문제가있는 것처럼 느껴질 수 있습니다. 또한 코드. 도움을 주시면 감사하겠습니다.

내 코드 : 여기

#include <string.h> 
char currentLines[20][80]; 
char identifier[1000][80]; 
void foundWord(char *, int); 
int numLine = 1; 
%} 

%s newStr 
%x BLOCK 

%% 
"/*"      BEGIN(BLOCK); 
<BLOCK>[^*\n]*   
<BLOCK>"*"+[^*/\n]* 
<BLOCK>\n    ++numLine; 
<BLOCK>"*"+"/"  BEGIN(INITIAL); 

"\n"        ++numLine; 

auto      ; 
break      ; 
case      ; //full list of reserved words... 

[*]?[a-zA-Z][a-zA-Z0-9_]* foundWord(yytext, numLine); 
[^a-zA-Z0-9_]+    ; 
[0-9]+      ; 
%% 

void foundWord(char* newStr, int nLine) 
{ 
char num[10]; 
sprintf (newStr, "%d", nLine); 

int i; 
for(i = 0; i <= 20; i++) 
{ 
    if (strcmp(identifier[i], newStr) == 0) 
    { 
     strcat(currentLines[i], ", "); 
     strcat(currentLines[i], num); 
     return; 
    } 
} 

strcpy(identifier[i], newStr); 
strcat(identifier[i], ": "); 
strcpy(currentLines[i], num); 
} 

그리고는 렉스 라이브러리 'C_words.l를'연결을 시도 내가 쓴 메이크입니다.

이것은 입력 파일입니다.

#include <stdio.h> 
void main() 
{ 
    int m,n; 

printf("Enter the values for M and N\n"); 
scanf("%d %d", &m,&n); 

if(m == n) 
    printf("M and N are equal\n"); 
else 
    printf("M and N are not equal\n"); 

} 

답변

1

만들 때 -p 옵션을 사용해 보셨습니까?

예컨대

C_words.exe : C_words.o lex.yy.o 
    gcc -g -p -o C_words.exe C_words.o lex.yy.c -ll 
lex.yy.o : lex.yy.c 
    gcc -g -p -c lex.yy.c 
lex.yy.c : C_words.l 
    lex C_words.l 
+0

'yywrap의 다중 정의'를 처리 한 것처럼 보입니다.하지만 여전히 많은 오류 목록이 있습니다. – Nicktard

2

Make는 C_words.l에서 C_words.o를 빌드하는 방법을 알기에 충분히 똑똑합니다. 따라서 C_words.o (C_words.l)와 lex.yy.o (C_words.o)를 연결하는 것입니다. 대신

C_words.exe : C_words.o lex.yy.o 
    gcc -g -o C_words.exe C_words.o lex.yy.c -ll 
lex.yy.o : lex.yy.c 
    gcc -g -c lex.yy.c 
lex.yy.c : C_words.l 
    lex C_words.l 

시도의

:

C_words.exe : C_words.o 
    gcc -g -o C_words.exe C_words.o -ll 

참고 : C_words.c라는 이름의 파일이있는 경우 다음 C_words.c 및 C_words.l을 구축 할 때 충돌합니다. C_words.c를 main.c 또는 그와 비슷한 이름으로 바꿉니다. makefile을 다음으로 변경하십시오 :

C_words.exe : C_words.o main.o 
    gcc -g -o C_words.exe C_words.o main.o -ll 
+0

그게 오류를 해결하는 것, 그러나 프로그램이 아무것도 보이지 않는 것, 그걸 살펴볼 때가 – Nicktard

+1

foundWord()가 printf를 넣었습니까? 나는 [*] 무엇인지 모르겠다. ID 규칙에서 시작해야합니다. –

+1

이것은 다음과 같습니다 : sprintf (yytext, "% d", nLine); yytext를 쓸어 버릴거야. 당신은 yytext가 필요합니다. –

관련 문제