File: lexical.cpp

package info (click to toggle)
aces3 3.0.6-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 82,460 kB
  • sloc: fortran: 225,647; ansic: 20,413; cpp: 4,349; makefile: 953; sh: 137
file content (518 lines) | stat: -rw-r--r-- 14,402 bytes parent folder | download | duplicates (6)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
*  Copyright (c) 2003-2010 University of Florida
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  The GNU General Public License is included in this distribution
*  in the file COPYRIGHT.
*/ 
/******************************
lexical.cpp
Lei Wang
wang@qtp.ufl.edu
May 2004
******************************/
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <iomanip>
//#include "lexical.h"
#include "keywdcnt.h"
#include "variable.h"

using namespace std;

int outArray[max_output_size]={0};
//the array that contains the index of output.

int outarray_size=0;
//real size of outArray

int errorArray[max_output_size]={0};
//the array that contains the index of output when meet errors.

int errorarray_size=0;
//real size of errorArray

#define max(a,b) ((a)>(b))?(a):(b)
/*
struct symbol
{
    string name;
    int type;
    int location;
};
*/

double floatconst[max_output_size];
int floatno=0;// for input
//int floatno1=0;for getout
int intconst[max_output_size];
int intno=0;


/*====================================
The function insertsymbol insert variable id into symboltable.

lexaryp:  the position of lexarray
  idno:     total number of ids
  symbolno: total number of symbols
  idname:    the name of the id need to be inserted into symbol table
  stackno:  the total number of stackitems

int insertsymbol(string id,int lexaryp, 
                  int &symbolno,  int &stackno, int type)
{
    int i;
    int identical =0;
    for (i=0; i<=symbolno; i++)
    {
        if(symboltable[i].name==id)
        {
            identical =1;
            break;
        }
    }
    if (!identical)
    {
        stackno++;
        symbolno++;

        
        symboltable[symbolno].name=id;
        //cout <<"new idname="<<id<<endl;
        symboltable[symbolno].type = type;
        symboltable[symbolno].location = stackno;
        return stackno;
    }
    else //the array exists in symboltable
    {
        cout<<"redeclaration====="<< i<<endl;
        return i;
    }
}
====================================*/





/************************************
The above is the syntax part.
Below is the lexical part
************************************/



/*====================================
insertp function inserts i as index of keywords
into the next available position of outArray 
====================================*/
void insertp(int i)
{
    outArray[outarray_size]=i;
    if (outarray_size<max_output_size)
        outarray_size ++;
    else
    {
        printf("outSourceArray length is out of bounds. Please contact code writer.\n");
        printf("   outarray_size = %d, max_output_size = %d\n",outarray_size,max_output_size);
        exit(1);
    }
}



/*====================================
inserterror function inserts i as the position of
the unknown character into the next 
available position of errorArray 
====================================*/
void inserterror(int i)
{
    errorArray[errorarray_size]=i;
    errorarray_size++;
}



/*====================================
insertid function inserts an id (character string) from position start 
to position end,and converts to string, then stores into the next 
available position string name[]. idno idicates the position
of the inserted string in string idname[].  
====================================*/
void insertid(int &idno, int start, int end, 
              string idname[], char sourceArray[])
{
    int i=0;
    int ii=0;
    
    char temp[128]="";
    for (i=start; i<=end;i++)
    {
        temp[ii++]=sourceArray[i];
    }
    //temp[ii]='\n';
    idname[idno]=temp;
    idno++;
}



/*====================================
insertfloat function inserts a float from char from position start to 
position end,and converts to float, then stores into the next available 
position in floatconst[]. floatno idicates the position of the inserted 
float in floatconst[].  
====================================*/
void insertfloat(char sourceArray[], int start, int end)
{
    char *temp = new char[end-start+2];
    int i;
    int ii=0;
    for (i=start; i<=end; i++)
        temp[ii++]=sourceArray[i];
    temp[ii]='\0';

    floatconst[floatno]=atof((const char *)temp);
    floatno++;
    delete temp;
}



