0
votes

When I write a yylex() for a yacc parser, the yylex() usually return symbol at a time, that is, the yylex() must be called muti-times until the file to an end.

But when I write a main function for a lex scanner, I just call the yylex() once, but the whole file still fully scanned.

void main(int argc, char* argv[]) {
    printf("start\n");
    yyin = fopen(argv[1], "r");
    yylex();
    printf("word count: %d\n", wordCount);
    fclose(yyin);
}

Why?

1
Without the program and data, no one's going to give a useful answer.Thomas Dickey
It very much depends on whether the actions in the scanner definition include return commands. The actions are inserted directly into yylex.rici
@rici you are right, the yylex control flow very much depends on the action, I'v read the generated code and found that it just copy the action code to the switch case segment.user2269707

1 Answers

0
votes

Sorry for asking a silly question, I have read the c file generated by lex, and find that the action code is pasted in a switch case segment, so, as @rici said, it is very much depend on what you write in the action, since my code in action does not return, so one call for yylex will go through the stream. When there's a return, I should use a while() to call yylex.