-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlexer.lex
103 lines (77 loc) · 2.79 KB
/
lexer.lex
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
%option noinput
%option nounput
%option noyywrap
%option yylineno
%{
#include <iostream>
#include <cstdlib>
#include "ast.hpp"
using namespace std;
#include "parser.tab.hpp"
%}
%%
/* Keywords */
"if" { return if_token; }
"else" { return else_token; }
"switch" { return switch_token; }
"case" { return case_token; }
"default" { return default_token; }
"for" { return for_token; }
"do" { return do_token; }
"while" { return while_token; }
"break" { return break_token; }
"continue" { return continue_token; }
"goto" { return goto_token; }
"enum" { return enum_token; }
"return" { return return_token; }
"sizeof" { return sizeof_token; }
"struct" { return struct_token; }
"union" { return union_token; }
"auto" { return auto_token; }
"register" { return register_token; }
"static" { return static_token; }
"extern" { return extern_token; }
"typedef" { return typedef_token; }
"const" { return const_token; }
/* Types */
"void" { return void_token; }
"char"|"short"|"int"|"long"|"float"|"double"|"signed"|"unsigned" { return type_token; }
/* Include header and define */
"#include"([ ]+)?((<(\\.|[^>])+>)|(\"(\\.|[^"])+\")) { return include_token; }
"#define"[ ]+[a-zA-z_][a-zA-z_0-9]*([ ]+[-]?[0-9]+("."[0-9]+)?)? { return define_token; }
/* Identifiers */
[_a-zA-Z][_a-zA-Z0-9]* {
yylval.s = new string(yytext);
return id_token;
}
/* Strings */
["]([.]|[^\"])*["] { return string_const; }
/* Numeric literals */
[0-9]+ { return val_const; }
[0-9]+"."[0-9]+ { return val_const; }
['].['] { return val_const; }
/* Multi-character operators */
"||" { return or_token; }
"&&" { return and_token; }
"<=" { return leq_token; }
">=" { return geq_token; }
"==" { return eq_token; }
"!=" { return neq_token; }
"<<" { return lsh_token; }
">>" { return rsh_token; }
"->" { return point_token; }
"*="|"/="|"+="|"%="|">>="|"-="|"<<="|"&="|"^="|"|=" { return asgn_token; }
"++"|"--" { return unary_token; }
/* One-character operators */
[-+*/:;=,.|'(){}\[\]%?&^!~&<>] { return *yytext; }
/* Single-line and multi-line comments */
"//"(\\.|[^\n])*[\n] { }
[/][*]([^*]|[*]*[^*/])*[*]+[/] { }
/* Ignore white spaces, tabs, newlines */
[ \t\n] { }
/* Tokens unspecific for C language */
. {
cerr << "Lexical error: Unknown character '" << *yytext << "'." << endl;
exit(EXIT_FAILURE);
}
%%