summaryrefslogtreecommitdiff
path: root/zwgc/parser.y
blob: 30f4088d5ca5f679e328f7a84d64b968ac5a42fa (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
%{
/* This file is part of the Project Athena Zephyr Notification System.
 * It is one of the source files comprising zwgc, the Zephyr WindowGram
 * client.
 *
 *      Created by:     Marc Horowitz <marc@athena.mit.edu>
 *
 *      $Id$
 *
 *      Copyright (c) 1989 by the Massachusetts Institute of Technology.
 *      For copying and distribution information, see the file
 *      "mit-copyright.h".
 */

#include <sysdep.h>

#if (!defined(lint) && !defined(SABER))
static const char rcsid_parser_y[] = "$Id$";
#endif

#include <zephyr/mit-copyright.h>

/* Saber-C suppressions because yacc loses */

/*SUPPRESS 288*/
/*SUPPRESS 287*/

#include <stdio.h>
#include "lexer.h"
#include "parser.h"
#include "node.h"
#include "zwgc.h"

static void yyerror(char *);

/*
 * the_program - local variable used to communicate the program's node
 *               representation from the program action to the parse_file
 *               function.
 */

static Node *the_program;
%}

%union{
    char *text;
    struct _Node *node;
}

%start program

%token  ERROR
%token  <text>    VARNAME VARREF STRING SHOW

%token  APPENDPORT BUFFER BREAK CLOSEINPUT CLOSEOUTPUT
%token  CLOSEPORT CASE CLEARBUF DEFAULT DISPLAY DO DOWNCASE
%token  ELSE ELSEIF ENDCASE ENDIF ENDWHILE EXEC EXECPORT EXIT
%token  FIELDS GET GETENV IF INPUTPORT LANY LBREAK LSPAN
%token  MATCH NOOP NOT OUTPUTPORT PRINT PROTECT VERBATIM PUT RANY RBREAK
%token  RSPAN SET SUBSTITUTE THEN UPCASE WHILE ZVAR STYLESTRIP

%type <node> expr varname string
%type <node> exprlist comma_exprlist varnamelist
%type <node> statement statements program elseparts elseifparts
%type <node> match matchlist

%left '|'
%left '&'
%left EQ NEQ REGEQ REGNEQ
%left '+'
%left '!'

%%

/*
 * A program is simply a list of statements: (may be NULL if no statements...)
 */
program : statements
        { the_program = reverse_list_of_nodes($1);
	  $$ = the_program; }
        ;

varname : VARNAME
       { $$ = node_create_string_constant(VARNAME_OPCODE, $1); }
        ;

string : STRING
       { $$ = node_create_string_constant(STRING_CONSTANT_OPCODE, $1); }
       ;

expr : '(' expr ')'
        { $$ = $2; }

     | string
        { $$ = $1; }
     | VARREF
       { $$ = node_create_string_constant(VARREF_OPCODE, $1); }

     | '!' expr
        { $$ = node_create_unary(NOT_OPCODE, $2); }

     | expr '+' expr
        { $$ = node_create_binary(PLUS_OPCODE, $1, $3); }
     | expr '|' expr                             /* note "or" == '|' */
        { $$ = node_create_binary(OR_OPCODE, $1, $3); }
     | expr '&' expr                             /* note "and" == '&' */
        { $$ = node_create_binary(AND_OPCODE, $1, $3); }
     | expr EQ expr
        { $$ = node_create_binary(EQ_OPCODE, $1, $3); }
     | expr NEQ expr
        { $$ = node_create_binary(NEQ_OPCODE, $1, $3); }
     | expr REGEQ expr
        { $$ = node_create_binary(REGEQ_OPCODE, $1, $3); }
     | expr REGNEQ expr
        { $$ = node_create_binary(REGNEQ_OPCODE, $1, $3); }

     | BUFFER '(' ')'
        { $$ = node_create_noary(BUFFER_OPCODE); }

     | SUBSTITUTE '(' expr ')'
        { $$ = node_create_unary(SUBSTITUTE_OPCODE, $3); }
     | PROTECT '(' expr ')'
	{ $$ = node_create_unary(PROTECT_OPCODE, $3); }
     | VERBATIM '(' expr ')'
	{ $$ = node_create_unary(VERBATIM_OPCODE, $3); }
     | GETENV '(' expr ')'
        { $$ = node_create_unary(GETENV_OPCODE, $3); }
     | UPCASE '(' expr ')'
        { $$ = node_create_unary(UPCASE_OPCODE, $3); }
     | DOWNCASE '(' expr ')'
        { $$ = node_create_unary(DOWNCASE_OPCODE, $3); }
     | ZVAR '(' expr ')'
        { $$ = node_create_unary(ZVAR_OPCODE, $3); }
     | GET '(' expr ')'
        { $$ = node_create_unary(GET_OPCODE, $3); }
     | STYLESTRIP '(' expr ')'
        { $$ = node_create_unary(STYLESTRIP_OPCODE, $3); }

     | LANY '(' expr ',' expr ')'
        { $$ = node_create_binary(LANY_OPCODE, $3, $5 ); }
     | RANY '(' expr ',' expr ')'
        { $$ = node_create_binary(RANY_OPCODE, $3, $5 ); }
     | LBREAK '(' expr ',' expr ')'
        { $$ = node_create_binary(LBREAK_OPCODE, $3, $5 ); }
     | RBREAK '(' expr ',' expr ')'
        { $$ = node_create_binary(RBREAK_OPCODE, $3, $5 ); }
     | LSPAN '(' expr ',' expr ')'
        { $$ = node_create_binary(LSPAN_OPCODE, $3, $5 ); }
     | RSPAN '(' expr ',' expr ')'
        { $$ = node_create_binary(RSPAN_OPCODE, $3, $5 ); }
     ;

statement : NOOP
              { $$ = node_create_noary(NOOP_OPCODE); }
          | SET varname '=' expr
              { $$ = node_create_binary(SET_OPCODE, $2, $4); }
	  | FIELDS varnamelist
              { $$ = node_create_unary(FIELDS_OPCODE,
				       reverse_list_of_nodes($2)); }

         /*
  	  * Output to & control of output buffer statements:
	  */
 	  | PRINT exprlist
              { $$ = node_create_unary(PRINT_OPCODE,
				       reverse_list_of_nodes($2)); }
	  | SHOW
              { $$ = node_create_unary(PRINT_OPCODE,
		       node_create_unary(SUBSTITUTE_OPCODE,
			 node_create_string_constant(STRING_CONSTANT_OPCODE,
						     $1))); }
          | CLEARBUF
	      { $$ = node_create_noary(CLEARBUF_OPCODE); }

          /*
	   * Statements to manage ports:
	   */
          | APPENDPORT expr expr
              { $$ = node_create_binary(APPENDPORT_OPCODE, $2, $3); }
          | EXECPORT expr expr exprlist
              { $3->next = reverse_list_of_nodes($4);
		$$ = node_create_binary(EXECPORT_OPCODE, $2, $3); }
          | INPUTPORT expr expr
              { $$ = node_create_binary(INPUTPORT_OPCODE, $2, $3); }
          | OUTPUTPORT expr expr
              { $$ = node_create_binary(OUTPUTPORT_OPCODE, $2, $3); }
	  | PUT expr exprlist
              { $$ = node_create_binary(PUT_OPCODE, $2,
					reverse_list_of_nodes($3)); }
	  | PUT
              { $$ = node_create_binary(PUT_OPCODE, 0, 0); }
          | CLOSEINPUT expr
              { $$ = node_create_unary(CLOSEINPUT_OPCODE, $2); }
          | CLOSEOUTPUT expr
              { $$ = node_create_unary(CLOSEOUTPUT_OPCODE, $2); }
          | CLOSEPORT expr
              { $$ = node_create_unary(CLOSEPORT_OPCODE, $2); }

          /*
	   * Statements to run subprocesses without I/O to them:
	   */
	  | EXEC expr exprlist
              { $2->next = reverse_list_of_nodes($3);
		$$ = node_create_unary(EXEC_OPCODE, $2); }

          /*
	   * Control statements:
	   */
          | IF expr THEN statements elseparts ENDIF
              { Node *n = node_create_binary(IF_OPCODE, $2,
					     reverse_list_of_nodes($4));
		n->next = $5;
		$$ = node_create_unary(IF_STMT_OPCODE, n); }
	  | CASE expr matchlist ENDCASE
              { $$ = node_create_binary(CASE_OPCODE, $2,
					reverse_list_of_nodes($3)); }
	  | WHILE expr DO statements ENDWHILE
              { $$ = node_create_binary(WHILE_OPCODE, $2,
					reverse_list_of_nodes($4)); }
          | BREAK
              { $$ = node_create_noary(BREAK_OPCODE); }
          | EXIT
              { $$ = node_create_noary(EXIT_OPCODE); }
	  ;

elseparts : elseifparts
                { $$ = reverse_list_of_nodes($1); }
          | elseifparts ELSE statements
                { $$ = node_create_binary(ELSE_OPCODE, 0,
					  reverse_list_of_nodes($3));
		  $$->next = $1;
	          $$ = reverse_list_of_nodes($$); }
          ;

/* elseifparts needs to be reversed before using... */
elseifparts : /* empty */
                { $$ = 0; }
            | elseifparts ELSEIF expr THEN statements
                { $$ = node_create_binary(ELSEIF_OPCODE, $3,
					  reverse_list_of_nodes($5));
		  $$->next = $1; }
            ;

match : MATCH comma_exprlist statements
                { $$ = node_create_binary(MATCHLIST_OPCODE,
					  reverse_list_of_nodes($2),
					  reverse_list_of_nodes($3)); }
      | DEFAULT statements
                { $$ = node_create_binary(DEFAULT_OPCODE, 0,
					  reverse_list_of_nodes($2)); }
      ;

/*
 * Various lists of non-terminals like expr's and varname's.  Each is
 * built up as a linked list using the nodes' next fields.  To prevent
 * Yacc stack overflow on long lists, these are put on the linked list
 * BACKWARDS.  The user of these must first call reverse_list_of_nodes
 * on one of these before using it.  All except comma_exprlist
 * allow 0 elements on the list in which case their value is NULL.
 * (comma_exprlist requires at least one element)
 */

exprlist : /* empty */
             { $$ = 0; }
	 | exprlist expr
             { $$ = $2;
	       $$->next = $1; }
	 ;

comma_exprlist : expr
                 { $$ = $1; }
               | comma_exprlist ',' expr
                 { $$ = $3;
		   $$->next = $1; }
	       ;

varnamelist : /* empty */
             { $$ = 0; }
            | varnamelist varname
             { $$ = $2;
	       $$->next = $1; }
	    ;

matchlist : /* empty */
                { $$ = 0; }
          | matchlist match
                { $$ = $2;
		  $$->next = $1; }
          ;

statements : /* empty */
        { $$ = 0; }
           | statements statement
        { $$ = $2;
	  $$->next = $1; }
           ;

%%

/*
 * error_occured - Set to true when a parse error is reported.  If it is false
 *                 at the time a parse error is reported, a message is
 *                 printed on stderr.  See report_parse_error for more
 *                 details.
 */

static int error_occured = 0;

/*
 *  Parser-Lexer Internal Routine:
 *
 *    void report_parse_error(char *error_message, int line_number)
 *        Modifies: error_occured, stderr
 *        Effects: This routine is called to report a parser or lexer
 *                 error.  Error_message is the error message and line_number
 *                 the line number it occured on.  The reported error message
 *                 is of the form "....<error_message> on line <line #>.\n".
 *                 This routine sets error_occured (local to parser.y) to
 *                 true.  If it was previously false, the error message
 *                 is reported to the user via stderr. 
 */

void
report_parse_error(char *error_message,
		   int line_number)
{
    if (error_occured)
      return;
    error_occured = 1;

    fprintf(stderr, "zwgc: error in description file: %s on line %d.\n",
	    error_message, line_number);
    fflush(stderr);
}

/*
 *  yyerror - internal routine - used by yacc to report syntax errors and
 *            stack overflow errors.
 */
 
static void yyerror(char *message)
{
    report_parse_error(message, yylineno);
}

/*
 *    struct _Node *parse_file(FILE *input_file)
 *        Requires: input_file is opened for reading, no pointers to
 *                  existing nodes will ever be dereferened.
 *        Modifies: *input_file, stderr, all existing nodes
 *        Effects: First this routine destroys all nodes.  Then it parses
 *                 input_file as a zwgc description langauge file.  If
 *                 an error is encountered, an error message is printed
 *                 on stderr and NULL is returned.  If no error is
 *                 encountered, a pointer to the node representation of
 *                 the parsed program is returned, suitable for passing to
 *                 exec.c.  Note that NULL will also be returned for a
 *                 empty file & is a valid program.  Either way, input_file
 *                 is closed before this routine returns.
 */

struct _Node *
parse_file(FILE *input_file)
{
    the_program = NULL;
    error_occured = 0;
    node_DestroyAllNodes();

    lex_open(input_file);
    yyparse();
    fclose(input_file);

    if (error_occured) {
	node_DestroyAllNodes();
	the_program = NULL;
    }

#ifdef DEBUG
    if (zwgc_debug) {
	printf("****************************************************************************\n");
	node_display(the_program);
	printf("****************************************************************************\n");
    }
#endif
    
    return(the_program);
}