Sujet : Re: help with a logic algorithm
De : thiago.adams (at) *nospam* gmail.com (Thiago Adams)
Groupes : comp.lang.cDate : 04. Apr 2024, 21:46:48
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <b20712de-d348-407a-8a60-ed1fa5b28d87@gmail.com>
References : 1 2
User-Agent : Mozilla Thunderbird
On 04/04/2024 13:06, jak wrote:
Thiago Adams ha scritto:
I need an algorithm that finds the possible states of variables used in "ifs"
>
for instance, I need to find out possible states:
>
if (a && b){
>
// 'a' is true
// 'b' is true
>
}
else
{
// 'a' maybe be true or false
// 'b' maybe be true or false
}
>
More complex:
>
if (a && b || c){
// 'a' maybe be true or false
// 'b' maybe be true or false
// 'c' maybe be true or false
}
else
{
// 'a' maybe be true or false
// 'b' maybe be true or false
// 'c' maybe be true or false
}
>
I think this algorithm may already exists.. but not finding it.
Maybe something related with predicates.
>
>
I'm not sure I understand exactly what you want to obtain,
but if I understand I can propose, to you, a simple idea:
#include <stdio.h>
int main()
{
#define SFMT "a:%-5.5s b: %-5.5s c: %-5.5s\n"
#define BIT(v, t) (v & t)
#define TST(v) ((v) ? "true" : "false")
unsigned char a = 1 << 0,
b = 1 << 1,
c = 1 << 2;
for(unsigned char i = 0; i <= 7; i++)
{
if(BIT(i, a) && BIT(i, b) || BIT(i, c))
{
printf("Then: " SFMT, TST(BIT(i, a)),
TST(BIT(i, b)),
TST(BIT(i, c)));
}
else
{
printf("Else: " SFMT, TST(BIT(i, a)),
TST(BIT(i, b)),
TST(BIT(i, c)));
}
}
return 0;
}
output:
Else: a:false b: false c: false
Else: a:true b: false c: false
Else: a:false b: true c: false
Then: a:true b: true c: false
Then: a:false b: false c: true
Then: a:true b: false c: true
Then: a:false b: true c: true
Then: a:true b: true c: true
the algorithm is be used in flow analysis of C code.
I realized my previous approach will not work because I also need intermediate result.
for instance
if (p && p->i) {}
I need to know p is not-null after && , I cannot wait until the end of expression. The algorithm must propagate the results.
I am thinking on alternatives..