il.l 6.2 KB
Newer Older
Jeroen Vreeken's avatar
Jeroen Vreeken committed
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
/*
	Copyright Jeroen Vreeken (pe1rxq@amsat.org), 2009
	Copyright Stichting C.A. Muller Radioastronomiestation, 2009

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */

%option noinput

%{
#include <stdio.h>
#include <inttypes.h>
#include <ctype.h>
#include "il.tab.h"

static void __attribute__ ((unused)) yyunput (int c,char *buf_ptr  );

%}

digit	[0-9]
letter	[a-zA-Z]
id	[a-zA-Z0-9_]+
number	[0-9]+

intnumber	[+-]*{number}

realsimple	[+-]?{number}"."{number} 
realexp		[+-]?{number}"."?{number}?"e"[+-]*{number} 
real		{realsimple}|{realexp}

binarynumber	"2#"[01_]+
octalnumber	"8#"[0-7]+
hexnumber	"16#"[0-9a-fA-F]+

literal		{intnumber}|{real}|{binarynumber}|{octalnumber}|{hexnumber}

timeday		[0-9]+("d"|"D")
timehour	[0-9]+("h"|"H")
timeminute	[0-9]+("m"|"M")
timesecond	[0-9]+("s"|"S")
timemsecond	[0-9]+("ms")
time		[tT]"#"{timeday}?{timehour}?{timeminute}?{timesecond}?{timemsecond}?

vartype		"BOOL"|"BYTE"|"WORD"|"DWORD"|"SINT"|"USINT"|"INT"|"UINT"|"DINT"|"UDINT"|"LINT"|"ULINT"|"REAL"|"LREAL"|"TIME"
varpref		("_TO_"{vartype})*|("#"{literal})*

%%

"PROGRAM"	{ return PROGRAMSYM; }

"SYSTEM_TIME"	{ return SYSTEM_TIMESYM; }


"VAR"		{ return VARSYM; }
"VAR_INPUT"	{ return VAR_INPUTSYM; }
"VAR_OUTPUT"	{ return VAR_OUTPUTSYM; }
"VAR_CONFIG"	{ return VAR_CONFIGSYM; }
"END_VAR"	{ return END_VARSYM; }
"BOOL"/{varpref}	{ return BOOLSYM; }
"BYTE"/{varpref}	{ return UINT8SYM; }
"WORD"/{varpref}	{ return UINT16SYM; }
"DWORD"/{varpref}	{ return UINT32SYM; }
"SINT"/{varpref}	{ return SINT8SYM; }
"USINT"/{varpref}	{ return UINT8SYM; }
"INT"/{varpref}		{ return SINT16SYM; }
"UINT"/{varpref}	{ return UINT16SYM; }
"DINT"/{varpref}	{ return SINT32SYM; }
"UDINT"/{varpref}	{ return UINT32SYM; }
"LINT"/{varpref}	{ return SINT64SYM; }
"ULINT"/{varpref}	{ return UINT64SYM; }
"REAL"/{varpref}	{ return REAL32SYM; }
"LREAL"/{varpref}	{ return REAL64SYM; }
"TIME"/{varpref}	{ return UINT32SYM; }

"LD"		{ return LDSYM; }
"ST"		{ return STSYM; }
"LDN"		{ return LDNSYM; }
"STN"		{ return STNSYM; }
"AND"		{ return ANDSYM; }
"OR"		{ return ORSYM; }
"ANDN"		{ return ANDNSYM; }
"ORN"		{ return ORNSYM; }
"XOR"		{ return XORSYM; }
"JMP"		{ return JMPSYM; }
"JMPC"		{ return JMPCSYM; }
"JMPCN"		{ return JMPCNSYM; }
"ADD"		{ return ADDSYM; }
"SUB"		{ return SUBSYM; }
"MUL"		{ return MULSYM; }
"DIV"		{ return DIVSYM; }
"MOD"		{ return MODSYM; }
"GT"		{ return GTSYM; }
"EQ"		{ return EQSYM; }
"NE"		{ return NESYM; }
"LE"		{ return LESYM; }
"LT"		{ return LTSYM; }
"S"		{ return SSYM; }
"R"		{ return RSYM; }
"N"		{ return NEGATESYM; }
"("		{ return BEGINPARSYM; }
")"		{ return ENDPARSYM; }

"ABS"		{ return ABSSYM; }
"SQRT"		{ return SQRTSYM; }
"LN"		{ return LNSYM; }
"LOG"		{ return LOGSYM; }
"EXP"		{ return EXPSYM; }
"SIN"		{ return SINSYM; }
"COS"		{ return COSSYM; }
"TAN"		{ return TANSYM; }
"ASIN"		{ return ASINSYM; }
"ACOS"		{ return ACOSSYM; }
"ATAN"		{ return ATANSYM; }
"EXPT"		{ return EXPTSYM; }

"SHL"		{ return SHLSYM; }
"SHR"		{ return SHRSYM; }
"ROL"		{ return ROLSYM; }
"ROR"		{ return RORSYM; }
"NOT"		{ return NOTSYM; }

"MIN"		{ return MINSYM; }
"MAX"		{ return MAXSYM; }
"LIMIT"		{ return LIMITSYM; }
"SEL"		{ return SELSYM; }

"TRUNC"		{ return TRUNCSYM; }

":="		{ return LETSYM; }
","		{ return COMMASYM; }

"%"{id}		{ yylval.string = strdup(yytext+1); return VARIABLE; }

{time}		{ 
			uint32_t time = 0;
			int numval = 0;
			char *tmp;
			
			for (tmp = yytext + 2; *tmp; tmp++) {
				if (isdigit(*tmp)) {
					numval = numval * 10 + *tmp - '0';
				} else {
					switch (*tmp) {
						case 'd':
						case 'D':
							time += numval * 
							    (24*3600*1000);
							break;
						case 'h':
						case 'H':
							time += numval * 
							    (3600*1000);
							break;
						case 'm':
						case 'M':
							if (tmp[1] == 's' ||
							    tmp[1] == 'S') {
								tmp++;
								time += numval;
							} else {
								time += numval *
								    (60*1000);
							}
							break;
						case 's':
						case 'S':
							time += numval * 1000;
							break;
					}
					numval = 0;
				}
			}
			yylval.string = malloc(12);
			sprintf(yylval.string, "%d", time);
			return LITERALUINT;
		}

{binarynumber}	{
			uint32_t binnum = 0;
			
			char *tmp;
			
			for (tmp = yytext + 2; *tmp; tmp++) {
				switch (*tmp) {
					case '1':
						binnum = binnum << 1;
						binnum |= 1;
						break;
					case '0':
						binnum = binnum << 1;
						break;
					case '_':
						break;
				}
			}
			yylval.string = malloc(12);
			sprintf(yylval.string, "0x%08x", binnum);
			
			return LITERALUINT; 
		}

{real}		{ yylval.string = strdup(yytext); return LITERALREAL; }

{octalnumber}	{
			yylval.string = strdup(yytext+1);
			yylval.string[0] = '0';
			return LITERALUINT;
		}

{hexnumber}	{
			yylval.string = strdup(yytext+1);
			yylval.string[0] = '0';
			yylval.string[1] = 'x';
			return LITERALUINT;
		}

{intnumber}	{ yylval.string = strdup(yytext); return LITERALSINT; }
"TRUE"		{ yylval.string = strdup("true"); return LITERALUINT; }
"FALSE"		{ yylval.string = strdup("false"); return LITERALUINT; }

{id}[ \t]*":"/[^=]	{ 
			yylval.string = strdup(yytext);
			/* cut off trailing semi-colon and whitespace */
			while (yylval.string[strlen(yylval.string)-1] == ' ' ||
			       yylval.string[strlen(yylval.string)-1] == '\t'||
			       yylval.string[strlen(yylval.string)-1] == ':')
				yylval.string[strlen(yylval.string)-1] = 0;
			return LABEL;
		}

"_TO_"/{vartype}	{
	 		  return TYPECONVERSIONSYM; 
		}

"#"/{literal}	{ return TYPEDLITERALSYM; }

{id}		{ yylval.string = strdup(yytext); return VARIABLE; }

";"		/* skip */

"//".+[\n\r]	/* skip comments */
"/*".+"*/"	/* skip comments */
"(*".+"*)"	/* skip comments */
[ \t\r]		/* skip whitespace */
"\n"		{ yyset_lineno(yyget_lineno()+1);}

%%

int yywrap(void){return 1;}