Logo AND Algorithmique Numérique Distribuée

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