aboutsummaryrefslogtreecommitdiff
path: root/pkg/utilities/nttools/tunits/parseunits.x
blob: 1a42078557435a68669cbd9f47fe1445232de748 (plain) (blame)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
include	<ctype.h>

#* B.Simon	??		original
#  Phil Hodge	12-Jul-2005	add 'bool yydebug' and 'int get_token()' to
#				parse_units

define	YYMAXDEPTH	32
define	YYOPLEN		1
define	yyparse		unit_parse

define	SZ_SHORTSTR	31

define	Y_WRONG		257
define	Y_DONE		258
define	Y_LPAR		259
define	Y_RPAR		260
define	Y_CU		261
define	Y_SQ		262
define	Y_ID		263
define	Y_NUM		264
define	Y_DIV		265
define	Y_MUL		266
define	Y_POW		267
define	yyclearin	yychar = -1
define	yyerrok		yyerrflag = 0
define	YYMOVE		call amovi (Memi[$1], Memi[$2], YYOPLEN)
define	YYERRCODE	256

# line 159 "parseunits.y"


# PARSE_UNITS -- Parse a units string into the internal format

pointer procedure parse_units (ab, units)

pointer	ab		# i: abbreviation hash table
char	units[ARB]	# i: expression to be parsed
#--
include "parseunits.com"

int	len, fd
pointer	sp

string	syntax	"Syntax error in units"

bool	yydebug
int	strlen(), stropen(), yyparse()
int	get_token()
extern	get_token

begin
	len = strlen (units) + 1
	fd = stropen (units, len, READ_ONLY)

	call smark (sp)
	call salloc (tokbuf, SZ_FNAME, TY_CHAR)

	debug = NO
	yydebug = (debug == YES)
	nxttok = 0
	abrev = ab
	tun = NULL	

	if (yyparse (fd, yydebug, get_token) == ERR)
	    call tuniterr (syntax, units)

	call close (fd)
	call sfree (sp)
	return (tun)
end

# GET_TOKEN -- Retrieve next token from units string

int procedure get_token (fd, value)

int	fd		# i: File containing expression to be lexed
pointer	value		# o: Address on parse stack to store token
#--
include	"parseunits.com"

char	ch
int	type, index, powers[4]
pointer	sp, typename, token

string	pownames  "sq,square,cu,cubic"
data	powers    / Y_SQ, Y_SQ, Y_CU, Y_CU /

bool	streq()
int	getc(), word_match()

begin
	call smark (sp)
	call salloc (typename, SZ_FNAME, TY_CHAR)

	token = tokbuf + nxttok
	Memi[value] = token

	repeat {
	    ch = getc (fd, ch)
	} until (ch != ' ' && ch != '\t')

	if (ch == EOF) {
	    type = Y_DONE
	    call strcpy ("END", Memc[typename], SZ_FNAME)

	} else if (IS_ALPHA (ch)) {
	    type = Y_ID
	    call strcpy ("IDENT", Memc[typename], SZ_FNAME)

	    while (IS_ALPHA (ch)) {
		Memc[tokbuf+nxttok] = ch
		nxttok = nxttok + 1
		ch = getc (fd, ch)
	    }
	    call ungetc (fd, ch)

	    Memc[tokbuf+nxttok] = EOS
	    index = word_match (Memc[token], pownames)

	    if (index > 0) {
		type = powers[index]
		call strcpy ("POWER", Memc[typename], SZ_FNAME)

	    } else if (streq (Memc[token], "per")) {
		type = Y_DIV
		call strcpy ("DIV", Memc[typename], SZ_FNAME)
	    }

	} else if (ch == '-' || IS_DIGIT (ch)) {
	    type = Y_NUM
	    call strcpy ("NUMBER", Memc[typename], SZ_FNAME)

	    Memc[tokbuf+nxttok] = ch
	    nxttok = nxttok + 1
	    ch = getc (fd, ch)

	    while (IS_DIGIT (ch)) {
		Memc[tokbuf+nxttok] = ch
		nxttok = nxttok + 1
		ch = getc (fd, ch)
	    }
	    call ungetc (fd, ch)

	} else {
	    Memc[tokbuf+nxttok] = ch
	    nxttok = nxttok + 1

	    switch (ch) {
	    case '*':
		ch = getc (fd, ch)
		if (ch == '*') {
		    type = Y_POW
		    call strcpy ("EXPON", Memc[typename], SZ_FNAME)

		    Memc[tokbuf+nxttok] = ch
		    nxttok = nxttok + 1
		} else {
		    type = Y_MUL
		    call strcpy ("MUL", Memc[typename], SZ_FNAME)

		    call ungetc (fd, ch)
		}

	    case '/':
		type = Y_DIV
		call strcpy ("DIV", Memc[typename], SZ_FNAME)

	    case '^':
		type = Y_POW
		call strcpy ("EXPON", Memc[typename], SZ_FNAME)

	    default:
		type = Y_WRONG
		call strcpy ("ERROR", Memc[typename], SZ_FNAME)
	    }
	}

	Memc[tokbuf+nxttok] = EOS
	nxttok = nxttok + 1

	if (debug == YES) {
	    call eprintf ("Token is %s [%s]\n")
	    if (Memc[token] == EOS)  {
		call pargstr ("EOS")
	    } else {
		call pargstr (Memc[token])
	    }
	    call pargstr (Memc[typename])
	}

	call sfree (sp)
	return (type)
