summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/datamodel/promela/parser/promela.ypp
blob: 6147d4069c7e4095a6ee752ae6c6bb92c6f58760 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/** Subset extracted from spin.y by Stefan Radomski 2014                  */

/***** spin: spin.y *****/

/* Copyright (c) 1989-2003 by Lucent Technologies, Bell Laboratories.     */
/* All Rights Reserved.  This software is for educational purposes only.  */
/* No guarantee whatsoever is expressed or implied by the distribution of */
/* this code.  Permission is given to distribute this code provided that  */
/* this introductory message is not removed and no monies are exchanged.  */
/* Software written by Gerard J. Holzmann.  For tool documentation see:   */
/*             http://spinroot.com/                                       */
/* Send all bug-reports and/or questions to: bugs@spinroot.com            */

%{
#include "../PromelaParser.h"
#include "promela.tab.hpp"
#include <sys/types.h>
#include <stdarg.h>

#define YYMAXDEPTH	20000	// default is 10000
#define YYDEBUG		1
#define YYERROR_VERBOSE 1

extern int promela_lex (PROMELA_STYPE* yylval_param, void* yyscanner);

using namespace uscxml;
%}

%pure-parser
%debug
%file-prefix "promela"
%parse-param { uscxml::PromelaParser* ctx }
%lex-param {void * scanner}
%parse-param {void * scanner}
%define api.prefix promela_
%defines

%union {
  uscxml::PromelaParserNode* node;
	char* value;
}

%error-verbose

/* %type <node> expr_lst */
%type <node> expr pfld varref decl_lst stmnt_lst vardcl ivar var_list one_decl prargs
%type <node> stmnt Stmnt const_expr nlst vis arg

%token  VAR_ARRAY VARLIST DECL DECLLIST STMNT COLON EXPR NAMELIST

%token  '(' ')'
%token  '[' ']'
%token  '{' '}'
%token	ASSERT PRINT PRINTM
%token  <value> LEN STRING
%token  TYPEDEF MTYPE INLINE RETURN LABEL OF
%token  GOTO BREAK ELSE SEMI ARROW
%token  IF FI DO OD FOR SELECT IN SEP DOTDOT
%token  HIDDEN SHOW ISLOCAL
%token  <value> CONST TYPE XU			/* val */
%token  <value> NAME UNAME PNAME INAME		/* sym */
%token  CLAIM TRACE INIT	LTL	/* sym */
%token  COMMA

%right	ASGN
%left OR AND
%left	BITOR BITXOR BITAND
%left	EQ NE
%left	GT LT GE LE
%left	LSHIFT RSHIFT
%left	PLUS MINUS 
%left TIMES DIVIDE MODULO
%left	INCR DECR
%left COMPL
%right NEG
%left	DOT

%%


/** PROMELA Grammar Rules **/

program	: 
  decl_lst { 
    ctx->ast = $1; 
    ctx->type = PromelaParser::PROMELA_DECL; 
  }
  | expr { 
    ctx->ast = $1; 
    ctx->type = PromelaParser::PROMELA_EXPR;
  }
  | stmnt_lst { 
    ctx->ast = $1; 
    ctx->type = PromelaParser::PROMELA_STMNT;
  }
  ;

varref	: cmpnd			{}
	;

pfld	: NAME			{ $$ = ctx->value(NAME, yylval.value); }
	| NAME '[' expr ']' {}
	;

cmpnd	: pfld			{}
				sfld			{}
	;

sfld	: /* empty */		{}
	| DOT cmpnd %prec DOT	{}
	;

/*
expr_lst: expr       	{ $$ = ctx->node(EXPR, 1, $1); }
  | expr SEMI       	{ $$ = ctx->node(EXPR, 1, $1); }
	| expr SEMI expr_lst { $$ = ctx->node(EXPR, 2, $1, $3); }
	;
*/

expr    : '(' expr ')'		{ $$ = $2; }
	| expr PLUS expr		{ $$ = ctx->node(PLUS, 2, $1, $3); }
	| expr MINUS expr		{ $$ = ctx->node(MINUS, 2, $1, $3); }
	| expr TIMES expr		{ $$ = ctx->node(TIMES, 2, $1, $3); }
	| expr DIVIDE expr		{ $$ = ctx->node(DIVIDE, 2, $1, $3); }
	| expr MODULO expr		{ $$ = ctx->node(MODULO, 2, $1, $3); }
	| expr BITAND expr		{ $$ = ctx->node(BITAND, 2, $1, $3); }
	| expr BITXOR expr		{ $$ = ctx->node(BITXOR, 2, $1, $3); }
	| expr BITOR expr		{ $$ = ctx->node(BITOR, 2, $1, $3); }
	| expr GT expr		{ $$ = ctx->node(GT, 2, $1, $3); }
	| expr LT expr		{ $$ = ctx->node(LT, 2, $1, $3); }
	| expr GE expr		{ $$ = ctx->node(GE, 2, $1, $3); }
	| expr LE expr		{ $$ = ctx->node(LE, 2, $1, $3); }
	| expr EQ expr		{ $$ = ctx->node(EQ, 2, $1, $3); }
	| expr NE expr		{ $$ = ctx->node(NE, 2, $1, $3); }
	| expr AND expr		{ $$ = ctx->node(AND, 2, $1, $3); }
	| expr OR  expr		{ $$ = ctx->node(OR, 2, $1, $3); }
	| expr LSHIFT expr	{ $$ = ctx->node(LSHIFT, 2, $1, $3); }
	| expr RSHIFT expr	{ $$ = ctx->node(RSHIFT, 2, $1, $3); }
	| NEG expr		      { $$ = ctx->node(NEG, 1, $2); }
	| MINUS expr %prec MINUS	{ $$ = ctx->node(MINUS, 1, $2); }

	| LEN '(' varref ')'	{ $$ = ctx->node(LEN, 1, $3);  }
	| varref		{  }
	| CONST 		{ $$ = ctx->value(CONST, yylval.value); }
	;


