summaryrefslogtreecommitdiffstats
path: root/src/H5DZtrans.c
blob: 5e546213f496a41dd84af3425db03715fe419361 (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
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
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdf.ncsa.uiuc.edu/HDF5/doc/Copyright.html.  If you do not have     *
 * access to either file, you may request a copy from hdfhelp@ncsa.uiuc.edu. *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*
 *  Programmer: Bill Wendling <wendling@ncsa.uiuc.edu>
 *              25. August 2003
 */

/*
 * This is the context-free grammar for our expressions:
 *
 * expr     :=  term    | term '+ term      | term '-' term
 * term     :=  factor  | factor '*' factor | factor '/' factor
 * factor   :=  number      |
 *              symbol      |
 *              '-' factor  |   // unary minus
 *              '+' factor  |   // unary plus
 *              '(' expr ')'
 * symbol   :=  [a-zA-Z][a-zA-Z0-9]*
 * number   :=  INTEGER | FLOAT
 *      // INTEGER is a C long int
 *      // FLOAT is a C double
 */

#define H5DZ_DEBUG

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

/* Token types */
typedef enum {
    ERROR,
    INTEGER,
    FLOAT,
    SYMBOL,
    PLUS,
    MINUS,
    MULT,
    DIVIDE,
    LPAREN,
    RPAREN,
    END
} token_type;

/* The token */
typedef struct {
    const char *tok_expr;       /* Holds the original expression        */

    /* Current token values */
    token_type  tok_type;       /* The type of the current token        */
    const char *tok_begin;      /* The beginning of the current token   */
    const char *tok_end;        /* The end of the current token         */

    /* Previous token values */
    token_type  tok_last_type;  /* The type of the last token           */
    const char *tok_last_begin; /* The beginning of the last token      */
    const char *tok_last_end;   /* The end of the last token            */
} token;

typedef union {
    char   *sym_val;
    long    int_val;
    double  float_val;
} num_val;

typedef struct node {
    struct node    *lchild;
    struct node    *rchild;
    token_type      type;
    num_val         value;
} node;

/* local functions */
static void H5DZ_destroy_parse_tree(node *tree);
static token *H5DZ_get_token(token *current);
static node *H5DZ_parse_expression(token *current);
static node *H5DZ_parse_term(token *current);
static node *H5DZ_parse_factor(token *current);
static node *H5DZ_new_node(token_type type);

#ifdef H5DZ_DEBUG
static void H5DZ_debug(node *tree);
static void H5DZ_print(node *tree, FILE *stream);
#endif  /* H5DZ_DEBUG */

/*-------------------------------------------------------------------------
 * Function:    H5DZ_unget_token
 * Purpose:     Rollback the token to the previous token retrieved. There
 *              should only need to be one level of rollback necessary
 *              for our grammar.
 * Return:      Always succeeds.
 * Programmer:  Bill Wendling
 *              26. August 2003
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
H5DZ_unget_token(token *current)
{
    /* check args */
    assert(current);

    current->tok_type = current->tok_last_type;
    current->tok_begin = current->tok_last_begin;
    current->tok_end = current->tok_last_end;
}