end
define	YYNPROD		15
define	YYLAST		43
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

# Parser for yacc output, translated to the IRAF SPP language.  The contents
# of this file form the bulk of the source of the parser produced by Yacc.
# Yacc recognizes several macros in the yaccpar input source and replaces
# them as follows:
#	A	user suppled "global" definitions and declarations
# 	B	parser tables
# 	C	user supplied actions (reductions)
# The remainder of the yaccpar code is not changed.

define	yystack_	10		# statement labels for gotos
define	yynewstate_	20
define	yydefault_	30
define	yyerrlab_	40
define	yyabort_	50

define	YYFLAG		(-1000)		# defs used in user actions
define	YYERROR		goto yyerrlab_
define	YYACCEPT	return (OK)
define	YYABORT		return (ERR)


# YYPARSE -- Parse the input stream, returning OK if the source is
# syntactically acceptable (i.e., if compilation is successful),
# otherwise ERR.  The parameters YYMAXDEPTH and YYOPLEN must be
# supplied by the caller in the %{ ... %} section of the Yacc source.
# The token value stack is a dynamically allocated array of operand
# structures, with the length and makeup of the operand structure being
# application dependent.

int procedure yyparse (fd, yydebug, yylex)

int	fd			# stream to be parsed
bool	yydebug			# print debugging information?
int	yylex()			# user-supplied lexical input function
extern	yylex()

short	yys[YYMAXDEPTH]		# parser stack -- stacks tokens
pointer	yyv			# pointer to token value stack
pointer	yyval			# value returned by action
pointer	yylval			# value of token
int	yyps			# token stack pointer
pointer	yypv			# value stack pointer
int	yychar			# current input token number
int	yyerrflag		# error recovery flag
int	yynerrs			# number of errors

short	yyj, yym		# internal variables
pointer	yysp, yypvt
short	yystate, yyn
int	yyxi, i
errchk	salloc, yylex


include	"parseunits.com"

char		units[SZ_FNAME]

int		num_unstr()
pointer		mul_unstr(), div_unstr(), pow_unstr(), set_unstr()

