File: presburger_analysis.c

package info (click to toggle)
mona 1.4-18-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 3,316 kB
  • sloc: ansic: 13,825; cpp: 12,615; sh: 4,569; makefile: 111; lisp: 48
file content (98 lines) | stat: -rw-r--r-- 2,494 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* presburger_analysis.c - DFA package example application */

/*
 * MONA
 * Copyright (C) 1997-2013 Aarhus University.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the  Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335,
 * USA.
 */

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "dfa.h"
#include "mem.h"

int decode_example(char *example, int row, int num_rows)
{
  /* decode one row of the example string */
  int i, val = 0, length = strlen(example)/(num_rows+1);
  for (i = length-1; i > 0; i--)
    val = val<<1 | (example[row*length+i] == '1');
  return val;
}

int main(int argc, char *argv[])
{
  char **vars;
  int *orders;
  DFA *a;
  char *example;
  unsigned indices[1];
  unsigned index;
  int i;

  if (argc != 3) {
    printf("usage: %s <dfa-file> <variable-name>\n", argv[0]);
    exit(-1);
  }

  /* initialize the BDD package */
  bdd_init();

  /* import the automaton */
  a = dfaImport(argv[1], &vars, &orders);
  if (!a) {
    printf("error: unable to import '%s'\n", argv[1]);
    exit(-1);
  }

  /* find the index */
  for (index = 0; vars[index]; index++)
    if (strcmp(vars[index], argv[2]) == 0)
      break;
  if (!vars[index]) {
    printf("error: '%s' not found in '%s'\n", argv[2], argv[1]);
    exit(-1);
  }

  /* 'dfaMakeExample' finds a string leading from the initial state
     to a nearest accepting state, 
     this string represents a binary encoded number for
     each free variable */
  indices[0] = index;
  example = dfaMakeExample(a, 1, 1, indices);
  
  /* print the result */
  if (!example)
    printf("relation is unsatisfiable!\n");
  else {
    printf("satisfying example:\n"
	   "%s = %d\n", 
	   argv[2], decode_example(example, 0, 1));
    mem_free(example);
  }

  /* clean up */
  dfaFree(a);

  for (i = 0; vars[i]; i++)
    mem_free(vars[i]);
  mem_free(vars);
  mem_free(orders);

  return 0;
}