-
Notifications
You must be signed in to change notification settings - Fork 6
/
lexer.l
67 lines (64 loc) · 2.54 KB
/
lexer.l
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
%x COMMENT
%{
#include "grammar.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../common/trees.h"
extern char *yytext;
extern int column;
extern FILE * yyin;
extern YYSTYPE yylval;
void yyerror(const char *str);
%}
%option yylineno
D [0-9]
L [a-zA-Z_]
E [Ee][+-]?{D}+
delim [ \t\n]
ws {delim}+
%%
"/*" { BEGIN(COMMENT) ; }
<COMMENT>"*/" { BEGIN(INITIAL); }
<COMMENT>{ws} {printf("inside commenet ws\n");}
<COMMENT>([^*]|\en)+|.
<COMMENT><<EOF>> {printf("Unterminated comment\n"); return 0;}
/* C++ comment, a common extension */
"//".*\n
"int" { yylval.str=strdup(yytext); return(TYPE); }
"void" { yylval.str=strdup(yytext); return(TYPE);}
"main" { yylval.str=strdup(yytext); return(ID);}
"return" { return(RETURN); }
"for" { return(FOR);}
"while" { return(WHILE);}
"if" { return(IF); }
"else" { return(ELSE);}
"struct" { return(STRUCT);}
";" { return(SEMI); }
"," { return(COMMA); }
"=" { return(ASSIGNOP); }
(>=)|(<=)|(==)|(!=)|<|> { yylval.str=strdup(yytext); return(RELOP); }
"+" { return(PLUS); }
"-" { return(MINUS); }
"*" { return(STAR); }
"/" { return(DIV); }
"%" { return(MOD);}
"^" { return(POWER);}
"&&" { return(AND); }
"&" { return(SINGALAND); }
"||" { return(OR); }
"!" { return(NOT); }
"(" { return(LP); }
")" { return(RP); }
"[" { return(LB); }
"]" { return(RB); }
"{" { return(LC); }
"}" { return(RC); }
"." { return(GETMEMBER); }
{L}({D}|{L})* { yylval.str=strdup(yytext); return(ID); }
{D}{L}({D}|{L})* { return (ERRID); }
[ \t\v\f\r\n] { }
(([1-9]|-){D}*)|0 { yylval.str=strdup(yytext); return(INT);}
%%
int yywrap()
{return 1;}