short	yyexca[6]
data	(yyexca(i),i=  1,  6)	/  -1,   1,   0,  -1,  -2,   0/
short	yyact[43]
data	(yyact(i),i=  1,  8)	/  23,  10,  11,  13,  13,  12,  11,  13/
data	(yyact(i),i=  9, 16)	/  12,  11,  13,  17,   3,  24,  16,   4/
data	(yyact(i),i= 17, 24)	/  22,   8,   9,   7,   4,  19,   8,   9/
data	(yyact(i),i= 25, 32)	/   7,   8,   9,   7,  18,   2,   6,   5/
data	(yyact(i),i= 33, 40)	/   1,   0,  14,   0,  15,   0,   0,   0/
data	(yyact(i),i= 41, 43)	/   0,  20,  21/
short	yypact[25]
data	(yypact(i),i=  1,  8)	/-244,-1000,-257,-1000,-239,-236,-1000,-253/
data	(yypact(i),i=  9, 16)	/-235,-242,-1000,-239,-239,-248,-260,-1000/
data	(yypact(i),i= 17, 24)	/-251,-1000,-1000,-1000,-263,-264,-1000,-1000/
data	(yypact(i),i= 25, 25)	/-1000/
short	yypgo[5]
data	(yypgo(i),i=  1,  5)	/   0,  32,  29,  31,  30/
short	yyr1[15]
data	(yyr1(i),i=  1,  8)	/   0,   1,   1,   2,   2,   2,   2,   2/
data	(yyr1(i),i=  9, 15)	/   3,   3,   4,   4,   4,   4,   4/
short	yyr2[15]
data	(yyr2(i),i=  1,  8)	/   0,   2,   1,   3,   3,   3,   3,   1/
data	(yyr2(i),i=  9, 15)	/   2,   1,   3,   2,   2,   2,   1/
short	yychk[25]
data	(yychk(i),i=  1,  8)	/-1000,  -1,  -2, 256, 259,  -3,  -4, 263/
data	(yychk(i),i=  9, 16)	/ 261, 262, 258, 266, 265, 267,  -2,  -4/
data	(yychk(i),i= 17, 24)	/ 267, 264, 263, 263,  -2,  -2, 264, 260/
data	(yychk(i),i= 25, 25)	/ 264/
short	yydef[25]
data	(yydef(i),i=  1,  8)	/   0,  -2,   0,   2,   0,   7,   9,  14/
data	(yydef(i),i=  9, 16)	/   0,   0,   1,   0,   0,   0,   0,   8/
data	(yydef(i),i= 17, 24)	/   0,  11,  12,  13,   4,   5,   6,   3/
data	(yydef(i),i= 25, 25)	/  10/

begin
	call smark (yysp)
	call salloc (yyv, (YYMAXDEPTH+2) * YYOPLEN, TY_STRUCT)

	# Initialization.  The first element of the dynamically allocated
	# token value stack (yyv) is used for yyval, the second for yylval,
	# and the actual stack starts with the third element.

	yystate = 0
	yychar = -1
	yynerrs = 0
	yyerrflag = 0
	yyps = 0
	yyval = yyv
	yylval = yyv + YYOPLEN
	yypv = yylval

yystack_
	# SHIFT -- Put a state and value onto the stack.  The token and
	# value stacks are logically the same stack, implemented as two
	# separate arrays.

	if (yydebug) {
	    call printf ("state %d, char 0%o\n")
		call pargs (yystate)
		call pargi (yychar)
	}
	yyps = yyps + 1
	yypv = yypv + YYOPLEN
	if (yyps > YYMAXDEPTH) {
	    call sfree (yysp)
	    call eprintf ("yacc stack overflow\n")
	    return (ERR)
	}
	yys[yyps] = yystate
	YYMOVE (yyval, yypv)

yynewstate_
	# Process the new state.
	yyn = yypact[yystate+1]

	if (yyn <= YYFLAG)
	    goto yydefault_			# simple state

	# The variable "yychar" is the lookahead token.
	if (yychar < 0) {
	    yychar = yylex (fd, yylval)
	    if (yychar < 0)
		yychar = 0
	}
	yyn = yyn + yychar
	if (yyn < 0 || yyn >= YYLAST)
	    goto yydefault_

	yyn = yyact[yyn+1]
	if (yychk[yyn+1] == yychar) {		# valid shift
	    yychar = -1
	    YYMOVE (yylval, yyval)
	    yystate = yyn
	    if (yyerrflag > 0)
		yyerrflag = yyerrflag - 1
	    goto yystack_
	}

