-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexical.l
115 lines (105 loc) · 2.22 KB
/
lexical.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
109
110
111
112
113
114
%option noyywrap nodefault yylineno case-insensitive
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "syntactic.tab.h"
void to_lowercase(char *s)
{
int i = 0;
for (i = 0; s[i]; i++)
s[i] |= ('A' <= s[i] && s[i] <= 'Z') ? (1 << 5) : 0;
}
%}
WHITE [\t ]
LETTER [a-zA-Z]
DIGIT [0-9]
NUMBER [0-9]+
SIGN ("+"|"-")?
INTEGER {SIGN}{NUMBER}
REAL1 {SIGN}{NUMBER}"."{NUMBER}((e|E){INTEGER})?
REAL2 {SIGN}"."{NUMBER}((e|E){INTEGER})?
REAL3 {SIGN}{NUMBER}"."((e|E){INTEGER})?
REAL4 {SIGN}{NUMBER}((e|E){INTEGER})
REAL ({REAL1}|{REAL2}|{REAL3}|{REAL4})
%s WRITE_STATE FMT_STR
%%
{WHITE} {}
\n return EOL;
"!"([^\n])* {
yytext++;
yylval = yytext;
return COMMENT;
}
"'" return QUOTE;
"(" return OPEN_PARENS;
")" return CLOSE_PARENS;
"{" return OPEN_BRACKET;
"}" return CLOSE_BRACKET;
"parameter" return PARAMETER;
"::" return VAR_DEF_SEPARATOR;
"," return COMMA;
";" return SEMICOLON;
"^" return POWER;
"**" return POWER;
"*" return TIMES;
"+" return PLUS;
"-" return MINUS;
"/" return DIVIDE;
"=" return EQUAL;
"do" return DO_KEYWORD;
"then" return THEN_KEYWORD;
"if" return IF_KEYWORD;
"else" return ELSE_KEYWORD;
".true." return TRUE_KEYWORD;
".false." return FALSE_KEYWORD;
".eq." return EQUAL_KEYWORD;
".ne." return BNE_KEYWORD;
".lt." return BLT_KEYWORD;
".gt." return BGT_KEYWORD;
".ge." return BGE_KEYWORD;
".le." return BLE_KEYWORD;
".and." return AND_KEYWORD;
".or." return OR_KEYWORD;
"'"([^'])*"'" {
yytext = yytext + 1;
yytext[strlen(yytext)-1] = 0;
//printf("'%s' : STRING\n", yytext);
yylval = yytext;
return STRING;
}
"implicit" return IMPLICIT;
"none" return NONE;
"print" return PRINT_COMMAND;
"write" BEGIN(WRITE_STATE); return WRITE_COMMAND;
<WRITE_STATE>{
"(*,*)" BEGIN(INITIAL); return(FMT_ANY);
"(*,'(" BEGIN(FMT_STR); return(FMT_BEG);
<FMT_STR>{
")')" BEGIN(INITIAL); return(FMT_END);
({NUMBER})?(e|es|l|a|x|t|i|f)({NUMBER}("."{NUMBER})?)? {
yylval = yytext;
return(FMT_TXT);
}
}
}
"read" return READ_COMMAND;
"real" return REAL_KEYWORD;
"program" return PROGRAM_KEYWORD;
"end" return END_KEYWORD;
"integer" return INTEGER_KEYWORD;
{REAL} {
yylval = yytext;
return REAL_NUM;
}
{INTEGER} {
yylval = yytext;
return INT_NUM;
}
[a-z][a-zA-Z0-9_]* {
to_lowercase(yytext);
yylval = yytext;
return IDENTIFIER;
}
.
%%