-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp02lex.l
157 lines (153 loc) · 6.42 KB
/
p02lex.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
%{
//--------------------------------------------------------------------
// File p02lex.l defines a prototype scanner for expressions.
// The scanner definition is a lex specification.
//--------------------------------------------------------------------
// Author 1: Steven Chambers
// Student ID: *20342421
// E-Mail: [email protected]
// Author 2: Melicent King
// Student ID: *20355157
// E-mail: [email protected]
// Course: CMSC 4023 - Programming Languages
// CRN: 12105
// Project: p02
// Due: November 5, 2014
// Account: tt060
//--------------------------------------------------------------------
//--------------------------------------------------------------------
// Standard C and C++ Library Include Files
//--------------------------------------------------------------------
#include <string>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
using namespace std;
//--------------------------------------------------------------------
// Application Includes
//--------------------------------------------------------------------
#include "p02lex.h"
#include "p02tkn.h"
//--------------------------------------------------------------------
//Function prototypes and variable definitions
//--------------------------------------------------------------------
int lineNo = 1;
int colNo = 1;
int TokenMgr(int t);
void TokenPrint(ostream& o,int t);
//--------------------------------------------------------------------
//Global Variables
//--------------------------------------------------------------------
static string spelling[]=
{"NOTOKEN","INTLIT","PLUS","MINUS","STAR","SLASH","LPAREN","RPAREN",
"LBRACKET","RBRACKET","COLON","SEMICOLON","COMMA","PERIOD",
"RANGE","ASSIGNOP","EQU","NEQ","LES","GRT","GEQ","CHRLIT",
"REALIT","ID","AND","ARRAY","BEGAN","DIV","DO","ELSE","END","FOR",
"FUNCTION","IF","INTEGER","MOD","NOT","OF","OR","PROCEDURE",
"PROGRAM","REAL","REPEAT","THEN","TO","UNTIL","VAR","WHILE",
"LEQ","ERROR"
};
%}
punctuation ("+"|"-"|"*"|"/"|"("|")"|"["|"]"|":"|";"|","|".."|":="|"="|"<>"|"<"|"<="|">"|">=")
emptySpace [ \t\n]+
%%
\n {lineNo++; colNo = 1;/*allow proper counting of lines/reset columns*/}
[ \t]+ {colNo++;/* allow proper counting of columns*/}
[{][^{]*[}] ;//allow for scanner to ignore comments
[aA][nN][dD] return TokenMgr(AND);
[aA][rR][rR][aA][yY] return TokenMgr(ARRAY);
[bB][eE][gG][iI][nN] return TokenMgr(BEGAN);
[dD][iI][vV] return TokenMgr(DIV);
[dD][oO] return TokenMgr(DO);
[eE][lL][sS][eE] return TokenMgr(ELSE);
[eE][nN][dD] return TokenMgr(END);
[fF][oO][rR] return TokenMgr(FOR);
[fF][uU][nN][cC][tT][iI][oO][nN] return TokenMgr(FUNCTION);
[iI][fF] return TokenMgr(IF);
[iI][nN][tT][eE][gG][eE][rR] return TokenMgr(INTEGER);
[mM][oO][dD] return TokenMgr(MOD);
[nN][oO][tT] return TokenMgr(NOT);
[oO][fF] return TokenMgr(OF);
[pP][rR][oO][cC][eE][dD][uU][rR][eE] return TokenMgr(PROCEDURE);
[pP][rR][oO][gG][rR][aA][mM] return TokenMgr(PROGRAM);
[rR][eE][aA][lL] return TokenMgr(REAL);
[rR][eE][pP][eE][aA][tT] return TokenMgr(REPEAT);
[tT][hH][eE][nN] return TokenMgr(THEN);
[tT][oO] return TokenMgr(TO);
[uU][nN][tT][iI][lL] return TokenMgr(UNTIL);
[vV][aA][rR] return TokenMgr(VAR);
[wW][hH][iI][lL][eE] return TokenMgr(WHILE);
[0-9]+ return TokenMgr(INTLIT);
[0-9]+\.[0-9]*([Ee][+-]?[0-9]+)?/{punctuation}|{emptySpace} return TokenMgr(REALIT);
[0-9]*\.[0-9]+([Ee][+-]?[0-9]+)?/{punctuation}|{emptySpace} return TokenMgr(REALIT);
[0-9]+[Ee][+-]?[0-9]+/{punctuation}|{emptySpace} return TokenMgr(REALIT);
\'[^{\n]\'|'''' return TokenMgr(CHRLIT);//ask turner about this
[a-zA-Z_][a-zA-Z0-9_]* return TokenMgr(ID);
"+" return TokenMgr(PLUS);
"-" return TokenMgr(MINUS);
"*" return TokenMgr(STAR);
"/" return TokenMgr(SLASH);
"(" return TokenMgr(LPAREN);
")" return TokenMgr(RPAREN);
"[" return TokenMgr(LBRACKET);
"]" return TokenMgr(RBRACKET);
":" return TokenMgr(COLON);
";" return TokenMgr(SEMICOLON);
"," return TokenMgr(COMMA);
"." return TokenMgr(PERIOD);
".." return TokenMgr(RANGE);
":=" return TokenMgr(ASSIGNOP);
"=" return TokenMgr(EQU);
"<>" return TokenMgr(NEQ);
"<" return TokenMgr(LES);
"<=" return TokenMgr(LEQ);
">" return TokenMgr(GRT);
">=" return TokenMgr(GEQ);
. return TokenMgr(ERROR);
%%
//--------------------------------------------------------------------
//Class Lexer implementation
//--------------------------------------------------------------------
int yywrap(){return 1;}
int TokenMgr(int t)
{
TokenPrint(cout,t);
colNo+= strlen(yytext);
return t;
}
string textToLower() {
string toPrint = "";
char txt[strlen(yytext)];
strcpy(txt, yytext);
char uppers[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
char lowers[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
for (int i = 0; i < strlen(txt); i++) {
toPrint += txt[i];
for (int j = 0; j < 26; j++) {
if (txt[i]==uppers[j]) {
toPrint.at(i) = lowers[j];
break;
}
}
}
return toPrint;
}
void TokenPrint(ostream& o,int t) {
string toPrint = textToLower();
o << endl;
o << "Token:Code="<< t << " "
<< "Name= "<<setw(10)<< right << spelling[t-NOTOKEN]<< " "
<< " line= "<<lineNo << " "
<< "col= " << setw(3) << right << colNo << " ";
o << "Spelling= \"" << toPrint << "\"";
}
//--------------------------------------------------------------------
//Constructor Lexer is used to redirect the input file stream from the
//keyboard to input file stream i.
//--------------------------------------------------------------------
Lexer::Lexer(FILE* i){yyin=i;}
int Lexer::Scan(void){return yylex();}
//-----------------------End of Lex Definition------------------------