Diff of /c99.y [1b9e15] .. [b8cc5d]  Maximize  Restore

Switch to unified view

a/c99.y b/c99.y
...
...
9
%token BOOL COMPLEX IMAGINARY
9
%token BOOL COMPLEX IMAGINARY
10
%token STRUCT UNION ENUM ELLIPSIS
10
%token STRUCT UNION ENUM ELLIPSIS
11
11
12
%token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
12
%token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
13
13
14
%union { char str[128]; }
14
%union { char str[4096]; }
15
15
16
%start translation_unit
16
%start translation_unit
17
17
18
%{
18
%{
19
#include <string.h>
20
#include <stdio.h>
21
22
// get tail of a string
23
#define TAIL(str) (&str[strlen(str)-1])
24
25
19
/* fifo to store expression strings */
26
/* fifo to store expression strings */
20
#define NULL 0
27
#define NULL 0
21
28
22
#define STMT_SIZE 128
29
#define STMT_SIZE 128
23
#define FIFO_SIZE 20
30
#define FIFO_SIZE 20
...
...
42
49
43
// push an entry (typically a declaration into the fifo
50
// push an entry (typically a declaration into the fifo
44
void fifo_push(fifo_entry_t type, const char *psz);
51
void fifo_push(fifo_entry_t type, const char *psz);
45
52
46
// pop one entry from the specific category
53
// pop one entry from the specific category
47
char *fifo_pop(fifo_entry_t type);
54
char *fifo_pop(fifo_entry_t type);
55
char *lifo_pop(fifo_entry_t type);
48
56
49
// tag all from category "all" to new type
57
// tag all from category "all" to new type
50
// this way we can collect new stuff in all and push
58
// this way we can collect new stuff in all and push
51
// it to the correct section when we know more about
59
// it to the correct section when we know more about
52
// our context
60
// our context
53
void fifo_tag(fifo_entry_t type);
61
void fifo_tag(fifo_entry_t type);
54
62
63
// map c datatype to iec (standard and userdefined)
64
char *maptype(char *ctype);
65
66
55
%}
67
%}
56
68
57
%%
69
%%
58
70
59
primary_expression
71
primary_expression
...
...
230
    : conditional_expression
242
    : conditional_expression
231
    ;
243
    ;
232
244
233
declaration
245
declaration
234
    : declaration_specifiers ';'
246
    : declaration_specifiers ';'
235
    { sprintf($<str>$, "%s %s",$<str>1, $<str>2); }
247
    { sprintf($<str>$, "%s %s\n",$<str>1, $<str>2); }
236
    | declaration_specifiers init_declarator_list ';'
248
    | declaration_specifiers init_declarator_list ';'
237
    {
249
    {
238
        char *p;
250
        char *p;
239
        while( (p=fifo_pop(decl_all)) != NULL)
251
        while( (p=fifo_pop(decl_all)) != NULL)
240
        {
252
        {
253
//          sprintf(TAIL($<str>$), p, $<str>1);
241
            printf(p, $<str>1);
254
            printf(p, $<str>1);
242
            strcpy($<str>$, "");
255
            strcpy($<str>$, "");
243
        }
256
        }
244
    }
257
    }
245
    ;
258
    ;
...
...
247
declaration_specifiers
260
declaration_specifiers
248
    : storage_class_specifier
261
    : storage_class_specifier
249
    | storage_class_specifier declaration_specifiers
262
    | storage_class_specifier declaration_specifiers
250
    { sprintf($<str>$, "%s %s",$<str>1, $<str>2); }
263
    { sprintf($<str>$, "%s %s",$<str>1, $<str>2); }
251
    | type_specifier
264
    | type_specifier
265
    {
266
        sprintf($<str>$, "%s",maptype($<str>1));
267
    }
252
    | type_specifier declaration_specifiers
268
    | type_specifier declaration_specifiers
253
    { sprintf($<str>$, "%s %s",$<str>1, $<str>2); }
