Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
12
Добавлен:
07.08.2013
Размер:
164.86 Кб
Скачать

Заключение

В результате выполнения данного курсового проекта был создан компилятор программы на подмножестве языка Pascal в программу на языке ассемблер.

Данная программа выполняет лексический и синтаксический анализ исходной программы и генерирует ассемблерный код. Для синтаксического разбора был использован нисходящий метод разбора.

Таким образом, разработанная в ходе курсового проекта программа-компилятор соответствует требованиям технического задания.

Список используемой литературы:

  1. Ахо А.В., Сети Р., Ульман Д.Д. «Компиляторы: принципы, технологии и инструменты» Москва 2003.

  2. Хантер Р. «Проектирование и конструирование компиляторов» Под ред. Савинкова В.М. пер.с англ М. Финансы и статистика 1984г. 232 с..

  3. Разработка компиляторов: метод. Указ. по курсовому проектированию. – Муром: ИПЦ МИВлГУ, 2007 – 52с.

  4. Фаронов В.В. Delphi. Программирование на языке высокого уровня: Учебник для вузов – Спб.: Питер, 2006. – 640с.: ил.

  5. Юров В.И. Assembler. - Спб.: Питер, 2000. – 624с.

Приложение а

Листинг программы:

unit Unit1;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, Menus, XPMan, StdCtrls, Buttons, ComCtrls, ExtCtrls,ShellAPI;

type

TForm1 = class(TForm)

MainMenu1: TMainMenu;

Project1: TMenuItem;

Compile1: TMenuItem;

Ebout1: TMenuItem;

Hep1: TMenuItem;

AboutTheProgram1: TMenuItem;

ShowHelp1: TMenuItem;

Contakts1: TMenuItem;

ListBox1: TListBox;

Setings1: TMenuItem;

ListBox2: TListBox;

Memo1: TMemo;

Save1: TMenuItem;

Load1: TMenuItem;

NewProject1: TMenuItem;

Panel1: TPanel;

StatusBar1: TStatusBar;

SpeedButton1: TSpeedButton;

Memo2: TMemo;

Memo3: TMemo;

SpeedButton2: TSpeedButton;

SpeedButton3: TSpeedButton;

procedure FormActivate(Sender: TObject);

procedure SpeedButton1Click(Sender: TObject);

procedure Memo1Change(Sender: TObject);

procedure SpeedButton2Click(Sender: TObject);

procedure Contakts1Click(Sender: TObject);

