File: gram.y

package info (click to toggle)
enscript 1.6.5.90-3.2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,456 kB
  • sloc: ansic: 33,708; sh: 5,383; makefile: 649; yacc: 457; lex: 428; perl: 340; lisp: 109; sed: 16
file content (217 lines) | stat: -rw-r--r-- 6,430 bytes parent folder | download | duplicates (5)
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
%{
/* 								-*- c -*-
 * Grammar for states.
 * Copyright (c) 1997-1998 Markku Rossi.
 *
 * Author: Markku Rossi <mtr@iki.fi>
 */

/*
 * This file is part of GNU Enscript.
 *
 * Enscript is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Enscript is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Enscript.  If not, see <http://www.gnu.org/licenses/>.
 */

/*
 * $Id: gram.y,v 1.1.1.1 2003/03/05 07:25:52 mtr Exp $
 */

#include "defs.h"
%}

%union
{
  List *lst;
  Node *node;
  Cons *cons;
  Stmt *stmt;
  Expr *expr;
}

%token <node> tSYMBOL tREGEXP tSTRING tINTEGER tREAL
%token tSUB tSTATE tSTART tSTARTRULES tNAMERULES tBEGIN tEND tRETURN tIF tELSE
%token tLOCAL tWHILE tFOR tEXTENDS

%right '=' tADDASSIGN tSUBASSIGN tMULASSIGN tDIVASSIGN
%right '?' ':'
%left tOR
%left tAND
%left tEQ tNE
%left '<' '>' tGE tLE
%left '+' '-'
%left '*' tDIV
%right '!' tPLUSPLUS tMINUSMINUS
%left '[' ']'

%type <lst> regexp_sym_list symbol_list rest_symbol_list staterules
%type <lst> stmt_list expr_list rest_expr_list locals locals_rest
%type <stmt> stmt
%type <expr> expr cond_expr
%type <cons> staterule local_def

%%

file	: /* empty */
	| file toplevel
	;

toplevel : tSTART '{' stmt_list '}'	{ start_stmts = $3; }
	| tSTARTRULES '{' regexp_sym_list '}'
					{ startrules = $3; }
	| tNAMERULES '{' regexp_sym_list '}'
					{ namerules = $3; }
	| tSTATE tSYMBOL '{' staterules '}'
					{ define_state ($2, NULL, $4); }
	| tSTATE tSYMBOL tEXTENDS tSYMBOL '{' staterules '}'
					{ define_state ($2, $4, $6); }
	| stmt				{ list_append (global_stmts, $1); }
	;

regexp_sym_list : /* empty */ 		{ $$ = list (); }
	| regexp_sym_list tREGEXP tSYMBOL ';'
					{ list_append ($1, cons ($2, $3)); }
	;

staterules : /* empty */ 		{ $$ = list (); }
	| staterules staterule		{ list_append ($1, $2); }

staterule : tBEGIN '{' stmt_list '}' 	{ $$ = cons (RULE_BEGIN, $3); }
	| tEND '{' stmt_list '}' 	{ $$ = cons (RULE_END, $3); }
	| tREGEXP '{' stmt_list '}'	{ $$ = cons ($1, $3); }
	| tSYMBOL '{' stmt_list '}'	{ $$ = cons ($1, $3); }
	;

symbol_list : /* empty */		{ $$ = list (); }
	| rest_symbol_list	 	{ $$ = $1; }
	;

rest_symbol_list : tSYMBOL		{ $$ = list (); list_append ($$, $1); }
	| rest_symbol_list ',' tSYMBOL 	{ list_append ($1, $3); }
	;

locals	: /* empty */			{ $$ = list (); }
	| tLOCAL locals_rest ';'	{ $$ = $2; }
	;

locals_rest : local_def			{ $$ = list (); list_append ($$, $1); }
	| locals_rest ',' local_def	{ list_append ($1, $3); }
	;

local_def : tSYMBOL			{ $$ = cons ($1, NULL); }
	| tSYMBOL '=' expr		{ $$ = cons ($1, $3); }
	;

stmt_list : /* empty */			{ $$ = list (); }
	| stmt_list stmt 		{ list_append ($1, $2); }
	;

stmt	: tRETURN ';'			{ $$ = mk_stmt (sRETURN, NULL, NULL,
							NULL, NULL); }
	| tRETURN expr ';'		{ $$ = mk_stmt (sRETURN, $2, NULL,
							NULL, NULL); }
	| tSUB tSYMBOL '(' symbol_list ')' '{' locals stmt_list '}'
					{ $$ = mk_stmt (sDEFSUB, $2,
							cons (cons ($4, $7),
							      $8),
							NULL, NULL); }
	| '{' stmt_list '}'		{ $$ = mk_stmt (sBLOCK, $2, NULL,
							NULL, NULL); }
	| tIF '(' expr ')' stmt		{ $$ = mk_stmt (sIF, $3, $5, NULL,
							NULL); }
	| tIF '(' expr ')' stmt tELSE stmt
					{ $$ = mk_stmt (sIF, $3, $5, $7,
							NULL); }
	| tWHILE '(' expr ')' stmt 	{ $$ = mk_stmt (sWHILE, $3, $5,
							NULL, NULL); }
	| tFOR '(' cond_expr ';' expr ';' cond_expr ')' stmt
					{ $$ = mk_stmt (sFOR, $3, $5, $7,
							$9); }
	| expr ';'			{ $$ = mk_stmt (sEXPR, $1, NULL,
							NULL, NULL); }
	;

