-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscanner.l
86 lines (73 loc) · 1.93 KB
/
scanner.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/* pre defs */
L [a-zA-Z]
D [0-9]
/* C code */
%{
#include <stdio.h>
#include <stdlib.h>
#include "y.tab.h"
int yyerror(char *);
extern int yyval, yylineno;
%}
/* definitions */
%%
/* tokens */
/* COMMENTS */
"//"[^\n]* { /* DO NOTHING */ }
[/][*][^(*|\/\/)]*[*]+([^*/][^*]*[*]+)*[/] { /* DO NOTHING */ }
/* TYPES */
"void"|"boolean"|"char"|"string"|"int"|"intp"|"charp" { yylval.string = strdup(yytext); return TYPE; }
"null" { yylval.string = strdup(yytext); return NULL_TYPE; }
/* CONDITIONS */
"if" return IF;
"else" return ELSE;
/* LOOPS */
"while" return WHILE;
"do" return DOWHILE;
"for" return FOR;
/* KEYWORDS */
"return" return RETURN;
/* OPERATORS */
"&&" return AND;
"/" return DIV;
"=" return ASS;
"==" return EQUAL;
">" return GT;
">=" return GTE;
"<" return LT;
"<=" return LTE;
"-" return MINUS;
"!" return NOT;
"!=" return NOTEQUAL;
"||" return OR;
"+" return PLUS;
"*" return MUL;
"&" return ADDRESS;
"^" return CONTENT;
/* Literal Lexemes */
/* others */
";" return SEMICOLON;
":" return COLON;
"," return COMMA;
"{" return O_CURL;
"}" return C_CURL;
"(" return O_PAREN;
")" return C_PAREN;
"|" return VERT_LINE;
"]" return C_BRACK;
"[" return O_BRACK;
'.' { yylval.string = strdup(yytext); return CHAR_LITERAL; }
"true" return BOOL_TRUE;
"false" return BOOL_FALSE;
{L}({L}|{D}|_)* { yylval.string = strdup(yytext); return IDENTIFIER; }
\"([^\\\"]|\\.)*\" { yylval.string = strdup(yytext); return STRING_LITERAL; }
/* ints types */
(0|[1-9][0-9]*|0[xX][0-9a-fA-F]+|0[0-7]+|[01]+b) { yylval.string = strdup(yytext); return INTEGER; }
[ \t\f\v] ; /* ignore blank spaces */
[\n] { yylineno = yylineno + 1; }
. yyerror("invalid character");
%%
/* subroutines */
int yywrap(void) {
return 1;
}