-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscanner.l
100 lines (84 loc) · 3.41 KB
/
scanner.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
%{
#include "globals.h"
#include "util.h"
#include "scan.h"
#include <iostream>
/* lexeme of identifier or reserved word */
char tokenString[MAXTOKENLEN+1];
static int yylex(void);
%}
%Start MultiLineComment SingleLineComment
%%
<INITIAL>"/*" BEGIN MultiLineComment;
<MultiLineComment>"*"+\/ BEGIN (INITIAL);
<MultiLineComment>[^\n\*]* |
<MultiLineComment>"*"+[^\n\/] ;//Deletes most characters
<MultiLineComment>\n lineno++;
<MultiLineComment>. ;//Deletes missing characters if needed
<MultiLineComment><<EOF>> ;// Ignore
<INITIAL>"//" BEGIN SingleLineComment;
<SingleLineComment>.* ;
<SingleLineComment>\n lineno++;BEGIN (INITIAL);
<SingleLineComment><<EOF>> BEGIN (INITIAL);
"else" return ELSE;
"ELSE" return ELSE;
"if" return IF;
"IF" return IF;
"int" return INT;
"INT" return INT;
"return" return RETURN;
"RETURN" return RETURN;
"void" return VOID;
"VOID" return VOID;
"while" return WHILE;
"WHILE" return WHILE;
[_@:]?[a-zA-Z][_a-zA-Z0-9]* return ID;
[0-9]+ return NUM;
[ \t] ;
\n lineno++;
\r ;
"+" return PLUS;
"-" return MINUS;
"*" return TIMES;
"/" return SLASH;
\% return MOD;
\& return BITWISEAND;
\| return BITWISEOR;
\^ return XOR;
"<<" return LEFTSHIFT;
"<" return LESSER;
"<=" return LESSEQ;
">>" return RIGHTSHIFT;
">" return GREATER;
">=" return GREATEQ;
"==" return EQCOMP;
"!=" return NOTEQ;
"=" return EQATR;
";" return SEMI;
"," return COMMA;
"(" return LPAREN;
")" return RPAREN;
"{" return LBRACKET;
"}" return RBRACKET;
"[" return LBRACE;
"]" return RBRACE;
. std::cout << "\nERROR: Uncaught character at line " << lineno <<"\n"; return ERROR;
%%
TokenType getToken(void)
{ static int firstTime = TRUE;
TokenType currentToken;
if (firstTime)
{ firstTime = FALSE;
lineno++;
yyin = source;
yyout = listing;
}
currentToken = yylex();
if( currentToken == ID || currentToken== NUM)
strncpy(tokenString,yytext,MAXTOKENLEN);
if (TraceScan) {
fprintf(listing,"\t%d: ",lineno);
printToken(currentToken,yytext);
}
return currentToken;
}