source: git/Singular/scanner.l @ 408aed

spielwiese
Last change on this file since 408aed was 408aed, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes: fixed "int overflow" bug in scanner.l/grammar.y git-svn-id: file:///usr/local/Singular/svn/trunk@717 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.5 KB
Line 
1%{
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5/* $Id: scanner.l,v 1.8 1997-09-12 08:27:11 Singular Exp $ */
6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9#include <ctype.h>
10
11#include "mod2.h"
12#include "tok.h"
13#include "stype.h"
14#include "ipshell.h"
15#include "mmemory.h"
16#include "febase.h"
17
18int mmread(FILE* f, char* b, int l);
19#define ALLOC(a) Alloc((a))
20int yylineno  = 0;
21static int noeof = 0;
22enum
23{
24  noeof_brace = 1,
25  noeof_asstring,
26  noeof_block,
27  noeof_string,
28  noeof_bracket,
29  noeof_procname
30};
31static int blocknest = 0;
32extern char * yytext;
33//extern unsigned char * yytext;
34extern int yyleng;
35extern int inputswitch;
36extern BOOLEAN noringvars;
37extern int inerror;
38
39static char * dupyytext()
40{
41  //int i = strlen((char *)yytext);
42  //if (i>0) yytext[i-1] = '\0';
43  if (yyleng>0) yytext[yyleng-1] = '\0';
44  return mstrdup((char *)yytext);
45}
46
47static char * dupyytextNL()
48{
49  int i = yyleng;//strlen((char *)yytext);
50  char * rc = (char*)AllocL( 3 + i );
51  if (i>0)
52  {
53    yytext[i-1] = '\0';
54    strcpy( rc, (char *)yytext );
55  }
56  else
57    i++;
58  rc[i-1] = '\n';
59  rc[i] = '\n';
60  rc[i+1] = '\0';
61  return rc;
62}
63
64  #undef YY_DECL
65  #define YY_DECL int yylex(YYSTYPE* lvalp)
66
67  #undef yywrap
68  int yywrap() { return exitVoice(); }
69
70  #undef YY_INPUT
71  #define YY_INPUT(buf,result,max_size) \
72          if ( (result = mmread( (yyin), (char *) buf, max_size )) < 0 ) \
73              YY_FATAL_ERROR( "read() in flex scanner failed" );
74
75  #undef YY_USER_ACTION
76  #define YY_USER_ACTION \
77          if (inerror==1) { Print("   skipping text from `%s`",yytext);inerror=2; }
78
79%}
80
81digit          [0-9]
82letter         [@a-zA-Z\']
83integer        {digit}+
84monom          {letter}+{digit}*
85rgvars         ({digit}+[/])*{digit}+{monom}+
86realnum        {digit}+"."{digit}+("e"[+-]{digit}+)?
87name           ({letter}({letter}*{digit}*_*)*|_)
88parname        #
89
90/* %start START */
91
92%x string
93%x block
94%x blockstr
95%x brace
96%x bracestr
97%x bracket
98%x asstring
99
100%%
101\/\/[^\n]*               { }
102^#![^\n]*                { }
103pause[ \t\n]*[\.;]       { fePause(); }
104while                    { blocknest = 0; noeof = noeof_brace; BEGIN(brace);
105                           return WHILE_CMD;}
106for                      { blocknest = 0; noeof = noeof_brace; BEGIN(brace);
107                           return FOR_CMD;}
108
109("help"|"?")[ \t\n]*     { noeof = noeof_asstring;
110                           BEGIN(asstring);
111                           return HELP_CMD;
112                         }
113
114example[ \t\n]*          { noeof = noeof_asstring;
115                           BEGIN(asstring);
116                           return EXAMPLE_CMD;
117                         }
118
119proc[ \t]+{name}[ \t]*\( {
120                           char c; char *cp;
121                           lvalp->name = mstrdup(iiProcName((char *)yytext,c,cp));
122                           noeof = noeof_procname;
123                           blocknest = 1;
124                           BEGIN(brace);
125                           return PROC_DEF;
126                         }
127<asstring>[^;\n]+        {
128                           lvalp->name = mstrdup((char *)yytext);
129                           noeof = 0; BEGIN(INITIAL);
130                           return STRINGTOK;
131                         }
132<asstring>;              {
133                           noeof = 0; BEGIN(INITIAL);
134                           return *yytext;
135                         }
136
137<brace>"\""              {
138                           noeof = noeof_string;
139                           BEGIN(bracestr);
140                           yymore();
141                         }
142<brace>"("               { if (blocknest++) yymore(); }
143<brace>[^;\(\)]          { if (blocknest) yymore(); }
144<brace>";"               {
145                           if (blocknest)
146                           {
147                             lvalp->name = dupyytext();
148                             return STRINGTOK;
149                           }
150                         }
151<brace>")"               {
152                           if (--blocknest <= 0)
153                           {
154                             noeof = 0;
155                             BEGIN(INITIAL);
156                             lvalp->name = dupyytext();
157                             return STRINGTOK;
158                           }
159                           yymore();
160                         }
161<bracestr>"\""           {
162                           noeof = noeof_brace;
163                           BEGIN(brace);
164                           yymore();
165                         }
166<bracestr>[^\"]          { yymore(); }
167<bracket>"("             { return '('; }
168<bracket>","             { return ','; }
169<bracket>[ \t\n]*        { ; }
170<bracket>[^\(\), \t\n]*  {
171                           lvalp->name = mstrdup((char *)yytext);
172                           return STRINGTOK;
173                         }
174<bracket>\"[^\"]*\"      {
175                           lvalp->name = mstrdup((char *)yytext);
176                           return STRINGTOK;
177                         }
178<bracket>")"             {
179                           noeof = 0; BEGIN(INITIAL);
180                           return ')';
181                         }
182
183"{"                      {
184                           blocklineno = yylineno;
185                           blocknest = 1;
186                           noeof = noeof_block;
187                           BEGIN(block);
188                         }
189<block>"\""              {
190                           noeof = noeof_string;
191                           BEGIN(blockstr);
192                           yymore();
193                         }
194<blockstr>[^\"]          { yymore(); }
195<blockstr>"\\\\"         { yymore(); }
196<blockstr>"\\\""         { yymore(); }
197<blockstr>"\""           {
198                           noeof = noeof_block;
199                           BEGIN(block);
200                           yymore();
201                         }
202<block>[^\{\}\"]*        { yymore(); }
203<block>"{"               { blocknest++; yymore(); }
204<block>"}"               {
205                           if (--blocknest <= 0)
206                           {
207                             BEGIN(INITIAL);
208                             noeof = 0;
209                             lvalp->name = dupyytextNL();
210                             return BLOCKTOK;
211                           }
212                           yymore();
213                         }
214"\""                     { BEGIN(string); noeof = noeof_string;}
215~                        { return SYS_BREAK; }
216<string>[^\"]            { yymore(); }
217<string>"\\\\"           { yymore(); }
218<string>"\\\""           { yymore(); }
219<string>"\""             {
220                           char * s;
221                           noeof = 0;
222                           BEGIN(INITIAL);
223                           s = lvalp->name = dupyytext();
224                           while (*yytext)
225                           {
226                             if (*yytext == '\\') yytext++;
227                             *s++ = *yytext++;
228                           }
229                           *s++ = *yytext++;
230                           return STRINGTOK;
231                         }
232
233[ \t\r\n]                /* skip whitespace */
234".."                     { return DOTDOT; }
235"--"                     { return MINUSMINUS; }
236"++"                     { return PLUSPLUS  ; }
237"=="                     { return EQUAL_EQUAL; }
238"&&"                     { return '&'; }
239"||"                     { return '|'; }
240"<="                     { return LE; }
241">="                     { return GE; }
242"!"                      { return NOT; }
243"!="                     { return NOTEQUAL; }
244"<>"                     { return NOTEQUAL; }
245"**"                     { return '^'; }
246\\                       { return '\\'; }
247newline                  {
248                           lvalp->name = mstrdup("\n");
249                           return STRINGTOK;
250                         }
251{integer}                {
252                           lvalp->name = (char *)yytext;
253                           if ((currRing!=NULL)
254                           && (strlen(lvalp->name)>MAX_INT_LEN))
255                           {
256                             lvalp->name = mstrdup((char *)yytext);
257                             return RINGVAR;
258                           }
259                           else
260                             return INT_CONST;
261                         }
262{integer}\/{integer}     {
263                           lvalp->name = mstrdup((char *)yytext);
264                           return RINGVAR;
265                         }
266\$[\.;]?                 {
267                           #ifdef HAVE_TCL
268                           if (tclmode)
269                             PrintTCL('Q',0,NULL);
270                           else
271                           #endif
272                           { if (BVERBOSE(0)) printf("\n$Bye.\n"); }
273                           #ifdef sun
274                           #ifndef __svr4__
275                           #ifdef HAVE_FEREAD
276                           fe_reset_input_mode(0,NULL);
277                           #endif
278                           _cleanup();
279                           _exit(0);
280                           #endif
281                           #endif
282                           exit(0);
283                         }
284(quit|exit)[ \t\n]*[\.;]?  {
285                           #ifdef HAVE_TCL
286                             if (tclmode)
287                               PrintTCL('Q',0,NULL);
288                             else
289                           #endif
290                           #ifdef MM_STAT
291                           mmStat(-500);
292                           #endif
293                           { if (BVERBOSE(0)) printf("\nAuf Wiedersehen.\n"); }
294                           #ifdef MDEBUG
295                             #ifndef macintosh
296                               #ifdef HAVE_FEREAD
297                                 #ifdef HAVE_ATEXIT
298                                   fe_reset_input_mode();
299                                 #else
300                                   fe_reset_input_mode(0,NULL);
301                                 #endif
302                               #endif
303                             #endif
304                             #ifdef MLIST
305                             mmTestList();
306                             #endif
307                           #endif
308                           #ifdef sun
309                           #ifndef __svr4__
310                           _cleanup();
311                           _exit(0);
312                           #endif
313                           #endif
314                           exit(0);
315                         }
316
317{rgvars}|{realnum}       {
318                           lvalp->name = mstrdup((char *)yytext);
319                           return RINGVAR;
320                         }
321
322({parname}|{name})       {
323                           /* {name} */
324                           int rc=0;
325                           if (yytext[strlen((char *)yytext)-1] == '\n')
326                           {
327                             yytext[strlen((char *)yytext)-1] = '\0';
328                           }
329                           if (yyleng > 1)
330                           {
331                             rc = IsCmd((char *)yytext,lvalp->i);
332                             if (rc) return rc;
333                           }
334                           lvalp->name = mstrdup((char *)yytext);
335                           return UNKNOWN_IDENT;
336                         }
337
338.                        {
339                           /*if (*yytext == '\n') REJECT;*/
340                           lvalp->i = *yytext;   /* token has own value */
341                           return *yytext;
342                         }
343%%
344
345
346int mmread(FILE* f, char* b, int l)
347{
348  int rc;
349  b[0] = '\0';
350  if (inputswitch <=0)
351  {
352    char* s;
353    int offset=0;
354    showInput();
355NewRead:
356    if (f==stdin)
357      s = fe_fgets_stdin(b+offset,l-offset);
358    else
359      s = fgets(b+offset,l-offset,f);
360    if (s==NULL)
361    {
362      if (noeof)
363      {
364        switch (noeof)
365        {
366          case noeof_brace:
367          case noeof_block:
368            WerrorS("premature end of file while reading {...}");
369            break;
370          case noeof_asstring:
371            WerrorS("premature end of file while reading till `.`");
372            break;
373          case noeof_string:
374            WerrorS("premature end of file while reading string");
375            break;
376          case noeof_bracket:
377            WerrorS("premature end of file while reading (...)");
378            break;
379          case noeof_procname:
380            WerrorS("premature end of file while reading proc");
381            break;
382        }
383        exit(1);
384      }
385      return 0;
386    }
387    /*else*/
388    {
389      rc=strlen(s)-2;
390      if (s[rc]=='\\')
391      {
392        s[rc]='\0';
393        offset+=rc;
394        if (offset<l) goto NewRead;
395      }
396    }
397    if (feProt&PROT_I)
398    {
399      fputs(s,feProtFile);
400    }
401    rc=strlen(b);
402  }
403  else
404  {
405    rc=readbuf(b,l);
406  }
407  yylineno++;
408  //if ((rc != strlen(b)) printf("++funny error found! (scanner:rc=%d, len=%d)\n",
409  //  rc,(int)strlen(b));
410  if (rc>1)
411  {
412    strncpy(my_yylinebuf,b,rc-1);
413    if (my_yylinebuf[rc-2] == '\n') my_yylinebuf[rc-2] = '\0';
414    my_yylinebuf[rc-1] = '\0';
415    if (((si_echo>voice) && (strncmp(b,";RETURN();",10)!=0))
416    || (traceit&TRACE_SHOW_LINE)
417    || (traceit&TRACE_SHOW_LINE1))
418    {
419      printf("%s",b);
420      mflush();
421      if (traceit&TRACE_SHOW_LINE)
422      {
423        while(fgetc(stdin)!='\n');
424      }
425    }
426    else if (traceit&TRACE_SHOW_LINENO)
427    {
428      printf("{%d}",yylineno);
429      mflush();
430    }
431  }
432  else
433    my_yylinebuf[0] = '\0';
434  return rc;
435}
436
437void * myynewbuffer()
438{
439  void * oldb = yy_current_buffer;
440  yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
441  return oldb;
442}
443
444void myyoldbuffer(void * oldb)
445{
446  yy_delete_buffer(yy_current_buffer);
447  yy_switch_to_buffer((YY_BUFFER_STATE)oldb);
448}
Note: See TracBrowser for help on using the repository browser.