269
    {
270
        sprintf($<str>$, "%s %s",$<str>1, $<str>2);
271
        sprintf($<str>$, "%s",maptype($<str>$));
272
    }
254
    | type_qualifier
273
    | type_qualifier
255
    | type_qualifier declaration_specifiers
274
    | type_qualifier declaration_specifiers
256
    { sprintf($<str>$, "%s %s",$<str>1, $<str>2); }
275
    { sprintf($<str>$, "%s %s",$<str>1, $<str>2); }
257
    | function_specifier
276
    | function_specifier
258
    | function_specifier declaration_specifiers
277
    | function_specifier declaration_specifiers
...
...
368
    ;
387
    ;
369
388
370
declarator
389
declarator
371
    : pointer direct_declarator
390
    : pointer direct_declarator
372
    { 
391
    { 
373
        sprintf($<str>$, "%s POINTER TO %%s;\n",$<str>2);
392
        sprintf($<str>$, "%s: POINTER TO %%s;\n",$<str>2);
374
        fifo_push(decl_all, $<str>$);
393
        fifo_push(decl_all, $<str>$);
375
    }
394
    }
376
    | direct_declarator
395
    | direct_declarator
377
    {
396
    {
378
        if (strlen($<str>1) > 0)
397
        if (strlen($<str>1) > 0)
379
        {
398
        {
380
            sprintf($<str>$, "%s %%s;\n",$<str>1);
399
            sprintf($<str>$, "%s: %%s;\n",$<str>1);
381
            fifo_push(decl_all, $<str>$);
400
            fifo_push(decl_all, $<str>$);
382
        }
401
        }
383
    }
402
    }
384
    ;
403
    ;
385
404
...
...
389
    | '(' declarator ')'
408
    | '(' declarator ')'
390
    | direct_declarator '[' type_qualifier_list assignment_expression ']'
409
    | direct_declarator '[' type_qualifier_list assignment_expression ']'
391
    | direct_declarator '[' type_qualifier_list ']'
410
    | direct_declarator '[' type_qualifier_list ']'
392
    | direct_declarator '[' assignment_expression ']'
411
    | direct_declarator '[' assignment_expression ']'
393
    {
412
    {
394
        sprintf($<str>$, "%s: ARRAY[ %s ] OF %%s;\n",$<str>1, $<str>3);
413
        if (strlen($<str>1) > 0)
414
        {
415
            sprintf($<str>$, "%s: ARRAY[0..(%s-1)] OF %%s;\n",$<str>1, $<str>3);
416
        }
417
        else
418
        {
419
            char *p;
420
            p = lifo_pop(decl_all);
421
            sprintf($<str>1, ",0..(%s-1)] OF %%s;\n",$<str>3);
422
            strcpy($<str>$, p);
423
            strcpy(strchr($<str>$, ']'), $<str>1);
424
//          sprintf($<str>$, p, $<str>1);
425
        }
395
        fifo_push(decl_all, $<str>$);
426
        fifo_push(decl_all, $<str>$);
396
        strcpy($<str>$, "");
427
        strcpy($<str>$, "");
397
    }
428
    }
398
    | direct_declarator '[' STATIC type_qualifier_list assignment_expression ']'
429
    | direct_declarator '[' STATIC type_qualifier_list assignment_expression ']'
399
    | direct_declarator '[' type_qualifier_list STATIC assignment_expression ']'
430
    | direct_declarator '[' type_qualifier_list STATIC assignment_expression ']'
400
    | direct_declarator '[' type_qualifier_list '*' ']'
431
    | direct_declarator '[' type_qualifier_list '*' ']'
401
    | direct_declarator '[' '*' ']'
432
    | direct_declarator '[' '*' ']'
402
    | direct_declarator '[' ']'
433
    | direct_declarator '[' ']'
403
    | direct_declarator '(' parameter_type_list ')'
434
    | direct_declarator '(' parameter_type_list ')'
404
    { sprintf($<str>$, "FUNCTION %s:",$<str>1); }
