C堆栈上的Frama-C和ACSL实现

时间:2015-09-02 08:38:53

标签: c frama-c

我在ACSL中将前置条件和后置条件实现到我的代码中时遇到了一些麻烦。

我想完成一个练习,我已经实现了一个堆栈(就像评论中的非正式规范),现在我需要像非正式规范中那样实现前置条件和后置条件。我遇到的麻烦是我试图编写ACSL部分并试图用我的实现编译frama-C但是它没有用,所有的行都带有红点。

我不明白为什么它不起作用,但我很可能无法编写ACSL代码。

有人能帮帮我吗?如果有人可以在我的堆栈实现上写一些例子,那将对我有很大帮助。

非常感谢

我编写的代码, 文件Stack.h是Stack的实现,我写了一个简单的线性main.c来证明代码的正确性(在测试用例中)。练习的非正式细节是stack.h代码之前的注释

File Stack.h:

/*  create_stack

    Inputs: none
    Outputs: S (a stack)
    Preconditions: none
    Postconditions: S is defined and empty

destroy_stack

    Inputs: S (a stack)
    Outputs: S' (i.e. S changed)
    Preconditions: none
    Postconditions: S' is undefined. All resources (e.g. memory)   allocated to S have been released. No stack operation can be performed on S'.

is_empty

    Inputs: S (a stack)
    Outputs: is_empty (boolean)
    Preconditions: none
    Postconditions: is_empty is true iff S is empty.

top

    Inputs: S (a stack)
    Outputs: E (a stack element)
    Preconditions: S is not empty
    Postconditions: E is the top element on S (S is unchanged)

 pop

    Inputs: S (a stack)
    Outputs: S' (i.e. S is changed)
    Preconditions: S is not empty
    Postconditions: Because S is not empty, it consist of two parts: a top element T and a stack R of remaining elements. S'=R.

push

    Inputs: S (a stack) and V (a value)
    Outputs: S' (i.e. S changed)
    Preconditions: V is of appropriate type for an element of S
    Postconditions: S' has V as its top element and S as its remaining */

#define STACK_MAX 100

struct Stack {
int     data[STACK_MAX];
int     size;
};
typedef struct Stack Stack;


Stack *Stack_Init()
{
Stack *S = malloc(sizeof(Stack));   //alloco la memoria per il puntatore
assert(S != NULL);          //controllo che non sia null
S->size = 0;            //inizializzo il puntatore
return S;               //ritorno il puntatore
}

void Stack_Destroy(Stack *S)
{
free(S);
}

int Stack_Is_Empty(Stack *S)
{
if (S->size == 0) 
return 1;
else 
return 0; 
}

int Stack_Top(Stack *S)
{

return S->data[S->size-1];      //restituisco l'elemento in testa
}

void Stack_Pop(Stack *S)
{

    S->size--;
}

void Stack_Push(Stack *S, int d)
{
if (S->size < STACK_MAX)
    S->data[S->size++] = d;
else
    fprintf(stderr, "Error: stack full\n");
}

文件main.c:

    #include <stdio.h>
    #include <assert.h>
    #include <stdlib.h>
    #include "stack.h"

    int main (){

    struct Stack* S = Stack_Init();  //chiamo la funzione che mi ritorna il puntatore alla memoria di una struttura Stack inizializzata a 0 con dim massima 100
    int p;
    printf("che elemento vuoi inserire?\n");
    scanf("%d", &p);
    Stack_Push(S,p);
    printf("inseriscine un altro \n");
    scanf("%d", &p);
    Stack_Push(S,p);
    printf("l'elemento che ora è in testa è %d \n", Stack_Top(S));
    printf("togliamone uno\n");
    Stack_Pop(S);
    printf("l'elemento che ora è in testa è %d \n", Stack_Top(S));
    printf("lo stack e vuoto ? \n %d \n", Stack_Is_Empty(S));
    printf("togliamone un altro\n");
    Stack_Pop(S);
    printf("ora lo stack e vuoto ? \n %d \n", Stack_Is_Empty(S));
    printf("distruggiamo lo stack \n");
    Stack_Destroy(S);

    return 0;
    }

0 个答案:

没有答案