1/* yylex - scanner front-end for flex */
2
3/*  Copyright (c) 1990 The Regents of the University of California. */
4/*  All rights reserved. */
5
6/*  This code is derived from software contributed to Berkeley by */
7/*  Vern Paxson. */
8
9/*  The United States Government has rights in this work pursuant */
10/*  to contract no. DE-AC03-76SF00098 between the United States */
11/*  Department of Energy and the University of California. */
12
13/*  This file is part of flex. */
14
15/*  Redistribution and use in source and binary forms, with or without */
16/*  modification, are permitted provided that the following conditions */
17/*  are met: */
18
19/*  1. Redistributions of source code must retain the above copyright */
20/*     notice, this list of conditions and the following disclaimer. */
21/*  2. Redistributions in binary form must reproduce the above copyright */
22/*     notice, this list of conditions and the following disclaimer in the */
23/*     documentation and/or other materials provided with the distribution. */
24
25/*  Neither the name of the University nor the names of its contributors */
26/*  may be used to endorse or promote products derived from this software */
27/*  without specific prior written permission. */
28
29/*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30/*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
32/*  PURPOSE. */
33
34#include <ctype.h>
35#include "flexdef.h"
36#include "parse.h"
37
38
39/* yylex - scan for a regular expression token */
40
41int     yylex ()
42{
43	int     toktype;
44	static int beglin = false;
45	extern char *yytext;
46
47	if (eofseen)
48		toktype = EOF;
49	else
50		toktype = flexscan ();
51
52	if (toktype == EOF || toktype == 0) {
53		eofseen = 1;
54
55		if (sectnum == 1) {
56			synerr (_("premature EOF"));
57			sectnum = 2;
58			toktype = SECTEND;
59		}
60
61		else
62			toktype = 0;
63	}
64
65	if (trace) {
66		if (beglin) {
67			fprintf (stderr, "%d\t", num_rules + 1);
68			beglin = 0;
69		}
70
71		switch (toktype) {
72		case '<':
73		case '>':
74		case '^':
75		case '$':
76		case '"':
77		case '[':
78		case ']':
79		case '{':
80		case '}':
81		case '|':
82		case '(':
83		case ')':
84		case '-':
85		case '/':
86		case '\\':
87		case '?':
88		case '.':
89		case '*':
90		case '+':
91		case ',':
92			(void) putc (toktype, stderr);
93			break;
94
95		case '\n':
96			(void) putc ('\n', stderr);
97
98			if (sectnum == 2)
99				beglin = 1;
100
101			break;
102
103		case SCDECL:
104			fputs ("%s", stderr);
105			break;
106
107		case XSCDECL:
108			fputs ("%x", stderr);
109			break;
110
111		case SECTEND:
112			fputs ("%%\n", stderr);
113
114			/* We set beglin to be true so we'll start
115			 * writing out numbers as we echo rules.
116			 * flexscan() has already assigned sectnum.
117			 */
118			if (sectnum == 2)
119				beglin = 1;
120
121			break;
122
123		case NAME:
124			fprintf (stderr, "'%s'", nmstr);
125			break;
126
127		case CHAR:
128			switch (yylval) {
129			case '<':
130			case '>':
131			case '^':
132			case '$':
133			case '"':
134			case '[':
135			case ']':
136			case '{':
137			case '}':
138			case '|':
139			case '(':
140			case ')':
141			case '-':
142			case '/':
143			case '\\':
144			case '?':
145			case '.':
146			case '*':
147			case '+':
148			case ',':
149				fprintf (stderr, "\\%c", yylval);
150				break;
151
152			default:
153				if (!isascii (yylval) || !isprint (yylval))
154					fprintf (stderr,
155						 "\\%.3o",
156						 (unsigned int) yylval);
157				else
158					(void) putc (yylval, stderr);
159				break;
160			}
161
162			break;
163
164		case NUMBER:
165			fprintf (stderr, "%d", yylval);
166			break;
167
168		case PREVCCL:
169			fprintf (stderr, "[%d]", yylval);
170			break;
171
172		case EOF_OP:
173			fprintf (stderr, "<<EOF>>");
174			break;
175
176		case OPTION_OP:
177			fprintf (stderr, "%s ", yytext);
178			break;
179
180		case OPT_OUTFILE:
181		case OPT_PREFIX:
182		case CCE_ALNUM:
183		case CCE_ALPHA:
184		case CCE_BLANK:
185		case CCE_CNTRL:
186		case CCE_DIGIT:
187		case CCE_GRAPH:
188		case CCE_LOWER:
189		case CCE_PRINT:
190		case CCE_PUNCT:
191		case CCE_SPACE:
192		case CCE_UPPER:
193		case CCE_XDIGIT:
194			fprintf (stderr, "%s", yytext);
195			break;
196
197		case 0:
198			fprintf (stderr, _("End Marker\n"));
199			break;
200
201		default:
202			fprintf (stderr,
203				 _
204				 ("*Something Weird* - tok: %d val: %d\n"),
205				 toktype, yylval);
206			break;
207		}
208	}
209
210	return toktype;
211}
212