/*====================================
insertint function inserts an int from char from position start to position
end, and converts to int, then stores into the next available position in 
intconst[]. intno idicates the position of the inserted int in intconst[].  
====================================*/
void insertint(char sourceArray[], int start, int end)
{
    char *temp = new char[end-start+2];
    int i;
    int ii=0;
    for (i=start; i<=end; i++)
        temp[ii++]=sourceArray[i];
    temp[ii]='\0';

    intconst[intno]=atoi((const char *)temp);
    intno++;
    delete temp;
}



/*====================================
lexical function lexicalizes the characters stored in sourceArray,
variable sAsize tells the function the size of sourceArray is. 
idno is the number of total Ids (including duplications).
====================================*/
void lexical(char sourceArray[], int sAsize, 
             int &idno, string idname[], int &rnumber)
{
    int psource =0;               //position of sourceArray
    int temp=0;
    int i;                        //for iteration
    int foundeq;
    int strsize=0;

    /*find start of sial*/
    while (psource+3<sAsize&&
        strncmp((const char*)&(sourceArray[psource]),"sial", 4))
    {
        if (sourceArray[psource]=='\n')
            rnumber++;
        psource++;
    }

    while (psource <sAsize)
    {
        if (sourceArray[psource]>='a'&& sourceArray[psource]<='z')
        //possible identifier
        {
            temp=psource+1;
            while (temp<sAsize&&
                    ((sourceArray[temp]>='a'&& sourceArray[temp]<='z')
                    ||(sourceArray[temp]<='9'&& sourceArray[temp]>='0')
                    ||sourceArray[temp]=='_')
                  )
            {
                temp++;//check the rest are 
            }

            //see if the possible identifier is a key word
            foundeq=0;
            for (i=0; i<noperat_keywords; i++)
            {

                strsize=max(temp-psource, signed(strlen(keywords[i])));
                if(!strncmp((const char*)&(sourceArray[psource]), 
                        keywords[i], 
                        strsize))
                {
                    insertp(i);
                    foundeq=1;
                    break;
                }
            }
            if (foundeq==0)
            {
                insertp(id);
                if (psource+maxidlen>=temp)
                    insertid(idno, psource, temp-1, idname, sourceArray);
                else
                    insertid(idno, psource, psource+maxidlen, idname, sourceArray);
            }
            psource=temp; /*move pointer to the next position*/
        }
        else if (sourceArray[psource] == '=')
        {
            if (psource+1<sAsize && sourceArray[psource+1] == '=')   //==
            {
                insertp(eq);
                psource+=2;
            }
            else
            {
                insertp(assign);
                psource++;
            }
        }
        else if (sourceArray[psource] == '!')
        {
            if (psource+1<sAsize && sourceArray[psource+1] == '=')   //!=
            {
                insertp(ne);
                psource+=2;
            }
            else
            {
                insertp(error);
                psource++;
            }
        }
        else if (sourceArray[psource] == '#')                   /*comment*/
        {
            temp=psource+1;
            while (temp<sAsize&&sourceArray[temp]!='\n')
                temp++;
            psource=temp;
        }
        else if (sourceArray[psource] == '+')
        {
            if (psource+1<sAsize && sourceArray[psource+1] == '=')    //+=
            {
                insertp(addassign);
                psource+=2;
            }
            else                                  //+
            {
                insertp(add);
                psource++;
            }
        }
        else if (sourceArray[psource] == '-')
        {
            if (psource+1<sAsize && sourceArray[psource+1] == '=')    //-=
            {
                insertp(subassign);
                psource+=2;
            }
            else                                  //-
            {
                insertp(sub);
                psource++;
            }
        }
        else if (sourceArray[psource] == '*')
        {
            if (psource+1<sAsize && sourceArray[psource+1] == '=')    //*=
            {
                insertp(multassign);
                psource+=2;
            }
            else                                  //*
            {
                insertp(mult);
                psource++;
            }
        }
        else if (sourceArray[psource] == '/')
        {
            if (psource+1<sAsize && sourceArray[psource+1] == '=')    // /=
            {
                insertp(divassign);
                psource+=2;
            }
            else                                  // /
            {
                insertp(divide);
                psource++;
            }
        }
        else if (sourceArray[psource] == '^')
        {
            insertp(tensor);
            psource++;
        }
        else if (sourceArray[psource] == '<')
        {
            if (psource+1<sAsize && sourceArray[psource+1] == '=')    // <=
            {
                insertp(le);
                psource+=2;
            }
            else                                  // <
            {
                insertp(lt);
                psource++;
            }
        }
        else if (sourceArray[psource] == '>')
        {
            if (psource+1<sAsize && sourceArray[psource+1] == '=')    // >=
            {
                insertp(ge);
                psource+=2;
            }
            else                                  // >
            {
                insertp(gt);
                psource++;
            }
        }
        else if (sourceArray[psource] <= char('9')
            &&sourceArray[psource]>= char ('0'))    //number
        {
            temp=psource+1;
            while (temp<sAsize && sourceArray[temp] <='9'
                &&sourceArray[temp] >='0')
            {
                temp++;
            }
            if (sourceArray[temp] == '.')               // float
            {
                temp=temp+1;
                while (temp<sAsize && sourceArray[temp] <='9'
                    &&sourceArray[temp] >='0')    
                {
                    temp++;
                }
                insertp(fconst);
                insertfloat(sourceArray, psource, temp-1);
                psource = temp;
            }
            else                                           //integer
            {
                insertp(iconst);
                insertint(sourceArray, psource, temp-1);
                psource = temp;//something
            }
        }//end of else if, end of number
        else if (sourceArray[psource] == ',')    // ,
        {
            insertp(comma);
            psource ++;
        }//end of else if, end of ','
        else if (sourceArray[psource] == '\n')   //\n
        {
            insertp(returnn);
            psource ++;
        }
        else if (sourceArray[psource] == ' '
            ||sourceArray[psource] == '\t')
        {
            psource ++;
        }
        else if (sourceArray[psource] == '(')
        {
            insertp(lparen);
            psource ++;
        }
        else if (sourceArray[psource] == ')') 
        {
            insertp(rparen);
            psource ++;
        }
        else if (sourceArray[psource] == '[')
        {
            insertp(lbracket);
            psource ++;
        }
        else if (sourceArray[psource] == ']')
        {
            insertp(rbracket);
            psource ++;
        }
        else                                    //any other case
        {
            insertp(error);
            inserterror(psource);
            psource ++;
        }

    }//end of while, end of sourceArray
}



