change the lexer

master
jakeallen 11 months ago
parent e39d30671b
commit c3fae88b83

@ -9,9 +9,11 @@
#include "ast.h"
#include "parser.h"
#define TESTINFO(type,value) printf("LexToken(%s,%s,%d))\n",type,value,yylineno)
int line_cnt = 1;
%}
MultilineComment "/*"([^\*]|(\*)*[^\*/])*(\*)*"*/"
SingleLineComment "//"[^\n]+
Lex_err [1-9][0-9]*[a-zA-Z]+[0-9]*|0[0-7]*[8-9a-zA-Z_]+[0-9a-zA-Z_]*
@ -26,39 +28,39 @@ Lex_err [1-9][0-9]*[a-zA-Z]+[0-9]*|0[0-7]*[8-9a-zA-Z_]+[0-9a-zA-Z_]*
}
"\n" { line_cnt++; }
[ \t] { }
"int" { return INT; }
"float" { return FLOAT; }
"void" { return VOID; }
"const" { return CONST; }
"return" { return RETURN; }
"if" { return IF; }
"else" { return ELSE; }
"while" { return WHILE; }
"break" { return BREAK; }
"continue" { return CONTINUE; }
"(" { return LP; }
")" { return RP; }
"[" { return LB; }
"]" { return RB; }
"{" { return LC; }
"}" { return RC; }
"," { return COMMA; }
";" { return SEMICOLON; }
"+" { return PLUS; }
"-" { return MINUS; }
"!" { return NOT; }
"=" { return ASSIGN; }
"*" { return MUL; }
"/" { return DIV; }
"%" { return MOD; }
"&&" { return AND; }
"||" { return OR; }
"==" { return EQ; }
"!=" { return NE; }
"<" { return LT; }
"<=" { return LE; }
">" { return GT; }
">=" { return GE; }
"int" { TESTINFO("INT"); return INT; }
"float" { TESTINFO("FLOAT"); return FLOAT; }
"void" { TESTINFO("VOID");return VOID; }
"const" { TESTINFO("CONST"); return CONST; }
"return" { TESTINFO("RETURN"); return RETURN; }
"if" { TESTINFO("IF"); return IF; }
"else" { TESTINFO("ELSE") return ELSE; }
"while" { TESTINFO("WHILE");return WHILE; }
"break" { TESTINFO("BREAK");return BREAK; }
"continue" { TESTINFO("CONTINUE"); return CONTINUE; }
"(" { TESTINFO("LP");return LP; }
")" { TESTINFO("RP");return RP; }
"[" { TESTINFO("LB");return LB; }
"]" { TESTINFO("RB");return RB; }
"{" { TESTINFO("LC");return LC; }
"}" { TESTINFO("RC");return RC; }
"," { TESTINFO("COMMA");return COMMA; }
";" { TESTINFO("SEMI");return SEMICOLON; }
"+" { TESTINFO("PLUS");return PLUS; }
"-" { TESTINFO(MINUS);return MINUS; }
"!" { TESTINFO("NOT");return NOT; }
"=" { TESTINFO("ASSIGN");return ASSIGN; }
"*" { TESTINFO("MUL");return MUL; }
"/" { TESTINFO("DIV");return DIV; }
"%" { TESTINFO("MOD");return MOD; }
"&&" { TESTINFO("AND");return AND; }
"||" { TESTINFO("OR");return OR; }
"==" { TESTINFO("EQ");return EQ; }
"!=" { TESTINFO("NE");return NE; }
"<" { TESTINFO("LT");return LT; }
"<=" { TESTINFO("LE");return LE; }
">" { TESTINFO("GT");return GT; }
">=" { TESTINFO("GE");return GE; }
0[xX][0-9a-fA-F]* {
int val = 0;
int len = strlen(yytext);
@ -70,6 +72,7 @@ Lex_err [1-9][0-9]*[a-zA-Z]+[0-9]*|0[0-7]*[8-9a-zA-Z_]+[0-9a-zA-Z_]*
val += yytext[i] - 'a' + 10;
}
yylval.int_val = val;
TESTINFO("INT_LIT");
return INT_LIT;
}
@ -77,9 +80,13 @@ Lex_err [1-9][0-9]*[a-zA-Z]+[0-9]*|0[0-7]*[8-9a-zA-Z_]+[0-9a-zA-Z_]*
yylval.str_val = (char *)malloc(strlen(yytext) + 1);
strcpy(yylval.str_val, yytext);
yylval.str_val[strlen(yytext)] = '\0';
TESTINFO("ID");
return ID;
}
[0-9]*\.[0-9]+f?|[0-9]+e-?[0-9]+f? { yylval.float_val = atof(yytext); return FLOAT_LIT; }
[0-9]*\.[0-9]+f?|[0-9]+e-?[0-9]+f? {
yylval.float_val = atof(yytext);
TESTINFO("FLOAT_LIT");
return FLOAT_LIT; }
{Lex_err} { return LEX_ERR; }
[1-9][0-9]*|0 { yylval.int_val = atoi(yytext); return INT_LIT; }
0[0-7]+ {
@ -88,6 +95,7 @@ Lex_err [1-9][0-9]*[a-zA-Z]+[0-9]*|0[0-7]*[8-9a-zA-Z_]+[0-9a-zA-Z_]*
for (int i = 1; i < len; i++)
val = (val << 3) + yytext[i] - '0';
yylval.int_val = val;
TESTINFO("INT_LIT");
return INT_LIT;
}
. { }

Loading…
Cancel
Save