Lex - How to run / compile a lex program on commandline

孤人 提交于 2019-11-29 14:55:00

问题


I am very new to Lex and Yacc. I have a Lex program. Example: wordcount.l

I am using windows and putty.

I am just trying to run this file..

  1. Does the wordcount.l file go on the C drive?

  2. Do I compile the Lex program and it generates a .c program and then what do I run?

I tried on the command-line: Lex wordcount.l

but I just get file not found...

wordcount.l

%{  
#include <stdlib.h>
#include <stdio.h>

int charCount=0;
int wordCount=0;
int lineCount=0;
%}
%%
\n      {charCount++; lineCount++;}   
[^ \t\n]+   {wordCount++; charCount+=yyleng;}
.       {charCount++;}

%%
main(argc, argv)
int argc;
char** argv;
{           
if (argc > 1)
{
    FILE *file;
    file = fopen(argv[1], "r");
    if (!file)
    {
        fprintf(stderr, "Could not open %s\n", argv[1]);
        exit(1);
    }
    yyin = file;
}

yylex();
printf("%d   %d   %d\n", charCount, wordCount, lineCount);
}

In putty how do I compile and run this program?


回答1:


You first have to go to the directory which the file wordcount.l is in using cd. Then using lex wordcount.l will make the file lex.yy.c. To the run the program you need compile it with a c compiler such as gcc. With gcc you can compile it using gcc -lfl lex.yy.c. This will create a.out which can be run using ./a.out




回答2:


lex file.l
gcc lex.yy.c -ly -ll
./a.out

These also works. I am using this in Ubuntu 14.04.



来源:https://stackoverflow.com/questions/8859710/lex-how-to-run-compile-a-lex-program-on-commandline

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!