Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
d62c20dabd1724bb3dc6f53d4ac48356cb15c0d4
[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_state = xbt_strdup(id);
23   char* first_part = strtok(id,"_");
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
35   xbt_automaton_state_t state = NULL;
36   state = xbt_automaton_state_exists(parsed_automaton, id_state);
37   if(state == NULL){
38     state = xbt_automaton_state_new(parsed_automaton, type, id_state);
39   }
40
41   if(type==-1)
42     parsed_automaton->current_state = state;
43
44   if(src)
45     state_id_src = xbt_strdup(id_state);
46     
47 }
48
49 static void new_transition(char* id, xbt_automaton_exp_label_t label){
50
51   char* id_state = xbt_strdup(id);
52   xbt_automaton_state_t state_dst = NULL;
53   new_state(id, 0);
54   state_dst = xbt_automaton_state_exists(parsed_automaton, id_state);
55   xbt_automaton_state_t state_src = xbt_automaton_state_exists(parsed_automaton, state_id_src);
56   
57   //xbt_transition_t trans = NULL;
58   xbt_automaton_transition_new(parsed_automaton, state_src, state_dst, label);
59
60 }
61
62 static xbt_automaton_exp_label_t new_label(int type, ...){
63   xbt_automaton_exp_label_t label = NULL;
64   va_list ap;
65   va_start(ap,type);
66   switch(type){
67   case 0 : {
68     xbt_automaton_exp_label_t left = va_arg(ap, xbt_automaton_exp_label_t);
69     xbt_automaton_exp_label_t right = va_arg(ap, xbt_automaton_exp_label_t);
70     label = xbt_automaton_exp_label_new(type, left, right);
71     break;
72   }
73   case 1 : {
74     xbt_automaton_exp_label_t left = va_arg(ap, xbt_automaton_exp_label_t);
75     xbt_automaton_exp_label_t right = va_arg(ap, xbt_automaton_exp_label_t);
76     label = xbt_automaton_exp_label_new(type, left, right);
77     break;
78   }
79   case 2 : {
80     xbt_automaton_exp_label_t exp_not = va_arg(ap, xbt_automaton_exp_label_t);
81     label = xbt_automaton_exp_label_new(type, exp_not);
82     break;
83   }
84   case 3 : {
85     char* p = va_arg(ap, char*);
86     label = xbt_automaton_exp_label_new(type, p);
87     break;
88   }
89   case 4 : {
90     label = xbt_automaton_exp_label_new(type);
91     break;
92   }
93   }
94   va_end(ap);
95   return label;
96 }
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 }