Leksik va sintaksis tahlil



Download 305 Kb.
bet5/8
Sana27.04.2022
Hajmi305 Kb.
#585390
1   2   3   4   5   6   7   8
Bog'liq
Leksik va sintaksis tahlil

e1=1;

  • }

  • }

  • if(line[t+1]=='+' || line[t+1]=='-' || line[t+1]=='*' || line[t+1]=='/' ){

  • // sintaksis tahlil uchun

  • if(d=="="){

  • cout<

  • e1=1;

  • }

  • }




    1. float a;

    2. // raqam ekanligini aniqlash

    3. if(atof(h.c_str())){ a=atof(h.c_str());

    4. cout<<"NUMBER("<

    5. }

    6. else if(e1!=1){

    7. cout<<"IDENTIFIKATOR("<




    1. cout<<"\t\t\t\t OPERATOR("<

    2. }

    3. h2=0; h.clear();

    4. }

    5. else if(d==";" || d==","){ e2=t; s1=1; s=0; s2=0;

    6. // sintksis tahlil uchun

    7. if((d!=",") && line[t-1]=='+' || line[t-1]=='-' || line[t-1]=='*' || line[t-1]=='/' ){

    8. if(d!="=" && (line[t-2]=='+' || line[t-2]=='-' || line[t-2]=='*' || line[t-2]=='/') ){

    9. cout<

    10. e1=1;

    11. }

    12. }

    13. if(line[t+1]=='+' || line[t+1]=='-' || line[t+1]=='*' || line[t+1]=='/' ){

    14. // sintksis tahlil uchun

    15. if(d==","){

    16. cout<

    17. e1=1;

    18. }

    19. }

    20. // raqam ekanligini aniqlash

    21. float a;

    22. if(atof(h.c_str())){ a=atof(h.c_str());

    23. cout<<"NUMBER("<

    24. }

    25. else if(e1!=1){

    26. cout<<"IDENTIFIKATOR("<

    27. cout<<"\t\t\t\t DELIMETR("<

    28. }

    29. h2=0; h.clear();

    30. }

    31. else if(d==" "){ s2=1; s=0; s1=0;

    32. float a;

    33. // raqam ekanligini aniqlash

    34. if(atof(h.c_str())){ a=atof(h.c_str());

    35. cout<<"NUMBER("<

    36. }

    37. else

    38. if(!h.empty()) cout<<"IDENTIFIKATOR("<

    39. h2=0; h.clear();

    40. }

    41. else {

    42. h=h+d; h2++;

    43. }

    44. } // for siklini tugashi - bitta satrni tugashi

    45. // vazifadagi belgilar uchramagan satrlar

    46. if(s==0 && s1==0 && s2==0) {

    47. cout<<"IDENTIFIKATOR("<

    48. }

    49. i++; // satrlar soni

    50. }

    51. getchar();

    52. return 0;

    53. }

    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.

    1. c=a++b;

    2. c=+a+b;

    3. c=a+b++;

    4. c=++a+b;

    5. c=a+b+;

    6. a+;

    7. +a;

    8. +;

    9. ;+;

    10. ,+

    11. +,

    12. 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:

    1. #pragma once

    2. #include"stdafx.h"

    3. #include

    4. #include

    5. #include

    6. #include




    1. #define INT 01

    2. #define CHAR 02

    3. #define FLOAT 03

    4. #define LONG 04

    5. #define DOUBLE 05

    6. #define STRUCT 06

    7. #define PRINTF 07

    8. #define SCANF 08

    9. #define FOR 09

    10. #define DO 10

    11. #define WHILE 11

    12. #define IF 12

    13. #define ELSE 13

    14. #define CONTINUE 14

    15. #define BREAK 15

    16. #define DEFAULT 16

    17. #define CASE 17

    18. #define GOTO 18

    19. #define SWITCH 19

    20. #define OB 20

    21. #define CB 21

    22. #define LP 22

    23. #define RP 23

    24. #define EQUALTO 24

    25. #define LT 25

    26. #define GT 26

    27. #define PLUS 27

    28. #define MINUS 28

    29. #define MUL 29

    30. #define DIV 30

    31. #define COLON 31

    32. #define SCOLON 32

    33. #define TERNARY 33

    34. #define COMMA 34

    35. #define PLINE 35

    36. #define CARAT 36

    37. #define DIGIT 37

    38. #define ID 38

    39. #define OSB 39

    40. #define CSB 40

    41. #define HSH 41

    42. #define PER 42

    43. #define MAIN 43

    44. #define VOID 44

    45. #define NOTEQU 45

    46. #define AND 46

    47. #define BSH 47

    48. #define DQ 48

    49. #define SQ 49

    50. #define DOT 50

    51. #define INCLUDE 51

    52. #define SPACE 52

    53. #define TAB 53

    54. #define NUM_ENTRIES 54

    55. int token_no=0,key=0,ident=0,sp=0,op=0,digit=0;

    56. char keyM[100][100],identM[100][100],spM[10000],opM[100][100],digitM[1000];




    1. using namespace std;




    1. struct table

    2. {

    3. char name[25];

    4. int token;

    5. };




    1. struct table ref_tab[NUM_ENTRIES] = {




    1. {"int", INT},

    2. {"char", CHAR},

    3. {"float", FLOAT},

    4. {"long", LONG},

    5. {"double", DOUBLE},

    6. {"struct", STRUCT},

    7. {"printf", PRINTF},

    8. {"do", DO},

    9. {"while", WHILE},

    10. {"if", IF},

    11. {"else", ELSE},

    12. {"continue", CONTINUE},

    13. {"break", BREAK},

    14. {"default", DEFAULT},

    15. {"case", CASE},

    16. {"goto", GOTO},

    17. {"switch", SWITCH},

    18. {"{", OB},

    19. {"}", CB},

    20. {"(", LP},

    21. {")", RP},

    22. {"=", EQUALTO},

    23. {"<", LT},

    24. {">", GT},

    25. {"+", PLUS},

    26. {"-", MINUS},

    27. {"*", MUL},

    28. {"/", DIV},

    29. {":", COLON},

    30. {";", SCOLON},

    31. {"?", TERNARY},

    32. {",", COMMA},

    33. {"|", PLINE},

    34. {"^", CARAT},

    35. {"[", OSB},

    36. {"]", CSB},

    37. {"#", HSH},

    38. {"%", PER},

    39. {"main", MAIN},

    40. {"void", VOID},

    41. {"!=", NOTEQU},

    42. {"&", AND},

    43. {"\\",BSH},

    44. {"\"",DQ},

    45. {"'", SQ},

    46. {"include",INCLUDE},

    47. };




    1. char out_token[NUM_ENTRIES+1][40]=

    2. {

    3. " ",

    4. "INT",

    5. "CHAR",

    6. "FLOAT",

    7. "LONG",

    8. "DOUBLE",

    9. "STRUCT",

    10. "PRINTF",

    11. "SCANF",

    12. "FOR",

    13. "DO",

    14. "WHILE",

    15. "IF",

    16. "ELSE",

    17. "CONTINUE",

    18. "BREAK",

    19. "DEFAULT",

    20. "CASE",

    21. "GOTO",

    22. "SWITCH",

    23. "{",

    24. "}",

    25. "(",

    26. ")",

    27. "=",

    28. "<",

    29. ">",

    30. "+",

    31. "-",

    32. "*",

    33. "/",

    34. ":",

    35. ";",

    36. "?",

    37. ",",

    38. "|",

    39. "^",

    40. "DIGIT",

    41. "ID",

    42. "[",

    43. "]",

    44. "#",

    45. "%",

    46. "MAIN",

    47. "VOID",

    48. "!=",

    49. "&",

    50. "\\",

    51. "\"",

    52. "'",

    53. ".",

    54. "INCLUDE"

    55. };


    1. namespace Leksik_analizator {




    1. using namespace System;

    2. using namespace System::ComponentModel;

    3. using namespace System::Collections;

    4. using namespace System::Windows::Forms;

    5. using namespace System::Data;

    6. using namespace System::Drawing;




    1. ///

    2. /// Summary for Form1

    3. ///


    4. public ref class Form1 : public System::Windows::Forms::Form

    5. {

    6. public:

    7. Form1(void)

    8. {

    9. InitializeComponent();

    10. //

    11. //TODO: Add the constructor code here

    12. //

    13. }




    1. protected:

    2. ///

    3. /// Clean up any resources being used.

    4. ///


    5. ~Form1()

    6. {

    7. if (components)

    8. {

    9. delete components;

    10. }

    11. }

    12. private: System::Windows::Forms::DataGridView^ dataGridView1;

    13. protected:

    14. private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column1;

    15. private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column2;

    16. private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column3;

    17. private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column4;

    18. private: System::Windows::Forms::DataGridViewTextBoxColumn^ Column5;

    19. private: System::Windows::Forms::Button^ button1;

    20. private: System::Windows::Forms::TextBox^ textBox1;

    21. private: System::Windows::Forms::Label^ label1;

    22. private: System::Windows::Forms::Label^ label2;

    23. private: System::Windows::Forms::Label^ label3;

    24. private: System::Windows::Forms::Label^ label4;

    25. private: System::Windows::Forms::Label^ label5;


    1. private:

    2. ///

    3. /// Required designer variable.

    4. ///


    5. System::ComponentModel::Container ^components;




    1. #pragma region Windows Form Designer generated code


    2. Download 305 Kb.

      Do'stlaringiz bilan baham:
  • 1   2   3   4   5   6   7   8




    Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©www.hozir.org 2024
    ma'muriyatiga murojaat qiling

    kiriting | ro'yxatdan o'tish
        Bosh sahifa
    юртда тантана
    Боғда битган
    Бугун юртда
    Эшитганлар жилманглар
    Эшитмадим деманглар
    битган бодомлар
    Yangiariq tumani
    qitish marakazi
    Raqamli texnologiyalar
    ilishida muhokamadan
    tasdiqqa tavsiya
    tavsiya etilgan
    iqtisodiyot kafedrasi
    steiermarkischen landesregierung
    asarlaringizni yuboring
    o'zingizning asarlaringizni
    Iltimos faqat
    faqat o'zingizning
    steierm rkischen
    landesregierung fachabteilung
    rkischen landesregierung
    hamshira loyihasi
    loyihasi mavsum
    faolyatining oqibatlari
    asosiy adabiyotlar
    fakulteti ahborot
    ahborot havfsizligi
    havfsizligi kafedrasi
    fanidan bo’yicha
    fakulteti iqtisodiyot
    boshqaruv fakulteti
    chiqarishda boshqaruv
    ishlab chiqarishda
    iqtisodiyot fakultet
    multiservis tarmoqlari
    fanidan asosiy
    Uzbek fanidan
    mavzulari potok
    asosidagi multiservis
    'aliyyil a'ziym
    billahil 'aliyyil
    illaa billahil
    quvvata illaa
    falah' deganida
    Kompyuter savodxonligi
    bo’yicha mustaqil
    'alal falah'
    Hayya 'alal
    'alas soloh
    Hayya 'alas
    mavsum boyicha


    yuklab olish