435
    {
436
        sprintf($<str>$, "FUNCTION %s:",$<str>1);
437
        fifo_push(decl_all, $<str>$);
438
        strcpy($<str>$, "");
439
    }
405
    | direct_declarator '(' identifier_list ')'
440
    | direct_declarator '(' identifier_list ')'
406
    { sprintf($<str>$, "FUNCTION %s:",$<str>1); }
441
    {
442
        sprintf($<str>$, "FUNCTION %s:",$<str>1);
443
        fifo_push(decl_all, $<str>$);
444
        strcpy($<str>$, "");
445
    }
407
    | direct_declarator '(' ')'
446
    | direct_declarator '(' ')'
408
    { sprintf($<str>$, "FUNCTION %s:",$<str>1); }
447
    {
448
        sprintf($<str>$, "FUNCTION %s:",$<str>1);
449
        fifo_push(decl_all, $<str>$);
450
        strcpy($<str>$, "");
451
    }
409
    ;
452
    ;
410
453
411
pointer
454
pointer
412
    : '*'
455
    : '*'
413
    | '*' type_qualifier_list
456
    | '*' type_qualifier_list
...
...
510
    ;
553
    ;
511
554
512
compound_statement
555
compound_statement
513
    : '{' '}'
556
    : '{' '}'
514
    | '{' block_item_list '}'
557
    | '{' block_item_list '}'
558
    { sprintf($<str>$, "%s",$<str>2); }
515
    ;
559
    ;
516
560
517
block_item_list
561
block_item_list
518
    : block_item
562
    : block_item
519
    | block_item_list block_item
563
    | block_item_list block_item
564
    { sprintf($<str>$, "%s %s",$<str>1, $<str>2); }
520
    ;
565
    ;
521
566
522
block_item
567
block_item
523
    : declaration
568
    : declaration
524
    | statement
569
    | statement
525
    { printf("%s\n",$<str>1); }
570
//  { printf("%s\n",$<str>1); }
526
    ;
571
    ;
527
572
528
expression_statement
573
expression_statement
529
    : ';'
574
    : ';'
530
    | expression ';'
575
    | expression ';'
531
    { sprintf($<str>$, "%s;",$<str>1); }
576
    { sprintf($<str>$, "%s;\n",$<str>1); }
532
    ;
577
    ;
533
578
534
selection_statement
579
selection_statement
535
    : IF '(' expression ')' statement
580
    : IF '(' expression ')' statement
536
    | IF '(' expression ')' statement ELSE statement
581
    | IF '(' expression ')' statement ELSE statement
...
...
564
    | declaration
609
    | declaration
565
    ;
610
    ;
566
611
567
function_definition
612
function_definition
568
    : declaration_specifiers declarator declaration_list compound_statement
613
    : declaration_specifiers declarator declaration_list compound_statement
569
    | declaration_specifiers declarator compound_statement
614
    {
570
    ;
615
        printf("%s %s %s %s", $<str>1, $<str>2, $<str>3, $<str>4);
571
616
    }
572
declaration_list
617
    | declaration_specifiers declarator compound_statement
573
    : declaration
618
    {
574
    | declaration_list declaration
619
        printf("%s %s %s", $<str>1, $<str>2, $<str>3);
575
    { printf("| %s %s\n",$<str>1, $<str>2); }
620
    }
576
    ;
621
    ;
577
622
578
623
declaration_list
579
%%
624
    : declaration
580
#include <stdio.h>
625
    | declaration_list declaration
581
626
    { sprintf($<str>$, "%s %s\n",$<str>1, $<str>2); }
582
extern char yytext[];
627
    ;
