OpenCores
URL https://opencores.org/ocsvn/eco32/eco32/trunk

Subversion Repositories eco32

[/] [eco32/] [trunk/] [lcc/] [lburg/] [gram.y] - Blame information for rev 18

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 hellwig
%{
2
#include 
3
#include 
4
#include "lburg.h"
5
static char rcsid[] = "$Id: gram.y,v 2.5 1997/11/21 18:59:34 drh Exp $";
6
/*lint -e616 -e527 -e652 -esym(552,yynerrs) -esym(563,yynewstate,yyerrlab) */
7
static int yylineno = 0;
8
%}
9
%union {
10
        int n;
11
        char *string;
12
        Tree tree;
13
}
14
%term TERMINAL
15
%term START
16
%term PPERCENT
17
 
18
%token          ID TEMPLATE CODE
19
%token               INT
20
%type           nonterm cost
21
%type             tree
22
%%
23
spec    : decls PPERCENT rules          { yylineno = 0; }
24
        | decls                         { yylineno = 0; }
25
        ;
26
 
27
decls   : /* lambda */
28
        | decls decl
29
        ;
30
 
31
decl    : TERMINAL  blist '\n'
32
        | START nonterm '\n'            {
33
                if (nonterm($2)->number != 1)
34
                        yyerror("redeclaration of the start symbol\n");
35
                }
36
        | '\n'
37
        | error '\n'                    { yyerrok; }
38
        ;
39
 
40
blist   : /* lambda */
41
        | blist ID '=' INT              { term($2, $4); }
42
        ;
43
 
44
rules   : /* lambda */
45
        | rules nonterm ':' tree TEMPLATE cost '\n'     { rule($2, $4, $5, $6); }
46
        | rules '\n'
47
        | rules error '\n'              { yyerrok; }
48
        ;
49
 
50
nonterm : ID                            { nonterm($$ = $1); }
51
        ;
52
 
53
tree    : ID                            { $$ = tree($1,  0,  0); }
54
        | ID '(' tree ')'               { $$ = tree($1, $3,  0); }
55
        | ID '(' tree ',' tree ')'      { $$ = tree($1, $3, $5); }
56
        ;
57
 
58
cost    : CODE                          { if (*$1 == 0) $$ = "0"; }
59
        ;
60
%%
61
#include 
62
#include 
63
#include 
64
#include 
65
#include 
66
 
67
int errcnt = 0;
68
FILE *infp = NULL;
69
FILE *outfp = NULL;
70
static char buf[BUFSIZ], *bp = buf;
71
static int ppercent = 0;
72
static int code = 0;
73
 
74
static int get(void) {
75
        if (*bp == 0) {
76
                bp = buf;
77
                *bp = 0;
78
                if (fgets(buf, sizeof buf, infp) == NULL)
79
                        return EOF;
80
                yylineno++;
81
                while (buf[0] == '%' && buf[1] == '{' && buf[2] == '\n') {
82
                        for (;;) {
83
                                if (fgets(buf, sizeof buf, infp) == NULL) {
84
                                        yywarn("unterminated %{...%}\n");
85
                                        return EOF;
86
                                }
87
                                yylineno++;
88
                                if (strcmp(buf, "%}\n") == 0)
89
                                        break;
90
                                fputs(buf, outfp);
91
                        }
92
                        if (fgets(buf, sizeof buf, infp) == NULL)
93
                                return EOF;
94
                        yylineno++;
95
                }
96
        }
97
        return *bp++;
98
}
99
 
100
void yyerror(char *fmt, ...) {
101
        va_list ap;
102
 
103
        va_start(ap, fmt);
104
        if (yylineno > 0)
105
                fprintf(stderr, "line %d: ", yylineno);
106
        vfprintf(stderr, fmt, ap);
107
        if (fmt[strlen(fmt)-1] != '\n')
108
                 fprintf(stderr, "\n");
109
        errcnt++;
110
        va_end(ap);
111
}
112
 
113
int yylex(void) {
114
        int c;
115
 
116
        if (code) {
117
                char *p;
118
                bp += strspn(bp, " \t\f");
119
                p = strchr(bp, '\n');
120
                if (p == NULL)
121
                        p = strchr(bp, '\n');
122
                while (p > bp && isspace(p[-1]))
123
                        p--;
124
                yylval.string = alloc(p - bp + 1);
125
                strncpy(yylval.string, bp, p - bp);
126
                yylval.string[p - bp] = 0;
127
                bp = p;
128
                code--;
129
                return CODE;
130
        }
131
        while ((c = get()) != EOF) {
132
                switch (c) {
133
                case ' ': case '\f': case '\t':
134
                        continue;
135
                case '\n':
136
                case '(': case ')': case ',':
137
                case ':': case '=':
138
                        return c;
139
                }
140
                if (c == '%' && *bp == '%') {
141
                        bp++;
142
                        return ppercent++ ? 0 : PPERCENT;
143
                } else if (c == '%' && strncmp(bp, "term", 4) == 0
144
                && isspace(bp[4])) {
145
                        bp += 4;
146
                        return TERMINAL;
147
                } else if (c == '%' && strncmp(bp, "start", 5) == 0
148
                && isspace(bp[5])) {
149
                        bp += 5;
150
                        return START;
151
                } else if (c == '"') {
152
                        char *p = strchr(bp, '"');
153
                        if (p == NULL) {
154
                                yyerror("missing \" in assembler template\n");
155
                                p = strchr(bp, '\n');
156
                                if (p == NULL)
157
                                        p = strchr(bp, '\0');
158
                        }
159
                        assert(p);
160
                        yylval.string = alloc(p - bp + 1);
161
                        strncpy(yylval.string, bp, p - bp);
162
                        yylval.string[p - bp] = 0;
163
                        bp = *p == '"' ? p + 1 : p;
164
                        code++;
165
                        return TEMPLATE;
166
                } else if (isdigit(c)) {
167
                        int n = 0;
168
                        do {
169
                                int d = c - '0';
170
                                if (n > (INT_MAX - d)/10)
171
                                        yyerror("integer greater than %d\n", INT_MAX);
172
                                else
173
                                        n = 10*n + d;
174
                                c = get();
175
                        } while (c != EOF && isdigit(c));
176
                        bp--;
177
                        yylval.n = n;
178
                        return INT;
179
                } else if (isalpha(c)) {
180
                        char *p = bp - 1;
181
                        while (isalpha(*bp) || isdigit(*bp) || *bp == '_')
182
                                bp++;
183
                        yylval.string = alloc(bp - p + 1);
184
                        strncpy(yylval.string, p, bp - p);
185
                        yylval.string[bp - p] = 0;
186
                        return ID;
187
                } else if (isprint(c))
188
                        yyerror("invalid character `%c'\n", c);
189
                else
190
                        yyerror("invalid character `\\%03o'\n", (unsigned char)c);
191
        }
192
        return 0;
193
}
194
 
195
void yywarn(char *fmt, ...) {
196
        va_list ap;
197
 
198
        va_start(ap, fmt);
199
        if (yylineno > 0)
200
                fprintf(stderr, "line %d: ", yylineno);
201
        fprintf(stderr, "warning: ");
202
        vfprintf(stderr, fmt, ap);
203
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.