Iklan

Jumat, 16 Desember 2011

Teknik Kompilasi : Membuat Scanner/Lexical Analisis

Lexical analisis atau sering disebut dengan scanner, bertugas sebelum proses syntax analyzer, dan intermediate code dilakukan, dimana tugas dari lexikal analisis adalah mendekomposisi/mengubah program sumber (dalam hal ini berarti "source code ") menjadi bagian - bagian kecil.
adapaun tugas - tugasnya secara mendetail adalah :
- mengidentifikasi semua besaran yang membangun suatu bahasa
- mentranformasikan ke token-token
- menentukan jenis dari token-token
- menangani kesalahan
- menangani tabel simbol

Berikut ini adalah source code dari scanner : 
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<conio.h>

typedef struct t1tag
{
    char nm[20];
} t1;

t1 Type[] = {"keyword","operator","identifier","number","string","illegal character",
                 "assignment","paranthesis","end of series","relational operator","array start",
             "array end","separator","separator","Unary Operator","Compiler directive",
             "converter","range"};
t1 Rsrv[] = {"BEGIN","END","IF","THEN","ELSE","DO","FOR","TO","WHILE","REPEAT","UNTIL",
                 "PROCEDURE","FUNCTION","INTEGER","REAL","BYTE","CHAR","TEXT","ARRAY","VAR",
             "RECORD","SET","TYPE","AND","CASE","CONST","DIC","DOWNTO","EXTERNAL","FILE",
             "FORWARD","GOTO","IN","INLINE","INTERUPT","LABEL","MOD","NIL","NOT","OF",
             "OR","PACKED","PROGRAM","WITH","BOOLEAN","INPUT","OUTPUT","WHILE","WRITELN"};
long s = 49;
char oprtr[] = {'.',',',';','=',':','[',']','(',')','-','+','/','*','>','<','$','#','@'};

long t = 18;
typedef struct ttyptg
{
    long no;
   char ctkn[50];
   int typ;
   long lline;
} ttyp;

int isReserved(char tmp[])
{
    long i=0;
   char *str;

   str = strupr( strdup( tmp ));
   while ( (i < s) && ( strcmp(str, Rsrv[i].nm) != 0))
   {
     i++;
   }
   if ( strcmp(str, Rsrv[i].nm) != 0)
   return 1;

return 0;
}

