📄 mylex.l
字号:
%{
struct lexvalStack
{
union lex{
char chr;
char *str;
int integer;
float real;
double dbl;
char name[30];
}lexValue;
char type;
};
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define false 0
#define ture 1
#include "myyacc.tab.h"
extern int lexVerbose;
extern int linesCount;
char lexeme[30];
char lexlogic[30];
char lexkey[30];
%}
digit [0-9]
letter [a-zA-Z]
%%
while {
strcpy(lexkey,yytext);
if(lexVerbose)
printf("relop:%s\n",lexkey);
return(K_WHILE);
}
do {
strcpy(lexkey,yytext);
if(lexVerbose)
printf("relop:%s\n",lexkey);
return(K_DO);
}
begin {
strcpy(lexkey,yytext);
if(lexVerbose)
printf("relop:%s\n",lexkey);
return(K_BEGIN);
}
end {
strcpy(lexkey,yytext);
if(lexVerbose)
printf("relop:%s\n",lexkey);
return(K_END);
}
and {
strcpy(lexlogic,yytext);
if(lexVerbose)
printf("relop:%s\n",lexlogic);
return(AND);
}
or {
strcpy(lexlogic,yytext);
if(lexVerbose)
printf("relop:%s\n",lexlogic);
return(OR);
}
not {
strcpy(lexlogic,yytext);
if(lexVerbose)
printf("relop:%s\n",lexlogic);
return(NOT);
}
{digit}+\.{digit}* {
yylval.real=(float)atof(yytext);
yylval.lexeme.type=2;
strcpy(lexeme,yytext);
if(lexVerbose)
printf("real:%g\n",yylval.real);
return(FNUMBER);
}
{digit}+ {
yylval.integer=(int)atoi(yytext);
yylval.lexeme.type=1;
strcpy(lexeme,yytext);
if(lexVerbose)
printf("int:%d\n",yylval.integer);
return(INUMBER);
}
{letter}[A-Za-z0-9_]* {
strcpy(lexeme,yytext);
yylval.lexeme.type=3;
if(lexVerbose)
printf("identifier:%s\n",lexeme);
return(ID);
}
\+ {
yylval.chr=yytext[0];
if(lexVerbose)
printf("opterator:%c\n",yylval.chr);
return('+');
}
\- {
yylval.chr=yytext[0];
if(lexVerbose)
printf("oprator:%c\n",yylval.chr);
return('-');
}
\* {
yylval.chr=yytext[0];
if(lexVerbose)
printf("oprator:%c\n",yylval.chr);
return('*');
}
\/ {
yylval.chr=yytext[0];
if(lexVerbose)
printf("oprator:%c\n",yylval.chr);
return('/');
}
\> {
yylval.chr=yytext[0];
if(lexVerbose)
printf("oprator:%c\n",yylval.chr);
return('>');
}
\< {
yylval.chr=yytext[0];
if(lexVerbose)
printf("oprator:%c\n",yylval.chr);
return('<');
}
"(" {
yylval.chr=yytext[0];
if(lexVerbose)
printf("separator:%c\n",yylval.chr);
return('(');
}
")" {
yylval.chr=yytext[0];
if(lexVerbose)
printf("separtor:%c\n",yylval.chr);
return(')');
}
"[" {
yylval.chr=yytext[0];
if(lexVerbose)
printf("separator:%c\n",yylval.chr);
return('[');
}
"]" {
yylval.chr=yytext[0];
if(lexVerbose)
printf("separtor:%c\n",yylval.chr);
return(']');
}
":=" {
yylval.chr=yytext[0];
if(lexVerbose)
printf("evaluator:%c\n",yylval.chr);
return('=');
}
";" {
yylval.chr=yytext[0];
if(lexVerbose)
printf("separtor:%c\n",yylval.chr);
return(';');
}
\n {
/*printf("line %d\n",linesCount);
linesCount++;*/
return('\n');
}
%%
int yywrap()
{
return(1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -