-
Notifications
You must be signed in to change notification settings - Fork 2
/
pascal.l
117 lines (106 loc) · 4.24 KB
/
pascal.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
115
116
/********************************************************************/
/* */
/* */
/* LEXER / SCANNER */
/* */
/* */
/* */
/********************************************************************/
%{
#include <stdio.h>
#include "stack.h"
#include "symbol.h"
#include "tac.h"
#include "y.tab.h"
SYMBTABLE *current_table;
extern SYMBTABLE *constant_symbol_table;
extern int line_count;
extern stack symbol_table_stack;
extern char *yytext;
#define token(x) x
%}
alpha [a-zA-Z_]
alphanum [a-zA-Z_0-9]
comment "{"[^}]*"}"
exponent e([+-])?[0-9]+
string \'([^'\n]|\'\')+\'
unsignedint [0-9]+
signedint "-"[0-9]+|"+"[0-9]+
whitespace [ \t]
newline [\n]
%%
">=" return token(GE);
"<=" return token(LE);
"<>" return token(NE);
":=" return token(ASSIGN);
".." return token(DOTDOT);
"AND" return token(AND);
"ARRAY" return token(ARRAY);
"BEGIN" return token(_BEGIN);
"CASE" return token(CASE);
"CONST" return token(CONST);
"DIV" return token(DIV);
"DO" return token(DO);
"DOWNTO" return token(DOWNTO);
"ELSE" return token(ELSE);
"END" return token(END);
"FILE" return token(_FILE);
"FOR" return token(FOR);
"FORWARD" return token(FORWARD);
"FUNCTION" return token(FUNCTION);
"GOTO" return token(GOTO);
"IF" return token(IF);
"IN" return token(IN);
"LABEL" return token(LABEL);
"MOD" return token(MOD);
"NIL" return token(NIL);
"NOT" return token(NOT);
"OF" return token(OF);
"OR" return token(OR);
"PACKED" return token(PACKED);
"PROCEDURE" return token(PROCEDURE);
"PROGRAM" return token(PROGRAM);
"RECORD" return token(RECORD);
"REPEAT" return token(REPEAT);
"SET" return token(SET);
"THEN" return token(THEN);
"TO" return token(TO);
"TYPE" return token(TYPE);
"UNTIL" return token(UNTIL);
"VAR" return token(VAR);
"WHILE" return token(WHILE);
"WITH" return token(WITH);
"NUM" return token(NUM);
"INTEGER" return token(INTEGER);
"REAL" return token(REAL);
"print" return token(print);
{alpha}{alphanum}* {
top_table( &symbol_table_stack, ¤t_table );
mkname( current_table, yytext, T_VAR );
return token(ID);
}
{signedint} {
mkname( constant_symbol_table, yytext, T_INT );
return token(NUM_INT);
}
{unsignedint} {
mkname( constant_symbol_table, yytext, T_INT );
return token(NUM_INT);
}
{unsignedint}\.{unsignedint}({exponent})? {
mkname( constant_symbol_table, yytext, T_REAL );
return token(NUM_REAL);
}
{unsignedint}{exponent} {
mkname( constant_symbol_table, yytext, T_REAL );
return token(NUM_REAL);
}
{string} {
mkname( constant_symbol_table, yytext, T_STRING );
return token(STRING);
}
{whitespace}+ ;
{newline} { line_count++; }
{comment}+ ;
. return token(yytext[0]);
%%