Leksik va sintaksis tahlil



Yüklə 253,22 Kb.
səhifə5/9
tarix10.05.2023
ölçüsü253,22 Kb.
#110396
1   2   3   4   5   6   7   8   9
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;

  • }

  • }




    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. };

    56. namespace Leksik_analizator {

    57. using namespace System;

    58. using namespace System::ComponentModel;

    59. using namespace System::Collections;

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

    61. using namespace System::Data;

    62. using namespace System::Drawing;

    63. ///

    64. /// Summary for Form1

    65. ///


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

    67. {

    68. public:

    69. Form1(void)

    70. {

    71. InitializeComponent();

    72. //

    73. //TODO: Add the constructor code here

    74. //

    75. }

    76. protected:

    77. ///

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

    79. ///


    80. ~Form1()

    81. {

    82. if (components)

    83. {

    84. delete components;

    85. }

    86. }

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

    88. protected:

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

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

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

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

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

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

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

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

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

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

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

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

    101. private:

    102. ///

    103. /// Required designer variable.

    104. ///


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

    106. #pragma region Windows Form Designer generated code

    107. ///

    108. /// Required method for Designer support - do not modify

    109. /// the contents of this method with the code editor.

    110. ///



    111. Yüklə 253,22 Kb.

      Dostları ilə paylaş:
  • 1   2   3   4   5   6   7   8   9




    Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
    rəhbərliyinə müraciət

    gir | qeydiyyatdan keç
        Ana səhifə


    yükləyin