lex 怎么用?

BluesTroy 2013-03-26 04:50:59
现在在学编译原理,用lex语言编写一个扫描器,但看了好多资料对lex的使用还是不太明白。我想知道具体怎么操作的,而不是那些正规表达式之类的语法。
1、怎么安装?
2、在哪里编写lex源文件?
3、我用记事本编写了源文件,然后在DOS里面用flex lex.l的命令得到了一个lex.yy.c,然后把lex.yy.c放到DEVc++ 里面想得到.exe文件,但是,用DEVc++编译时显示连接错误,如下:
[Linker error] undefined reference to `yywrap'
[Linker error] undefined reference to `yywrap'
[Linker error] undefined reference to `WinMain@16'
ld returned 1 exit status
这应该lex里面的函数库没有包括在里面吧? 怎么弄呢? 在网上貌似看到要在VC++里面用?用DEVc++不行吗?
使用lex是这样的流程吗? 另外还看到一个parser Generator,这个东西跟lex有关吗?怎么用的?
lex和yacc到底怎么用的,有哪位能回答一下吗?感谢。
...全文
597 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
BluesTroy 2013-05-11
  • 打赏
  • 举报
回复
#include<iostream>
#include<vector>
#include <algorithm>
using namespace std;
int main(){
    vector<int> v;
    int a=3;
    for(int i=0;i<10;i++){
        v.push_back(i);
    } 
    if (find(v.begin(), v.end(), a) != v.end()){
        cout<<"a is in v!"<<endl;
    }
    else{  
        cout<<"a is not in v!"<<endl;
    } 
    if(find(v.begin(),v.end(),int(50))==v.end())
        cout<<"50 is not in v!"<<endl;
    system("pause");
}
BluesTroy 2013-03-28
  • 打赏
  • 举报
回复
求助:我用lex写简单的词法分析器,写到这种程度,后面不会写了,能不能帮帮忙看看修改写写,也让我这个新手跟着学学。 1.怎么用lex原码实现过滤注释? 2.标志符的有效长度小于8个要怎么实现啊? 3.用文件输入输出怎么实现? 非常感谢。非常感谢。下面是我的代码。
%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct Token
{
   int type;
   char sVal[256];    //标识符有效长度小于8个 
};

%}

%option     noyywrap      

digit       [0-9]
number      {digit}+
letter      [_a-zA-Z]
alnum       [_a-zA-Z0-9]
identifier  {letter}{alnum}*
newline     \n
whitespace  [ \t]+
%start      COMMENT

%%

"program"       {return 3;}
"var"           {return 4;}
"procedure"     {return 5;}
"begin"         {return 6;}
"end"           {return 7;}
"if"            {return 8;}
"then"          {return 9;}
"else"          {return 10;}
"while"         {return 11;}
"do"            {return 12;} 
"call"          {return 13;}
"integer"       {return 14;}
"real"          {return 15;}
"+"             {return 16;}
"-"             {return 17;}
"*"             {return 18;}
"/"             {return 19;}
"~"             {return 20;}
":"             {return 21;}
"("             {return 22;}
")"             {return 23;}
"<"             {return 24;}
"<="            {return 25;}
">"             {return 26;}
">="            {return 27;}
":="            {return 28;}
"<>"            {return 29;}
","             {return 30;}
";"             {return 31;}
"."             {return 32;}
"&"             {return 33;}
"|"             {return 34;}
"!"             {return 35;}
"//"            ;
{identifier}    {return 1;}
{number}        {return 2;}
{newline}       {;} 
{whitespace}    {;}
.               {return 99;}

%%

struct Token getToken()
{ 
  struct Token token;
  token.type = yylex();
  strcpy(token.sVal,yytext);   // yytext是一个字符串指针 ,yyleng是词文的长度 
  return token;
}

int main(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;
    }

    while(1)
    {
       struct Token t=getToken();
       if(t.type==0) break;
       if(t.type==99){printf("(ERROR:%s)\n", t.sVal);}
       else printf("(%d,%s)\n", t.type,t.sVal);
    }
    return 0;
}
/*
int yywrap()
{
   return 1;
}
*/
求助2楼,求助大牛。
BluesTroy 2013-03-27
  • 打赏
  • 举报
回复
2楼回答的太棒了。 不过我对%option ... 还不太懂。 如果要用到lex的库函数的话怎么弄呢? 继续努力学习ing...
赵4老师 2013-03-27
  • 打赏
  • 举报
回复
英语也是一门计算机语言的说。
Axb 2013-03-27
  • 打赏
  • 举报
回复
1、怎么安装? 既然能用flex命令那么已经算是装好了。 2、在哪里编写lex源文件? 任意文本工具。 3、 如果单独使用lex的话可以在.l文件里加%option noyywrap,代码看起来是这样:

%{
/* C code to be copied verbatim */
#include <stdio.h>
%}

%option noyywrap

%%
[0-9]+  {printf("Saw an integer: %s\n", yytext);}
%%
//c codes blow
//.......
也可以定义一个yywrap函数,并且返回1(返回1代表不继续扫描其他文件) 最后一个错误是由于文件里没有定义main函数,在.l文件最后加入main函数就行了。 如果能看懂英文的话推荐OReilly的《flex and bison》这本书,讲的比较详细。
ForestDB 2013-03-27
  • 打赏
  • 举报
回复
`WinMain@16 建错项目了吧

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