1 %{ /* -*- c -*- = mode for emacs editor
4 TREELANG lexical analysis
6 ---------------------------------------------------------------------
8 Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
10 This program is free software; you can redistribute it and/or modify it
11 under the terms of the GNU General Public License as published by the
12 Free Software Foundation; either version 2, or (at your option) any
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
25 In other words, you are welcome to use, share and improve this program.
26 You are forbidden to forbid anyone else to use, share and improve
27 what you give them. Help stamp out software-hoarding!
29 ---------------------------------------------------------------------
31 Written by Tim Josling 1999-2001, based in part on other parts of
42 /* Avoid poisoned malloc problem. */
46 #include "diagnostic.h"
53 extern int option_lexer_trace;
56 void update_yylval (int a);
58 static int next_tree_lineno=1;
59 static int next_tree_charno=1;
61 static void update_lineno_charno (void);
62 static void dump_lex_value (int lexret);
64 #define SAVE_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
65 {fprintf (stderr, "\nlexer returning"); dump_lex_value (a);} return a;}
66 #define NOT_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
67 {fprintf (stderr, "\nlexer swallowing"); dump_lex_value (a);}}
80 /* Should really allocate only what we need. lll;. */
81 yylval = my_malloc (sizeof (struct prod_token_parm_item));
82 ((struct prod_token_parm_item *)yylval)->tp.tok.lineno = next_tree_lineno;
83 ((struct prod_token_parm_item *)yylval)->tp.tok.charno = next_tree_charno;
87 update_lineno_charno ();
88 NOT_RETURN (WHITESPACE);
93 update_lineno_charno ();
98 update_lineno_charno ();
99 SAVE_RETURN (LEFT_BRACE);
103 update_lineno_charno ();
104 SAVE_RETURN (RIGHT_BRACE);
108 update_lineno_charno ();
109 SAVE_RETURN (LEFT_PARENTHESIS);
113 update_lineno_charno ();
114 SAVE_RETURN (RIGHT_PARENTHESIS);
118 update_lineno_charno ();
123 update_lineno_charno ();
124 SAVE_RETURN (SEMICOLON);
128 update_lineno_charno ();
133 update_lineno_charno ();
138 update_lineno_charno ();
139 SAVE_RETURN (ASSIGN);
143 update_lineno_charno ();
144 SAVE_RETURN (EQUALS);
148 update_lineno_charno ();
149 SAVE_RETURN (INTEGER);
152 "external_reference" {
153 update_lineno_charno ();
154 SAVE_RETURN (EXTERNAL_REFERENCE);
157 "external_definition" {
158 update_lineno_charno ();
159 SAVE_RETURN (EXTERNAL_DEFINITION);
163 update_lineno_charno ();
164 SAVE_RETURN (STATIC);
168 update_lineno_charno ();
169 SAVE_RETURN (AUTOMATIC);
173 update_lineno_charno ();
178 update_lineno_charno ();
183 update_lineno_charno ();
188 update_lineno_charno ();
189 SAVE_RETURN (UNSIGNED);
193 update_lineno_charno ();
194 SAVE_RETURN (RETURN);
198 update_lineno_charno ();
203 update_lineno_charno ();
207 [A-Za-z_]+[A-Za-z_0-9]* {
208 update_lineno_charno ();
209 update_yylval (NAME);
210 if (option_lexer_trace)
212 fprintf (stderr, "\nlexer returning");
213 dump_lex_value (NAME);
219 update_lineno_charno ();
220 fprintf (stderr, "%s:%i:%i: Unrecognized character %c\n", in_fname,
221 ((struct prod_token_parm_item *)yylval)->tp.tok.lineno,
222 ((struct prod_token_parm_item *)yylval)->tp.tok.charno, yytext[0]);
229 Update line number (1-) and character number (1-). Call this
230 before processing the token. */
233 update_lineno_charno (void)
235 /* Update the values we send to caller in case we sometimes don't
236 tell them about all the 'tokens' eg comments etc. */
238 ((struct prod_token_parm_item *)yylval)->tp.tok.lineno = next_tree_lineno;
239 ((struct prod_token_parm_item *)yylval)->tp.tok.charno = next_tree_charno;
240 for ( yyl = 0; yyl < yyleng; ++yyl )
242 if ( yytext[yyl] == '\n' )
245 next_tree_charno = 1;
252 /* Fill in the fields of yylval - the value of the token. The token
255 update_yylval (int a)
257 struct prod_token_parm_item * tok;
260 tok->category = token_category;
262 tok->tp.tok.length = yyleng;
263 /* Have to copy yytext as it is just a ptr into the buffer at the
265 tok->tp.tok.chars = my_malloc (yyleng + 1);
266 memcpy (tok->tp.tok.chars, yytext, yyleng);
269 /* Trace the value LEXRET and the position and token details being
270 returned by the lexical analyser. */
273 dump_lex_value (int lexret)
276 fprintf (stderr, " %d l:%d c:%d ln:%d text=", lexret,
277 ((struct prod_token_parm_item *) yylval)->tp.tok.lineno,
278 ((struct prod_token_parm_item *) yylval)->tp.tok.charno,
279 ((struct prod_token_parm_item *) yylval)->tp.tok.length);
280 for (ix = 0; ix < yyleng; ix++)
282 fprintf (stderr, "%c", yytext[ix]);
284 fprintf (stderr, " in hex:");
285 for (ix = 0; ix < yyleng; ix++)
287 fprintf (stderr, " %2.2x", yytext[ix]);
289 fprintf (stderr, "\n");