• 基于Python实现语法分析


    1. 需求分析

    使用 LR(1)法进行语法分析。

    根据 C 语言的文法生成 action 表和 goto 表,利用 action 表和 goto 表对词法分析的输出进行语法分析,构建语法树。

    2. 文法设计

    要求:给出如下语言成分的文法描述。

    • 声明语句(包括变量声明、数组声明、记录声明和过程声明)
    Defination -> Type Point id
    Type -> int | float | bool | id | double | Type [ const ]
    Point -> epsilon | *
    
    Struct -> struct id { Statement }
    Statement -> Defination ; | Defination ; Statement
    Function -> Type Point id ( Parameter ) { Process }
    Parameter -> epsilon | Defination | Defination , Parameter
    Process -> Module Return
    Return -> return id Index ; | return Index ; | epsilon
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 表达式及赋值语句(包括数组元素的引用和赋值)
    Assignment -> id Index = Value
    Value -> Value + Value | Value - Value | Value * Value | Value / Value | Call
    Value -> - Value
    Value -> ( Value )
    Value -> const | id Index
    Index -> [ Value ] Index | epsilon
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 分支语句:if_then_else
    Control -> If | If_Else | While
    If -> if ( Condition ) { Module }
    If_Else -> if ( Condition ) { Module } else { Module }
    
    • 1
    • 2
    • 3
    • 循环语句:do_while
    While -> while ( Condition ) { Module }
    Condition -> Condition and Condition | Condition or Condition | not Condition | ( Condition ) | Value Relop Value | true | false
    Relop  ->  < | <= | == | != | > | >=
    
    • 1
    • 2
    • 3
    • 过程调用语句
    Call -> id ( Transmit )
    Transmit -> epsilon | Value | Value , Transmit
    
    • 1
    • 2

    3. 系统设计

    要求:分为系统概要设计和系统详细设计。

    (1) 系统概要设计:给出必要的系统宏观层面设计图,如系统框架图、数据流图、功能模块结构图等以及相应的文字说明。

    程序主要由几部分组成

    • 词法分析结果加载器
    • action 表和 goto 表加载器
    • 语法分析器
    • 语法树打印器

    (2) 系统详细设计:对如下工作进行展开描述

    • 核心数据结构的设计

    在这里插入图片描述

    用于储存词法分析的结果,保存词法分析后的值、种类和行数。

    • 主要功能函数说明

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    • 程序核心部分的程序流程图

    在这里插入图片描述

    4. 系统实现及结果分析

    要求:对如下内容展开描述。

    • 系统实现过程中遇到的问题;
    • 输出该句法分析器的分析表;

    附件 action.txt 与 goto.txt

    • 针对一测试程序输出其句法分析结果;

    测试程序

    输出结果:

    Module (1)
       Function (1)
          Type (1)
             int (1)
          Point (1)
          id : sum (1)
          ( (1)
          Parameter (1)
             Defination (1)
                Type (1)
                   float (1)
                Point (1)
                id : a (1)
             , (1)
             Parameter (1)
                Defination (1)
                   Type (1)
                      int (1)
                   Point (1)
                   id : b (1)
          ) (1)
          { (1)
          Process (2)
             Module (2)
                Defination (2)
                   Type (2)
                      float (2)
                   Point (2)
                   id : ans (2)
                ; (2)
                Module (3)
                   Assignment (3)
                      id : ans (3)
                      Index (3)
                      = (3)
                      Value (3)
                         Value (3)
                            id : a (3)
                            Index (3)
                         + (3)
                         Value (3)
                            id : b (3)
                            Index (3)
                   ; (3)
                   Module (3)
             Return (4)
                return (4)
                id : ans (4)
                Index (4)
                ; (4)
          } (5)
       Module (7)
          Struct (7)
             struct (7)
             id : student (7)
             { (7)
             Statement (8)
                Defination (8)
                   Type (8)
                      int (8)
                   Point (8)
                   id : x (8)
                ; (8)
                Statement (9)
                   Defination (9)
                      Type (9)
                         int (9)
                      Point (9)
                      id : y (9)
                   ; (9)
             } (10)
          Module (12)
             Defination (12)
                Type (12)
                   int (12)
                Point (12)
                id : a (12)
             ; (12)
             Module (13)
                Defination (13)
                   Type (13)
                      Type (13)
                         int (13)
                      [ (13)
                      const : 3 (13)
                      ] (13)
                   Point (13)
                   id : b (13)
                ; (13)
                Module (14)
                   Defination (14)
                      Type (14)
                         int (14)
                      Point (14)
                      id : c (14)
                   ; (14)
                   Module (15)
                      Defination (15)
                         Type (15)
                            int (15)
                         Point (15)
                         id : f (15)
                      ; (15)
                      Module (16)
                         Defination (16)
                            Type (16)
                               int (16)
                            Point (16)
                               * (16)
                            id : k (16)
                         ; (16)
                         Module (18)
                            Assignment (18)
                               id : a (18)
                               Index (18)
                               = (18)
                               Value (18)
                                  const : 1 (18)
                            ; (18)
                            Module (19)
                               Assignment (19)
                                  id : b (19)
                                  Index (19)
                                     [ (19)
                                     Value (19)
                                        const : 0 (19)
                                     ] (19)
                                     Index (19)
                                  = (19)
                                  Value (19)
                                     const : 1 (19)
                               ; (19)
                               Module (20)
                                  Assignment (20)
                                     id : c (20)
                                     Index (20)
                                     = (20)
                                     Value (20)
                                        Value (20)
                                           id : a (20)
                                           Index (20)
                                        + (20)
                                        Value (20)
                                           id : b (20)
                                           Index (20)
                                              [ (20)
                                              Value (20)
                                                 const : 0 (20)
                                              ] (20)
                                              Index (20)
                                  ; (20)
                                  Module (21)
                                     Assignment (21)
                                        id : f (21)
                                        Index (21)
                                        = (21)
                                        Value (21)
                                           Call (21)
                                              id : sum (21)
                                              ( (21)
                                              Transmit (21)
                                                 Value (21)
                                                    Value (21)
                                                       id : a (21)
                                                       Index (21)
                                                    + (21)
                                                    Value (21)
                                                       id : b (21)
                                                       Index (21)
                                                          [ (21)
                                                          Value (21)
                                                             const : 0 (21)
                                                          ] (21)
                                                          Index (21)
                                              ) (21)
                                     ; (21)
                                     Module (23)
                                        Control (23)
                                           IfElse (23)
                                              if (23)
                                              ( (23)
                                              Condition (23)
                                                 Value (23)
                                                    id : a (23)
                                                    Index (23)
                                                 Relop (23)
                                                    == (23)
                                                 Value (23)
                                                    const : 0 (23)
                                              ) (23)
                                              { (23)
                                              Module (24)
                                                 Assignment (24)
                                                    id : b (24)
                                                    Index (24)
                                                       [ (24)
                                                       Value (24)
                                                          const : 1 (24)
                                                       ] (24)
                                                       Index (24)
                                                    = (24)
                                                    Value (24)
                                                       const : 1 (24)
                                                 ; (24)
                                                 Module (24)
                                              } (25)
                                              else (25)
                                              { (25)
                                              Module (26)
                                                 Assignment (26)
                                                    id : b (26)
                                                    Index (26)
                                                       [ (26)
                                                       Value (26)
                                                          const : 1 (26)
                                                       ] (26)
                                                       Index (26)
                                                    = (26)
                                                    Value (26)
                                                       const : 0 (26)
                                                 ; (26)
                                                 Module (26)
                                              } (27)
                                        Module (28)
                                           Assignment (28)
                                              id : a (28)
                                              Index (28)
                                              = (28)
                                              Value (28)
                                                 const : 1 (28)
                                           ; (28)
                                           Module (30)
                                              Control (30)
                                                 While (30)
                                                    while (30)
                                                    ( (30)
                                                    Condition (30)
                                                       Value (30)
                                                          id : f (30)
                                                          Index (30)
                                                       Relop (30)
                                                          < (30)
                                                       Value (30)
                                                          const : 4 (30)
                                                    ) (30)
                                                    { (30)
                                                    Module (31)
                                                       Assignment (31)
                                                          id : b (31)
                                                          Index (31)
                                                             [ (31)
                                                             Value (31)
                                                                const : 2 (31)
                                                             ] (31)
                                                             Index (31)
                                                          = (31)
                                                          Value (31)
                                                             Value (31)
                                                                id : b (31)
                                                                Index (31)
                                                                   [ (31)
                                                                   Value (31)
                                                                      const : 2 (31)
                                                                   ] (31)
                                                                   Index (31)
                                                             + (31)
                                                             Value (31)
                                                                const : 1 (31)
                                                       ; (31)
                                                       Module (31)
                                                    } (32)
                                              Module (30)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 输出针对此测试程序对应的语法错误报告;

    第 28 行出现错误,进行报错

    Syntax error at Line [28]:illegal ==
    
    • 1
    • 对实验结果进行分析。

    实验结果正确无误
    const : 1 (31)
    ; (31)
    Module (31)
    } (32)
    Module (30)

    
    - 输出针对此测试程序对应的语法错误报告;
    
    
    第 28 行出现错误,进行报错
    
    ```c++
    Syntax error at Line [28]:illegal ==
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 对实验结果进行分析。

    实验结果正确无误

  • 相关阅读:
    【STM32CubeMX】NRF24L01模块实现“1对1“及“1对多“无线通信
    P1135 奇怪的电梯
    vue3 笔记
    OpenCV安装、QT、VS配置项目设置
    Kotlin的协程:flow
    C++ 炼气期之结构体
    未找到 [“sitemapLocation“] 对应的 sitemap.json 文件
    十四、Qt主机信息与网络编程
    重学设计模式-模板方法模式
    Day18ACL
  • 原文地址:https://blog.csdn.net/newlw/article/details/126126823