səhifə 5/9 tarix 10.05.2023 ölçüsü 253,22 Kb. #110396
7-laboratoriya
e1=1;
}
}
if(line[t+1]=='+' || line[t+1]=='-' || line[t+1]=='*' || line[t+1]=='/' ){
// sintaksis tahlil uchun
if(d=="="){
cout<
e1=1;
}
}
float a;
// raqam ekanligini aniqlash
if(atof(h.c_str())){ a=atof(h.c_str());
cout<<"NUMBER("<
}
else if(e1!=1){
cout<<"IDENTIFIKATOR("<
cout<<"\t\t\t\t OPERATOR("<
}
h2=0; h.clear();
}
else if(d==";" || d==","){ e2=t; s1=1; s=0; s2=0;
// sintksis tahlil uchun
if((d!=",") && line[t-1]=='+' || line[t-1]=='-' || line[t-1]=='*' || line[t-1]=='/' ){
if(d!="=" && (line[t-2]=='+' || line[t-2]=='-' || line[t-2]=='*' || line[t-2]=='/') ){
cout<
e1=1;
}
}
if(line[t+1]=='+' || line[t+1]=='-' || line[t+1]=='*' || line[t+1]=='/' ){
// sintksis tahlil uchun
if(d==","){
cout<
e1=1;
}
}
// raqam ekanligini aniqlash
float a;
if(atof(h.c_str())){ a=atof(h.c_str());
cout<<"NUMBER("<
}
else if(e1!=1){
cout<<"IDENTIFIKATOR("<
cout<<"\t\t\t\t DELIMETR("<
}
h2=0; h.clear();
}
else if(d==" "){ s2=1; s=0; s1=0;
float a;
// raqam ekanligini aniqlash
if(atof(h.c_str())){ a=atof(h.c_str());
cout<<"NUMBER("<
}
else
if(!h.empty()) cout<<"IDENTIFIKATOR("<
h2=0; h.clear();
}
else {
h=h+d; h2++;
}
} // for siklini tugashi - bitta satrni tugashi
// vazifadagi belgilar uchramagan satrlar
if(s==0 && s1==0 && s2==0) {
cout<<"IDENTIFIKATOR("<
}
i++; // satrlar soni
}
getchar();
return 0;
}
Dastur natijasi:
Matnli fayl (1.cpp) ma’lumotlari:
Matindagi xatolik (6- satr, 18- belgi ) - dasturning natijasida “Arifmetik ifodada + ishora ketma-ket kelgan ” xabari bilan izohlangan. Ushbu ishoradan oldin ham + ishorasini tokenlar jadvali – (OPERATOR(6,17) - +) ko’rinishida keltirilgan.
Dastur tahlili:
Kiruvchi ma’lumotlar matnli fayldan o’zlashtirib olindi (1.cpp ). Vazifada takidlangan belgilar (nuqta-vergul, identifikatorlar, haqiqiy sonlar, o’zlashtirish belgisi va figurali qavslar ) ajratib olindi. Qolgan belgilar esa IDENTIFIKATOR ko’rinishida qabul qilindi. atof() funksiya bilan raqamga tekshirildi. Arifmetik ishoralar ketma-ketligi sintaksis tahli qilindi – ya’niy xar xil xatolik bo’ladigan vaziyatlar aniqlab olindi.
Masalan. Ishora o’rnida + ishorasi tanlandi .
c=a++b;
c=+a+b;
c=a+b++;
c=++a+b;
c=a+b+;
a+;
+a;
+;
;+;
,+
+,
int a++;
dastur 12 ta holat uchun sintaksis tahlil qiladi va ushbu 12 ta holat matnli faylda uchrasa xatolik haqida xabar chiqaradi.
Quyida Visual Studio ning Windows application muhitida tuzilgan tashqi ko’rinishga ega bo’lgan leksik tahli qiluvchi datur keltirilgan:
Dastur ko’rinishi quyidagicha:
Daturning to’liq matni:
#pragma once
#include"stdafx.h"
#include
#include
#include
#include
#define INT 01
#define CHAR 02
#define FLOAT 03
#define LONG 04
#define DOUBLE 05
#define STRUCT 06
#define PRINTF 07
#define SCANF 08
#define FOR 09
#define DO 10
#define WHILE 11
#define IF 12
#define ELSE 13
#define CONTINUE 14
#define BREAK 15
#define DEFAULT 16
#define CASE 17
#define GOTO 18
#define SWITCH 19
#define OB 20
#define CB 21
#define LP 22
#define RP 23
#define EQUALTO 24
#define LT 25
#define GT 26
#define PLUS 27
#define MINUS 28
#define MUL 29
#define DIV 30
#define COLON 31
#define SCOLON 32
#define TERNARY 33
#define COMMA 34
#define PLINE 35
#define CARAT 36
#define DIGIT 37
#define ID 38
#define OSB 39
#define CSB 40
#define HSH 41
#define PER 42
#define MAIN 43
#define VOID 44
#define NOTEQU 45
#define AND 46
#define BSH 47
#define DQ 48
#define SQ 49
#define DOT 50
#define INCLUDE 51
#define SPACE 52
#define TAB 53
#define NUM_ENTRIES 54
int token_no=0,key=0,ident=0,sp=0,op=0,digit=0;
char keyM[100][100],identM[100][100],spM[10000],opM[100][100],digitM[1000];
using namespace std;
struct table
{
char name[25];
int token;
};
struct table ref_tab[NUM_ENTRIES] = {
{"int", INT},
{"char", CHAR},
{"float", FLOAT},
{"long", LONG},
{"double", DOUBLE},
{"struct", STRUCT},
{"printf", PRINTF},
{"do", DO},
{"while", WHILE},
{"if", IF},
{"else", ELSE},
{"continue", CONTINUE},
{"break", BREAK},
{"default", DEFAULT},
{"case", CASE},
{"goto", GOTO},
{"switch", SWITCH},
{"{", OB},
{"}", CB},
{"(", LP},
{")", RP},
{"=", EQUALTO},
{"<", LT},
{">", GT},
{"+", PLUS},
{"-", MINUS},
{"*", MUL},
{"/", DIV},
{":", COLON},
{";", SCOLON},
{"?", TERNARY},
{",", COMMA},
{"|", PLINE},
{"^", CARAT},
{"[", OSB},
{"]", CSB},
{"#", HSH},
{"%", PER},
{"main", MAIN},
{"void", VOID},
{"!=", NOTEQU},
{"&", AND},
{"\\",BSH},
{"\"",DQ},
{"'", SQ},
{"include",INCLUDE},
};
char out_token[NUM_ENTRIES+1][40]=
{
" ",
"INT",
"CHAR",
"FLOAT",
"LONG",
"DOUBLE",
"STRUCT",
"PRINTF",
"SCANF",
"FOR",
"DO",
"WHILE",
"IF",
"ELSE",
"CONTINUE",
"BREAK",
"DEFAULT",
"CASE",
"GOTO",
"SWITCH",
"{",
"}",
"(",
")",
"=",
"<",
">",
"+",
"-",
"*",
"/",
":",
";",
"?",
",",
"|",
"^",
"DIGIT",
"ID",
"[",
"]",
"#",
"%",
"MAIN",
"VOID",
"!=",
"&",
"\\",
"\"",
"'",
".",
"INCLUDE"
};
namespace Leksik_analizator {
using namespace System ;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
///
/// Summary for Form1
///
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
///
/// Clean up any resources being used.
///
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::DataGridView^ dataGridView1;
protected:
private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column1;
private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column2;
private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column3;
private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column4;
private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column5;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Label^ label5;
private:
///
/// Required designer variable.
///
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
Dostları ilə paylaş: