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

Implementing semantic analysis based on Python

編輯:Python

1. Requirements Analysis

Can analyze the following types of statements, build symbol tables and generate intermediate code (three-address instructions and quaternary form):

  • Declaration statements (including variable declarations, array declarations, record declarations, and procedure declarations)
  • Expressions and assignment statements (including reference and assignment of array elements)
  • Branch statement: if_then_else
  • Loop statement: do_while
  • procedure call statement

Able to identify semantic errors in test cases, including

  • Variables (including arrays, pointers, structures) or procedures are used undeclared
  • Variable (including array, pointer, structure) or procedure name repeated declaration
  • The types of operands do not match (also the types of expressions on both sides of the assignment number do not match)
  • Type mismatch between
  • operator and operand
    • An rvalue-only expression appears to the left of the assignment number
    • Array subscript is not an integer
    • Use the array access operator for non-array variables
    • Use the "." operator for non-struct type variables
    • Use the procedure call operator for non-procedure names
    • Procedure call does not match the type or number of arguments
    • The function return type is wrong

It gives the exact location of the error.The format of the output error message is as follows: Semantic error at Line [line number]: [description text]

2. Grammar Design

Requirement: Give the semantic actions corresponding to the following language components

  • Declaration statements (including variable declarations, array declarations, record declarations, and procedure declarations)

  • Expressions and assignment statements (including reference and assignment of array elements)

  • Branch statement: if_then_else

  • Loop statement: do_while

  • procedure call statement

3. System Design

Requirements: It is divided into system outline design and system detailed design.

  • System outline design: Provide the necessary macro-level design diagrams of the system, such as system frame diagrams, data flow diagrams, functional module structure diagrams, etc., as well as corresponding text descriptions.

  • Detailed system design: expand the description of the following work

Design of core data structure

This structure is the node of the parse tree, which is used to store the content, attribute, depth and child node information of the word.

Supports returning all instances of all classes.When returning attribute information, if the node does not have the attribute, an error will be reported.

Sub-node information and attribute information can be added.

This structure is a quadruple, which is used to store the content of the quadruple.

Analysis operations on quadruples are also included

4. System implementation and result analysis

Requirements: Expand a description of the following.

  • Problems encountered during system implementation;

The system has no way to deal with variables used without declaration.Because the dictionary is used when storing the attributes of the variable, when the variable is called, if the variable is not declared, then there is no it in the symbol table, and there is no key about its dictionary, then the program will report a keyerror error.

  • Output its semantic analysis results for a test program;

The test sample is as follows:

struct student {int age;}int sum(int x, int y) {int ret;ret = x + y;return ret;}int main() {float a;a = 1.567;int b;int[5][5] c;c[1][1] = 10;b = c[1][1];int d;int *e;d = sum(b, c[1][1]);if(d > 10) {a = 1.0;} else {a = 2.0;}while(b < 10) {d = d + 1;b = b + 1;}return 0;}

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