OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [treelang/] [lex.l] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/* -*- c -*- = mode for emacs editor
2
 
3
   TREELANG lexical analysis
4
 
5
   ---------------------------------------------------------------------
6
 
7
   Copyright (C) 1986, 87, 89, 92-96, 1997, 1999, 2000, 2001, 2002, 2003,
8
   2004, 2005 Free Software Foundation, Inc.
9
 
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
13
   later version.
14
 
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.
19
 
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, 51 Franklin Street, Fifth Floor,
23
   Boston, MA 02110-1301, USA.
24
 
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!
28
 
29
   ---------------------------------------------------------------------
30
 
31
   Written by Tim Josling 1999-2001, based in part on other parts of
32
   the GCC compiler.  */
33
 
34
%{
35
#include "config.h"
36
#include "system.h"
37
#include "coretypes.h"
38
#include "tm.h"
39
#include "input.h"
40
#include "tree.h"
41
 
42
/* Token defs.  */
43
#include "treelang.h"
44
#include "parse.h"
45
#include "treetree.h"
46
#include "toplev.h"
47
 
48
extern int option_lexer_trace;
49
 
50
int yylex (void);
51
void update_yylval (int a);
52
 
53
static int next_tree_charno = 1;
54
static int lineno = 1;
55
 
56
static void update_lineno_charno (void);
57
static void dump_lex_value (int lexret);
58
 
59
#define SAVE_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
60
   {fprintf (stderr, "\nlexer returning"); dump_lex_value (a);} return a;}
61
#define NOT_RETURN(a) {update_yylval (a); if (option_lexer_trace)\
62
   {fprintf (stderr, "\nlexer swallowing"); dump_lex_value (a);}}
63
#ifndef USE_MAPPED_LOCATION
64
#undef LINEMAP_POSITION_FOR_COLUMN
65
#define LINEMAP_POSITION_FOR_COLUMN(INPUT, LINETABLE, COL)
66
#endif
67
%}
68
 
69
%option nostack
70
%option nounput
71
%option noyywrap
72
%option pointer
73
%option nodefault
74
 
75
%%
76
 
77
 {
78
   /* ??? Should really allocate only what we need.  */
79
   yylval = my_malloc (sizeof (struct prod_token_parm_item));
80
   LINEMAP_POSITION_FOR_COLUMN (input_location, &line_table,
81
                                next_tree_charno);
82
   ((struct prod_token_parm_item *)yylval)->tp.tok.location = input_location;
83
   ((struct prod_token_parm_item *)yylval)->tp.tok.charno = next_tree_charno;
84
 }
85
 
86
[ \n\t]+ {
87
  update_lineno_charno ();
88
  NOT_RETURN (WHITESPACE);
89
}
90
 
91
"//".*  {
92
  /* Comment.  */
93
  update_lineno_charno ();
94
  NOT_RETURN (COMMENT);
95
}
96
 
97
"{" {
98
  update_lineno_charno ();
99
  SAVE_RETURN (LEFT_BRACE);
100
}
101
 
102
"}" {
103
  update_lineno_charno ();
104
  SAVE_RETURN (RIGHT_BRACE);
105
}
106
 
107
"(" {
108
  update_lineno_charno ();
109
  SAVE_RETURN (LEFT_PARENTHESIS);
110
}
111
 
112
")" {
113
  update_lineno_charno ();
114
  SAVE_RETURN (RIGHT_PARENTHESIS);
115
}
116
 
117
"," {
118
  update_lineno_charno ();
119
  SAVE_RETURN (COMMA);
120
}
121
 
122
";" {
123
  update_lineno_charno ();
124
  SAVE_RETURN (SEMICOLON);
125
}
126
 
127
"+" {
128
  update_lineno_charno ();
129
  SAVE_RETURN (tl_PLUS);
130
}
131
 
132
"-" {
133
  update_lineno_charno ();
134
  SAVE_RETURN (tl_MINUS);
135
}
136
 
137
"=" {
138
  update_lineno_charno ();
139
  SAVE_RETURN (ASSIGN);
140
}
141
 
142
"==" {
143
  update_lineno_charno ();
144
  SAVE_RETURN (EQUALS);
145
}
146
 
147
[+-]?[0-9]+ {
148
  update_lineno_charno ();
149
  SAVE_RETURN (INTEGER);
150
}
151
 
152
"external_reference" {
153
  update_lineno_charno ();
154
  SAVE_RETURN (EXTERNAL_REFERENCE);
155
}
156
 
