KEMBAR78
Level of Program Correctness_Program_Reasoning.pptx
Levels of Program Correctness
19CSE205 : PROGRAM REASONING
Dr. Chandan Kumar & Mr. Hajarathaiah K.
Department of Computer Science and Engineering
Jul - Dec 2023
Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 1/ 10
Contents
1 Classifying correctness
2 Examples of error types
3 Role of static analyzers
4 Lexical correctness
5 Syntax correctness
6 Semantic correctness
7 Logic correctness
8 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
correctness
Syntax
correctness
Semantic
correctness
Logic
correctness
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
variety of useful information. In this case, we are 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
Dictionary
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.
area = breadth * 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
| . . .
declaration ← TYPE ID
(COMMA ID)*
SEMI
← ID ASSIGN
expr SEMI
← ID (op expr)*
← PLUS
| MINUS
assignment
expr
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-by-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 we interested in
lexical correctness? NO Compilers are good at this!
Are we interested in
syntax correctness? NO Compilers are good at this!
Are we interested in
semantic correctness? YES To a limited extent.
Are we 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

Level of Program Correctness_Program_Reasoning.pptx

  • 1.
    Levels of ProgramCorrectness 19CSE205 : PROGRAM REASONING Dr. Chandan Kumar & Mr. Hajarathaiah K. Department of Computer Science and Engineering Jul - Dec 2023 Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 1/ 10
  • 2.
    Contents 1 Classifying correctness 2Examples of error types 3 Role of static analyzers 4 Lexical correctness 5 Syntax correctness 6 Semantic correctness 7 Logic correctness 8 Focus of this course Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 2/ 10
  • 3.
    Classifying correctness Correctness isa 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 correctness Syntax correctness Semantic correctness Logic correctness 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
  • 4.
    Examples of errortypes (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
  • 5.
    Role of staticanalyzers Static analysis refers to the process of analyzing source code to derive variety of useful information. In this case, we are 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 Dictionary 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
  • 6.
    Lexical correctness Lexical correctnessis 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. area = breadth * 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
  • 7.
    Syntax correctness Syntax correctnessis 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 | . . . declaration ← TYPE ID (COMMA ID)* SEMI ← ID ASSIGN expr SEMI ← ID (op expr)* ← PLUS | MINUS assignment expr op | MULT | DIV int x, y; y = x + 23.65; Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 7/ 10
  • 8.
    Semantic correctness Semantic correctnessis 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-by-zero Memory faults File exceptions Use exception handling feature! Chandan Kumar & Hajarathaiah K. 19CSE205 : PROGRAM REASONING Jul - Dec 2023 8/ 10
  • 9.
    Logic correctness Logic correctnessimplies 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
  • 10.
    Focus of thiscourse Are we interested in lexical correctness? NO Compilers are good at this! Are we interested in syntax correctness? NO Compilers are good at this! Are we interested in semantic correctness? YES To a limited extent. Are we 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