Разработка компиляторов. Ишакова Е.Н. - 52 стр.

UptoLike

Составители: 

52
la.cpp
#include "la.h"
// перевод строки в нижний регистр
void tolower(string &s){
for (unsigned long i = 0; i < s.length(); i++)
s[i] = tolower(s[i]);
}
int Scanner::Scan(){
par t;
pos = 0;
State = SH;
errcode = 0;
gc();
while(State != SV && State != SER){
while(State != SV && cur != '\n' &&
isspace(cur))
gc();
if (State == SV)
break;
if (letter()){ // буква
State = SI;
S = cur;
for(gc(); State != SV && (letter() ||
digit()); gc())
S += cur;
//tolower(S);
look(&A->R);
if (z >= 0)
out(0, z);
else {
put(&A->I);
out(3, z);
}
} else if (digit()){ //число
State = SN;
S = cur;
for(gc(); State != SV && (digit() ||
strchr("ABCDEFabcdef", cur)); gc())
S += cur;
if (strchr("HhDdOoBb", cur)){
S += cur;
gc();
} else if (cur == '.'){ // дробная часть
State = SND;
S += cur;
for(gc(); State != SV && digit(); gc())
S += cur;
if (cur == 'e' || cur == 'E'){ // порядок
S += cur;
gc();
State = SNS;
if (cur == '+' || cur == '-'){
S += cur;
gc();
}
State = SNP;
for(; State != SV && digit(); gc())
S += cur;
}
} else if ((digit() || cur == '+' || cur == '-') &&
(S[S.length() - 1] == 'e' || S[S.length() - 1] == 'E')){ //
порядок
State = SNP;
for(gc(); State != SV && digit(); gc())
S += cur;
}
put(&A->N);
out(2, z);
} else if (cur == '{'){ // комментарий
State = SC;
for(gc(); State != SV && cur != '}'; gc());
if (State == SV){
errcode = 1;
break;
}
gc();
} else if (cur == '<'){ // < <= <>
State = SL;
gc();
if (cur == '=' || cur == '>'){
S = "<";
S += cur;
gc();
}
else
S = "<";
look(&A->D);
out(1, z);
} else if (cur == '>'){ // > >=
State = SG;
gc();
if (cur == '='){
S = ">=";
gc();
}
else
S = ">";
look(&A->D);
out(1, z);
} else if (cur == ':'){ // : :=
State = SS;
gc();
if (cur == '='){
S = ":=";
gc();
}
else
S = ":";
look(&A->D);
out(1, z);
32
Лист
la.cpp

#include "la.h"                                                      State = SNP;
                                                                     for(; State != SV && digit(); gc())
// перевод строки в нижний регистр                                      S += cur;
void tolower(string &s){                                           }
   for (unsigned long i = 0; i < s.length(); i++)               } else if ((digit() || cur == '+' || cur == '-') &&
     s[i] = tolower(s[i]);                              (S[S.length() - 1] == 'e' || S[S.length() - 1] == 'E')){ //
}                                                       порядок
                                                                       State = SNP;
int Scanner::Scan(){                                               for(gc(); State != SV && digit(); gc())
   par t;                                                             S += cur;
   pos = 0;                                                     }
   State = SH;                                                  put(&A->N);
   errcode = 0;                                                 out(2, z);
   gc();                                                     } else if (cur == '{'){ // комментарий
   while(State != SV && State != SER){                          State = SC;
      while(State != SV && cur != '\n' &&                       for(gc(); State != SV && cur != '}'; gc());
isspace(cur))                                                   if (State == SV){
         gc();                                                     errcode = 1;
      if (State == SV)                                             break;
         break;                                                 }
      if (letter()){ // буква                                   gc();
         State = SI;                                         } else if (cur == '<'){ // < <= <>
         S = cur;                                               State = SL;
         for(gc(); State != SV && (letter() ||                  gc();
digit()); gc())                                                 if (cur == '=' || cur == '>'){
            S += cur;                                              S = "<";
         //tolower(S);                                             S += cur;
         look(&A->R);                                              gc();
         if (z >= 0)                                            }
            out(0, z);                                          else
         else {                                                    S = "<";
            put(&A->I);                                         look(&A->D);
            out(3, z);                                          out(1, z);
         }                                                   } else if (cur == '>'){ // > >=
      } else if (digit()){ //число                              State = SG;
         State = SN;                                            gc();
         S = cur;                                               if (cur == '='){
         for(gc(); State != SV && (digit() ||                      S = ">=";
strchr("ABCDEFabcdef", cur)); gc())                                gc();
            S += cur;                                           }
         if (strchr("HhDdOoBb", cur)){                          else
            S += cur;                                              S = ">";
            gc();                                               look(&A->D);
         } else if (cur == '.'){ // дробная часть               out(1, z);
           State = SND;                                      } else if (cur == ':'){ // : :=
            S += cur;                                           State = SS;
            for(gc(); State != SV && digit(); gc())             gc();
               S += cur;                                        if (cur == '='){
            if (cur == 'e' || cur == 'E'){ // порядок              S = ":=";
                     S += cur;                                     gc();
               gc();                                            }
               State = SNS;                                     else
               if (cur == '+' || cur == '-'){                      S = ":";
                  S += cur;                                     look(&A->D);
                  gc();                                         out(1, z);
               }
                                                                                                              Лист
                                                                                                              32


                                                                                                                 52