procedure SpeedButton3Click(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;

var

Form1 : TForm1;

code : string;

bufer : string;

TT : array [1..14] of string=('var','integer','begin','for','to','do','end',',',';','+','*','.',':',':=');

TI : array [1..20] of string;

TL : array [1..20] of string;

nID : integer;

nL : integer;

BC : boolean=false;

SC : boolean=false;

implementation

uses Unit2,Unit3;

{$R *.dfm}

procedure Start (Caption : string);

var {Caption : string;}

i : integer;

begin

for i:=1 to Length(Caption) do

begin

Form1.Memo1.Text:=Form1.Memo1.Text+Caption[i];

Form1.Memo1.Refresh;

Sleep(15);

end;

end;

procedure LoadSurceCode (); //налчие файла, проверка на присутствие коментария и загрузка

var a : boolean;

b : string;

c, i : integer;

begin

Form1.Memo1.Lines.LoadFromFile('codes/code.txt');

b:=Form1.Memo1.Text;

for c:=1 to Length(b) do

begin

if b[c]='{' then

begin

i:=c;

while b[i]<>'}' do

begin

b[i]:=' ';

i:=i+1;

end;

b[i]:=' ';

end;

end;

code:=b;

end;

procedure ShowOperands(bufer : string; typ : integer);

var i,j,result,result2,y,result3 : integer;

begin

result:=0;

result2:=0;

result3:=0;

if (typ=0)or(typ=2)or (typ=3) then

begin

for i:=1 to length(TT)do

begin

if TT[i]=bufer then

begin

Form1.ListBox1.Items.Add('T('+IntToStr(i)+')');

result:=1;

end;

end;

if result=0 then

begin

for j:=1 to nID do

begin

if TI[j]=bufer then

begin

Form1.ListBox1.Items.Add('I('+IntToStr(j)+')');

result2:=1;

end;

end;

if result2=0 then

begin

nID:=nID+1;

TI[nID]:=bufer;

Form1.ListBox1.Items.Add('I('+IntToStr(nID)+')');

end;

end;

end;

if typ = 1 then

begin

for y:=1 to 20 do

begin

if bufer=TL[y] then

begin

Form1.ListBox1.Items.Add('L('+IntToStr(y)+')');

result3:=1;

break;

end;

end;

if result3=0 then

begin

nL:=nL+1;

TL[nL]:=bufer;

Form1.ListBox1.Items.Add('L('+IntToStr(nL)+')');

end;

end;

end;

procedure GetOperand (); //получение операндов

var i,j,leng : integer;

hint : string;

begin

i:=1;

j:=1;

LoadSurceCode ();

leng:=length(code);

while j<= leng do

begin

if code[j] in ['a'..'z'] then

begin

while code[j] in ['a'..'z'] do

begin

bufer:=bufer+code[j];

j:=j+1;

end;

ShowOperands(bufer,0);

Form1.ListBox2.Items.Add(bufer);

bufer:='';

end

else

if code[j] in ['0'..'9'] then

begin

while code[j] in ['0'..'9'] do

begin

bufer:=bufer+code[j];

j:=j+1;

end;

ShowOperands(bufer,1);

Form1.ListBox2.Items.Add(bufer);

bufer:='';

end

else

if code[j] in [',','.',';','+','*'] then

begin

ShowOperands(code[j],2);

Form1.ListBox2.Items.Add(code[j]);

j:=j+1;

end

else

if code[j] = ':' then

begin

if code[j+1]='='then

begin

hint:=code[j]+code[j+1];

ShowOperands(hint,3);

Form1.ListBox2.Items.Add(hint);

j:=j+2;

end

else

begin

ShowOperands(code[j],2);

Form1.ListBox2.Items.Add(code[j]);

j:=j+1;

end;

end

else

j:=j+1;

end;

end;

procedure TForm1.FormActivate(Sender: TObject);

begin

Form1.SpeedButton3.Hide;

nID:=0;

nL:=0;

Memo1.Clear;

form1.Memo2.Clear;

form1.Memo3.Clear;

Start('{You Welcom AESC!}');

Form1.Memo1.Lines.Add('');

Start('{This Application Create student MIBU / PO-107}');

Form1.Memo1.Lines.Add('');

Start('{2009}');

Form1.Memo1.Lines.Add('');

Start('{Version 1.0.0 / AElitSoft}');

Form1.Memo1.Lines.Add('');

Start('{Add your Code Hear}');

end;

procedure PUSHTG();

var loop : integer;

run:string;

begin

for loop:=1 to Form1.ListBox1.Items.Count do

begin

run:=Form1.ListBox1.Items[loop-1];

if (run[1]='I') or (run[1]='L')then begin TG[loop]:=run[1];end;

if run[1]='T' then begin TG[loop]:=run;end;

end;

end;

////////////////////////////////////////////////////////

procedure TForm1.SpeedButton1Click(Sender: TObject);

var i,f : integer;

begin

if BC = true then begin

form1.StatusBar1.Panels[0].Text:='ErrorClick! Work Program incorrect! Restart program!';Form1.Icon.LoadFromFile('Icon/10.ico'); ShowMessage('ErrorClick! Work Program incorrect! Restart program!');

form1.SpeedButton1.Hide;

form1.SpeedButton3.Show;

exit;

end;

BC:=true;

GetOperand ();

PUSHTG();

unit3.OPREDELENIE();

i:=1;

unit3.SpisPeremPush();

if unit3.prog(i)<>0 then begin form1.StatusBar1.Panels[0].Text:='Error in source code!';Form1.Icon.LoadFromFile('Icon/10.ico'); ShowMessage('Error in source code!');end;

if unit3.SemanticError<>0 then begin form1.StatusBar1.Panels[0].Text:='Undeclared identifer!';Form1.Icon.LoadFromFile('Icon/10.ico'); ShowMessage('Undeclared identifer!');end;

if unit3.ErrorCodeGenerate<>0 then begin form1.StatusBar1.Panels[0].Text:='Error Generate Assembler Code!';Form1.Icon.LoadFromFile('Icon/10.ico'); ShowMessage('Error Generate Assembler Code!');end

else begin form1.StatusBar1.Panels[0].Text:='Generate Code Complete!'; ShowMessage('Generate Code Complete!'); end;

end;

procedure TForm1.Memo1Change(Sender: TObject);

var LCode:string;

begin

LCode:=Form1.Memo1.Text;

Form1.StatusBar1.Panels[0].Text:='Length Source Code : '+IntToStr(Length(LCode))+' '+'Folder : codes/code.txt';

end;

procedure TForm1.SpeedButton2Click(Sender: TObject);

begin

if (Form1.Memo3.Lines.Text<>'')and(SC=false) then

begin

Form1.Memo3.Lines.SaveToFile('codes/AsmSourceCode.asm');

ShowMessage('Data Saved!');

SC:=true;

end

else

begin

//ShowMessage('ErrorData! Error Save Data!');

Form1.Icon.LoadFromFile('Icon/10.ico');

form1.StatusBar1.Panels[0].Text:='ErrorData! Error Save Data!';

ShowMessage('ErrorData! Error Save Data!');

end;

end;

procedure TForm1.Contakts1Click(Sender: TObject);

begin

form2.ShowModal;

end;

function ExecuteFile(const FileName,Params,DefaultDir:string;

ShowCmd:Integer):THandle;

var

zFileName,zParams,zDir:array [0..79 ] of Char;

begin

Result :=ShellExecute(Application.MainForm.Handle,nil,

StrPCopy(zFileName,FileName),StrpCopy(zParams,Params),

StrPCopy(zDir,DefaultDir),ShowCmd);

application.Terminate;

end;

procedure TForm1.SpeedButton3Click(Sender: TObject);

begin

ExecuteFile('project1.exe'+'','','',SW_SHOW);

end;

end.

unit Unit2;

Соседние файлы в папке Курсач