vis	: /* empty */		{ $$ = ctx->node(SHOW, 0); }
	| HIDDEN		{ $$ = ctx->node(HIDDEN, 0); }
	| SHOW			{ $$ = ctx->node(SHOW, 0); }
	| ISLOCAL		{ $$ = ctx->node(ISLOCAL, 0); }
	;

one_decl: vis TYPE var_list	{ $$ = ctx->node(DECL, 3, $1, ctx->value(TYPE, $2), $3); }
	| vis UNAME var_list	{ $$ = ctx->node(UNAME, 2, $1, $3); }
	| vis TYPE ASGN '{' nlst '}' { $$ = ctx->node(DECL, 3, $1, ctx->value(TYPE, $2), $5); }
	;

decl_lst: one_decl            { $$ = $1; }
  | one_decl SEMI             { $$ = $1; }
  | one_decl SEMI decl_lst    {
    $$ = ctx->node(DECLLIST, 1, $1);
    if($3->type == DECLLIST) {
      $$->merge($3); delete $3; 
    } else {
      $$->push($3);
    }
  }
	;

var_list: ivar           { $$ = ctx->node(VARLIST, 1, $1); }
	| ivar COMMA var_list	{ $$ = ctx->node(VARLIST, 1, $1); $$->merge($3); delete $3; }
	;

ivar    : vardcl           	{ $$ = $1; }
	| vardcl ASGN expr   	{ $$ = ctx->node(ASGN, 2, $1, $3); }
	;

vardcl  : NAME  		{ $$ = ctx->value(NAME, $1); }
	| NAME COLON CONST	{ $$ = ctx->node(COLON, 2, ctx->value(NAME, $1), ctx->value(CONST, $3)); }
	| NAME '[' const_expr ']'	{ $$ = ctx->node(VAR_ARRAY, 2, ctx->value(NAME, $1), $3); }
	;

const_expr:	CONST			{ $$ = ctx->value(CONST, yylval.value); }
	| MINUS const_expr %prec MINUS	{ $$ = ctx->node(MINUS, 1, $2); }
	| '(' const_expr ')'		{ $$ = $2; }
	| const_expr PLUS const_expr	{ $$ = ctx->node(PLUS, 2, $1, $3); }
	| const_expr MINUS const_expr	{ $$ = ctx->node(MINUS, 2, $1, $3); }
	| const_expr TIMES const_expr	{ $$ = ctx->node(TIMES, 2, $1, $3); }
	| const_expr DIVIDE const_expr	{ $$ = ctx->node(DIVIDE, 2, $1, $3); }
	| const_expr MODULO const_expr	{ $$ = ctx->node(MODULO, 2, $1, $3); }
	;

nlst	: NAME			{ $$ = ctx->value(NAME, $1); }
	| nlst NAME 		{ 
    if ($1->type == NAME) {
      $$ = ctx->node(NAMELIST, 1, $1);
      $$->push(ctx->value(NAME, $2));
    } else {
      $1->push(ctx->value(NAME, $2));
    }
  }
	| nlst COMMA		{ $$ = $1; }
	;

stmnt_lst: stmnt       	{ $$ = $1; }
  | stmnt SEMI         	{ $$ = $1; }
	| stmnt SEMI stmnt_lst { $$ = ctx->node(STMNT, 2, $1, $3); }
	;

stmnt	: Stmnt			{ $$ = $1; }
	;

Stmnt	: varref ASGN expr	{ $$ = ctx->node(ASGN, 2, $1, $3); }
	| varref INCR		{ $$ = ctx->node(INCR, 1, $1); }
	| varref DECR		{ $$ = ctx->node(DECR, 1, $1); }
	| PRINT	'(' STRING prargs ')'		{ $$ = ctx->node(PRINT, 2, ctx->value(STRING, $3), $4); }
	| PRINTM '(' varref ')'	{ $$ = ctx->node(PRINTM, 1, $3); }
	| PRINTM '(' CONST ')'	{ $$ = ctx->node(PRINTM, 1, ctx->value(CONST, $3)); }
	| ASSERT expr    	{  }
	| expr		{ $$ = $1; }
	| varref ASGN INAME	{  } '(' args ')' Stmnt			{  }
	;

args    : /* empty */		{  }
	| arg			{  }
	;

prargs  : /* empty */		{ $$ = ctx->value(0, ""); }
	| COMMA arg		{ $$ = $2; }
	;

arg     : expr			{ $$ = $1; }
	| expr COMMA arg		{ $$ = ctx->node(0, 2, $1, $3); }
	;


%%