Logo AND Algorithmique Numérique Distribuée

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