Logo AND Algorithmique Numérique Distribuée

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