Level of Program Correctness_Program_Reasoning.pptx

chandankumar364348 15 views 10 slides May 07, 2024
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

Level of Program Correctness


Slide Content

Levels of Program Correctness 19CSE205 : PROGRAM REASONING Dr. Chandan Kumar & Mr. Hajarathaiah K. Department of Computer Science and Engineering Jul - Dec 202 3 Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 1 / 10

Contents 1 Classifying correctness 2 Examples of error types Role of static analyzers Lexical correctness Syntax c o r r e ctness Semantic correctness Logic correctness Focus of this course Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 2 / 10

Classifying correctness Correctness is a relative term. It indicates absence of errors in programs. Based on the types of errors in a program, correctness can be classifed into following levels. Lexical c o rrectness Syntax c o rrectness Semantic c o rrectness Logic c o rrectness 1 Lexical correctness refers to well-formedness of individual words in a program. Syntax correctness refers to well-formedness of each statement in a program. Semantic correctness refers to meaningfulness between different part of code or environment . Logic correctness refers to correctness with respect to program’s goal/objective . 2 3 4 Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 3 / 10

Examples of error types ( The examples are based on C programming language context ) Examples 23ab $? Lexical errors An ill-formed word/lexeme The compiler catches them Syntax errors An ill-formed statement The compiler catches them a + b = c; if (a == b) else a = b; Semantic errors An action out of context The compiler may catch them int x; . . . . . . x = ”hello”; int * p; *p = 5; FILE * f = fopen(”ab.c”,”w”); Or result in runtime error Note: ab.c may not exist Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 4 / 10

Role of static analyzers Static analysis refers to the process of analyzing source code to derive v a rie ty of useful inf o rmation. In this case, w e a re interested in ascertaining the correctness. The program is first turned into one or more data structure(s) and analysis is carried out. Data structures employed are some form or variants of Stack Tree Graph Diction a ry Static analyzers are usually automated . It takes the program source as input and spits out the inferences. A compiler is a good example of static analyzer. We will look briefly at how compilers catch these errors. Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 5 / 10 5 / 10

Lexical correctness Lexical correctness is accomplished by a graph, known as finite state automaton , which attempts to recognize each lexeme of the program, one by one, based on its structure. If recognized, the lexeme is classified. If not, compiler flags an error. a rea = b readth * height / 2; 1 2 4 3 5 a-z | a-z | | 0-9 0-9 0-9 . 0-9 0-9 Lexeme Token area IDENTIFIER = ASSIGN breadth IDENTIFIER * MULT height IDENTIFIER / DIV 2 INT CONST ; SEMI Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 6 / 10

Syntax correctness Syntax correctness is accomplished by representing the lexicalized source code in the form of a tree, known as parse tree , and checking if it adheres to syntax specifications of the language. program statement ← statement* ← declaration | assignment | . . . decl a ratio n ← TYPE ID (COMMA ID)* SEMI ← ID ASSIGN expr SEMI ← ID (op expr)* ← PLUS | MINUS assignment ex p r op | MULT | DIV int x, y; y = x + 23.65; Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 7 / 10

Semantic correctness Semantic correctness is accomplished by (i) a tree, known as Abstract Syntax Tree (AST) and (ii) a look-up table, known as Symbol Table . AST is a simplified version of parse tree. Compilers can ascertain only Sample program int x, y; y = x * 2; x is not initialized. So y cannot be computed. Assume default value or flag error/warning. Abstract Syntax Tree Symbol table var type value x int ? y int ? partial semantic correctness. Type mismatch Undeclared variable Uninitialized variable Function call & definition signature mistmatch Other errors slip into runtime. Division- b y-zero Memory faults File exceptions Use exception handling feature! Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 8 / 10

Logic correctness Logic correctness implies program exhibits ”correct” functionality or behavior. Errors in program logic does not result in compile time or runtime errors usually. An example: Computing factorial int factorial(int n) { int fact = 1; for (int i=2; i < =n; i++) fact = fact + i; return fact; } What is the flaw in this logic? There are million things that could go wrong in program logic! Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2020 9 / 10 Jul - Dec 2023 9 / 10

Focus of this course Are w e interested in lexical correctness? NO Compilers are good at this! Are w e interested in syntax correctness? NO Compilers are good at this! Are w e interested in semantic correctness? YES To a limited extent. Are w e interested in logic correctness? YES Main focus of this course! Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2020 10 / 10 Jul - Dec 2023 10 / 10
Tags