Разработка компилятора

Автор работы: Пользователь скрыл имя, 12 Декабря 2012 в 01:25, курсовая работа

Краткое описание

Наименование программного продукта – «Компилятор».
Область применения программного продукта – разработка программного обеспечения, трансляция с языка высокого уровня на язык ассемблера.
Разработка предназначена для компиляции программы из исходного кода в код на языке ассемблера.

Содержание работы

1 ТЕХНИЧЕСКОЕ ЗАДАНИЕ. 4
2 ОПИСАНИЕ ПРОГРАММЫ. 7
3 ТЕКСТ ПРОГРАММЫ. 17
4 КОНТРОЛЬНЫЙ ПРИМЕР ИСПОЛЬЗОВАНИЯ 47
ЗАКЛЮЧЕНИЕ 64

Содержимое работы - 1 файл

Курсовая СПО.docx

— 223.50 Кб (Скачать файл)

      26:writeln('Ожидалось  loop','  строка-',cur_stroka);

      27:writeln('Массив не  объявлен','  строка-',cur_stroka);

      28:writeln('Операнды имеют  неприводимый тип','  строка-',cur_stroka);

      29:writeln('Нельзя присвоить  переменнай массив','  строка-',cur_stroka);

      30:writeln('Ожидалось  while или until','  строка-',cur_stroka);

    end;

    close(f1);

    close(f2);

    close(f3);

    close(f4);

    halt;

    end;

    procedure setident;

  var j,cur:integer;

   begin

   cur:=nom_analiz;

   For i:=1 to cur do

    If analiz[i].table='i' then

      For j:=i to cur do

      If (analiz[i].table=analiz[j].table) and (analiz[i].nomer=analiz[j].nomer) then

        begin

        analiz[j].type_i:=analiz[i].type_i;

        If analiz[i].mas=1 then analiz[j].mas:=1;

        end;

   end;

   

   procedure pr;

   var st:string;

     begin

   current_analiz:=1;

   If (analiz[current_analiz].table<>'k') or (analiz[current_analiz].nomer<>1) then error(1);

   current_analiz:=current_analiz+1;

   If not (analiz[current_analiz].table='i')  then error(2);

   current_analiz:=current_analiz+1;

   if not ((analiz[current_analiz].table='s') and (analiz[current_analiz].nomer=1)) then error(3);

   current_analiz:=current_analiz+1;

   cur_stroka:=1;

   writeln(f6,'.model tiny');

   writeln(f6,'.Code');

   writeln(f6,'org 100h');

   writeln(f6,'');

   writeln(f6,'Start:');

   Declar;

   setident;

   Block;

   If not ((analiz[current_analiz].table='k') and (analiz[current_analiz].nomer=3)) then error(4);

   current_analiz:=current_analiz+1;

   If not ((analiz[current_analiz].table='s') and (tab_razdel[analiz[current_analiz].nomer]='.')) then error(5);

   writeln(f6,'');

   writeln(f6,'EndKode:');

   writeln(f6,'mov ax, 4Ch');

   writeln(f6,'int 21h');

   writeln(f6,'');

   writeln(f6,'.Data');

   reset(f7);

   while eof(f7)=false do

   begin

   readln(f7,st);

   writeln(f6,st);

   end;

  writeln(f6,'BUF':5,'DW':5, '5':3, 'DUP':5, '(?)');

  writeln(f6,'bufs1':5, 'DW':5, ' ?');

  writeln(f6,'bufs2':5, 'DW':5, ' ?');

  writeln(f6,'bufs3':5, 'DW':5, ' ?');

  writeln(f6,'BUFS':5, 'DB':5, '50':3, 'DUP':5, '(?)');

  writeln(f6,'$logic_true':5, 'db':5,' ''true $''');

  writeln(f6,'$logic_false':5, 'db':5,' ''false $''');

  writeln(f6,'');

   AddProcedures;

   writeln(f6,'END Start');

   end;

 

   procedure declar;

   var mas_i:array[0..10] of integer;

       cur_i,j:integer;

       flag:boolean;

   begin

    if not ((analiz[current_analiz].table='k') and (analiz[current_analiz].nomer=6)) then error(6);

    While not ((analiz[current_analiz+1].table='k') and (analiz[current_analiz+1].nomer=2)) do

        Begin

        cur_stroka:=cur_stroka+1;

        flag:=false;

        cur_i:=0;

        current_analiz:=current_analiz+1;

        if not (analiz[current_analiz].table='i') then error(2);

        if ((analiz[current_analiz+1].table='s') and (tab_razdel[analiz[current_analiz+1].nomer]=':')) then begin mas_i[cur_i]:=current_analiz;cur_i:=cur_i+1; end;

          While not ((analiz[current_analiz+1].table='s') and (tab_razdel[analiz[current_analiz+1].nomer]=':'))  do

            Begin

            flag:=true;

            mas_i[cur_i]:=current_analiz;cur_i:=cur_i+1;

                current_analiz:=current_analiz+1;

            If not ((analiz[current_analiz].table='s') and (tab_razdel[analiz[current_analiz].nomer]=',')) then error(7);

            current_analiz:=current_analiz+1;

            If not (analiz[current_analiz].table='i') then error(2);

                If ((analiz[current_analiz+1].table='s') and (tab_razdel[analiz[current_analiz+1].nomer]=':')) then begin mas_i[cur_i]:=current_analiz;cur_i:=cur_i+1; end;   

            End;

          if flag=false then cur_i:=current_analiz;

          current_analiz:=current_analiz+1;

          if not ((analiz[current_analiz].table='s') and (tab_razdel[analiz[current_analiz].nomer]=':')) then error(8);

          current_analiz:=current_analiz+1;

          if not (analiz[current_analiz].table='k') then error(9);

 

       Case analiz[current_analiz].nomer of

          16:begin

              If flag=true then

           begin

             for i:=0 to cur_i-1 do

             begin

             analiz[mas_i[i]].type_i:=tab_i[analiz[current_analiz].nomer];

             writeln(f7,tab_ident[analiz[mas_i[i]].nomer]:5,'DB':5,'(?)');

             end;

           end

           else  begin analiz[cur_i].type_i:=tab_i[analiz[current_analiz].nomer];Writeln(f7,tab_ident[analiz[cur_i].nomer]:5,'DB':5,'(?)') end;

             end;

          17:begin

              If flag=true then

           begin

             for i:=0 to cur_i-1 do

             begin

             analiz[mas_i[i]].type_i:=tab_i[analiz[current_analiz].nomer];

             writeln(f7,tab_ident[analiz[mas_i[i]].nomer]:5,'DW':5,'(?)');

             end;

           end

           else  begin analiz[cur_i].type_i:=tab_i[analiz[current_analiz].nomer];Writeln(f7,tab_ident[analiz[cur_i].nomer]:5,'DW':5,'(?)') end;

             end;

          18:begin

              If flag=true then

           begin

             for i:=0 to cur_i-1 do

             begin

             analiz[mas_i[i]].type_i:=tab_i[analiz[current_analiz].nomer];

             writeln(f7,tab_ident[analiz[mas_i[i]].nomer]:5,'DB':5,'(?)');

             end;

           end

           else  begin analiz[cur_i].type_i:=tab_i[analiz[current_analiz].nomer];Writeln(f7,tab_ident[analiz[cur_i].nomer]:5,'DB':5,'(?)') end;

             end;

          19:begin

              If flag=true then

           begin

             for i:=0 to cur_i-1 do

             begin

             analiz[mas_i[i]].type_i:=tab_i[analiz[current_analiz].nomer];

             writeln(f7,tab_ident[analiz[mas_i[i]].nomer]:5,'DB':5,'(?)');

             end;

           end

           else  begin analiz[cur_i].type_i:=tab_i[analiz[current_analiz].nomer];Writeln(f7,tab_ident[analiz[cur_i].nomer]:5,'DB':5,'(?)') end;

             end;

          20: begin

              mass;

              If flag=true then

           begin

             for i:=0 to cur_i-1 do

             begin

             analiz[mas_i[i]].type_i:=tab_i[analiz[current_analiz].nomer];

             analiz[mas_i[i]].mas:=1;

             If analiz[current_analiz].nomer=17 then writeln(f7,tab_ident[analiz[mas_i[i]].nomer]:5,'DW':5,ch2_mas-ch1_mas:3,'DUP':5,'(?)') else

             writeln(f7,tab_ident[analiz[mas_i[i]].nomer]:5,'DB':5,ch2_mas-ch1_mas:3,'DUP':5,'(?)');

             end;

           end

           else begin analiz[cur_i].type_i:=tab_i[analiz[current_analiz].nomer];analiz[cur_i].mas:=1;Writeln(f7,tab_ident[analiz[cur_i].nomer]:5,'DW':5,ch2_mas-ch1_mas:3,'DUP':5,'(?)') end;

             end;

    Else error(10);

    end;

    current_analiz:=current_analiz+1;

    if not ((analiz[current_analiz].table='s') and (analiz[current_analiz].nomer=1)) then error(3);

   end;

   cur_stroka:=cur_stroka+1;

   end;

 

  

  procedure block;

     //var  FR,TR,ER:real;

     Procedure FL;forward;

     procedure TL;forward;

     Procedure T;forward;

    Procedure F;forward;

     procedure E;forward;

     procedure operator;forward;

     procedure op_if;forward;

     procedure op_pr;forward;

     Procedure op_while; forward;

     Procedure op_read;forward;

     Procedure op_write;forward;

    

     procedure gen_metok;

     begin

     inc(cur_met);

     g_met:='MET'+inttostr(cur_met);

     end;

 

     procedure L;

     begin

     TL;

     While (analiz[current_analiz+1].table='k') and (analiz[current_analiz+1].nomer=27) do

       begin

       current_analiz:=current_analiz+1;

       if (analiz[current_analiz].table='k') and (analiz[current_analiz].nomer=27) then

       begin

       TL;

       writeln(f6,'POP AX');

      writeln(f6,'POP BX');

      writeln(f6,'OR AX,BX');

      writeln(f6,'PUSH AX');

       end else Error(11);

       end;

     end;

 

     Procedure TL;

     begin

     FL;

     While (analiz[current_analiz+1].table='k') and (analiz[current_analiz+1].nomer=25) do

       begin

       current_analiz:=current_analiz+1;

       if (analiz[current_analiz].table='k') and (analiz[current_analiz].nomer=25) then

       begin

       FL;

       writeln(f6,'POP AX');

      writeln(f6,'POP BX');

      writeln(f6,'AND AX,BX');

      writeln(f6,'PUSH AX');

       end else Error(11);

       end;

     end;

 

    Procedure FL;

    var zn_ot:string;

        met:string;

        a:integer;

    begin

      current_analiz:=current_analiz+1;

      If analiz[current_analiz].table='i' then

        begin

        a:=analiz[current_analiz].nomer;

        current_analiz:=current_analiz+1;

        if (analiz[current_analiz].table='s') and( tab_razdel[analiz[current_analiz].nomer]='[')then

             begin

             current_analiz:=current_analiz+1;

             E;

             If not(tab_razdel[analiz[current_analiz].nomer]=']') then error(12)

             else begin

                  writeln(f6,'POP BX');

                  writeln(f6,'MOV AL, ',tab_ident[a],'[BX]');

                  writeln(f6,'PUSH AX');

                  end;

             end else begin

                      writeln(f6,'MOV AL, ',tab_ident[a]);

                      writeln(f6,'PUSH AX');

                      end;

        end else

 

        If tab_razdel[analiz[current_analiz].nomer]='(' then

               begin

               //current_analiz:=current_analiz+1;

               l;

               current_analiz:=current_analiz+1;

               If not(tab_razdel[analiz[current_analiz].nomer]=')') then error(13);

               end else

        If tab_i[analiz[current_analiz].nomer]='true' then

        begin

        writeln(f6,'MOV AX,1');

        writeln(f6,'PUSH AX');

        exit;

        end

        else

        If tab_i[analiz[current_analiz].nomer]='false' then

        begin

        writeln(f6,'MOV AX,0');

        writeln(f6,'PUSH AX');

        exit;

        end

        else

        If tab_i[analiz[current_analiz].nomer]='not' then

         begin

         current_analiz:=current_analiz+1;

          FL; //change

          writeln(f6,'POP AX');

          writeln(f6,'NOT AX');

          writeln(f6,'PUSH AX');

          current_analiz:=current_analiz+1

         end else

        If tab_razdel[analiz[current_analiz].nomer]='!' then

          begin

          current_analiz:=current_analiz+1;

           E;

          If ((tab_razdel[analiz[current_analiz].nomer]<>'=') and (tab_razdel[analiz[current_analiz].nomer]<>'<') and (tab_razdel[analiz[current_analiz].nomer]<>'>') and (tab_hardrazdel[analiz[current_analiz].nomer]<>'<>') and (tab_hardrazdel[analiz[current_analiz].nomer]<>'<=') and (tab_hardrazdel[analiz[current_analiz].nomer]<>'>='))   then error(11)

          else

             begin

              If analiz[current_analiz].table='s' then zn_ot:=tab_razdel[analiz[current_analiz].nomer] else zn_ot:=tab_hardrazdel[analiz[current_analiz].nomer];

              current_analiz:=current_analiz+1;

               E;

                writeln(f6,'POP BX');  // b

                writeln(f6,'POP AX');  // a

                writeln(f6,'CMP AX,BX');

                gen_metok;

               If zn_ot='>=' then writeln(f6,'JGE ',g_met);

               If zn_ot='<=' then writeln(f6,'JLE ',g_met);

               If zn_ot='<>' then writeln(f6,'JNE ',g_met);

               If zn_ot='>' then writeln(f6,'JG ',g_met);

               If zn_ot='<' then writeln(f6,'JL ',g_met);

               if zn_ot='=' then writeln(f6,'JE ',g_met);

               writeln(f6,'PUSH 0');  // FALSE

               met:=g_met;

               gen_metok;

               writeln(f6,'JMP ',g_met);

               writeln(f6,met,':');

               writeln(f6,'PUSH 1');   // TRUE

               writeln(f6,g_met,':');

              if not(tab_razdel[analiz[current_analiz].nomer]='!') then error(15)

              else exit;

             end;

          end;

    end;

   

    procedure pr_mas;

    begin

    If analiz[current_analiz].mas=0 then error(27);

    current_analiz:=current_analiz+1;

    if not ((analiz[current_analiz].table='s') and (tab_razdel[analiz[current_analiz].nomer]='[')) then error(22);

    current_analiz:=current_analiz+1;

    E;

    if not ((analiz[current_analiz].table='s') and (tab_razdel[analiz[current_analiz].nomer]=']')) then error(12);

    end;

   

    procedure E;

    begin

    T;

    While (tab_razdel[analiz[current_analiz].nomer]<>')') and (tab_razdel[analiz[current_analiz].nomer]<>';') and (tab_razdel[analiz[current_analiz].nomer]<>',') and (tab_razdel[analiz[current_analiz].nomer]<>'=') and (tab_razdel[analiz[current_analiz].nomer]<>'!') and (tab_razdel[analiz[current_analiz].nomer]<>']')and(analiz[current_analiz].table<>'h')and (tab_razdel[analiz[current_analiz].nomer]<>'<') and (tab_razdel[analiz[current_analiz].nomer]<>'>') do

 

          If tab_razdel[analiz[current_analiz].nomer]='+' then begin current_analiz:=current_analiz+1; T;writeln(f6,'pop bx');writeln(f6,'pop ax');writeln(f6,'add ax,bx');writeln(f6,'push ax'); end

               else If  tab_razdel[analiz[current_analiz].nomer]='-' then begin current_analiz:=current_analiz+1;T;writeln(f6,'pop bx');writeln(f6,'pop ax');writeln(f6,'sub ax,bx');writeln(f6,'push ax');end

                                 else begin error(16);end;

 

    end;

 

    Procedure T;

    begin

    F;

    While (tab_razdel[analiz[current_analiz].nomer]<>'+') and (tab_razdel[analiz[current_analiz].nomer]<>'-') and (tab_razdel[analiz[current_analiz].nomer]<>')')and (tab_razdel[analiz[current_analiz].nomer]<>';') and (tab_razdel[analiz[current_analiz].nomer]<>',') and (tab_razdel[analiz[current_analiz].nomer]<>'=')and (tab_razdel[analiz[current_analiz].nomer]<>'!') and (tab_razdel[analiz[current_analiz].nomer]<>']') and(analiz[current_analiz].table<>'h') and (tab_razdel[analiz[current_analiz].nomer]<>'<') and (tab_razdel[analiz[current_analiz].nomer]<>'>') do

 

    If tab_razdel[analiz[current_analiz].nomer]='*' then begin current_analiz:=current_analiz+1; F;writeln(f6,'pop bx');writeln(f6,'pop ax');writeln(f6,'mul bx');writeln(f6,'push ax'); end

             else if  tab_razdel[analiz[current_analiz].nomer]='/' then begin current_analiz:=current_analiz+1;F;writeln(f6,'pop bx');writeln(f6,'pop ax');writeln(f6,'xor dx, dx');writeln(f6,'div bx');writeln(f6,'push ax');end

                              else begin error(16); end;

    end;

 

    Procedure F;

    var a:integer;

    begin

    If (analiz[current_analiz].table='i') or (analiz[current_analiz].table='c') then

    begin If (analiz[current_analiz].table='i') then

               begin

               cur_vir:=cur_vir+1;

               type_virag[cur_vir]:=analiz[current_analiz].type_i;

               if (analiz[current_analiz].mas=1) and (tab_razdel[analiz[current_analiz+1].nomer]<>'[')then error(29);

               If (analiz[current_analiz+1].table='s') and(tab_razdel[analiz[current_analiz+1].nomer]='[') then begin a:=analiz[current_analiz].nomer; pr_mas; writeln(f6,'POP BX'); writeln(f6,'MOV AX,2'); writeln(f6,'MUL BX'); writeln(f6,'MOV SI,AX');writeln(f6,'MOV AX, ',tab_ident[a],'[SI]');writeln(f6,'PUSH AX'); end

                else begin

                if analiz[current_analiz].type_i='integer' then begin Writeln(f6,'mov ax,',tab_ident[analiz[current_analiz].nomer]);writeln(f6,'push ax'); end

                else begin Writeln(f6,'mov al,',tab_ident[analiz[current_analiz].nomer]);writeln(f6,'push ax'); end;

                end;

                end

Информация о работе Разработка компилятора