void main(void)
{
    long i,j,ind,zz,N,NN,ln;
   char ch,nxt,instr[1024], tmp[100], FName[256];
   ttyp Token[1024];
   FILE *f;

   printf("Inputkan Nama File : \n");
   scanf("%s", FName);

   f = fopen(FName,"r");

   ln = 0;
   NN = 0;
   while (!feof(f))
   {
       N=0;
       ch = 0;
       while (('\n' != (ch = getc(f))) && (!feof(f)))
       {
          instr[N] = ch;
          N++;
       }
           instr[N] = 0;
           N++;

       //Token contruction here
       ind = 0;
       while (ind < (N - 1))
       {
           ch = instr[ind];
          while (ch ==32)
          {
          ind++;
         ch = instr[ind];
          }
          if (ind <N)
          {
              j=0;
             while ((j<t)&&(ch !=oprtr[j]))
             j++;
          }
         if (ch==oprtr[j])
         {
             zz=0;
            tmp[0] = ch;
            if (ind <N)
            nxt = instr[ind + 1];
            if ((nxt == '>') || (nxt =='='))
            {
                zz++;
               tmp[1] = nxt;
               ind += 2;
               Token[NN].typ = 16;
            }
            else
            ind++;
            switch (ch)
            {
                case ':': if (nxt == '=')
                               Token[NN].typ = 6;
                           else
                           Token[NN].typ = 1;
                            break;
               case '+' :
               case '-' :
               case '*' :
               case '/' : Token[NN].typ = 1; break;
               case '.' : if (nxt != '.') Token[NN].typ = 1; break;
               case ';' : Token[NN].typ = 8; break;
               case ',' : Token[NN].typ = 12; break;
               case '=' :
               case '>' :
               case '<' : Token[NN].typ = 9; break;
               case '[' : Token[NN].typ = 10; break;
               case ']' : Token[NN].typ = 11; break;
               case '(' :
               case ')' : Token[NN].typ = 7; break;
               case '$' : Token[NN].typ = 14; break;
               case '#' : Token[NN].typ = 15; break;
               case '@' : Token[NN].typ = 13; break;
            }
            zz++;
            tmp[zz] = 0;
            Token[NN].no = NN;
            strcpy(Token[NN].ctkn, tmp);
            Token[NN].lline = ln;
            NN++;
         }
         else if (ch == 39)
         {
             zz =0;
            ind++;
            nxt = instr[ind];
            tmp[0] = nxt;
            while (nxt != 39)
            {
                zz++;
               ind++;
               tmp[zz] = nxt = instr[ind];
            }
            tmp[zz] = 0;
            Token[NN].no = NN;
            strcpy(Token[NN].ctkn, tmp);
            Token[NN].typ = 4;
            Token[NN].lline = ln;
            NN++;
            ind++;
         }
         else if (((ch>=65) && (ch <= 90)) || ((ch >=97) && (ch<=122)) || (ch == 95))
         {
             zz= 0;
            tmp[0] = ch;
            nxt = instr[ind + 1];
            zz++;
            while (((nxt >= 65) && (nxt <= 90)) || ((nxt >=97) && (nxt <=122)) || (nxt == 95) || ((nxt >= 48 )&& (nxt <=57)))
            {
                ind++;
               ch = instr[ind];
               tmp[zz] = ch;
               zz++;
               nxt = instr[ind + 1];
            }
            tmp[zz] = 0;
            if (isReserved(tmp) == 0)
                    Token[NN].typ = 0;
                  else
                  Token[NN].typ = 2;
            Token[NN].no = NN;
            strcpy(Token[NN].ctkn, tmp);
            Token[NN].lline = ln;
            NN++;
            ind++;
         }
         else if ((nxt >= 48) &&(ch<=57))
         {
             zz = 0;
            tmp[0] = ch;
            nxt = instr[ind + 1];
            zz++;
                while (((nxt >= 48) &&(nxt <= 57)))
            {
                ind++;
               ch = instr[ind];
               tmp[zz]= ch;
               zz++;
               nxt = instr[ind +1];
            }
            tmp[zz] = 0;
            Token[NN].typ = 3;
            Token[NN].no = NN;
            strcpy(Token[NN].ctkn, tmp);
            Token[NN].lline = ln;
            NN++;
            ind++;
         }
         else
         {
             Token[NN].no = NN;
            Token[NN].ctkn[0] = ch;
            Token[NN].ctkn[1] = 1;
            Token[NN].typ = 5;
            Token[NN].lline = ln;
            NN++;
            ind++;
         } //if
      } //if
   }//while
   ln++;
//}

fclose(f);

f = fopen("Token.txt", "w");
fprintf(f,"\n No line token TIP\n");
fprintf(f,"________________________________________________________________________________\n");
for (i=0;i<NN;i++)
{
    fprintf(f, " %4ld   % 4ld    %30s      %20s\n",
   Token[i].no, Token[i].lline, Token[i].ctkn, Type[Token[i].typ].nm);
}
fclose(f);

}


Kode diatas sudah di test oleh saya sendiri dan bisa berjalan dengan baik.

Semoga bermanfaat


sumber : google.co.id

4 komentar:

Anonim mengatakan...

kalo dalam bhs c seperti apa ya? perulangannya saya bingung

Unknown mengatakan...
Komentar ini telah dihapus oleh pengarang.
M.Ikbal.S mengatakan...

int main(void)

Obby mengatakan...

mas maaf newbie,saya bngung slalu ada eror proj0000.cpp itu

Posting Komentar

Silahkan isi komentar anda disini!!!