/*-------------------------------------------------------------------------
 * Function:    H5DZ_get_token
 * Purpose:     Determine what the next valid token is in the expression
 *              string. The current position within the token string is
 *              kept internal to the token and handled by this and the
 *              unget_token function.
 * Return:      Succeess:       The passed in token with a valid tok_type
 *                              field.
 *              Failure:        The passed in token but with the tok_type
 *                              field set to ERROR.
 * Programmer:  Bill Wendling
 *              26. August 2003
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static token *
H5DZ_get_token(token *current)
{
    /* check args */
    assert(current);

    /* Save the last position for possible ungets */
    current->tok_last_type = current->tok_type;
    current->tok_last_begin = current->tok_begin;
    current->tok_last_end = current->tok_end;

    current->tok_begin = current->tok_end;

    while (current->tok_begin[0] != '\0') {
        if (isspace(current->tok_begin[0])) {
            /* ignore whitespace */
        } else if (isdigit(current->tok_begin[0]) ||
                   current->tok_begin[0] == '.') {
            current->tok_end = current->tok_begin;

            /*
             * integer          :=  digit-sequence
             * digit-sequence   :=  digit | digit digit-sequence
             * digit            :=  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
             */
            if (current->tok_end[0] != '.') {
                /* is number */
                current->tok_type = INTEGER;

                while (isdigit(current->tok_end[0]))
                    ++current->tok_end;
            }

            /*
             * float            :=  digit-sequence exponent |
             *                      dotted-digits exponent?
             * dotted-digits    :=  digit-sequence '.' digit-sequence?  |
             *                      '.' digit-sequence
             * exponent         :=  [Ee] [-+]? digit-sequence
             */
            if (current->tok_end[0] == '.' ||
                    current->tok_end[0] == 'e' ||
                    current->tok_end[0] == 'E') {
                current->tok_type = FLOAT;

                if (current->tok_end[0] == '.')
                    do {
                        ++current->tok_end;
                    } while (isdigit(current->tok_end[0]));

                if (current->tok_end[0] == 'e' ||
                    current->tok_end[0] == 'E') {
                    ++current->tok_end;

                    if (current->tok_end[0] == '-' ||
                        current->tok_end[0] == '+')
                        ++current->tok_end;

                    if (!isdigit(current->tok_end[0])) {
                        current->tok_type = ERROR;
                        fprintf(stderr, "Invalidly formatted floating point number\n");
                    }

                    while (isdigit(current->tok_end[0]))
                        ++current->tok_end;
                }

                /* Check that this is a properly formatted numerical value */
                if (isalpha(current->tok_end[0]) || current->tok_end[0] == '.') {
                    current->tok_type = ERROR;
                    fprintf(stderr, "Invalidly formatted floating point number\n");
                }
            }

            break;
        } else if (isalpha(current->tok_begin[0])) {
            /* is symbol */
            current->tok_type = SYMBOL;
            current->tok_end = current->tok_begin;

            while (isalnum(current->tok_end[0]))
                ++current->tok_end;

            break;
        } else {
            /* should be +, -, *, /, (, or ) */
            switch (current->tok_begin[0]) {
                case '+':   current->tok_type = PLUS;    break;
                case '-':   current->tok_type = MINUS;   break;
                case '*':   current->tok_type = MULT;    break;
                case '/':   current->tok_type = DIVIDE;  break;
                case '(':   current->tok_type = LPAREN;  break;
                case ')':   current->tok_type = RPAREN;  break;
                default:
                    current->tok_type = ERROR;
                    fprintf(stderr, "Unknown token: %c\n", current->tok_begin[0]);
                    return current;
            }

            current->tok_end = current->tok_begin + 1;
            break;
        }

        ++current->tok_begin;
    }

    if (current->tok_begin[0] == '\0')
        current->tok_type = END;

    return current;
}

/*-------------------------------------------------------------------------
 * Function:    H5DZ_destroy_parse_tree
 * Purpose:     Recursively destroys the expression tree.
 * Return:      Nothing
 * Programmer:  Bill Wendling
 *              25. August 2003
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
H5DZ_destroy_parse_tree(node *tree)
{
    if (!tree)
        return;

    if (tree->type == SYMBOL)
        free(tree->value.sym_val);

    H5DZ_destroy_parse_tree(tree->lchild);
    H5DZ_destroy_parse_tree(tree->rchild);
    free(tree);
}

/*-------------------------------------------------------------------------
 * Function:    H5DZ_parse
 * Purpose:     Entry function for parsing the expression string.
 * Return:      Success:    Valid NODE ptr to an expression tree.
 *              Failure:    NULL
 * Programmer:  Bill Wendling
 *              26. August 2003
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static node *
H5DZ_parse(const char *expression)
{
    token tok;

    if (!expression)
        return NULL;

    /* Set up the initial token for parsing */
    tok.tok_expr = tok.tok_begin = tok.tok_end = expression;
    return H5DZ_parse_expression(&tok);
}