/*====================================
output function printout lexicalized information to the output device,
the information are index stored in outArray.
index are changed meanings by string temp.
Once index of error meets, original character will be print out
using sourceArray.
====================================*/
void output(ostream &output, string temp[], char sourceArray[])
{
    int i;
    int errorcount=0;         //keep tracking how many errors have printout.

    output<<endl;
    for (i=0; i<outarray_size; i++)
        if (outArray[i]!=error)
            output<<temp[outArray[i]]<<endl;
        else                 //output is unrecongnized symbol
        {
            output<<"**unrecognized symbol: \""
                <<sourceArray[errorArray[errorcount]]<<"\""<<endl;
            errorcount++;
        }
    output<<endl;
}



/*====================================
output function printout one token to the output device,
the information are index stored in outArray. index are changed 
meanings by string temp. Once index of error meets, original 
character will be print out using sourceArray.
====================================*/
void outputone(ostream &output, string temp[], char sourceArray[], 
               int i, int &errorcount)
{
   
     //keep tracking how many errors have printout.

        if (outArray[i]!=error)
            output<<temp[outArray[i]]<<endl;
        else                 //output is unrecongnized symbol
        {
            output<<"**unrecongnized symbol: \""
                <<sourceArray[errorArray[errorcount]]<<"\""<<endl;
            errorcount++;
        }
        
}