简体   繁体   English

带flex的词法分析器

[英]lexical analyzer with flex

I am trying to build a lexical analyzer for a small language using flex. 我正在尝试使用flex为一种小语言构建词法分析器。

The lexical will read a text file of lexemes and give each lexeme a token and write the token in another file. 词汇将读取词素的文本文件,并给每个词素一个标记,并将标记写入另一个文件中。 the code was able to open the file and read from it using yylex() function. 该代码能够打开文件并使用yylex()函数读取文件。

The problem is the code did not write the tokens in the specified file. 问题是代码没有在指定的文件中写入令牌。 Also I've found, that the code can not recognize numbers and gives each letter of an identifier an IDENTIFIER token I tried many methods to overcome these problem and I used ac language websites to write a correct C code. 我还发现,该代码无法识别数字,并为标识符的每个字母赋予一个IDENTIFIER令牌。我尝试了多种方法来克服这些问题,并且我使用ac语言网站编写了正确的C代码。

A small notice is that I do not want to use a .y parser who read the lexeme and pass it to the lexical I want my lexical to read the token by it self and produce the token. 一个小小的通知是,我不想使用读取词素并将其传递给词法的.y解析器,我希望我的词法自己读取标记并生成标记。

So can any one help me to fine what is the problem? 那么有人可以帮助我解决问题吗?

%{
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
#define KEY_INT 259
#define KEY_VOID 258
#define KEY_INT 259
#define KEY_FLOAT 260
#define KEY_IF 261
#define KEY_ELSE 262
#define KEY_WHILE 263
#define KEY_FOR 264
#define KEY_RETURN 265
#define KEY_BREAK 266
#define AND_OP 267
#define OR_OP 268
#define SMALL_EQ_OP 269
#define GREAT_EQ_OP 270
#define EQ_OP 271
#define SMALL_OP 272
#define GREAT_OP 273
#define NOT_EQ_OP 274
#define ASSIGN_OP 275
#define OPN__BRACKET 276
#define CLS__BRACKET 277
#define SEMICOLON_SYMBOL 278
#define COMMA_SYMBOL 279
#define DOT_SYMBOL 280
#define ADD_OP 281
#define SUB_OP 282
#define ASTERISK_SYMBOL 283
#define SLASH_SYMBOL 284
#define INT_NUM 287
#define FLOAT_NUM 288
#define IDENTIFIER 289
int yylval;
FILE *yyin,*yyout;
int c;
%}
Letters     [a-zA-Z]
Digits      [0-9]
Sympols     [@#$%&*-+!"':;/?(),~`|^_=×{}<>]
%%
[/*][{Letters}|{Digits}|{Sympols}|\n|\t ]*[*/]      {}  
[-+]?[{Digits}]+            {yylval = atoi(yytext); return INT_NUM ;}
[-+]?[{Digits}]+.[{Digits}]+        {yylval = atoi(yytext);return FLOAT_NUM ;}
{Letters}[{Letters}|{Digits}|_]*    {return IDENTIFIER ;}
[\t\n ]+    {}
"void"      {return KEY_VOID ;}
"float"     {return KEY_FLOAT ;}
"if"        {printf( "KEY_IF\n") ;}
"else"      {return KEY_ELSE ;}
"while"     {return KEY_WHILE ;}
"for"       {return KEY_FOR ;}
"return"    {return KEY_RETURN ;}
"break"     {return KEY_BREAK ;}
"&&"        {return AND_OP ;}
"||"        {return OR_OP ;}
"<="        {return SMALL_EQ_OP ;}
">="        {return GREAT_EQ_OP ;}
"=="        {return EQ_OP ; }
"<"     {return SMALL_OP ;}
">"     {return GREAT_OP ;}
"!="        {return NOT_EQ_OP ;}
"="     {return ASSIGN_OP ;}
"("     {return OPN__BRACKET ;}
")"     {return CLS__BRACKET ;}
";"     {return SEMICOLON_SYMBOL ;}
","     {return COMMA_SYMBOL ;}
"."     {return DOT_SYMBOL ;}
"+"     {return ADD_OP ;}
"-"     {return SUB_OP ;}
"*"     {return ASTERISK_SYMBOL ;}
"/"     {return SLASH_SYMBOL ;}
.       {yyerror() ;}
%%
int yyerror (void)
{
printf("Invalid input\n");
exit(1);
}
int yywrap()
{
return 1;
}
main()
{
if((yyin=fopen("C:\\ProCompFlex\\GnuWin32\\bin\\input.txt","r"))==NULL)
{
printf("input.txt Not found !\n Press any key to exit ");
getch();
return;
}
c=yylex();
while(c!=NULL)
{
yyout=fopen("C:\\ProCompFlex\\GnuWin32\\bin\\token.txt","w");
fprintf(yyout,c," ",yylex(),"\n");
getchar();
}
fclose(yyout);
}

您需要将所有关键字规则放在IDENTIFIER规则之前。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM