1228072Sbapt/* yylex - scanner front-end for flex */
2228072Sbapt
3228072Sbapt/*  Copyright (c) 1990 The Regents of the University of California. */
4228072Sbapt/*  All rights reserved. */
5228072Sbapt
6228072Sbapt/*  This code is derived from software contributed to Berkeley by */
7228072Sbapt/*  Vern Paxson. */
8228072Sbapt
9228072Sbapt/*  The United States Government has rights in this work pursuant */
10228072Sbapt/*  to contract no. DE-AC03-76SF00098 between the United States */
11228072Sbapt/*  Department of Energy and the University of California. */
12228072Sbapt
13228072Sbapt/*  This file is part of flex. */
14228072Sbapt
15228072Sbapt/*  Redistribution and use in source and binary forms, with or without */
16228072Sbapt/*  modification, are permitted provided that the following conditions */
17228072Sbapt/*  are met: */
18228072Sbapt
19228072Sbapt/*  1. Redistributions of source code must retain the above copyright */
20228072Sbapt/*     notice, this list of conditions and the following disclaimer. */
21228072Sbapt/*  2. Redistributions in binary form must reproduce the above copyright */
22228072Sbapt/*     notice, this list of conditions and the following disclaimer in the */
23228072Sbapt/*     documentation and/or other materials provided with the distribution. */
24228072Sbapt
25228072Sbapt/*  Neither the name of the University nor the names of its contributors */
26228072Sbapt/*  may be used to endorse or promote products derived from this software */
27228072Sbapt/*  without specific prior written permission. */
28228072Sbapt
29228072Sbapt/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30228072Sbapt/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31228072Sbapt/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
32228072Sbapt/*  PURPOSE. */
33228072Sbapt
34228072Sbapt#include <ctype.h>
35228072Sbapt#include "flexdef.h"
36228072Sbapt#include "parse.h"
37228072Sbapt
38228072Sbapt
39228072Sbapt/* yylex - scan for a regular expression token */
40228072Sbapt
41228072Sbaptint     yylex ()
42228072Sbapt{
43228072Sbapt	int     toktype;
44228072Sbapt	static int beglin = false;
45228072Sbapt	extern char *yytext;
46228072Sbapt
47228072Sbapt	if (eofseen)
48228072Sbapt		toktype = EOF;
49228072Sbapt	else
50228072Sbapt		toktype = flexscan ();
51228072Sbapt
52228072Sbapt	if (toktype == EOF || toktype == 0) {
53228072Sbapt		eofseen = 1;
54228072Sbapt
55228072Sbapt		if (sectnum == 1) {
56228072Sbapt			synerr (_("premature EOF"));
57228072Sbapt			sectnum = 2;
58228072Sbapt			toktype = SECTEND;
59228072Sbapt		}
60228072Sbapt
61228072Sbapt		else
62228072Sbapt			toktype = 0;
63228072Sbapt	}
64228072Sbapt
65228072Sbapt	if (trace) {
66228072Sbapt		if (beglin) {
67228072Sbapt			fprintf (stderr, "%d\t", num_rules + 1);
68228072Sbapt			beglin = 0;
69228072Sbapt		}
70228072Sbapt
71228072Sbapt		switch (toktype) {
72228072Sbapt		case '<':
73228072Sbapt		case '>':
74228072Sbapt		case '^':
75228072Sbapt		case '$':
76228072Sbapt		case '"':
77228072Sbapt		case '[':
78228072Sbapt		case ']':
79228072Sbapt		case '{':
80228072Sbapt		case '}':
81228072Sbapt		case '|':
82228072Sbapt		case '(':
83228072Sbapt		case ')':
84228072Sbapt		case '-':
85228072Sbapt		case '/':
86228072Sbapt		case '\\':
87228072Sbapt		case '?':
88228072Sbapt		case '.':
89228072Sbapt		case '*':
90228072Sbapt		case '+':
91228072Sbapt		case ',':
92228072Sbapt			(void) putc (toktype, stderr);
93228072Sbapt			break;
94228072Sbapt
95228072Sbapt		case '\n':
96228072Sbapt			(void) putc ('\n', stderr);
97228072Sbapt
98228072Sbapt			if (sectnum == 2)
99228072Sbapt				beglin = 1;
100228072Sbapt
101228072Sbapt			break;
102228072Sbapt
103228072Sbapt		case SCDECL:
104228072Sbapt			fputs ("%s", stderr);
105228072Sbapt			break;
106228072Sbapt
107228072Sbapt		case XSCDECL:
108228072Sbapt			fputs ("%x", stderr);
109228072Sbapt			break;
110228072Sbapt
111228072Sbapt		case SECTEND:
112228072Sbapt			fputs ("%%\n", stderr);
113228072Sbapt
114228072Sbapt			/* We set beglin to be true so we'll start
115228072Sbapt			 * writing out numbers as we echo rules.
116228072Sbapt			 * flexscan() has already assigned sectnum.
117228072Sbapt			 */
118228072Sbapt			if (sectnum == 2)
119228072Sbapt				beglin = 1;
120228072Sbapt
121228072Sbapt			break;
122228072Sbapt
123228072Sbapt		case NAME:
124228072Sbapt			fprintf (stderr, "'%s'", nmstr);
125228072Sbapt			break;
126228072Sbapt
127228072Sbapt		case CHAR:
128228072Sbapt			switch (yylval) {
129228072Sbapt			case '<':
130228072Sbapt			case '>':
131228072Sbapt			case '^':
132228072Sbapt			case '$':
133228072Sbapt			case '"':
134228072Sbapt			case '[':
135228072Sbapt			case ']':
136228072Sbapt			case '{':
137228072Sbapt			case '}':
138228072Sbapt			case '|':
139228072Sbapt			case '(':
140228072Sbapt			case ')':
141228072Sbapt			case '-':
142228072Sbapt			case '/':
143228072Sbapt			case '\\':
144228072Sbapt			case '?':
145228072Sbapt			case '.':
146228072Sbapt			case '*':
147228072Sbapt			case '+':
148228072Sbapt			case ',':
149228072Sbapt				fprintf (stderr, "\\%c", yylval);
150228072Sbapt				break;
151228072Sbapt
152228072Sbapt			default:
153228072Sbapt				if (!isascii (yylval) || !isprint (yylval))
154228072Sbapt					fprintf (stderr,
155228072Sbapt						 "\\%.3o",
156228072Sbapt						 (unsigned int) yylval);
157228072Sbapt				else
158228072Sbapt					(void) putc (yylval, stderr);
159228072Sbapt				break;
160228072Sbapt			}
161228072Sbapt
162228072Sbapt			break;
163228072Sbapt
164228072Sbapt		case NUMBER:
165228072Sbapt			fprintf (stderr, "%d", yylval);
166228072Sbapt			break;
167228072Sbapt
168228072Sbapt		case PREVCCL:
169228072Sbapt			fprintf (stderr, "[%d]", yylval);
170228072Sbapt			break;
171228072Sbapt
172228072Sbapt		case EOF_OP:
173228072Sbapt			fprintf (stderr, "<<EOF>>");
174228072Sbapt			break;
175228072Sbapt
176228072Sbapt		case OPTION_OP:
177228072Sbapt			fprintf (stderr, "%s ", yytext);
178228072Sbapt			break;
179228072Sbapt
180228072Sbapt		case OPT_OUTFILE:
181228072Sbapt		case OPT_PREFIX:
182228072Sbapt		case CCE_ALNUM:
183228072Sbapt		case CCE_ALPHA:
184228072Sbapt		case CCE_BLANK:
185228072Sbapt		case CCE_CNTRL:
186228072Sbapt		case CCE_DIGIT:
187228072Sbapt		case CCE_GRAPH:
188228072Sbapt		case CCE_LOWER:
189228072Sbapt		case CCE_PRINT:
190228072Sbapt		case CCE_PUNCT:
191228072Sbapt		case CCE_SPACE:
192228072Sbapt		case CCE_UPPER:
193228072Sbapt		case CCE_XDIGIT:
194228072Sbapt			fprintf (stderr, "%s", yytext);
195228072Sbapt			break;
196228072Sbapt
197228072Sbapt		case 0:
198228072Sbapt			fprintf (stderr, _("End Marker\n"));
199228072Sbapt			break;
200228072Sbapt
201228072Sbapt		default:
202228072Sbapt			fprintf (stderr,
203228072Sbapt				 _
204228072Sbapt				 ("*Something Weird* - tok: %d val: %d\n"),
205228072Sbapt				 toktype, yylval);
206228072Sbapt			break;
207228072Sbapt		}
208228072Sbapt	}
209228072Sbapt
210228072Sbapt	return toktype;
211228072Sbapt}
212