expr	: tSTRING 			{ $$ = mk_expr (eSTRING, $1, NULL,
							NULL); }
	| tREGEXP			{ $$ = mk_expr (eREGEXP, $1, NULL,
							NULL); }
	| tINTEGER			{ $$ = mk_expr (eINTEGER, $1, NULL,
							NULL); }
	| tREAL				{ $$ = mk_expr (eREAL, $1, NULL,
							NULL); }
	| tSYMBOL			{ $$ = mk_expr (eSYMBOL, $1, NULL,
							NULL); }
	| '!' expr			{ $$ = mk_expr (eNOT, $2, NULL,
							NULL); }
	| expr tAND expr		{ $$ = mk_expr (eAND, $1, $3, NULL); }
	| expr tOR expr			{ $$ = mk_expr (eOR, $1, $3, NULL); }
	| tSYMBOL '(' expr_list ')'	{ $$ = mk_expr (eFCALL, $1, $3,
							NULL); }
	| tSYMBOL '=' expr		{ $$ = mk_expr (eASSIGN, $1, $3,
							NULL); }
	| tSYMBOL tADDASSIGN expr	{ $$ = mk_expr (eADDASSIGN, $1, $3,
							NULL); }
	| tSYMBOL tSUBASSIGN expr	{ $$ = mk_expr (eSUBASSIGN, $1, $3,
							NULL); }
	| tSYMBOL tMULASSIGN expr	{ $$ = mk_expr (eMULASSIGN, $1, $3,
							NULL); }
	| tSYMBOL tDIVASSIGN expr	{ $$ = mk_expr (eDIVASSIGN, $1, $3,
							NULL); }
	| tSYMBOL tPLUSPLUS		{ $$ = mk_expr (ePOSTFIXADD, $1, NULL,
							NULL); }
	| tSYMBOL tMINUSMINUS		{ $$ = mk_expr (ePOSTFIXSUB, $1, NULL,
							NULL); }
	| tPLUSPLUS tSYMBOL		{ $$ = mk_expr (ePREFIXADD, $2, NULL,
							NULL); }
	| tMINUSMINUS tSYMBOL		{ $$ = mk_expr (ePREFIXSUB, $2, NULL,
							NULL); }
	| expr '[' expr ']' '=' expr	{ $$ = mk_expr (eARRAYASSIGN, $1, $3,
							$6); }
	| '(' expr ')'			{ $$ = $2; }
	| expr '[' expr ']'		{ $$ = mk_expr (eARRAYREF, $1, $3,
							NULL); }
	| expr '?' expr ':' expr	{ $$ = mk_expr (eQUESTCOLON, $1, $3,
							$5); }
	| expr '*' expr			{ $$ = mk_expr (eMULT, $1, $3, NULL); }
	| expr tDIV expr		{ $$ = mk_expr (eDIV, $1, $3, NULL); }
	| expr '+' expr			{ $$ = mk_expr (ePLUS, $1, $3, NULL); }
	| expr '-' expr			{ $$ = mk_expr (eMINUS, $1, $3,
							NULL); }
	| expr '<' expr			{ $$ = mk_expr (eLT, $1, $3, NULL); }
	| expr '>' expr			{ $$ = mk_expr (eGT, $1, $3, NULL); }
	| expr tEQ expr			{ $$ = mk_expr (eEQ, $1, $3, NULL); }
	| expr tNE expr			{ $$ = mk_expr (eNE, $1, $3, NULL); }
	| expr tGE expr			{ $$ = mk_expr (eGE, $1, $3, NULL); }
	| expr tLE expr			{ $$ = mk_expr (eLE, $1, $3, NULL); }
	;

cond_expr : /* empty */			{ $$ = NULL; }
	| expr				{ $$ = $1; }
	;

expr_list : /* empty */ 		{ $$ = list (); }
	| rest_expr_list 		{ $$ = $1; }
	;

rest_expr_list: expr 	 		{ $$ = list (); list_append ($$, $1); }
	| rest_expr_list ',' expr  	{ list_append ($1, $3); }
	;

%%

void
yyerror (msg)
     char *msg;
{
  fprintf (stderr, "%s:%d: %s\n", yyin_name, linenum, msg);
}