yydefault_
	# Default state action.

	yyn = yydef[yystate+1]
	if (yyn == -2) {
	    if (yychar < 0) {
		yychar = yylex (fd, yylval)
		if (yychar < 0)
		    yychar = 0
	    }

	    # Look through exception table.
	    yyxi = 1
	    while ((yyexca[yyxi] != (-1)) || (yyexca[yyxi+1] != yystate))
		yyxi = yyxi + 2
	    for (yyxi=yyxi+2;  yyexca[yyxi] >= 0;  yyxi=yyxi+2) {
		if (yyexca[yyxi] == yychar)
		    break
	    }

	    yyn = yyexca[yyxi+1]
	    if (yyn < 0) {
		call sfree (yysp)
		return (OK)			# ACCEPT -- all done
	    }
	}


	# SYNTAX ERROR -- resume parsing if possible.

	if (yyn == 0) {
	    switch (yyerrflag) {
	    case 0, 1, 2:
		if (yyerrflag == 0) {		# brand new error
		    call eprintf ("syntax error\n")
yyerrlab_
		    yynerrs = yynerrs + 1
		    # fall through...
		}

	    # case 1:
	    # case 2: incompletely recovered error ... try again
		yyerrflag = 3

		# Find a state where "error" is a legal shift action.
		while (yyps >= 1) {
		    yyn = yypact[yys[yyps]+1] + YYERRCODE
		    if ((yyn >= 0) && (yyn < YYLAST) &&
			(yychk[yyact[yyn+1]+1] == YYERRCODE)) {
			    # Simulate a shift of "error".
			    yystate = yyact[yyn+1]
			    goto yystack_
		    }
		    yyn = yypact[yys[yyps]+1]

		    # The current yyps has no shift on "error", pop stack.
		    if (yydebug) {
			call printf ("error recovery pops state %d, ")
			    call pargs (yys[yyps])
			call printf ("uncovers %d\n")
			    call pargs (yys[yyps-1])
		    }
		    yyps = yyps - 1
		    yypv = yypv - YYOPLEN
		}

		# ABORT -- There is no state on the stack with an error shift.
yyabort_
		call sfree (yysp)
		return (ERR)


	    case 3: # No shift yet; clobber input char.

		if (yydebug) {
		    call printf ("error recovery discards char %d\n")
			call pargi (yychar)
		}

		if (yychar == 0)
		    goto yyabort_		# don't discard EOF, quit
		yychar = -1
		goto yynewstate_		# try again in the same state
	    }
	}


	# REDUCE -- Reduction by production yyn.

	if (yydebug) {
	    call printf ("reduce %d\n")
		call pargs (yyn)
	}
	yyps  = yyps - yyr2[yyn+1]
	yypvt = yypv
	yypv  = yypv - yyr2[yyn+1] * YYOPLEN
	YYMOVE (yypv + YYOPLEN, yyval)
	yym   = yyn

	# Consult goto table to find next state.
	yyn = yyr1[yyn+1]
	yyj = yypgo[yyn+1] + yys[yyps] + 1
	if (yyj >= YYLAST)
	    yystate = yyact[yypgo[yyn+1]+1]
	else {
	    yystate = yyact[yyj+1]
	    if (yychk[yystate+1] != -yyn)
		yystate = yyact[yypgo[yyn+1]+1]
	}

	# Perform action associated with the grammar rule, if any.
	switch (yym) {
	    
case 1:
# line 28 "parseunits.y"
{
		    # Normal exit. Return pointer to units structure
		    if (debug == YES)
			call eprintf ("\n")

		    tun = Memi[yypvt-YYOPLEN]
		    return (OK)
		}
case 2:
# line 36 "parseunits.y"
{
		    # Syntax error
		    if (debug == YES)
			call eprintf ("\n")

		    return (ERR)
		}
case 3:
# line 45 "parseunits.y"
{
		    # Parenthesized expression
		    Memi[yyval] = Memi[yypvt-2*YYOPLEN]
		}
case 4:
# line 49 "parseunits.y"
{
		    # Multiply two units expressions
		    Memi[yyval] = mul_unstr (Memi[yypvt-2*YYOPLEN], Memi[yypvt])
		    call free_unstr (Memi[yypvt-2*YYOPLEN])
		    call free_unstr (Memi[yypvt])

		    if (debug == YES) {
			call str_unstr (Memi[yyval], units, SZ_FNAME)
			call eprintf ("Units are %s\n")
			call pargstr (units)
		    }
		}
case 5:
# line 61 "parseunits.y"
{
		    # Divide two units expressions
		    Memi[yyval] = div_unstr (Memi[yypvt-2*YYOPLEN], Memi[yypvt])
		    call free_unstr (Memi[yypvt-2*YYOPLEN])
		    call free_unstr (Memi[yypvt])

		    if (debug == YES) {
			call str_unstr (Memi[yyval], units, SZ_FNAME)
			call eprintf ("Units are %s\n")
			call pargstr (units)
		    }
		}
case 6:
# line 73 "parseunits.y"
{
		    # Raise expression to a power
		    Memi[yyval] = pow_unstr (Memi[yypvt-2*YYOPLEN], num_unstr (Memc[Memi[yypvt]]))
		    call free_unstr (Memi[yypvt-2*YYOPLEN])

		    if (debug == YES) {
			call str_unstr (Memi[yyval], units, SZ_FNAME)
			call eprintf ("Units are %s\n")
			call pargstr (units)
		    }
		}
case 7:
# line 84 "parseunits.y"
{
		    # List of terms
		    Memi[yyval] = Memi[yypvt]
		}
case 8:
# line 89 "parseunits.y"
{
		    # Implicit multiplication
		    Memi[yyval] = mul_unstr (Memi[yypvt-YYOPLEN], Memi[yypvt])
		    call free_unstr (Memi[yypvt-YYOPLEN])
		    call free_unstr (Memi[yypvt])

		    if (debug == YES) {
			call str_unstr (Memi[yyval], units, SZ_FNAME)
			call eprintf ("Units are %s\n")
			call pargstr (units)
		    }
		}
case 9:
# line 101 "parseunits.y"
{
		    # Simple term
		    Memi[yyval] = Memi[yypvt]
		}
case 10:
# line 106 "parseunits.y"
{
		    # Raise units to a power
		    Memi[yyval] = set_unstr (abrev, Memc[Memi[yypvt-2*YYOPLEN]], 
				    	num_unstr (Memc[Memi[yypvt]]))

		    if (debug == YES) {
			call str_unstr (Memi[yyval], units, SZ_FNAME)
			call eprintf ("Units are %s\n")
			call pargstr (units)
		    }
		}
case 11:
# line 117 "parseunits.y"
{
		    # Implicitly raise to a power
		    Memi[yyval] = set_unstr (abrev, Memc[Memi[yypvt-YYOPLEN]], 
				    num_unstr (Memc[Memi[yypvt]]))

		    if (debug == YES) {
			call str_unstr (Memi[yyval], units, SZ_FNAME)
			call eprintf ("Units are %s\n")
			call pargstr (units)
		    }
		}
case 12:
# line 128 "parseunits.y"
{
		    # Cubic prefix
		    Memi[yyval] = set_unstr (abrev, Memc[Memi[yypvt]], 3)

		    if (debug == YES) {
			call str_unstr (Memi[yyval], units, SZ_FNAME)
			call eprintf ("Units are %s\n")
			call pargstr (units)
		    }
		}
case 13:
# line 138 "parseunits.y"
{
		    # Square prefix
		    Memi[yyval] = set_unstr (abrev, Memc[Memi[yypvt]], 2)

		    if (debug == YES) {
			call str_unstr (Memi[yyval], units, SZ_FNAME)
			call eprintf ("Units are %s\n")
			call pargstr (units)
		    }
		}
case 14:
# line 148 "parseunits.y"
{
		    # Simple name
		    Memi[yyval] = set_unstr (abrev, Memc[Memi[yypvt]], 1)

		    if (debug == YES) {
			call str_unstr (Memi[yyval], units, SZ_FNAME)
			call eprintf ("Units are %s\n")
			call pargstr (units)
		    }
		}	}

	goto yystack_				# stack new state and value
end