/*-------------------------------------------------------------------------
 * Function:    H5DZ_parse_expression
 * Purpose:     Beginning of the recursive descent parser to parse the
 *              expression. An expression is:
 *
 *                  expr     :=  term | term '+' term | term '-' term
 *
 * Return:      Success:    Valid NODE ptr to expression tree
 *              Failure:    NULL
 * Programmer:  Bill Wendling
 *              26. August 2003
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static node *
H5DZ_parse_expression(token *current)
{
    node *expr = H5DZ_parse_term(current);

    for (;;) {
        node *new_node;

        current = H5DZ_get_token(current);

        switch (current->tok_type) {
        case PLUS:
            new_node = H5DZ_new_node(PLUS);

            if (!new_node) {
                H5DZ_destroy_parse_tree(expr);
                expr = NULL;
                goto done;
            }

            new_node->lchild = expr;
            new_node->rchild = H5DZ_parse_term(current);

            if (!new_node->rchild) {
                H5DZ_destroy_parse_tree(new_node);
                fprintf(stderr, "Parsing error occurred\n");
                expr = NULL;
                goto done;
            }

            expr = new_node;
            break;
        case MINUS:
            new_node = H5DZ_new_node(MINUS);

            if (!new_node) {
                H5DZ_destroy_parse_tree(expr);
                expr = NULL;
                goto done;
            }

            new_node->lchild = expr;
            new_node->rchild = H5DZ_parse_term(current);

            if (!new_node->rchild) {
                H5DZ_destroy_parse_tree(new_node);
                fprintf(stderr, "Parsing error occurred\n");
                expr = NULL;
                goto done;
            }

            expr = new_node;
            break;
        case RPAREN:
            H5DZ_unget_token(current);
            goto done;
        case END:
            goto done;
        default:
            H5DZ_destroy_parse_tree(expr);
            expr = NULL;
            fprintf(stderr, "Parsing error around %c\n", current->tok_begin[0]);
            goto done;
        }
    }

done:
    return expr;
}

/*-------------------------------------------------------------------------
 * Function:    H5DZ_parse_term
 * Purpose:     Parses a term in our expression language. A term is:
 *
 *                  term :=  factor | factor '*' factor | factor '/' factor
 *
 * Return:      Success:    Valid NODE ptr to expression tree
 *              Failure:    NULL
 * Programmer:  Bill Wendling
 *              26. August 2003
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static node *
H5DZ_parse_term(token *current)
{
    node *term = H5DZ_parse_factor(current);

    for (;;) {
        node *new_node;

        current = H5DZ_get_token(current);

        switch (current->tok_type) {
        case MULT:
            new_node = H5DZ_new_node(MULT);

            if (!new_node) {
                H5DZ_destroy_parse_tree(term);
                term = NULL;
                goto done;
            }

            new_node->lchild = term;
            new_node->rchild = H5DZ_parse_factor(current);

            if (!new_node->rchild) {
                H5DZ_destroy_parse_tree(term);
                fprintf(stderr, "Parsing error occurred\n");
                term = NULL;
                goto done;
            }

            term = new_node;
            break;
        case DIVIDE:
            new_node = H5DZ_new_node(DIVIDE);

            if (!new_node) {
                H5DZ_destroy_parse_tree(term);
                term = NULL;
                goto done;
            }

            new_node->lchild = term;
            new_node->rchild = H5DZ_parse_factor(current);
            term = new_node;

            if (!new_node->rchild) {
                H5DZ_destroy_parse_tree(term);
                fprintf(stderr, "Parsing error occurred\n");
                term = NULL;
                goto done;
            }

            break;
        case RPAREN:
            H5DZ_unget_token(current);
            goto done;
        case END:
            goto done;
        default:
            H5DZ_unget_token(current);
            goto done;
        }
    }

done:
    return term;
}

/*-------------------------------------------------------------------------
 * Function:    H5DZ_parse_factor
 * Purpose:     Parses a factor in our expression language. A factor is:
 *
 *                  factor   :=  number      |  // C long or double
 *                               symbol      |  // C identifier
 *                               '-' factor  |  // unary minus
 *                               '+' factor  |  // unary plus
 *                               '(' expr ')'
 *
 * Return:      Success:    Valid NODE ptr to expression tree
 *              Failure:    NULL
 * Programmer:  Bill Wendling
 *              26. August 2003
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static node *
H5DZ_parse_factor(token *current)
{
    node *factor, *new_node;

    current = H5DZ_get_token(current);

    switch (current->tok_type) {
    case INTEGER:
        factor = H5DZ_new_node(INTEGER);

        if (!factor)
            goto done;

        sscanf(current->tok_begin, "%ld", &factor->value.int_val);
        break;
    case FLOAT:
        factor = H5DZ_new_node(FLOAT);

        if (!factor)
            goto done;

        sscanf(current->tok_begin, "%lf", &factor->value.float_val);
        break;
    case SYMBOL:
        factor = H5DZ_new_node(SYMBOL);

        if (!factor)
            goto done;

        factor->value.sym_val = calloc(current->tok_end - current->tok_begin + 1, 1);
        strncpy(factor->value.sym_val, current->tok_begin,
                current->tok_end - current->tok_begin);
        break;
    case LPAREN:
        factor = H5DZ_parse_expression(current);

        if (!factor)
            goto done;

        current = H5DZ_get_token(current);

        if (current->tok_type != RPAREN) {
            H5DZ_destroy_parse_tree(factor);
            fprintf(stderr, "Syntax error around %c\n", current->tok_begin[0]);
            factor = NULL;
            goto done;
        }

        break;
    case RPAREN:
        /* We shouldn't see a ) right now */
        H5DZ_destroy_parse_tree(factor);
        fprintf(stderr, "Syntax error: unexpected ')' \n");
        factor = NULL;
        goto done;
    case PLUS:
        /* unary + */
        new_node = H5DZ_parse_factor(current);

        if (new_node) {
            if (new_node->type != INTEGER && new_node->type != FLOAT &&
                    new_node->type != SYMBOL) {
                H5DZ_destroy_parse_tree(new_node);
                H5DZ_destroy_parse_tree(factor);
                fprintf(stderr, "Parsing error occurred\n");
                return NULL;
            }

            factor = new_node;
            new_node = H5DZ_new_node(PLUS);

            if (!new_node) {
                H5DZ_destroy_parse_tree(factor);
                fprintf(stderr, "Parsing error occurred\n");
                factor = NULL;
                goto done;
            }

            new_node->rchild = factor;
            factor = new_node;
        } else {
            H5DZ_destroy_parse_tree(factor);
            fprintf(stderr, "Parsing error occurred\n");
            return NULL;
        }

        break;
    case MINUS:
        /* unary - */
        new_node = H5DZ_parse_factor(current);

        if (new_node) {
            if (new_node->type != INTEGER && new_node->type != FLOAT &&
                    new_node->type != SYMBOL) {
                H5DZ_destroy_parse_tree(new_node);
                H5DZ_destroy_parse_tree(factor);
                fprintf(stderr, "Parsing error occurred\n");
                return NULL;
            }

            factor = new_node;
            new_node = H5DZ_new_node(MINUS);

            if (!new_node) {
                H5DZ_destroy_parse_tree(factor);
                fprintf(stderr, "Parsing error occurred\n");
                factor = NULL;
                goto done;
            }

            new_node->rchild = factor;
            factor = new_node;
        } else {
            H5DZ_destroy_parse_tree(factor);
            fprintf(stderr, "Parsing error occurred\n");
            return NULL;
        }

        break;
    case END:
        goto done;
    }

