如果在末尾添加一个函数调用,C程序将不执行任何操作



因此,我目前正在尝试编写一个相当简单的程序,该程序将文件作为输入,读取文件,并对其进行词法分析(稍后将进行解析和解释(

我以前写过类似的程序,效果很好,但令人惊讶的是,当我在末尾添加函数时,这个程序会挂起。

以下功能完美:

#include <stdio.h>
#include <stdlib.h>
#include "include/slex.h"
int main(void) {
// Read the file and save it into source.
printf("startn");
FILE* fp = fopen("test.sl", "r");
printf("fileopenn");
fseek(fp, 0, SEEK_END);
printf("seekn");
char* source = malloc((ftell(fp) + 1) * sizeof (char)); // +1 because of the null terminator.
printf("allocationn");
fseek(fp, 0, SEEK_SET);
char c;
int i = 0;
while ((c = fgetc(fp)) != EOF) {
source[i++] = c;
} // Iterate through every single character in the file and store it into source.

source[i] = '';
fclose(fp);
// Now lex the file;
printf("Lexn");
lexer_t lexer;
printf("lex2n");
lexer_init(&lexer, source);
printf("lex3n");
/*
lex(&lexer);
printf("lex4");
tl_print(&lexer.tokens);
*/
}

但当我取消注释(希望这是一个实际的单词(lex(&lexer)时,它就挂起了。它不打印以前的语句。

函数lexslex.c中定义,slex.c包括selex.h

我用gcc -Wall Wextra -o sl main.c slex.c编译了它,它没有给我任何警告,也没有任何错误。

void lex(lexer_t*lexer(:

void lex(lexer_t* lexer) {
printf("lex"); // Debugging
// Some people would call this function "make_tokens", I will call it "lex".
while (lexer->current != '') {
if (lexer->current == 'n') {
token_t token = {.type = NEWLINE};
tl_append(&lexer->tokens, token);
}
if (isdigit(lexer->current)) {
token_t token = {.type = INT, .int_value = lex_integer(lexer)};
tl_append(&lexer->tokens, token);
}
else if (isalpha(lexer->current)) {
token_t token = {.type = ID, .id_value = lex_identifier(lexer)};
tl_append(&lexer->tokens, token);
}
}
}

我希望有人能解决我的问题,因为我不明白。祝你今天愉快,谢谢你。

如果你需要更多信息,请毫不犹豫地询问,只要发表评论,我就会编辑我的问题。

正如Bill Lynch所说,在lex(&lexer);之前添加fflush(stdout);解决了我的问题。感谢每一个来问这个问题的人,非常感谢你的帮助。祝大家今天过得愉快。

Credit@Bill Lynch(第一条评论(指出";应用程序很可能会挂起";具有无限循环。

printf()确认操作是一回事。检查返回代码并返回有用的消息是另一回事。

下面是代码的重写(未经测试(版本,使用短名称别名来提高在不同位置执行的内容的清晰度。

#include <stdio.h>
#include <stdlib.h>
#include "include/slex.h"
void lex( lexer_t *l ) {
fprintf( stderr, "Entered lex()n" ); // debug
char c;
while( ( c = l->current ) != '' ) {
token_t t = { 0 };
if( c == 'n') t.type = NEWLINE;
// still infinite loop...
// what "advances" the pointer?? Credit @Craig Estey
else if( isdigit( c ) ) t.type = INT, t.int_value = lex_integer( l );
else if( isalpha( c) ) t.type = ID, t.id_value = lex_identifier( l );
else { // equivalent to "default:" in a "switch"
fprintf( stderr, "Un-lex-able char ('%c') encounteredn", c );
exit( EXIT_FAILURE );
}
tl_append( &l->tokens, t );
}
}
int main() {
char *fname = "test.sl";
FILE* fp = fopen( fname, "r" );
if( fp == NULL ) {
fprintf( stderr, "Failed to open %sn", fname );
exit( EXIT_FAILURE );
}
fseek( fp, 0, SEEK_END );
size_t size = ftell( fp );
fseek(fp, 0, SEEK_SET);
fprintf( stderr, "Size %zun", size ); // debug
char *buf = malloc( (size + 1) * sizeof *buf ); // +1 because of the null terminator.
if( buf == NULL ) {
fprintf( stderr, "Failed to alloc block %zun", size + 1 );
exit( EXIT_FAILURE );
}
size_t nread = fread( buf, sizeof buf[0], size, fp );
if( nread != size ) {
fprintf( stderr, "Expected %zu. Read %zun", size. nread );
exit( EXIT_FAILURE );
}
fclose( fp );
buf[ nread ] = '';
lexer_t lexer;
lexer_init( &lexer, buf );
lex( &lexer );
// free( buf ); // Unsure if you refer back to original..
tl_print( &lexer.tokens );
return 0;
}

你可以添加许多乐观的";取得进展";printf可以随心所欲地调用。这是悲观的";不工作";打印推进所需的报表。

最新更新