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

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

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

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

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

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

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

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

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

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

               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]=')' then current_analiz:=current_analiz+1

                                                                    else begin error(16);exit; end; end

                                  else begin error(16); end;

    end;

   

    procedure CH;

     begin

     current_analiz:=current_analiz+1;

     If analiz[current_analiz].table<>'l' then error(30);

     //If (analiz[current_analiz+1].table<>'r') and (tab_razdel[analiz[current_analiz+1].nomer]<>';') then error(3);

     end;

   

    procedure op_pr;

     var b:string;

         flag:boolean;

         prisv:lek;

         M_i:array[1..10] of lek;

         ii,i1:integer;

 

    begin

    ii:=0;

    b:='';

    Repeat

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

    If analiz[current_analiz].type_i='' then error(17);

    If b='' then b:=analiz[current_analiz].type_i else if b<>analiz[current_analiz].type_i then error(28);

    //prisv:=analiz[current_analiz];

    ii:=ii+1;

    M_i[ii]:=analiz[current_analiz];

    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 (( analiz[current_analiz].table<>'s') and (tab_razdel[ analiz[current_analiz].nomer]<>']')) then error(12);

    current_analiz:=current_analiz+1;

    end;

    If (( analiz[current_analiz].table='s') and (tab_razdel[ analiz[current_analiz].nomer]=',')) then current_analiz:=current_analiz+1; {проверяем символ ','}

    Until  not (( analiz[current_analiz-1].table='s') and (tab_razdel[ analiz[current_analiz-1].nomer]=','));

    //If ii=1 then current_analiz:=current_analiz+1;

    If not (( analiz[current_analiz].table='h') and (analiz[current_analiz].nomer=1)) then error(18);

    cur_vir:=0;

    If b='integer' then begin

                        current_analiz:=current_analiz+1;

                        E;

                        writeln(f6,'POP CX');

                        For i:=1 to cur_vir do

                         begin

                         If b<>type_virag[i] then error(28);

                         end;

                        for i1:=ii downto 1 do

                         begin

                           If M_i[i1].mas=1 then begin

                                                          writeln(f6,'POP BX');

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

                                                          writeln(f6,'MUL BX');

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

                                                          writeln(f6,'MOV ',tab_ident[m_i[i1].nomer],'[SI]',',CX');

                                                 end

                           else begin

                                writeln(f6,'mov ',tab_ident[m_i[i1].nomer],', CX');

                                end;

                         end;

                        end;

    if b='boolean' then begin

                        L;

                        writeln(f6,'POP AX');

                        current_analiz:=current_analiz+1;

                        For i:=1 to cur_vir do

                         begin

                         If b<>type_virag[i] then error(28);

                         end;

                         for i1:=ii downto 1 do

                         begin

                           If M_i[i1].mas=1 then begin

                                                          writeln(f6,'POP BX');

                                                          writeln(f6,'MOV ',tab_ident[m_i[i1].nomer],'[BX], AL');

                                                 end

                           else begin

                                writeln(f6,'mov ',tab_ident[m_i[i1].nomer],', al');

                                end;

                         end;

                        end;

    If b='byte' then begin

                        current_analiz:=current_analiz+1;

                        E;

                        writeln(f6,'POP AX');

                        For i:=1 to cur_vir do

                         begin

                         If b<>type_virag[i] then error(28);

                         end;

                          for i1:=ii downto 1 do

                         begin

                           If M_i[i1].mas=1 then begin

                                                          writeln(f6,'POP BX');

                                                          writeln(f6,'MOV ',tab_ident[m_i[i1].nomer],'[BX], AL');

                                                 end

                           else begin

                                writeln(f6,'mov ',tab_ident[m_i[i1].nomer],', al');

                                end;

                         end;

                        end;

    IF b='char' then begin

                     CH;

                     current_analiz:=current_analiz+1;

                      For i:=1 to cur_vir do

                         begin

                         If b<>type_virag[i] then error(28);

                         end;

                          for i1:=ii downto 1 do

                         begin

                           If M_i[i1].mas=1 then begin

                                                          writeln(f6,'POP BX');

                                                          writeln(f6,'MOV ',tab_ident[m_i[i1].nomer],',''',tab_liter[analiz[current_analiz-1].nomer],'''');

                                                 end

                           else begin

                                writeln(f6,'MOV ',tab_ident[m_i[i1].nomer],',''',tab_liter[analiz[current_analiz-1].nomer],'''');

                                end;

                         end;

                     end;

 

    For i:=1 to 10 do

     type_virag[i]:='';

    end;

 

   

    Procedure op_if;

    var met:string;

    begin

     L;

     writeln(f6,'POP AX');

     Writeln(f6,'CMP AX,0');

     gen_metok;

     writeln(f6,'JZ ',g_met);

     Current_analiz:=current_analiz+1;

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

     Operator;

     met:=g_met;

     gen_metok;

     writeln(f6,'JMP ',g_met);

     writeln(f6,met,':');

     If ((analiz[current_analiz+2].table='k') and (analiz[current_analiz+2].nomer=14)) then

       begin

       Current_analiz:=current_analiz+2;

       Operator;

       end;

     writeln(f6,g_met,':');

     End;

    

    Procedure op_while;

    var met:string;

    begin

      gen_metok;

      met:=g_met;

      Writeln(f6,g_met,':');

      L;

      writeln(f6,'POP AX');

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

      gen_metok;

      writeln(f6,'JNZ ',g_met);

      current_analiz:=current_analiz+1;

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

      operator;

      writeln(f6,'POP AX');

      writeln(f6,'JMP ',met);

      writeln(f6,g_met,':');

    end;

   

    Procedure op_read;

    var a:lek;

     begin

     current_analiz:=current_analiz+1;

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

      repeat

      current_analiz:=current_analiz+1;

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

      a:=analiz[current_analiz];

      current_analiz:=current_analiz+1;

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

         begin

         current_analiz:=current_analiz+2;

         E;

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

         writeln(f6,'POP BX');

         If a.type_i='integer' then

         begin

            writeln(f6,'CALL R_Ch');

            writeln(f6,'CALL R_Int');

            writeln(f6,'MOV ',tab_ident[a.nomer],'[BX], AX');

            writeln(f6,'CALL outline');

         end;

           If a.type_i='byte' then

         begin

            writeln(f6,'CALL R_Ch');

            writeln(f6,'CALL R_Int');

            writeln(f6,'MOV ',tab_ident[a.nomer],', Al');

            writeln(f6,'CALL outline');

         end;

         If a.type_i='boolean' then

         begin

            writeln(f6,'CALL R_B');

            writeln(f6,'POP AX');

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

            writeln(f6,'CALL outline');

         end;

         If a.type_i='char' then

         begin

            writeln(f6,'MOV BX,offset ',tab_ident[a.nomer]);

            writeln(f6,'CALL R_Ch');

            writeln(f6,'CALL outline');

         end;

         end;

        

           If a.type_i='integer' then

         begin

            writeln(f6,'CALL R_Ch');

            writeln(f6,'CALL R_Int');

            writeln(f6,'MOV ',tab_ident[a.nomer],', AX');

            writeln(f6,'CALL outline');

         end;

         If a.type_i='byte' then

         begin

            writeln(f6,'CALL R_Ch');

            writeln(f6,'CALL R_Int');

            writeln(f6,'MOV ',tab_ident[a.nomer],', Al');

            writeln(f6,'CALL outline');

         end;

         If a.type_i='boolean' then

         begin

            writeln(f6,'CALL R_B');

            writeln(f6,'POP AX');

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

            writeln(f6,'CALL outline');

         end;

         If a.type_i='char' then

         begin

            writeln(f6,'MOV BX,offset ',tab_ident[a.nomer]);

            writeln(f6,'CALL R_Ch');

            writeln(f6,'CALL outline');

         end;

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

      Until not ((analiz[current_analiz-1].table='s') and (tab_razdel[analiz[current_analiz-1].nomer]=','));

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

     end;

    

    Procedure op_write;

    var b:lek;

        st1:string;

        j:integer;

     begin

        current_analiz:=current_analiz+1;

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

         repeat

          current_analiz:=current_analiz+1;

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

       begin

        st1:=tab_liter[analiz[current_analiz].nomer]+'$';

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

        j:=1;

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

        while st1[j]<>'$' do

        begin

            writeln(f6,'MOV DX,','''',st1[j],'''');

            writeln(f6,'MOV BUFS[SI],DL');

            writeln(f6,'INC SI');

          j:=j+1;

        end;

        writeln(f6,'MOV DX,''$''');

        writeln(f6,'MOV BUFS[SI],DL');

        writeln(f6,'INC SI');

        writeln(f6,'MOV DX,offset BUFS');

        writeln(f6,'MOV AH,09h');

        writeln(f6,'INT 21h');

        current_analiz:=current_analiz+1;

      end;

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

      begin

        b:=analiz[current_analiz];

        E;

        if b.mas=1 then

        begin

          if b.type_i='integer' then

            writeln(f6,'CALL PR_Int');

          if b.type_i='byte' then

            writeln(f6,'CALL PR_Int');

           if b.type_i='char' then

           begin

            writeln(f6,'push dx');

            writeln(f6,'mov dx,offset ',tab_ident[b.nomer]);

            writeln(f6,'mov ah,9h');

            writeln(f6,'int 21h');

            writeln(f6,'pop dx');

          end;

          if b.type_i='boolean' then

            writeln(f6,'CALL output_logic');

        end

        else begin

        if b.type_i='boolean' then

          writeln(f6,'CALL output_logic');

        if b.type_i='char' then

        begin

          writeln(f6,'push dx');

          writeln(f6,'mov dx,offset ',tab_ident[b.nomer]);

          writeln(f6,'mov ah,9h');

          writeln(f6,'int 21h');

          writeln(f6,'pop dx');

        end;

        if b.type_i='integer' then

          writeln(f6,'CALL PR_Int');

        end;

        if b.type_i='byte' then

            writeln(f6,'CALL PR_Int');

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

        writeln(f6,'CALL outline')

        else begin

              writeln(f6,'mov dx,',32);

              writeln(f6,'mov ah,2h');

              writeln(f6,'int 21h');

             end;

 

         //if ((analiz[current_analiz+1].table='s') and (tab_razdel[analiz[current_analiz+1].nomer]=',')) then current_analiz:=current_analiz+1;

      end;

      Until not ((analiz[current_analiz].table='s') and (tab_razdel[analiz[current_analiz].nomer]=','));

       //current_analiz:=current_analiz+1;

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

     end;

 

    Procedure op_do;

    var a:lek;met:string;

    begin

     gen_metok;

     met:=g_met;

     writeln(f6,g_met,':');

     repeat

     operator;

     current_analiz:=current_analiz+1;

     until analiz[current_analiz+1].table='k';

     current_analiz:=current_analiz+1;

     If (analiz[current_analiz].table<>'k') and  (analiz[current_analiz].nomer<>29) then error(26);

     current_analiz:=current_analiz+1;

     If (analiz[current_analiz].table<>'k') and ((analiz[current_analiz].nomer<>11) or (analiz[current_analiz].nomer<>12)) then error(30);

     a:=analiz[current_analiz];

     L;

     If tab_i[a.nomer]='while' then

      begin

       writeln(f6,'POP AX');

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

       gen_metok;

       writeln(f6,'JNZ ',g_met);

      end;

     If tab_i[a.nomer]='until' then

      begin

        writeln(f6,'POP AX');

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

       gen_metok;

       writeln(f6,'JNZ ',g_met);

      end;

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

      writeln(f6,'JMP ',met);

      writeln(f6,g_met,':');

    end;

   

    Procedure operator;

    begin

    If (analiz[current_analiz+1].table<>'k') then begin current_analiz:=current_analiz+1;op_pr;current_analiz:=current_analiz-1 end

    else

      begin

      current_analiz:=current_analiz+1;

      cur_stroka:=cur_stroka+1;

      if (analiz[current_analiz].nomer=9) and (analiz[current_analiz].table='k') then op_if;

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