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

Subversion Repositories eco32

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

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

powered by: WebSVN 2.1.0

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