用c语言构造词法分析程序,用Flex构造简易C语言词法分析器

在这里插入代码片/*

*t2.lex 词法分析器

*班级:计科1604

*学号:1030416414

*姓名:wyc(ORION233)

*时间:2018-11-4

*/

%option pointer

%option noyywrap

%x COMMENT

%{

#include #include #define MAXLEN 100#define MAXTYPE 10#define MAXSTR 15

char *a[MAXTYPE] = {"type","for","integer","decimal","identify","bracket","QUOTE","OPT","float"};

struct {

int line;//store line numbersint id;// store tagchar text[MAXSTR];//store yytext}tag[MAXLEN];//store tagsint flag = 0;//error flagint cnt = 0;//array notation counter int line=1;//line counter%}

DIGIT [0-9]

OINTEGER [1-9]{DIGIT}*

INTEGER ("+"|"-")?{OINTEGER}

DECIMAL {INTEGER}(.{OINTEGER})?

LETTER [a-zA-Z]

ID ({LETTER}|_)({LETTER}|_|{DIGIT})*

OPT ("+="|"-="|"*="|"/="|"+"|"-"|"*"|"/"|"<="|">="|"=="|"=")

INDEX {DIGIT}+(.{DIGIT}+)?e([+-])?{DIGIT}+

ERROR {DIGIT}+(.{DIGIT}+)?e

%%

"//".* {}

"/*" {BEGIN COMMENT;}

"*/" {BEGIN INITIAL;}

. {}

n {++line;}

(int|float|double|short) {

tag[cnt].line = line;

tag[cnt].id = 0;

strcpy(tag[cnt].text,yytext); //has bugs--use strcpy is ok,no pointer//printf("%s : %d--%dn",tag[cnt].text,yyleng,cnt);cnt++;

}

for {

tag[cnt].line = line;

tag[cnt].id = 1;

strcpy(tag[cnt].text,"");

//printf("%s : %d--%dn",tag[cnt].text,yyleng,cnt);cnt++;

}

{INTEGER} {

tag[cnt].line = line;

tag[cnt].id = 2;

strcpy(tag[cnt].text,yytext);

//printf("%s : %d--%dn",tag[cnt].text,yyleng,cnt);cnt++;

}

{DECIMAL} {

tag[cnt].line = line;

tag[cnt].id = 3;

strcpy(tag[cnt].text,yytext);

//printf("%s : %d--%dn",tag[cnt].text,yyleng,cnt);cnt++;

}

{ID} {

tag[cnt].line = line;

tag[cnt].id = 4;

strcpy(tag[cnt].text,yytext);

//printf("%s : %d--%dn",tag[cnt].text,yyleng,cnt);cnt++;

}

("("|")"|"{"|"}"|"["|"]") {

tag[cnt].line = line;

tag[cnt].id = 5;

strcpy(tag[cnt].text,yytext);

//printf("%s : %d--%dn",tag[cnt].text,yyleng,cnt);cnt++;

}

""" {

tag[cnt].line = line;

tag[cnt].id = 6;

strcpy(tag[cnt].text,"");

//printf("%s : %d--%dn",tag[cnt].text,yyleng,cnt);cnt++;

}

{OPT} {

tag[cnt].line = line;

tag[cnt].id = 7;

strcpy(tag[cnt].text,yytext);

//printf("%s : %d--%dn",tag[cnt].text,yyleng,cnt);cnt++;

}

{INDEX} {

tag[cnt].line = line;

tag[cnt].id = 8;

strcpy(tag[cnt].text,yytext);

//printf("%s : %d--%dn",tag[cnt].text,yyleng,cnt);cnt++;

}

{ERROR} {

flag = 1;

tag[cnt].line = line;

tag[cnt].id = -1;//sosostrcpy(tag[cnt].text,yytext);

cnt++;

}

. {}

%%

int main()

{

/*

*begin lexical analysis

*present all the tags

*but ...

*/

yylex();

if(flag) {//print ERRORSint i;

for(i=0;i

if(tag[i].id == -1){

printf("Error type A at Line %d: Illegal floating point number"%s".n",tag[i].line,tag[i].text);

break;

}

}

} else {

int i;

for(i=0;i

if(tag[i].id == 5){//print "QUOTE"printf("line%d:(%s,"%s")n",tag[i].line,a[tag[i].id],tag[i].text);

} else {

printf("line%d:(%s, %s)n",tag[i].line,a[tag[i].id],tag[i].text);

}

}

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值