Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'actor-yield' of github.com:Takishipp/simgrid into actor-yield
[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 #include <xbt/log.h>
16
17 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(xbt_automaton);
18
19 static xbt_automaton_t parsed_automaton;
20 char* state_id_src;
21
22 static void new_state(char* id, int src){
23   char* saveptr = NULL; // for strtok_r()
24   char* id_copy = xbt_strdup(id);
25   char* first_part = strtok_r(id_copy, "_", &saveptr);
26   int type = 0 ; // -1=initial state; 0=intermediate state; 1=final state
27
28   if(strcmp(first_part,"accept")==0){
29     type = 1;
30   }else{
31     char* second_part = strtok_r(NULL, "_", &saveptr);
32     if(strcmp(second_part,"init")==0){
33       type = -1;
34     }
35   }
36   free(id_copy);
37
38   xbt_automaton_state_t state = xbt_automaton_state_exists(parsed_automaton, id);
39   if(state == NULL){
40     state = xbt_automaton_state_new(parsed_automaton, type, id);
41   }
42
43   if(type==-1)
44     parsed_automaton->current_state = state;
45
46   if(src) {
47     if (state_id_src)
48       free(state_id_src);
49     state_id_src = xbt_strdup(id);
50   }
51 }
52
53 static void new_transition(char* id, xbt_automaton_exp_label_t label)
54 {
55   new_state(id, 0);
56   xbt_automaton_state_t state_dst = xbt_automaton_state_exists(parsed_automaton, id);
57   xbt_automaton_state_t state_src = xbt_automaton_state_exists(parsed_automaton, state_id_src);
58
59   //xbt_transition_t trans = NULL;
60   xbt_automaton_transition_new(parsed_automaton, state_src, state_dst, label);
61
62 }
63
64 static xbt_automaton_exp_label_t new_label(int type, ...){
65   xbt_automaton_exp_label_t label = NULL;
66   xbt_automaton_exp_label_t left;
67   xbt_automaton_exp_label_t right;
68   xbt_automaton_exp_label_t exp_not;
69   char *p;
70
71   va_list ap;
72   va_start(ap,type);
73   switch(type){
74   case 0 :
75     left = va_arg(ap, xbt_automaton_exp_label_t);
76     right = va_arg(ap, xbt_automaton_exp_label_t);
77     label = xbt_automaton_exp_label_new(type, left, right);
78     break;
79   case 1 :
80     left = va_arg(ap, xbt_automaton_exp_label_t);
81     right = va_arg(ap, xbt_automaton_exp_label_t);
82     label = xbt_automaton_exp_label_new(type, left, right);
83     break;
84   case 2 :
85     exp_not = va_arg(ap, xbt_automaton_exp_label_t);
86     label = xbt_automaton_exp_label_new(type, exp_not);
87     break;
88   case 3 :
89     p = va_arg(ap, char*);
90     label = xbt_automaton_exp_label_new(type, p);
91     break;
92   case 4 :
93     label = xbt_automaton_exp_label_new(type);
94     break;
95   default:
96     XBT_DEBUG("Invalid type: %d", type);
97     break;
98   }
99   va_end(ap);
100   return label;
101 }
102
103 #include "parserPromela.tab.cacc"
104
105 void xbt_automaton_load(xbt_automaton_t a, const char *file)
106 {
107   parsed_automaton = a;
108   yyin = fopen(file, "r");
109   if (yyin == NULL)
110     xbt_die("Failed to open automaton file `%s': %s", file, strerror(errno));
111   yyparse();
112 }