Compiler construction Lex(flex):如何提供输入以及在何处';谁的产量?

Compiler construction Lex(flex):如何提供输入以及在何处';谁的产量?,compiler-construction,yacc,lex,flex-lexer,Compiler Construction,Yacc,Lex,Flex Lexer,我对Flex非常陌生,我被困在这个早期阶段。我有一个Lex文件20.l,它的内容是 %{ /* a Lex program that adds line numbers to lines of text, printing the new text to the standard output */ #include <stdio.h> int lineno = 1; %} line .*\n %% {line} { printf("%5d %s", lineno++,

我对Flex非常陌生,我被困在这个早期阶段。我有一个Lex文件20.l,它的内容是

%{
/* a Lex program that adds line numbers
   to lines of text, printing the new text
   to the standard output
*/
#include <stdio.h>
int lineno = 1;
%}
line .*\n
%%
{line} { printf("%5d %s", lineno++, yytext); }
%%
main()
{ yylex(); return 0; }
还有文件lex.yy.c。然后我用

gcc lex.yy.c -o ADD -lfl
并得到了可执行文件ADD

现在,我如何使用此ADD向其他文本文件添加行号?例如,如果输入文件名为“try.c”,我应该使用什么命令?我尝试了“/ADD try.c”,但显然不起作用。输出是如何表示的

多谢各位。我知道这真是一个愚蠢的问题,但似乎没有人教如何在网上做到这一点

我尝试了“/ADD try.c”

/ADD

输出显示在标准输出上。如果需要不同的文件处理,可以编写自己的main()。

来启用./ADD try.c.e.,而不使用重定向 您可以使用“用户代码”部分中的以下代码替换主代码:

void main (int argc, char **argv)
{
    if (argc>0)
        yyin =fopen(argv[0], "r");
    else
        yyin = stdin;

    yylex();
}
您可以增强输入参数选项解析,以实现更好的输入和输出控制

./ADD < try.c
void main (int argc, char **argv)
{
    if (argc>0)
        yyin =fopen(argv[0], "r");
    else
        yyin = stdin;

    yylex();
}