Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of scm.gforge.inria.fr:/gitroot/simgrid/simgrid
[simgrid.git] / src / xbt / automaton / automatonparse_promela.c
1 /* methods for implementation of automaton from promela description */
2
3 /* Copyright (c) 2011-2017. The SimGrid Team. All rights reserved.          */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "src/internal_config.h"
9 #include "xbt/automaton.h"
10 #include <errno.h>
11 #include <string.h>   /* strerror */
12 #if HAVE_UNISTD_H
13 # include <unistd.h>   /* isatty */
14 #endif
15
16 static xbt_automaton_t parsed_automaton;
17 char* state_id_src;
18
19 static void new_state(char* id, int src){
20
21   char* id_copy = xbt_strdup(id);
22   char* first_part = strtok(id_copy,"_");
23   int type = 0 ; // -1=initial state; 0=intermediate state; 1=final state
24
25   if(strcmp(first_part,"accept")==0){
26     type = 1;
27   }else{
28     char* second_part = strtok(NULL,"_");
29     if(strcmp(second_part,"init")==0){
30       type = -1;
31     }
32   }
33   free(id_copy);
34
35   xbt_automaton_state_t state = xbt_automaton_state_exists(parsed_automaton, id);
36   if(state == NULL){
37     state = xbt_automaton_state_new(parsed_automaton, type, id);
38   }
39
40   if(type==-1)
41     parsed_automaton->current_state = state;
42
43   if(src) {
44     if (state_id_src)
45       free(state_id_src);
46     state_id_src = xbt_strdup(id);
47   }
48 }
49
50 static void new_transition(char* id, xbt_automaton_exp_label_t label)
51 {
52   new_state(id, 0);
53   xbt_automaton_state_t state_dst = xbt_automaton_state_exists(parsed_automaton, id);
54   xbt_automaton_state_t state_src = xbt_automaton_state_exists(parsed_automaton, state_id_src);
55
56   //xbt_transition_t trans = NULL;
57   xbt_automaton_transition_new(parsed_automaton, state_src, state_dst, label);
58
59 }
60
61 static xbt_automaton_exp_label_t new_label(int type, ...){
62   xbt_automaton_exp_label_t label = NULL;
63   xbt_automaton_exp_label_t left;
64   xbt_automaton_exp_label_t right;
65   xbt_automaton_exp_label_t exp_not;
66   char *p;
67
68   va_list ap;
69   va_start(ap,type);
70   switch(type){
71   case 0 :
72     left = va_arg(ap, xbt_automaton_exp_label_t);
73     right = va_arg(ap, xbt_automaton_exp_label_t);
74     label = xbt_automaton_exp_label_new(type, left, right);
75     break;
76   case 1 :
77     left = va_arg(ap, xbt_automaton_exp_label_t);
78     right = va_arg(ap, xbt_automaton_exp_label_t);
79     label = xbt_automaton_exp_label_new(type, left, right);
80     break;
81   case 2 :
82     exp_not = va_arg(ap, xbt_automaton_exp_label_t);
83     label = xbt_automaton_exp_label_new(type, exp_not);
84     break;
85   case 3 :
86     p = va_arg(ap, char*);
87     label = xbt_automaton_exp_label_new(type, p);
88     break;
89   case 4 :
90     label = xbt_automaton_exp_label_new(type);
91     break;
92   }
93   va_end(ap);
94   return label;
95 }
96
97 #include "parserPromela.tab.cacc"
98
99 void xbt_automaton_load(xbt_automaton_t a, const char *file)
100 {
101   parsed_automaton = a;
102   yyin = fopen(file, "r");
103   if (yyin == NULL)
104     xbt_die("Failed to open automaton file `%s': %s", file, strerror(errno));
105   yyparse();
106 }