1 Star 0 Fork 0

朱罗俊/SysY2022语法分析

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lex.l 3.84 KB
一键复制 编辑 原始数据 按行查看 历史
朱罗俊 提交于 2022-05-04 14:54 . first commit2
%option noyywrap
%option yylineno
%{
#include <stdio.h>
#include <stdlib.h>
enum Token {
INT=258, FLOAT, VOID, CONST, RETURN, IF, ELSE, FOR, WHILE, DO, BREAK, CONTINUE,
LP, RP, LB, RB, LC, RC, COMMA, SEMICOLON, QUESTION, COLON,
MINUS, NOT, TILDE, ASSIGN, ADD, MUL, DIV, MOD, AND, OR,
EQ, NE, LT, LE, GT, GE,
ID, INT_LIT, FLOAT_LIT,
LEX_ERR
};
%}
digit [0-9]
hex [a-fA-F0-9]
letter [a-zA-Z_]
E ([Ee][+-]?{digit}+)
P ([Pp][+-]?{digit}+)
FS (f|F|l|L)
IS ((u|U)|(u|U)?(l|L|ll|LL)|(l|L|ll|LL)(u|U))
%%
\/\/[^\n]* {}
\/\*(\n|.)*\*\/ {}
"int" {printf("%s : INT\n", yytext); return INT; }
"float" {printf("%s : FLOAT\n", yytext); return FLOAT; }
"void" {printf("%s : VOID\n", yytext); return VOID; }
"const" {printf("%s : CONST\n", yytext); return CONST; }
"return" {printf("%s : RETURN\n", yytext); ; return RETURN; }
"if" {printf("%s : IF\n", yytext); return IF; }
"else" {printf("%s : ELSE\n", yytext); return ELSE; }
"for" {printf("%s : FOR\n", yytext); return FOR; }
"while" {printf("%s : WHILE\n", yytext); return WHILE; }
"do" {printf("%s : DO\n", yytext); return DO; }
"break" {printf("%s : BREAK\n", yytext); return BREAK; }
"continue" {printf("%s : CONTINUE\n", yytext); return CONTINUE; }
"(" {printf("%s : LP\n", yytext); return LP; }
")" {printf("%s : RP\n", yytext); return RP; }
"[" {printf("%s : LB\n", yytext); return LB; }
"]" {printf("%s : RB\n", yytext); return RB; }
"{" {printf("%s : LC\n", yytext); return LC; }
"}" {printf("%s : RC\n", yytext); return RC; }
"," {printf("%s : COMMA\n", yytext); return COMMA; }
";" {printf("%s : SEMICOLON\n", yytext); return SEMICOLON; }
"?" {printf("%s : QUESTION\n", yytext); return QUESTION; }
":" {printf("%s : COLON\n", yytext); return COLON; }
"!" {printf("%s : NOT\n", yytext); return NOT; }
"=" {printf("%s : ASSIGN\n", yytext); return ASSIGN; }
"-" {printf("%s : MINUS\n", yytext); return MINUS; }
"+" {printf("%s : ADD\n", yytext); return ADD; }
"*" {printf("%s : MUL\n", yytext); return MUL; }
"/" {printf("%s : DIV\n", yytext); return DIV; }
"%" {printf("%s : MOD\n", yytext); return MOD; }
"&&" {printf("%s : AND\n", yytext); return AND; }
"||" {printf("%s : OR\n", yytext); return OR; }
"==" {printf("%s : EQ\n", yytext); return EQ; }
"!=" {printf("%s : NE\n", yytext); return NE; }
"<" {printf("%s : LT\n", yytext); return LT; }
"<=" {printf("%s : LE\n", yytext); return LE; }
">" {printf("%s : GT\n", yytext); return GT; }
">=" {printf("%s : GE\n", yytext); return GE; }
{letter}({letter}|{digit})* {printf("%s : ID\n", yytext); return ID; }
[1-9]{digit}* {printf("%s : INT_LIT\n", yytext); return INT_LIT; }
0[xX]{hex}+ {printf("%s : INT_LIT\n", yytext); return INT_LIT; }
0[0-7]* {printf("%s : INT_LIT\n", yytext); return INT_LIT; }
{digit}*"."{digit}+{E}?{FS}? { printf("%s : FLOAT_LIT\n", yytext); return FLOAT_LIT; }
{digit}+"."{digit}*{E}?{FS}? { printf("%s : FLOAT_LIT\n", yytext); return FLOAT_LIT; }
0[0-7]*{E}{FS}? { printf("%s : FLOAT_LIT\n", yytext); return FLOAT_LIT; }
0[xX]{hex}+{P}{FS}? { printf("%s : FLOAT_LIT\n", yytext); return FLOAT_LIT; }
0[xX]{hex}*"."{hex}+{P}?{FS}? { printf("%s : FLOAT_LIT\n", yytext); return FLOAT_LIT; }
0[xX]{hex}+"."{hex}*{P}?{FS}? { printf("%s : FLOAT_LIT\n", yytext); return FLOAT_LIT; }
0[0-7][e]
[\t\n\r ] {}
0{digit}+ {printf("Lexical error - line %d : %s\n", yylineno,yytext); return LEX_ERR; }
[0-9][0-9a-zA-Z]+ {printf("Lexical error - line %d : %s\n", yylineno,yytext); return LEX_ERR; }
. {printf("Lexical error %d : %s\n", yylineno,yytext); return LEX_ERR; }
%%
int main(int argc, char** argv) {
if(argc > 1) {
if(!(yyin = fopen(argv[1], "r"))) {
perror(argv[1]);
return 1;
}
int tmp=INT;
while(tmp>=INT&&tmp<=LEX_ERR){
tmp=yylex();
}
fclose(yyin);
} else printf("No source file given!\n");
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhuluojun/syntax-analysis-of-sys-y2022.git
git@gitee.com:zhuluojun/syntax-analysis-of-sys-y2022.git
zhuluojun
syntax-analysis-of-sys-y2022
SysY2022语法分析
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385