-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpc.l
More file actions
149 lines (132 loc) · 4.22 KB
/
pc.l
File metadata and controls
149 lines (132 loc) · 4.22 KB
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
%{
#include <stdio.h>
#include "Tree.h"
#include "Scope.h"
#include "y.tab.hpp"
#define LEX_DBG 0
void echo(char*);
int lineno = 1;
%}
%x COMMENT1
%x COMMENT
ident [a-zA-Z][a-zA-Z0-9]*
number [0-9]+
whitespace [ \t]+
%%
"program" { echo("PROGRAM"); return PROGRAM; }
"var" { echo("VAR"); return VAR; }
"array" { echo("ARRAY"); return ARRAY; }
"of" { echo("OF"); return OF; }
"integer" { echo("INTEGER"); return INTEGER; }
"real" { echo("REAL"); return REAL; }
"function" { echo("FUNCTION"); return FUNCTION; }
"procedure" { echo("PROCEDURE"); return PROCEDURE; }
"begin" { echo("BBEGIN"); return BBEGIN; }
"end" { echo("END"); return END; }
"if" { echo("IF"); return IF; }
"then" { echo("THEN"); return THEN; }
"else" { echo("ELSE"); return ELSE; }
"and" { echo("AND"); return AND; }
"while" { echo("WHILE"); return WHILE; }
"do" { echo("DO"); return DO; }
"not" { echo("NOT"); return NOT; }
"to" { echo("TO"); return TO; }
"for" { echo("FOR"); return FOR; }
".." { echo("DOTDOT"); return DOTDOT; }
":=" { echo("ASSIGNOP"); return ASSIGNOP; }
"=" {
if (LEX_DBG) fprintf(stderr, "RELOP[%s]", yytext);
yylval.opval = EQ;
return RELOP;
}
"<>" {
if (LEX_DBG) fprintf(stderr, "RELOP[%s]", yytext);
yylval.opval = NE;
return RELOP;
}
"<" {
if (LEX_DBG) fprintf(stderr, "RELOP[%s]", yytext);
yylval.opval = LT;
return RELOP;
}
"<=" {
if (LEX_DBG) fprintf(stderr, "RELOP[%s]", yytext);
yylval.opval = LE;
return RELOP;
}
">" {
if (LEX_DBG) fprintf(stderr, "RELOP[%s]", yytext);
yylval.opval = GT;
return RELOP;
}
">=" {
if (LEX_DBG) fprintf(stderr, "RELOP[%s]", yytext);
yylval.opval = GE;
return RELOP;
}
"or" {
if (LEX_DBG) fprintf(stderr, "ADDOP[%s]", yytext);
yylval.opval = OR;
return ADDOP;
}
"+" {
if (LEX_DBG) fprintf(stderr, "ADDOP[%s]", yytext);
yylval.opval = PLUS;
return ADDOP;
}
"-" {
if (LEX_DBG) fprintf(stderr, "ADDOP[%s]", yytext);
yylval.opval = MINUS;
return ADDOP;
}
"and" {
if (LEX_DBG) fprintf(stderr, "MULOP[%s]", yytext);
yylval.opval = AND;
return MULOP;
}
"*" {
if (LEX_DBG) fprintf(stderr, "MULOP[%s]", yytext);
yylval.opval = STAR;
return MULOP;
}
"/" {
if (LEX_DBG) fprintf(stderr, "MULOP[%s]", yytext);
yylval.opval = SLASH;
return MULOP;
}
{number} {
if (LEX_DBG) fprintf(stderr, "INUM[%d]", atoi(yytext));
yylval.ival = atoi(yytext);
return INUM;
}
{number}"."{number} {
if (LEX_DBG) fprintf(stderr, "RNUM[%f]", atof(yytext));
yylval.rval = atof(yytext);
return RNUM;
}
{ident} {
if (LEX_DBG)fprintf(stderr, "ID[%s]", strdup(yytext));
yylval.sval = strdup(yytext);
return ID;
}
"//" { BEGIN(COMMENT1); }
<COMMENT1>"\n" { ++lineno; BEGIN(INITIAL); }
<COMMENT1>. { ; }
"(*" { BEGIN(COMMENT); }
<COMMENT>\n { ++lineno; }
<COMMENT>"*)" { BEGIN(INITIAL); } /* reset lex to normal state */
<COMMENT>. { ; }
{whitespace} { ; }
\n {
++lineno;
if (LEX_DBG) fprintf(stderr,"%s", yytext);
}
. {
if(LEX_DBG) fprintf(stderr, "{%c}", yytext[0]);
return yytext[0];
}
%%
void echo(char* message) {
if (LEX_DBG)
fprintf(stderr, "{%s} ", message);
}