aboutsummaryrefslogtreecommitdiffstats
path: root/c_lexer/src/c_lexer.flex
blob: c2a42c4089c9ef181f1b181d85aba724f2df9a19 (plain)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
%option noyywrap

%{

#include "c_lexer.hpp"

#include <sstream>
#include <stdlib.h>

int lineCount = 1;
int spaceCount = 1;
int sourceLineCount = 1;

std::string fileName;

%}

KEYWORD auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|if|static|while

IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]*

OPERATOR [.][.][.]|[<>][<>][=]|[-][-]|[+][+]|[|][|]|[#][#]|[&][&]|[+\-*\/<>=!%^|&][=]|[<][<]|[->][>]|[<>&=+\/\-*(){}\[\]\.,%~!?:|^;]

FRACTIONALCONSTANT (([0-9]*\.[0-9]+)|([0-9]+\.))
EXPONENTPART ([eE][+-]?[0-9]+)

FLOATINGSUFFIX ([flFL])
INTEGERSUFFIX ([uU][lL]|[lL][uU]|[uUlL])

DECIMALCONSTANT ([1-9][0-9]*)
OCTALCONSTANT ([0][0-7]*)
HEXCONSTANT ([0][xX][0-9A-Fa-f]+)

CHARCONSTANT ('(([\\]['])|([^']))+')

STRINGLITERAL ["](([\\]["])|([^"]))*["]

NEWLINE (\r\n?|\n)

WHITESPACE [ ]

TAB \t

PREPROC [#][ ][0-9]+[ ]{STRINGLITERAL}[ 0-9]*

ALL .

%%

{KEYWORD} {
    yylval = new std::string(yytext);
    return Keyword;
}

{IDENTIFIER} {
    yylval = new std::string(yytext);
    return Identifier;
}

{OPERATOR} {
    yylval = new std::string(yytext);
    return Operator;
}

{FRACTIONALCONSTANT}{EXPONENTPART}?{FLOATINGSUFFIX}? {
    yylval = new std::string(yytext);
    return Constant;
}

([0-9]+){EXPONENTPART}{FLOATINGSUFFIX}? {
    yylval = new std::string(yytext);
    return Constant;
}

{HEXCONSTANT}{INTEGERSUFFIX}? {
    yylval = new std::string(yytext);
    return Constant;
}

{DECIMALCONSTANT}{INTEGERSUFFIX}? {
    yylval = new std::string(yytext);
    return Constant;
}

{OCTALCONSTANT}{INTEGERSUFFIX}? {
    yylval = new std::string(yytext);
    return Constant;
}

{CHARCONSTANT} {
    std::string tmp(yytext);
    yylval = new std::string(tmp.substr(1, tmp.length()-2));
    return Constant;
}

{STRINGLITERAL} {
    std::string tmp(yytext);
    yylval = new std::string(tmp.substr(1, tmp.length()-2));
    return StringLiteral;
}

{NEWLINE} {
    spaceCount = 1;
    lineCount++;
    sourceLineCount++;
}

{WHITESPACE} {
    spaceCount++;
}

{PREPROC} {
    int srcLineInt;

    yylval = new std::string(yytext);
    std::stringstream preProcLine((*yylval).substr(1, (*yylval).length()));
    preProcLine >> srcLineInt >> fileName;
    sourceLineCount = srcLineInt - 1;
    fileName = fileName.substr(1, fileName.length() - 2);
}

{TAB} {
    spaceCount += 8;
}

{ALL} {
    yylval = new std::string(yytext);
    return Invalid;
}

%%