583
extern int column;
628
584
629
585
void yyerror(char const *s)
630
%%
586
{
631
#include <stdio.h>
587
    fflush(stdout);
632
588
    printf("\n%*s\n%*s\n", column, "^", column, s);
633
extern char yytext[];
589
}
634
extern int column;
590
635
591
int main(void)
636
void yyerror(char const *s)
592
{
637
{
593
    yyparse();
638
    fflush(stdout);
594
    return 0;
639
    printf("\n%*s\n%*s\n", column, "^", column, s);
595
}
640
}
596
641
597
642
int main(void)
598
/* fifo implementation */
643
{
599
void fifo_push(fifo_entry_t type, const char *psz)
644
    yyparse();
600
{
645
    return 0;
601
    unsigned int fifow_next = (s_fifos[type].write + 1) % FIFO_SIZE; 
646
}
602
    if (fifow_next != s_fifos[type].read)
647
603
    {
648
604
        //s_fifo[s_fifow] = psz;
649
/* fifo implementation */
605
        strcpy(s_fifos[type].fifo[s_fifos[type].write], psz);
650
void fifo_push(fifo_entry_t type, const char *psz)
606
        s_fifos[type].write = fifow_next;
651
{
607
    }
652
    unsigned int fifow_next = (s_fifos[type].write + 1) % FIFO_SIZE; 
608
}
653
    if (fifow_next != s_fifos[type].read)
609
654
    {
610
char *fifo_pop(fifo_entry_t type)
655
        //s_fifo[s_fifow] = psz;
611
{
656
        strcpy(s_fifos[type].fifo[s_fifos[type].write], psz);
612
    unsigned int fifor_prev;
657
        s_fifos[type].write = fifow_next;
613
    if (s_fifos[type].read != s_fifos[type].write)
658
    }
614
    {
659
}
615
        fifor_prev = s_fifos[type].read;
660
616
        s_fifos[type].read = (s_fifos[type].read + 1) % FIFO_SIZE;
661
char *lifo_pop(fifo_entry_t type)
617
        return s_fifos[type].fifo[fifor_prev];
662
{
618
    }
663
    if (s_fifos[type].read != s_fifos[type].write)
619
    return NULL;
664
    {
620
}
665
        s_fifos[type].write = (s_fifos[type].write + FIFO_SIZE - 1) % FIFO_SIZE;
621
666
        return s_fifos[type].fifo[s_fifos[type].write];
622
void fifo_tag(fifo_entry_t type)
667
    }
623
{
668
    return NULL;
624
    char *p;
669
}
625
    while ((p = fifo_pop(decl_all)) != NULL)
670
626
        fifo_push(type, p);
671
char *fifo_pop(fifo_entry_t type)
672
{
673
    unsigned int fifor_prev;
674
    if (s_fifos[type].read != s_fifos[type].write)
675
    {
676
        fifor_prev = s_fifos[type].read;
677
        s_fifos[type].read = (s_fifos[type].read + 1) % FIFO_SIZE;
678
        return s_fifos[type].fifo[fifor_prev];
679
    }
680
    return NULL;
681
}
682
683
void fifo_tag(fifo_entry_t type)
684
{
685
    char *p;
686
    while ((p = fifo_pop(decl_all)) != NULL)
687
        fifo_push(type, p);
688
}
689
690
char *maptype(char *ctype)
691
{
692
    if (!strcmp(ctype, "char"))
693
        return "BYTE";
694
    if (!strcmp(ctype, "short"))
695
        return "INT";
696
    if (!strcmp(ctype, "int"))
697
        return "DINT";
698
    if (!strcmp(ctype, "long"))
699
        return "DINT";
700
    if (!strcmp(ctype, "long DINT"))
701
        return "LINT";
702
703
    if (!strcmp(ctype, "unsigned BYTE"))
704
        return "UBYTE";
705
    if (!strcmp(ctype, "unsigned INT"))
706
        return "UINT";
707
    if (!strcmp(ctype, "unsigned DINT"))
708
        return "UDINT";
709
    if (!strcmp(ctype, "unsigned DINT"))
710
        return "UDINT";
711
    if (!strcmp(ctype, "unsigned LINT"))
712
        return "ULINT";
713
    printf("maptype: not found: %s\n", ctype);
714
    return ctype;
627
}
715
}