ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 18.04.2025

Просмотров: 396

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

Начальное содержимое автомата: <S>

#1 Заменить (<совокупность операторов>), Держать

#2 Заменить (<оператор><совокупность операторов>), Держать

#3 Вытолкнуть, Держать

#4 Заменить (<оператор присваивания>), Держать

#5 Заменить (<условный оператор>), Держать

#6 Заменить (<оператор цикла>), Держать

#7 Заменить (‘=’<E> {Присвоить}‘;’), Сдвиг

#8 Заменить (<T><E-список>), Держать

#9 Заменить (<T> {Сложить}<E-список>), Сдвиг

#10 Вытолкнуть, Держать

#11 Заменить (<F><T-список>), Держать

#12 Заменить (<F> {Умножить}<T-список>), Сдвиг

#13 Вытолкнуть, Держать

#14 Заменить (<P><F-список>), Держать

#15 Заменить (<P> {В степень}<F-список>), Сдвиг

#16 Вытолкнуть, Держать

#17 Вытолкнуть, Сдвиг

#18 Вытолкнуть, Сдвиг

#19 Заменить (<лог.выр.>{Переход по 0}then<оператор><else-часть>), Сдвиг

#20 Заменить ({Безусловный переход} {Метка}<оператор> {Метка}), Сдвиг

#21 Заменить ({Метка}), Держать

#22 Заменить ({Метка}<лог.выр.>{Переход по сравнению}<совокупность операторов>

{Безусловный переход} {Метка}end_while), Сдвиг

#23 Заменить (<P><логический-список>’)’), Сдвиг

#24 Заменить (<P>{Равно}), Сдвиг

#25 Заменить (<P>{Не равно}), Сдвиг

ВС Вытолкнуть, Сдвиг

Д Допустить

  1. Пример замены магазинных символов, содержащих атрибутные поля:

  • Оператор цикла:

Текущая входная цепочка

Содержание магазина

