summaryrefslogtreecommitdiff
path: root/Lexer.l
blob: c75e552042bcec92d5fc9e003ed462ddfefe4ab1 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
%{
#include <map>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <algorithm>

#include "Log.hh"
#include "Driver.hh"
#include "Lexer.hh"

using pcc::Parser;
using pcc::Location;
%}

%option nodefault
%option never-interactive
%option nounistd
%option case-insensitive
%option noyywrap nounput noinput batch debug

decimal         ([1-9][0-9]*)|0
octal           0[0-7]*
hexadecimal     0[xX][0-9a-fA-F]+

integer         {decimal}|{octal}|{hexadecimal}

real_exp        [0-9]*(\.[0-9]*)?[eE]{decimal}
real_noexp      [0-9]*\.[0-9]+

real            {real_exp}|{real_noexp}

trival_char     [0-9a-zA-Z_{}[\]#()<>%:;.?*+\-/^&|~!=,\\" \t\n\v\f]
escaped_char    \\[\'?\\abfnrtv]
hex_char        \\x[0-9a-fA-F]{1,2}
oct_char        \\[0-7]{1,3}
char_value      {trival_char}|{escaped_char}|{hex_char}|{oct_char}|\\'

char            '{char_value}'
string          '{char_value}+'

identifier      [a-z_][0-9a-z_]*

white           [\t\f\r ]

comment1        \{([^}])*\}
comment2        \(\*((\*[^\)])|([^*]))*\*\)
comment3        \(\*\*\*\)
comment         {comment1}|{comment2}|{comment3}

%{
#define YY_USER_ACTION loc.columns(yyleng);
%}

%%

%{
  Location& loc = driver.location_;
  loc.step();
%}

":="        {return Parser::make_ASSIGN(loc);}
":"         {return Parser::make_COLON(loc);}
";"         {return Parser::make_SEMICOLON(loc);}
"..."       {return Parser::make_DOTDOTDOT(loc);}
".."        {return Parser::make_DOTDOT(loc);}
"."         {return Parser::make_DOT(loc);}
","         {return Parser::make_COMMA(loc);}
"["         {return Parser::make_LBRACKET(loc);}
"]"         {return Parser::make_RBRACKET(loc);}
"("         {return Parser::make_LPARENTHESIS(loc);}
")"         {return Parser::make_RPARENTHESIS(loc);}

"+"         {return Parser::make_ADD(loc);}
"-"         {return Parser::make_SUB(loc);}
"*"         {return Parser::make_MUL(loc);}
"/"         {return Parser::make_REAL_DIV(loc);}
"div"       {return Parser::make_DIV(loc);}
"mod"       {return Parser::make_MOD(loc);}
"^"         {return Parser::make_CARET(loc);}
"@"         {return Parser::make_AT(loc);}

"and"       {return Parser::make_AND(loc);}
"not"       {return Parser::make_NOT(loc);}
"or"        {return Parser::make_OR(loc);}
"xor"       {return Parser::make_XOR(loc);}
"shl"       {return Parser::make_SHL(loc);}
"shr"       {return Parser::make_SHR(loc);}

"<"         {return Parser::make_LT(loc);}
"<="        {return Parser::make_LE(loc);}
">"         {return Parser::make_GT(loc);}
">="        {return Parser::make_GE(loc);}
"="         {return Parser::make_EQ(loc);}
"<>"        {return Parser::make_NE(loc);}

"begin"     {return Parser::make_BEGINS(loc);}
"end"       {return Parser::make_ENDS(loc);}
"program"   {return Parser::make_PROGRAM(loc);}
"function"  {return Parser::make_FUNCTION(loc);}
"extern"    {return Parser::make_EXTERN(loc);}
"var"       {return Parser::make_VAR(loc);}
"const"     {return Parser::make_CONST(loc);}
"type"      {return Parser::make_TYPE(loc);}
"if"        {return Parser::make_IF(loc);}
"then"      {return Parser::make_THEN(loc);}
"else"      {return Parser::make_ELSE(loc);}
"while"     {return Parser::make_WHILE(loc);}
"do"        {return Parser::make_DO(loc);}
"repeat"    {return Parser::make_REPEAT(loc);}
"until"     {return Parser::make_UNTIL(loc);}
"for"       {return Parser::make_FOR(loc);}
"to"        {return Parser::make_TO(true, loc);}
"downto"    {return Parser::make_TO(false, loc);}
"case"      {return Parser::make_CASE(loc);}
"break"     {return Parser::make_BREAK(loc);}
"continue"  {return Parser::make_CONTINUE(loc);}

"void"      {return Parser::make_VOID(loc);}
"boolean"   {return Parser::make_BOOLEAN(loc);}
"char"      {return Parser::make_CHAR(loc);}
"short"     {return Parser::make_SHORT(loc);}
"integer"   {return Parser::make_INTEGER(loc);}
"int64"     {return Parser::make_INT64(loc);}
"real"      {return Parser::make_REAL(loc);}
"string"    {return Parser::make_STRING(loc);}
"array"     {return Parser::make_ARRAY(loc);}
"of"        {return Parser::make_OF(loc);}
"record"    {return Parser::make_RECORD(loc);}

"true"      {return Parser::make_BOOLEAN_LITERAL(true, loc);}
"false"     {return Parser::make_BOOLEAN_LITERAL(false, loc);}

{integer} {
    long long number = std::strtoll(yytext, NULL, 0);
    return Parser::make_INTEGER_LITERAL(number, loc);
}

{real} {
    float number = std::strtof(yytext, NULL);
    return Parser::make_REAL_LITERAL(number, loc);
}

{char} {
    char *s, *end, c;
    s = yytext + 1;
    end = s + yyleng - 2;
    pcc::Lexer::GetUnescapedChar(s, end, &c);
    return Parser::make_CHAR_LITERAL(c, loc);
}

{string} {
    char *s, *end, *p;
    s = p = yytext + 1;
    end = s + yyleng - 2;
    while (s < end) {
        s = pcc::Lexer::GetUnescapedChar(s, end, p);
        p++;
    }
    *p++ = '\0';
    return Parser::make_STRING_LITERAL(std::vector<char>(yytext + 1, p), loc);
}

{identifier} {
    return Parser::make_IDENTIFIER(yytext, loc);
}

{white}+ {loc.step();}
\n+      {loc.lines(yyleng); loc.step();}

{comment} {
    int i;
    for(i=0; i<yyleng; i++){
        if(yytext[i]=='\n'){
            loc.lines(1);
            loc.step();
        }
    }
}

. {
    pcc::Log(pcc::PCC_ERROR, "unrecognized token \"%s\"", yytext);
}

<<EOF>>  {return Parser::make_FILE_END(loc);}

%%

namespace pcc {
int Driver::StartLexer(std::string& filename) {
    yy_flex_debug = (GetLogLevel()==LogLevel::PCC_DEBUG);
    yyin = std::fopen(filename.c_str(), "r");
    if (yyin == NULL) {
        Log(LogLevel::PCC_ERROR, "failed to open file %s", filename.c_str());
        return -1;
    }
    return 0;
}

void Driver::StopLexer() { std::fclose(yyin); }
} // namespace pcc