source: trunk/essentials/sys-devel/flex/parse.y@ 3212

Last change on this file since 3212 was 3043, checked in by bird, 19 years ago

-> essentials

File size: 21.1 KB
Line 
1/* parse.y - parser for flex input */
2
3%token CHAR NUMBER SECTEND SCDECL XSCDECL NAME PREVCCL EOF_OP
4%token OPTION_OP OPT_OUTFILE OPT_PREFIX OPT_YYCLASS OPT_HEADER
5%token OPT_TABLES
6
7%token CCE_ALNUM CCE_ALPHA CCE_BLANK CCE_CNTRL CCE_DIGIT CCE_GRAPH
8%token CCE_LOWER CCE_PRINT CCE_PUNCT CCE_SPACE CCE_UPPER CCE_XDIGIT
9
10/*
11 *POSIX and AT&T lex place the
12 * precedence of the repeat operator, {}, below that of concatenation.
13 * Thus, ab{3} is ababab. Most other POSIX utilities use an Extended
14 * Regular Expression (ERE) precedence that has the repeat operator
15 * higher than concatenation. This causes ab{3} to yield abbb.
16 *
17 * In order to support the POSIX and AT&T precedence and the flex
18 * precedence we define two token sets for the begin and end tokens of
19 * the repeat operator, '{' and '}'. The lexical scanner chooses
20 * which tokens to return based on whether posix_compat or lex_compat
21 * are specified. Specifying either posix_compat or lex_compat will
22 * cause flex to parse scanner files as per the AT&T and
23 * POSIX-mandated behavior.
24 */
25
26%token BEGIN_REPEAT_POSIX END_REPEAT_POSIX BEGIN_REPEAT_FLEX END_REPEAT_FLEX
27
28
29%{
30/* Copyright (c) 1990 The Regents of the University of California. */
31/* All rights reserved. */
32
33/* This code is derived from software contributed to Berkeley by */
34/* Vern Paxson. */
35
36/* The United States Government has rights in this work pursuant */
37/* to contract no. DE-AC03-76SF00098 between the United States */
38/* Department of Energy and the University of California. */
39
40/* This file is part of flex. */
41
42/* Redistribution and use in source and binary forms, with or without */
43/* modification, are permitted provided that the following conditions */
44/* are met: */
45
46/* 1. Redistributions of source code must retain the above copyright */
47/* notice, this list of conditions and the following disclaimer. */
48/* 2. Redistributions in binary form must reproduce the above copyright */
49/* notice, this list of conditions and the following disclaimer in the */
50/* documentation and/or other materials provided with the distribution. */
51
52/* Neither the name of the University nor the names of its contributors */
53/* may be used to endorse or promote products derived from this software */
54/* without specific prior written permission. */
55
56/* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
57/* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
58/* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
59/* PURPOSE. */
60
61/* Some versions of bison are broken in that they use alloca() but don't
62 * declare it properly. The following is the patented (just kidding!)
63 * #ifdef chud to fix the problem, courtesy of Francois Pinard.
64 */
65#ifdef YYBISON
66/* AIX requires this to be the first thing in the file. What a piece. */
67# ifdef _AIX
68 #pragma alloca
69# endif
70#endif
71
72#include "flexdef.h"
73#include "tables.h"
74
75/* The remainder of the alloca() cruft has to come after including flexdef.h,
76 * so HAVE_ALLOCA_H is (possibly) defined.
77 */
78#ifdef YYBISON
79# ifdef __GNUC__
80# ifndef alloca
81# define alloca __builtin_alloca
82# endif
83# else
84# if HAVE_ALLOCA_H
85# include <alloca.h>
86# else
87# ifdef __hpux
88void *alloca ();
89# else
90# ifdef __TURBOC__
91# include <malloc.h>
92# else
93char *alloca ();
94# endif
95# endif
96# endif
97# endif
98#endif
99
100/* Bletch, ^^^^ that was ugly! */
101
102
103int pat, scnum, eps, headcnt, trailcnt, anyccl, lastchar, i, rulelen;
104int trlcontxt, xcluflg, currccl, cclsorted, varlength, variable_trail_rule;
105
106int *scon_stk;
107int scon_stk_ptr;
108
109static int madeany = false; /* whether we've made the '.' character class */
110int previous_continued_action; /* whether the previous rule's action was '|' */
111
112#define format_warn3(fmt, a1, a2) \
113 do{ \
114 char fw3_msg[MAXLINE];\
115 snprintf( fw3_msg, MAXLINE,(fmt), (a1), (a2) );\
116 warn( fw3_msg );\
117 }while(0)
118
119/* Expand a POSIX character class expression. */
120#define CCL_EXPR(func) \
121 do{ \
122 int c; \
123 for ( c = 0; c < csize; ++c ) \
124 if ( isascii(c) && func(c) ) \
125 ccladd( currccl, c ); \
126 }while(0)
127
128/* While POSIX defines isblank(), it's not ANSI C. */
129#define IS_BLANK(c) ((c) == ' ' || (c) == '\t')
130
131/* On some over-ambitious machines, such as DEC Alpha's, the default
132 * token type is "long" instead of "int"; this leads to problems with
133 * declaring yylval in flexdef.h. But so far, all the yacc's I've seen
134 * wrap their definitions of YYSTYPE with "#ifndef YYSTYPE"'s, so the
135 * following should ensure that the default token type is "int".
136 */
137#define YYSTYPE int
138
139%}
140
141%%
142goal : initlex sect1 sect1end sect2 initforrule
143 { /* add default rule */
144 int def_rule;
145
146 pat = cclinit();
147 cclnegate( pat );
148
149 def_rule = mkstate( -pat );
150
151 /* Remember the number of the default rule so we
152 * don't generate "can't match" warnings for it.
153 */
154 default_rule = num_rules;
155
156 finish_rule( def_rule, false, 0, 0, 0);
157
158 for ( i = 1; i <= lastsc; ++i )
159 scset[i] = mkbranch( scset[i], def_rule );
160
161 if ( spprdflt )
162 add_action(
163 "YY_FATAL_ERROR( \"flex scanner jammed\" )" );
164 else
165 add_action( "ECHO" );
166
167 add_action( ";\n\tYY_BREAK\n" );
168 }
169 ;
170
171initlex :
172 { /* initialize for processing rules */
173
174 /* Create default DFA start condition. */
175 scinstal( "INITIAL", false );
176 }
177 ;
178
179sect1 : sect1 startconddecl namelist1
180 | sect1 options
181 |
182 | error
183 { synerr( _("unknown error processing section 1") ); }
184 ;
185
186sect1end : SECTEND
187 {
188 check_options();
189 scon_stk = allocate_integer_array( lastsc + 1 );
190 scon_stk_ptr = 0;
191 }
192 ;
193
194startconddecl : SCDECL
195 { xcluflg = false; }
196
197 | XSCDECL
198 { xcluflg = true; }
199 ;
200
201namelist1 : namelist1 NAME
202 { scinstal( nmstr, xcluflg ); }
203
204 | NAME
205 { scinstal( nmstr, xcluflg ); }
206
207 | error
208 { synerr( _("bad start condition list") ); }
209 ;
210
211options : OPTION_OP optionlist
212 ;
213
214optionlist : optionlist option
215 |
216 ;
217
218option : OPT_OUTFILE '=' NAME
219 {
220 outfilename = copy_string( nmstr );
221 did_outfilename = 1;
222 }
223 | OPT_PREFIX '=' NAME
224 { prefix = copy_string( nmstr ); }
225 | OPT_YYCLASS '=' NAME
226 { yyclass = copy_string( nmstr ); }
227 | OPT_HEADER '=' NAME
228 { headerfilename = copy_string( nmstr ); }
229 | OPT_TABLES '=' NAME
230 { tablesext = true; tablesfilename = copy_string( nmstr ); }
231 ;
232
233sect2 : sect2 scon initforrule flexrule '\n'
234 { scon_stk_ptr = $2; }
235 | sect2 scon '{' sect2 '}'
236 { scon_stk_ptr = $2; }
237 |
238 ;
239
240initforrule :
241 {
242 /* Initialize for a parse of one rule. */
243 trlcontxt = variable_trail_rule = varlength = false;
244 trailcnt = headcnt = rulelen = 0;
245 current_state_type = STATE_NORMAL;
246 previous_continued_action = continued_action;
247 in_rule = true;
248
249 new_rule();
250 }
251 ;
252
253flexrule : '^' rule
254 {
255 pat = $2;
256 finish_rule( pat, variable_trail_rule,
257 headcnt, trailcnt , previous_continued_action);
258
259 if ( scon_stk_ptr > 0 )
260 {
261 for ( i = 1; i <= scon_stk_ptr; ++i )
262 scbol[scon_stk[i]] =
263 mkbranch( scbol[scon_stk[i]],
264 pat );
265 }
266
267 else
268 {
269 /* Add to all non-exclusive start conditions,
270 * including the default (0) start condition.
271 */
272
273 for ( i = 1; i <= lastsc; ++i )
274 if ( ! scxclu[i] )
275 scbol[i] = mkbranch( scbol[i],
276 pat );
277 }
278
279 if ( ! bol_needed )
280 {
281 bol_needed = true;
282
283 if ( performance_report > 1 )
284 pinpoint_message(
285 "'^' operator results in sub-optimal performance" );
286 }
287 }
288