-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspl.l
134 lines (128 loc) · 3.23 KB
/
spl.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
%{
#ifdef PRINT
#define TOKEN(t) printf("Token: " #t "\n");
#define NUMBER_TOKEN(t) printf("Number %d Token: " #t "\n", atoi(yytext));
#define ID_TOKEN(t) printf("ID %s Token: " #t "\n", yytext);
#define FLOAT_TOKEN(t) printf("Float %d Token:" #t "\n", atof(yytext));
#else
#define TOKEN(t) return(t);
#define NUMBER_TOKEN(t) yylval.iVal = installId(yytext);return(t);
#define FLOAT_TOKEN(t) yylval.iVal = installId(yytext);return(t);
#define ID_TOKEN(t) yylval.iVal = installId(yytext); return(t);
/* Declare Symbol Table Type and Array as imported types */
#include <string.h>
#include <stdlib.h>
extern SYMTABNODEPTR symTab[SYMTABSIZE];
extern int currentSymTabSize;
int installId(char *);
#endif
%}
delim [ \t\n\r]
ws {delim}+
digit [0-9]
letter [a-zA-z]
number {digit}+
id {letter}({letter}|{digit})*
char "'"{letter}"'"
realNo {number}+\.{number}+
%%
";" TOKEN(SEMICOLON_T)
":=" TOKEN(ASSIGNS_T)
":" TOKEN(COLON_T)
"." TOKEN(FULLSTOP_T)
"," TOKEN(COMMA_T)
"->" TOKEN(ARROW_T)
"(" TOKEN(BRA_T)
")" TOKEN(KET_T)
"=" TOKEN(EQUALS_T)
"<>" TOKEN(LESS_GREATER_T)
"<" TOKEN(LESS_THAN_T)
">" TOKEN(GREATER_THAN_T)
"<=" TOKEN(LESS_THAN_EQUALS_T)
">=" TOKEN(GREATER_THAN_EQUALS_T)
"+" TOKEN(PLUS_T)
"-" TOKEN(MINUS_T)
"*" TOKEN(MULTIPLY_T)
"/" TOKEN(DIVISION_T)
"'" TOKEN(APOST_T)
ENDP TOKEN(ENDP_T)
DECLARATIONS TOKEN(DECLARATIONS_T)
CODE TOKEN(CODE_T)
OF TOKEN(OF_T)
TYPE TOKEN(TYPE_T)
CHARACTER TOKEN(CHARACTER_T)
INTEGER TOKEN(INTEGER_T)
REAL TOKEN(REAL_T)
IF TOKEN(IF_T)
THEN TOKEN(THEN_T)
ENDIF TOKEN(ENDIF_T)
ELSE TOKEN(ELSE_T)
DO TOKEN(DO_T)
WHILE TOKEN(WHILE_T)
ENDWHILE TOKEN(ENDWHILE_T)
ENDDO TOKEN(ENDDO_T)
FOR TOKEN(FOR_T)
IS TOKEN(IS_T)
BY TOKEN(BY_T)
TO TOKEN(TO_T)
ENDFOR TOKEN(ENDFOR_T)
WRITE TOKEN(WRITE_T)
NEWLINE TOKEN(NEWLINE_T)
READ TOKEN(READ_T)
NOT TOKEN(NOT_T)
AND TOKEN(AND_T)
OR TOKEN(OR_T)
{id} ID_TOKEN(IDENTIFIER_T)
{number} ID_TOKEN(NUMBER_T)
{char} ID_TOKEN(CHARACTER_CONSTANT_T)
{realNo} FLOAT_TOKEN(FLOAT_NUMBER_T)
{ws} ; /* Nothing */
. fprintf(stderr, "Error: Unexpected symbol found: %s (%d)\n", yytext, yytext[0]);
%%
#ifndef PRINT
SYMTABNODEPTR newSymTabNode()
{
return ((SYMTABNODEPTR)malloc(sizeof(SYMTABNODE)));
}
/* Look up an identifier in the symbol table, if its there return
its index. If its not there, put it in the end position,
as long as the table isn't full, and return its index.
*/
int lookup(char *s)
{
extern SYMTABNODEPTR symTab[SYMTABSIZE];
extern int currentSymTabSize;
int i;
for(i=0; i<currentSymTabSize; i++)
{
if(strncmp(s,symTab[i]->identifier,IDLENGTH) == 0)
{
return (i);
}
}
return (-1);
}
int installId(char *id)
{
extern SYMTABNODEPTR symTab[SYMTABSIZE];
extern int currentSymTabSize;
int index;
index = lookup(id);
if (index >= 0)
{
return (index);
}
else
if (currentSymTabSize >= SYMTABSIZE)
/* SYMTAB is full */
return (NOTHING) ;
else
{
symTab[currentSymTabSize] = newSymTabNode();
/* Recommended code for preventing buffer overrun on bounded strings */
strncpy(symTab[currentSymTabSize]->identifier,id,IDLENGTH);
symTab[currentSymTabSize]->identifier[IDLENGTH-1] = '\0';
return(currentSymTabSize++);
}
}
#endif