while (i # 10)

1

<оператор цикла>

i = i + 1;

0

end_while¶

while (i # 10)

14

While

i = i + 1;

13

{Метка}

end_while¶

12

1

11

<логическое выражение>

10

8

9

{Переход по сравнению}

8

(-1)

7

0

6

<совокупность операторов>

5

{Безусловный переход}

4

1

3

{Метка}

2

0

1

end_while

0


В массив меток добавляется два указателя на массив команд, а в магазин записываются адреса этих ячеек в массиве меток.

  • Условный оператор:

Текущая входная цепочка

Содержание магазина

if(d=c)then

1

<условный оператор>

a=d*2;

0

else a=d^3;¶

if(d=c)then

10

If

a=d*2;

9

<логическое выражение>

else a=d^3;¶

8

6

7

{Переход по 0}

6

(-1)

5

0

4

Then

3

<оператор>

2

<else-часть>

1

0

0


  1. Тестовый пример

  1. Текст программы


Оглавление

Main.cpp

__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) 17

void Prints(rez &R) 17

void __fastcall TForm1::BitBtn1Click(TObject *Sender) 18

LeksBlok.h

void Strcpy(char mas[], int n, char *txt, int m); 19

void LeksichBlok(Ident &I, char *txt); 19

LeksBlok.cpp

void Strcpy(char mas[], int n, char *txt, int m) 19

void LeksichBlok(Ident &I, char *txt) 20

Sintaksis.h

void Sint(Ident &I, rez &R); 21

Sintaksis.cpp

void Sint(Ident &I, rez &R) 21

Interpretator.h

void Inter(Ident &I, rez &R); 27

Interpretator.cpp

int pow (int x, int y) 27

void Inter(Ident &I, rez &R) 28

Data.h

struct elem 28

struct rez 29

class ErrOverFlow 29

class ErrIdent 29

class ErrSimb 29

class ErrSearch 29

class ErrInvChain 29

class Ident 29

Data.cpp

Ident::Ident() 29

Ident::~Ident() 29

int Hesh (int d, char *c, char name[] = 0) 30

int Ident::Add(elem z) 30

int Ident::Search (elem el) 30

void Ident::Delete(elem el) 30

Stack_mass.h

class ErrEmptyStack {}; 31

template <class TYPE> class Stack 31

Main.cpp

#include <vcl.h>

#pragma hdrstop

#include "Main.h"

#include "DataBase.h"

#include "Stack_mass.h"

#include "Sintaksis.h"

#include "Interpretator.h"

#include "LeksBlok.h"

#pragma package(smart_init)

#pragma resource "*.dfm"

TForm1 *Form1;

//------------------------------------------------------------------------------------------------------------------------------------------------------

__fastcall TForm1::TForm1(TComponent* Owner)

: TForm(Owner)

{

StringGrid1->Cells[0][0] = "№";

StringGrid1->Cells[1][0] = "Имя";

StringGrid1->Cells[2][0] = "Значение";

StringGrid2->Cells[0][0] = "№";

StringGrid2->Cells[1][0] = "Команда";

}

//------------------------------------------------------------------------------------------------------------------------------------------------------

Void Prints(rez &r)

{

int size = R.Data.Size();

Form1->StringGrid1->RowCount = size;

for (int i = 0; i < size; ++i)

{

Form1->StringGrid1->Cells[0][i + 1] = i;

elem el = R.Data[i];

if (el.clas < 4)

Form1->StringGrid1->Cells[1][i + 1] = el.name;

else

Form1->StringGrid1->Cells[1][i + 1] = "NEW";

Form1->StringGrid1->Cells[2][i + 1] = el.value;

}

size = R.Command.Size();

Form1->StringGrid2->RowCount = size + 1;

for (int i = 0; i < size; ++i)

Form1->StringGrid2->Cells[0][i + 1] = i;

for (int i = 0; i < size; )

{

int el = R.Command[i];

switch (el)

{

case K_PRISV:

Form1->StringGrid2->Cells[1][++i] = "Присвоить";

for (int ptr = 0; ptr < 2; ++ptr)

Form1->StringGrid2->Cells[1][++i] = R.Command[i];

break;

case K_SLOJ:

Form1->StringGrid2->Cells[1][++i] = "Сложить";

for (int ptr = 0; ptr < 3; ++ptr)

Form1->StringGrid2->Cells[1][++i] = R.Command[i];

break;


case K_UMNOJ:

Form1->StringGrid2->Cells[1][++i] = "Умножить";

for (int ptr = 0; ptr < 3; ++ptr)

Form1->StringGrid2->Cells[1][++i] = R.Command[i];

break;

case K_V_STEP:

Form1->StringGrid2->Cells[1][++i] = "Возвести в степень";

for (int ptr = 0; ptr < 3; ++ptr)

Form1->StringGrid2->Cells[1][++i] = R.Command[i];

break;

case K_PEREHOD_0:

Form1->StringGrid2->Cells[1][++i] = "Переход по 0";

Form1->StringGrid2->Cells[1][++i] = R.Command[i];

Form1->StringGrid2->Cells[1][++i] = R.Label[R.Command[i]];

break;

case K_PEREHOD_BEZ_USL:

Form1->StringGrid2->Cells[1][++i] = "Безусловный переход";

Form1->StringGrid2->Cells[1][++i] = R.Label[R.Command[i]];

break;

case K_PEREHOD_SRAVN:

Form1->StringGrid2->Cells[1][++i] = "Переход по сравнению";

Form1->StringGrid2->Cells[1][++i] = R.Command[i];

Form1->StringGrid2->Cells[1][++i] = R.Label[R.Command[i]];

break;

case K_RAVNO:

Form1->StringGrid2->Cells[1][++i] = "Логическое \"равно\"";

for (int ptr = 0; ptr < 3; ++ptr)

Form1->StringGrid2->Cells[1][++i] = R.Command[i];

break;

case K_NE_RAVNO:

Form1->StringGrid2->Cells[1][++i] = "Логическое \"не равно\"";

for (int ptr = 0; ptr < 3; ++ptr)

Form1->StringGrid2->Cells[1][++i] = R.Command[i];

break;

}

}

}

//------------------------------------------------------------------------------------------------------------------------------------------------------