-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner-specification.l
108 lines (80 loc) · 2.03 KB
/
scanner-specification.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
%{
#include <cstdio>
#include <iostream>
#include <cstring>
#include "parser.tab.h"
extern int yylex();
int lineNumber = 0;
%}
%option noyywrap
NUM [0-9]+
VALID_STRING_CHAR [^"\n\r]
INVALID_STRING_CHAR [\n\r]
STRING {VALID_STRING_CHAR}*
IDENT [A-Za-z][A-Za-z0-9]*
BOOL (true|false)
%x STRING_MATCHING
%%
"+" { return PLUS; }
"-" { return MINUS; }
"*" { return MULT; }
"/" { return DIV; }
"%" { return MODULO; }
"and" { return AND; }
"or" { return OR; }
"not" { return NOT; }
"=" { return NUM_EQ; }
"<" { return LESS; }
"<=" { return LESS_EQ; }
">" { return GREATER; }
">=" { return GREATER_EQ; }
"<>" { return NUM_NOT_EQ; }
"==" { return STR_EQ; }
"!=" { return STR_NOT_EQ; }
"(" { return OPEN_BRACKET; }
")" { return CLOSE_BRACKET; }
"," { return COMMA; }
"length" { return LENGTH; }
"position" { return POSITION; }
"substring" { return SUBSTRING; }
"concatenate" { return CONCATENATE; }
"readint" { return READINT; }
"readstr" { return READSTR; }
"if" { return IF; }
"then" { return THEN; }
"else" { return ELSE; }
"while" { return WHILE; }
"do" { return DO; }
"print" { return PRINT; }
":=" { return ASSIGN; }
"begin" { return BEGIN_INSTRUCTION; }
"end" { return END_INSTRUCTION; }
";" { return LINE_END; }
"exit" { return EXIT; }
{NUM} {
yylval.integer_value = strtol(yytext, nullptr, 10);
return NUM;
}
["] { BEGIN(STRING_MATCHING); }
<STRING_MATCHING>{STRING}["] {
string input = string(yytext);
string content = input.substr(0, input.size() - 1);
yylval.string_value = strdup(content.c_str());
BEGIN(INITIAL);
return STRING;
}
<STRING_MATCHING>{INVALID_STRING_CHAR} {
cout << "Błąd parsowania wartości typu string w linii: " << ++lineNumber << "!" << endl;
exit(-1);
}
{BOOL} {
yylval.bool_value = string(yytext) == "true";
return BOOL;
}
{IDENT} {
yylval.string_value = strdup(yytext);
return IDENT;
}
[\n] { ++lineNumber; }
[ \t] ;
%%