done:
    return factor;
}

/*-------------------------------------------------------------------------
 * Function:    H5DZ_new_node
 * Purpose:     Create and initilize a new NODE structure.
 * Return:      Success:    Valid NODE ptr
 *              Failure:    NULL
 * Programmer:  Bill Wendling
 *              26. August 2003
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static node *
H5DZ_new_node(token_type type)
{
    node *new_node = calloc(1, sizeof(node));
 
    if (new_node) {
        new_node->type = type;
    } else {
        fprintf(stderr, "Out of memory\n");
    }

    return new_node;
}

#ifdef H5DZ_DEBUG
/*-------------------------------------------------------------------------
 * Function:    H5DZ_debug
 * Purpose:     Print out the expression in a nice format which displays
 *              the precedences explicitly with parentheses.
 * Return:      Nothing
 * Programmer:  Bill Wendling
 *              26. August 2003
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
H5DZ_debug(node *tree)
{
    fprintf(stderr, "Expression: ");
    H5DZ_print(tree, stderr);
    fprintf(stderr, "\n");
}

/*-------------------------------------------------------------------------
 * Function:    H5DZ_print
 * Purpose:     Print out the expression in a nice format which displays
 *              the precedences explicitly with parentheses.
 * Return:      Nothing
 * Programmer:  Bill Wendling
 *              26. August 2003
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
H5DZ_print(node *tree, FILE *stream)
{
    /* check args */
    assert(stream);

    if (!tree)
        return;

    if (tree->type == INTEGER) {
        fprintf(stream, "%ld", tree->value.int_val);
    } else if (tree->type == FLOAT) {
        fprintf(stream, "%f", tree->value.float_val);
    } else if (tree->type == SYMBOL) {
        fprintf(stream, "%s", tree->value.sym_val);
    } else {
        fprintf(stream, "(");
        H5DZ_print(tree->lchild, stream);

        switch (tree->type) {
        case PLUS:      fprintf(stream, "+"); break;
        case MINUS:     fprintf(stream, "-"); break;
        case MULT:      fprintf(stream, "*"); break;
        case DIVIDE:    fprintf(stream, "/"); break;
        default: fprintf(stream, "Invalid expression tree\n");
            return;
        }

        H5DZ_print(tree->rchild, stream);
        fprintf(stream, ")");
    }
}
#endif  /* H5DZ_DEBUG */

int main(int argc, char **argv)
{
    node *n;
    printf("Parsing Expression: \"%s\"\n", argv[1]);
    n = H5DZ_parse(argv[1]);
    H5DZ_debug(n);
    return 0;
}