程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Syntax analysis based on Python

編輯:Python

1. 需求分析

使用 LR(1)Method syntax analysis.

根據 C Language grammar generated action 表和 goto 表,利用 action 表和 goto Table on the output of the lexical analysis syntax analysis,構建語法樹.

2. 文法設計

要求:Given the following language ingredient grammar description.

  • 聲明語句(包括變量聲明、數組聲明、Record statement and process statement)
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
  • Expression and assignment statements(Including array element references and assignment)
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
  • 分支語句:if_then_else
Control -> If | If_Else | While
If -> if ( Condition ) { Module }
If_Else -> if ( Condition ) { Module } else { Module }
  • 循環語句:do_while
While -> while ( Condition ) { Module }
Condition -> Condition and Condition | Condition or Condition | not Condition | ( Condition ) | Value Relop Value | true | false
Relop -> < | <= | == | != | > | >=
  • 過程調用語句
Call -> id ( Transmit )
Transmit -> epsilon | Value | Value , Transmit

3. 系統設計

要求:Divided into the system summary design and detailed design.

(1) 系統概要設計:Given the necessary system macro design,Such as system frame、數據流圖、Function module structure and the corresponding caption.

Program is mainly composed of several parts

  • Lexical analysis results loader
  • action 表和 goto Table loader
  • 語法分析器
  • The syntax tree printing machine

(2) 系統詳細設計:Description of the following work carried out by

  • The design of the core data structure

Used to store the lexical analysis of the results,Save the lexical analysis after the value of the、Type and number of lines.

  • 主要功能函數說明



  • Program the core part of the program flow chart

4. System implementation and result analysis

要求:On the following description.

  • The problems in the system implementation process;
  • The output of the syntactic analyzer analysis table;

附件 action.txt 與 goto.txt

  • For a test program output its syntactic analysis results;

測試程序

輸出結果:

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)
  • Output corresponding syntax error report for the test procedure;

第 28 行出現錯誤,進行報錯

Syntax error at Line [28]:illegal ==
  • 對實驗結果進行分析.

The experimental results is correct
const : 1 (31)
; (31)
Module (31)
} (32)
Module (30)


- Output corresponding syntax error report for the test procedure;
第 28 行出現錯誤,進行報錯
```c++
Syntax error at Line [28]:illegal ==
  • 對實驗結果進行分析.

The experimental results is correct


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved