词法分析

Posted SJcnh_Yan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了词法分析相关的知识,希望对你有一定的参考价值。

  1 #include <iostream>
  2 #include <fstream>
  3 #define max 500
  4 using namespace std;
  5 
  6 char pr[max];
  7 int ll,rr;
  8 int n = 0;
  9 int m = 0;
 10 char * keyw[5] = { "if","else","main","int","return"};
 11 
 12 ofstream outfile;
 13 
 14 
 15 
 16 typedef struct identifier {
 17     char token[8];
 18     identifier *next;
 19 }idtf;
 20 
 21 typedef struct number {
 22     int data;
 23     number *next;
 24 }num;
 25 
 26 num *number = (num *)malloc(sizeof(num));
 27 idtf *idtfier = (idtf *)malloc(sizeof(idtf));
 28 
 29 num *p = number;
 30 idtf *q = idtfier;
 31 
 32 int ch = 0;
 33 
 34 int infile() {
 35     FILE * pFile;
 36     long lSize;
 37     
 38     size_t result;
 39 
 40     pFile = fopen("program.txt", "rb");   // 打开文件
 41     if (pFile == NULL) { fputs("File error", stderr); exit(1); }
 42 
 43     // 获取文件大小:
 44     fseek(pFile, 0, SEEK_END);
 45     lSize = ftell(pFile);
 46     rewind(pFile);
 47 
 48     result = fread(pr, 1, lSize, pFile);
 49     if (result != lSize) { fputs("Reading error", stderr); exit(3); }
 50 
 51 
 52     for (int j = 0; j<lSize; j++) {
 53         cout << pr[j] ;
 54     }
 55     fclose(pFile);
 56     return 0;
 57 }
 58 
 59 
 60 void scanner() {
 61     
 62     ch = pr[n++];
 63     while (ch ==  )
 64     {
 65         ch = pr[n++];
 66         
 67     }
 68     int i = 0;
 69     if ((ch >= a&&ch <= z) ||( ch >= A&&ch <= Z)) {//识别标识符
 70         idtf *s = (idtf *)malloc(sizeof(idtf));
 71         
 72         while ((ch >= a&&ch <= z) || (ch >= A&&ch <= Z) ||( ch >= 0 && ch <=9)) {
 73             s->token[i++] = ch;
 74             ch = pr[n++];
 75         }
 76         s->token[i++] = \0;
 77         n--;
 78         for (i = 0; i < 5; i++) {
 79             if (strcmp(s->token, keyw[i]) == 0) {
 80                 ll = 14;
 81                 rr = i+1;
 82                 return;
 83             }
 84         }
 85         
 86             
 87                 i = 0;
 88                 ll = 0;
 89                 idtf *cmp = idtfier;
 90                 while (cmp->next != NULL) {
 91                     if (strcmp(s->token, cmp->next->token) == 0) {
 92                         rr = i+1;    
 93                         return;
 94                     }
 95                     i++;
 96                     cmp = cmp->next;
 97                 }
 98                 q->next = s;
 99                 s->next = NULL;
100                 q = s;
101                 rr = i + 1;
102                 
103                 return;
104             
105 
106         }
107         
108     
109     else if (ch >= 0 && ch <= 9) {//判断是否为数字常量
110         num *s = (num *)malloc(sizeof(num));
111         int sum=0;
112         while (ch >= 0 && ch <= 9) {
113             sum = 10 * sum + ch - 48;
114             ch = pr[n++];
115             
116         }    
117         
118         n--;
119         s->data = sum;
120         p->next = s;
121         s->next = NULL;
122         p = s;
123         ll = 1; m++;rr = m;
124     
125         }
126     else switch (ch) {
127     case =: {ll = 2; rr = =; return; }
128     case (: {ll = 3; rr = (; return; }
129     case ): {ll = 4; rr = ); return; }
130     case {: {ll = 5; rr = {; return; }
131     case }: {ll = 6; rr = }; return; }
132     case +: {ll = 7; rr = +; return; }
133     case -: {ll = 8; rr = -; return; }
134     case *: {ll = 9; rr = *; return; }
135     case /: {ll = 10; rr = /; return; }
136     case %: {ll = 11; rr = %; return; }
137     case ,: {ll = 12; rr = ,; return; }
138     case ;: {ll = 13; rr = ;; return; }
139     default: {ll = -1; break; }
140     }
141     
142 }
143     
144 
145 
146 
147 
148 int main() {
149     number->next = NULL;
150     idtfier->next = NULL;
151     
152     infile();
153     outfile.open("out.txt");
154     while (ch != #) {
155         scanner();
156         if (ll == 1 || ll == 0||ll==14) {
157             outfile << "<" << ll << "," << rr << ">" << endl;
158         }
159         else if (ll >= 2 && ll <= 13) {
160             char rc = rr;
161             outfile << "<" << ll << "," << rc << ">" << endl;
162         }
163         }
164     outfile.close();
165     
166     
167         
168         
169     system("PAUSE");
170     return 0;
171         
172 }

 

以上是关于词法分析的主要内容,如果未能解决你的问题,请参考以下文章

词法分析

词法分析器代码复审

实验一 词法分析器+编译原理

Second-词法分析

词法分析器

js词法分析