157
"external_definition" {
158
  update_lineno_charno ();
159
  SAVE_RETURN (EXTERNAL_DEFINITION);
160
}
161
 
162
"static" {
163
  update_lineno_charno ();
164
  SAVE_RETURN (STATIC);
165
}
166
 
167
"automatic" {
168
  update_lineno_charno ();
169
  SAVE_RETURN (AUTOMATIC);
170
}
171
 
172
"int" {
173
  update_lineno_charno ();
174
  SAVE_RETURN (INT);
175
}
176
 
177
"char" {
178
  update_lineno_charno ();
179
  SAVE_RETURN (CHAR);
180
}
181
 
182
"void" {
183
  update_lineno_charno ();
184
  SAVE_RETURN (VOID);
185
}
186
 
187
"unsigned" {
188
  update_lineno_charno ();
189
  SAVE_RETURN (UNSIGNED);
190
}
191
 
192
"return" {
193
  update_lineno_charno ();
194
  SAVE_RETURN (tl_RETURN);
195
}
196
 
197
"if" {
198
  update_lineno_charno ();
199
  SAVE_RETURN (IF);
200
}
201
 
202
"else" {
203
  update_lineno_charno ();
204
  SAVE_RETURN (ELSE);
205
}
206
 
207
[A-Za-z_]+[A-Za-z_0-9]* {
208
  update_lineno_charno ();
209
  update_yylval (NAME);
210
  if (option_lexer_trace)
211
    {
212
      fprintf (stderr, "\nlexer returning");
213
      dump_lex_value (NAME);
214
    }
215
  return NAME;
216
}
217
 
218
[^\n]  {
219
  update_lineno_charno ();
220
  error ("%HUnrecognized character %qc.",
221
         &((struct prod_token_parm_item *)yylval)->tp.tok.location,
222
         yytext[0]);
223
}
224
 
225
%%
226
 
227
/*
228
   Update line number (1-) and character number (1-).  Call this
229
   before processing the token.  */
230
 
231
static void
232
update_lineno_charno (void)
233
{
234
   /* Update the values we send to caller in case we sometimes don't
235
      tell them about all the 'tokens' eg comments etc.  */
236
   int yyl;
237
   LINEMAP_POSITION_FOR_COLUMN (input_location, &line_table,
238
                                next_tree_charno);
239
   ((struct prod_token_parm_item *)yylval)->tp.tok.location = input_location;
240
   ((struct prod_token_parm_item *)yylval)->tp.tok.charno = next_tree_charno;
241
 
242
   for ( yyl = 0; yyl < yyleng; ++yyl )
243
      {
244
         if ( yytext[yyl] == '\n' )
245
            {
246
#ifdef USE_MAPPED_LOCATION
247
              source_location s = linemap_line_start (&line_table, ++lineno,
248
                                                      80);
249
              input_location = s;
250
#else
251
              input_line = ++lineno;
252
#endif
253
              next_tree_charno = 1;
254
            }
255
         else
256
           next_tree_charno++;
257
      }
258
}
259
 
260
/* Fill in the fields of yylval - the value of the token.  The token
261
   type is A.  */
262
void
263
update_yylval (int a)
264
{
265
  struct prod_token_parm_item * tok;
266
  tok = yylval;
267
 
268
  tok->category = token_category;
269
  tok->type = a;
270
  tok->tp.tok.length = yyleng;
271
  /* Have to copy yytext as it is just a ptr into the buffer at the
272
     moment.  */
273
  tok->tp.tok.chars = (unsigned char*) get_string (yytext, yyleng);
274
}
275
 
276
/* Trace the value LEXRET and the position and token details being
277
   returned by the lexical analyser.  */
278
 
279
static void
280
dump_lex_value (int lexret)
281
{
282
  int ix;
283
 
284
  fprintf (stderr, " %d l:%d c:%d ln:%d text=", lexret,
285
           LOCATION_LINE (((struct prod_token_parm_item *)
286
                          yylval)->tp.tok.location),
287
           ((struct prod_token_parm_item *) yylval)->tp.tok.charno,
288
           ((struct prod_token_parm_item *) yylval)->tp.tok.length);
289
 
290
  for (ix = 0; ix < yyleng; ix++)
291
    {
292
      fprintf (stderr, "%c", yytext[ix]);
293
    }
294
  fprintf (stderr, " in hex:");
295
  for (ix = 0; ix < yyleng; ix++)
296
    {
297
      fprintf (stderr, " %2.2x", yytext[ix]);
298
    }
299
  fprintf (stderr, "\n");
300
}
301
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.