代码拉取完成,页面将自动刷新
%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;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。