aboutsummaryrefslogtreecommitdiff
path: root/gfsm/gfsm/src/libgfsm/tests/compretest.y
blob: d55f4880c4b9008d09298509953b8d0774bea3e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*======================================================================
 * Bison Options
 */
%pure_parser

%{
/*======================================================================
 * Bison C Header
 */
#include <gfsmRegexCompiler.h>
#include <gfsmAutomatonIO.h>

#include "compretest.tab.h"
#include "compretest.lex.h"

#define my_compiler ((gfsmRegexCompiler*)reparser)

#define YYLEX_PARAM   ((gfsmRegexCompiler*)reparser)->scanner.yyscanner
#define YYPARSE_PARAM reparser

#define YYERROR_VERBOSE 1
#define compretest_yyerror(msg) \
  gfsm_scanner_carp((gfsmScanner*)reparser, (msg));  

%}

/*======================================================================
 * Bison Definitions
 */
%union {
   gfsmAutomaton *fsm; //-- automaton
   GString       *gs;  //-- needs to be freed by hand
   gchar          c;
   guint32        u;
   gfsmWeight     w;
}

%token <c>      TOK_UNKNOWN TOK_CHAR
%token <u>      TOK_UINT
%token <gs>     TOK_STRING
%token <w>      TOK_WEIGHT

%type  <u>      label
%type  <w>      weight
%type  <fsm>    regex

/*
empty { $$=gfsm_regex_automaton_epsilon(my_compiler); }
*/

/*
	|	gfsmRETChar %prec LAB
		{ $$=gfsm_regex_automaton_lab(my_compiler, $1); }
*/

// -- Operator precedence and associativity
%left CONCAT
%left LABCONCAT
%left WEIGHT
%right '%'             //-- non-AT&T: rmepsilon:   % REGEX
%right '$'             //-- non-AT&T: determinize: $ REGEX
%right '~'             //-- non-AT&T: connect:     ~ REGEX
%left '*' '+' '?' '^'
%right '!'
%left '@'
%left ':'
%left '-'
%left '&'
%left '|'

/*======================================================================
 * Bison Rules
 */
%%

regex:		'('regex ')'
		{ $$=$2; }

	|	label
		{ $$=gfsm_regex_compiler_label_fsm(my_compiler, $1); }

	|	label regex %prec LABCONCAT
		{ $$=gfsm_regex_compiler_prepend_lab(my_compiler, $1, $2); }

	|	regex regex %prec CONCAT
		{ $$=gfsm_regex_compiler_concat(my_compiler, $1, $2); }

	|	'%' regex
		{ $$=gfsm_regex_compiler_rmepsilon(my_compiler, $2); /* non-ATT */ }

	|	'$' regex
		{ $$=gfsm_regex_compiler_determinize(my_compiler, $2); /* non-ATT */ }

	|	'~' regex
		{ $$=gfsm_regex_compiler_connect(my_compiler, $2); /* non-ATT */ }

	|	regex '*'
		{ $$=gfsm_regex_compiler_closure(my_compiler,$1,FALSE); }

	|	regex '+'
		{ $$=gfsm_regex_compiler_closure(my_compiler,$1,TRUE); }

	|	regex '^' TOK_UINT
		{ $$=gfsm_regex_compiler_power(my_compiler,$1,$3); }

	|	regex '?'
		{ $$=gfsm_regex_compiler_optional(my_compiler,$1); }

	|	'!' regex
		{ $$=gfsm_regex_compiler_complement(my_compiler,$2); }

	|	regex '|' regex
		{ $$=gfsm_regex_compiler_union(my_compiler,$1,$3); }

	|	regex '&' regex
		{ $$=gfsm_regex_compiler_intersect(my_compiler,$1,$3); }

	|	regex ':' regex
		{ $$=gfsm_regex_compiler_product(my_compiler,$1,$3); }

	|	regex '@' regex
		{ $$=gfsm_regex_compiler_compose(my_compiler,$1,$3); }

	|	regex '-' regex
		{ $$=gfsm_regex_compiler_difference(my_compiler,$1,$3); }

	|	regex weight %prec WEIGHT
		{ $$=gfsm_regex_compiler_weight(my_compiler,$1,$2); }
		;

label:		TOK_CHAR
		{ $$=gfsm_regex_compiler_char2label(my_compiler, $1); }

	|	TOK_STRING
		{ $$=gfsm_regex_compiler_gstring2label(my_compiler, $1); }

	|	'[' TOK_STRING ']'
		{ $$=gfsm_regex_compiler_gstring2label(my_compiler, $2); }
	;

weight:		'<' TOK_WEIGHT '>' { $$=$2; }
	;

%%

/*======================================================================
 * User C Code
 */

int main (int argc, char **argv) {
  gfsmRegexCompiler *reparser = g_new0(gfsmRegexCompiler,1);
  gfsm_scanner_init((gfsmScanner*)reparser, "gfsmRegexCompiler", compretest_yy);


  //-- initialization
  reparser->srtype = gfsmSRTTropical;
  reparser->gstr   = g_string_new("");
  reparser->abet   = gfsm_string_alphabet_new();
  if (!gfsm_alphabet_load_filename(reparser->abet, "test.lab", &(reparser->scanner.err))) {
      g_printerr("%s: load failed for labels file '%s': %s\n",
		 *argv, "test.lab", (reparser->scanner.err ? reparser->scanner.err->message : "?"));
      exit(2);
  }

  //-- debug: lexer
  reparser->scanner.emit_warnings = TRUE;

  //-- parse
  compretest_yyparse(reparser);

  //-- sanity check
  if (reparser->scanner.err) {
    fprintf(stderr, "%s: %s\n", *argv, reparser->scanner.err->message);
  }

  if (reparser->fsm) {
    gfsm_automaton_save_bin_file(reparser->fsm, stdout, NULL);
  } else {
    fprintf(stderr, "%s: Error: no fsm!\n", *argv);
  }

  gfsm_scanner_free((gfsmScanner*)reparser);
 
  return 0;
}