【发布时间】:2020-07-18 09:37:38
【问题描述】:
我正在尝试创建一个可以识别浮点数/整数和 id 的 flex 文件: 有效的 int - 不允许以 0 开头。 有效的浮点数表示必须包含指数,其值为带或不带符号 2.78e+10 的整数。 有效id-只能以小写字母开头,多个下划线不能一个接一个出现
我不确定我错在哪里,如果我只有浮点数,我会返回浮点数和 int 和 id,但是当所有内容组合在一个文件中时,它就不起作用了。
这是我创建的文件:
%option noyywrap
%{
#include "Token.h"
#include <stdio.h>
#include <stdlib.h>
static int skip_single_line_comment(int num); //function for one line comment
static int skip_multiple_line_comment(int num);//function for multiple lines of comments
int line_num=0;
%}
ALPHA ([a-zA-Z])
DIGIT ([0-9])
Sign ([+|-])
Expo ([e]{Sign}?)
float_num ([1-9]+(\.({DIGIT}+{Expo}{DIGIT}+)))
int_num [1-9]{DIGIT}+
id ([a-z]+({ALPHA}|{DIGIT}|(\_({ALPHA}|{DIGIT})))*)
%%
{float_num} {
create_and_store_token(TOKEN_FLOAT, yytext, line_num);
fprintf(yyout,"Line %d : found token of type TOKEN_FLOAT, lexeme %s.\n", line_num, yytext);
}
\n {line_num++;}
{int_num} {
create_and_store_token(TOKEN_INTEGER, yytext, line_num);
fprintf(yyout,"Line %d : found token of type TOKEN_INTEGER , lexeme %s.\n", line_num, yytext);
}
{id} {
create_and_store_token(TOKEN_ID, yytext, line_num);
fprintf(yyout,"Line %d : found token of type TOKEN_ID, lexeme %s.\n", line_num, yytext);
}
"//" {line_num=skip_single_line_comment(line_num); fprintf(yyout,"The number of the line is:%d.\n", line_num);}
"/*" {line_num=skip_multiple_line_comment(line_num); fprintf(yyout,"The number of the line is:%d.\n", line_num);}
%%
static int
skip_single_line_comment(int num)
{
char c;
/* Read until we find \n or EOF */
while((c = input()) != '\n' && c != EOF)
;
/* Maybe you want to place back EOF? */
if(c == EOF)
unput(c);
return num=num+1;
}
static int
skip_multiple_line_comment(int num)
{
char c;
for(;;)
{
switch(input())
{
/* We expect ending the comment first before EOF */
case EOF:
fprintf(stderr, "Error unclosed comment, expect */\n");
exit(-1);
goto done;
break;
/* Is it the end of comment? */
case '*':
if((c = input()) == '/'){
num=num+1;
goto done;
}
unput(c);
break;
default:
/* skip this character */
break;
}
}
done:
/* exit entry */
return num ;
}
void main(int argc, char **argv){
yyin=fopen("C:\\temp\\test1.txt","r");
yyout=fopen("C:\\temp\\test1Soltion.txt","w");
yylex();}
输入文件:
21
41.e-21
a_23_e4_5
8
1.1E+21
a1_c23_e4_56
输出:
Line 0 : found token of type TOKEN_INTEGER , lexeme 21.
Line 1 : found token of type TOKEN_INTEGER , lexeme 41.
.Line 1 : found token of type TOKEN_ID, lexeme e.
-Line 1 : found token of type TOKEN_INTEGER , lexeme 21.
Line 2 : found token of type TOKEN_ID, lexeme a_23_e4_5.
81.1E+Line 4 : found token of type TOKEN_INTEGER , lexeme 21.
Line 5 : found token of type TOKEN_ID, lexeme a1_c23_e4_56.
【问题讨论】:
-
你的浮点数和整数的 RE 都是相似的,没有任何区别。这就是为什么它首先选择 int 而不是浮动,即使你认为它应该。请参阅example 词法分析器了解 C 语法。你会发现他们已经区分得很清楚了。
标签: c compilation flex-lexer