Lex-如何在命令行上运行/编译lex程序 [英] Lex - How to run / compile a lex program on commandline

查看:309
本文介绍了Lex-如何在命令行上运行/编译lex程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Lex和Yacc非常陌生.我有一个Lex程序.示例:wordcount.l

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. wordcount.l文件是否放在C驱动器上?

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

我是否编译Lex程序并生成一个.c程序,然后运行什么?

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

我在命令行上尝试了:Lex wordcount.l

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

但是我只是找不到文件...

but I just get file not found...

wordcount.l

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?

推荐答案

您首先必须使用cd转到文件wordcount.l所在的目录.然后使用lex wordcount.l将文件创建为lex.yy.c.要运行该程序,您需要使用 c编译器(例如gcc)对其进行编译.使用gcc,您可以使用gcc -lfl lex.yy.c进行编译.这将创建可以使用./a.out

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

这篇关于Lex-如何在命令行上运行/编译lex程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