source: git/Singular/scanner.ll @ 6c01d6b

spielwiese
Last change on this file since 6c01d6b was 6ce030f, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
removal of the $Id$ svn tag from everywhere NOTE: the git SHA1 may be used instead (only on special places) NOTE: the libraries Singular/LIB/*.lib still contain the marker due to our current use of svn
  • Property mode set to 100644
File size: 12.1 KB
Line 
1%{
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5#include <stdio.h>
6#include <string.h>
7#include <stdlib.h>
8#include <ctype.h>
9
10#include <kernel/mod2.h>
11#include <omalloc/omalloc.h>
12#include <Singular/tok.h>
13#include <Singular/stype.h>
14#include <Singular/ipshell.h>
15#include <kernel/febase.h>
16
17int feReadLine(char* b, int l);
18#define ALLOC(a) omAlloc((a))
19#ifndef NEW_FLEX
20#endif /* NEW_LEX */
21int blocknest = 0;
22extern char * yytext;
23//extern unsigned char * yytext;
24extern int yyleng;
25extern int inerror;
26
27// this is to  shadow the malloc/realloc
28// used by yy_flex_malloc/yy_flex_realloc
29// so that we can mark stuff as static
30static void* my_malloc(size_t size)
31{
32  void* addr = omAlloc(size);
33  omMarkAsStaticAddr(addr);
34  return addr;
35}
36
37static void* my_realloc(void* addr, size_t size)
38{
39  void* new_addr = omRealloc(addr, size);
40  omMarkAsStaticAddr(new_addr);
41  return new_addr;
42}
43static void my_free(void* addr)
44{
45  omFree(addr);
46}
47#undef malloc
48#define malloc my_malloc
49#undef realloc
50#define realloc my_realloc
51#undef free
52#define free my_free
53static char * dupyytext()
54{
55  char* s;
56  if (yyleng>0) yytext[yyleng-1] = '\0';
57  s = omStrDup((char *)yytext);
58  omMarkAsStaticAddr(s);
59  return s;
60}
61
62static char * dupyytextNL()
63{
64  int i = yyleng;//strlen((char *)yytext);
65  char * rc = (char*)omAlloc( 3 + i );
66  omMarkAsStaticAddr(rc);
67  if (i>0)
68  {
69    strncpy( rc, (char *)yytext, i-1 );
70  }
71  else
72  {
73    i++;
74  }
75  rc[i-1] = '\n';
76  rc[i] = '\n';
77  rc[i+1] = '\0';
78  return rc;
79}
80
81  #undef YY_DECL
82  #define YY_DECL int yylex(YYSTYPE* lvalp)
83
84  #undef yywrap
85  extern "C" {
86  int yywrap() { return exitVoice(); }
87  }
88
89  #undef YY_INPUT
90  #define YY_INPUT(buf,result,max_size) \
91          result = feReadLine( (char *) (buf), (max_size) )
92
93  #undef YY_USER_ACTION
94  #define YY_USER_ACTION \
95          if ((inerror==1)&&(*yytext>=' '))\
96          { Print("   skipping text from `%s`",yytext);inerror=2; }
97
98%}
99
100digit          [0-9]
101letter         [@a-zA-Z\']
102integer        {digit}+
103monom          {letter}+{digit}*
104rgvars         ({digit}+[/])*{digit}+{monom}+
105realnum        {digit}*"."{digit}+("e"[+-]{digit}+)?
106name           ({letter}({letter}*{digit}*_*)*|_)
107parname        #
108
109/* %start START */
110%option always-interactive
111
112%x string
113%x block
114%x blockstr
115%x brace
116%x bracestr
117%x bracket
118%x asstring
119
120%%
121\/\/[^\n]*               { }
122^#![^\n]*                { }
123"/*"                     {
124                           yy_noeof=noeof_comment;
125                           loop
126                           {
127                             register int c;
128                             while ( (c = yyinput()) != '*' && c != EOF );
129                             if ( c == '*' )
130                             {
131                               while ( (c = yyinput()) == '*' );
132                               if ( c == '/' ) break; /* found the end */
133                             }
134                             else
135                             {
136                               break;
137                             }
138                           }
139                           yy_noeof=0;
140                         }
141while                    { prompt_char='.';
142                           blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace);
143                           return WHILE_CMD;}
144for                      { prompt_char='.';
145                           blocknest = 0; yy_noeof = noeof_brace; BEGIN(brace);
146                           return FOR_CMD;}
147
148("help"|"?")[ \t\n]*     { yy_noeof = noeof_asstring;
149                           BEGIN(asstring);
150                           return HELP_CMD;
151                         }
152
153example[ \t\n]*          { yy_noeof = noeof_asstring;
154                           BEGIN(asstring);
155                           return EXAMPLE_CMD;
156                         }
157
158proc[ \t]+{name}[ \t]*\( {
159                           char c; char *cp;
160                           lvalp->name = omStrDup(iiProcName((char *)yytext,c,cp));
161                           yy_noeof = noeof_procname;
162                           blocknest = 1;
163                           BEGIN(brace);
164                           return PROC_DEF;
165                         }
166<asstring>[^;\n]+        {
167                           lvalp->name = omStrDup((char *)yytext);
168                           yy_noeof = 0; BEGIN(INITIAL);
169                           return STRINGTOK;
170                         }
171<asstring>;              {
172                           yy_noeof = 0; BEGIN(INITIAL);
173                           return *yytext;
174                         }
175
176<brace>"\""              {
177                           yy_noeof = noeof_string;
178                           BEGIN(bracestr);
179                           yymore();
180                         }
181<brace>"("               { if (blocknest++) yymore(); }
182<brace>[^;\(\)]          { if (blocknest) yymore(); }
183<brace>";"               {
184                           if (blocknest)
185                           {
186                             lvalp->name = dupyytext();
187                             return STRINGTOK;
188                           }
189                         }
190<brace>")"               {
191                           if (--blocknest <= 0)
192                           {
193                             yy_noeof = 0;
194                             BEGIN(INITIAL);
195                             lvalp->name = dupyytext();
196                             return STRINGTOK;
197                           }
198                           yymore();
199                         }
200<bracestr>"\""           {
201                           yy_noeof = noeof_brace;
202                           BEGIN(brace);
203                           yymore();
204                         }
205<bracestr>[^\"]          { yymore(); }
206<bracket>"("             { return '('; }
207<bracket>","             { return ','; }
208<bracket>[ \t\n]*        { ; }
209<bracket>[^\(\), \t\n]*  {
210                           lvalp->name = omStrDup((char *)yytext);
211                           return STRINGTOK;
212                         }
213<bracket>\"[^\"]*\"      {
214                           lvalp->name = omStrDup((char *)yytext);
215                           return STRINGTOK;
216                         }
217<bracket>")"             {
218                           yy_noeof = 0; BEGIN(INITIAL);
219                           return ')';
220                         }
221
222"{"                      {
223                           yy_blocklineno = yylineno;
224                           blocknest = 1;
225                           yy_noeof = noeof_block;
226                           BEGIN(block);
227                         }
228<block>"\""              {
229                           yy_noeof = noeof_string;
230                           BEGIN(blockstr);
231                           yymore();
232                         }
233<blockstr>[^\"]          { yymore(); }
234<blockstr>"\\\\"         { yymore(); }
235<blockstr>"\\\""         { yymore(); }
236<blockstr>"\""           {
237                           yy_noeof = noeof_block;
238                           BEGIN(block);
239                           yymore();
240                         }
241<block>[^\{\}\"]*        { yymore(); }
242<block>\/\/[^\n]*        { yymore(); }
243<block>"{"               { blocknest++; yymore(); }
244<block>"}"               {
245                           if (--blocknest <= 0)
246                           {
247                             BEGIN(INITIAL);
248                             yy_noeof = 0;
249                             lvalp->name = dupyytextNL();
250                             return BLOCKTOK;
251                           }
252                           yymore();
253                         }
254"\""                     { BEGIN(string); yy_noeof = noeof_string;}
255~                        { return SYS_BREAK; }
256<string>[^\"]            { yymore(); }
257<string>"\\\\"           { yymore(); }
258<string>"\\\""           { yymore(); }
259<string>"\""             {
260                           char * s;
261                           yy_noeof = 0;
262                           BEGIN(INITIAL);
263                           s = lvalp->name = dupyytext();
264                           while (*yytext)
265                           {
266                             if (*yytext == '\\') yytext++;
267                             *s++ = *yytext++;
268                           }
269                           *s++ = *yytext++;
270                           return STRINGTOK;
271                         }
272
273[ \t\r\n]                /* skip whitespace */
274".."                     { return DOTDOT; }
275"::"                     { return COLONCOLON; }
276"--"                     { return MINUSMINUS; }
277"++"                     { return PLUSPLUS  ; }
278"=="                     { return EQUAL_EQUAL; }
279"&&"                     { lvalp->i='&'; return LOGIC_OP; }
280"||"                     { lvalp->i='|'; return LOGIC_OP; }
281"<="                     { lvalp->i=LE; return COMP_OP; }
282">="                     { lvalp->i=GE; return COMP_OP; }
283"!"                      { return NOT; }
284"!="                     { return NOTEQUAL; }
285"<>"                     { return NOTEQUAL; }
286"**"                     { return '^'; }
287\\                       { return '\\'; }
288newline                  {
289                           lvalp->name = omStrDup("\n");
290                           return STRINGTOK;
291                         }
292{integer}                {
293                           lvalp->name = (char *)yytext;
294                           return INT_CONST;
295                         }
296{integer}\/{integer}     {
297                           lvalp->name = (char *)yytext;
298                           return RINGVAR;
299                         }
300\$                        {
301                           m2_end(-1);
302                         }
303(quit|exit)[ \t\n]*;     {
304                           #ifdef MM_STAT
305                           mmStat(-500);
306                           #endif
307                           #ifdef OM_TRACK
308                           #ifndef NDEBUG
309                             omPrintUsedTrackAddrs(stdout, 10);
310                           #endif
311                           #endif
312                           m2_end(0);
313                         }
314
315{rgvars}|{realnum}       {
316                           lvalp->name = (char *)yytext;
317                           return RINGVAR;
318                         }
319[0-9]+\."e"[+-][0-9]+    {
320                           lvalp->name = (char *)yytext;
321                           return RINGVAR;
322                         }
323[0-9]+\./[^\.]           {
324                           lvalp->name = (char *)yytext;
325                           return RINGVAR;
326                         }
327
328({parname}|{name})       {
329                           /* {name} */
330                           int rc=0;
331                           if (yytext[strlen((char *)yytext)-1] == '\n')
332                           {
333                             yytext[strlen((char *)yytext)-1] = '\0';
334                           }
335                           if (yyleng > 1)
336                           {
337                             rc = IsCmd((char *)yytext,lvalp->i);
338                             if (rc) return rc;
339                           }
340                           lvalp->name = omStrDup((char *)yytext);
341                           return UNKNOWN_IDENT;
342                         }
343
344.                       {
345                           /*if (*yytext == '\n') REJECT;*/
346                           register char ch= *yytext;
347                           lvalp->i = ch;
348                           switch(ch)
349                           {
350                             /* case '&': */
351                             case '|':
352                               return LOGIC_OP;
353                             /* case '/': */
354                             case '%':
355                             case '*':
356                               return MULDIV_OP;
357                             /* case '<': */
358                             case '>':
359                               return COMP_OP;
360                             default:
361                               break;
362                            }
363                            return ch;
364                         }
365%%
366
367void * myynewbuffer()
368{
369  void * oldb = YY_CURRENT_BUFFER;
370  yy_switch_to_buffer(yy_create_buffer(NULL, YY_BUF_SIZE));
371  return oldb;
372}
373
374void myyoldbuffer(void * oldb)
375{
376  yy_delete_buffer(YY_CURRENT_BUFFER);
377  yy_switch_to_buffer((YY_BUFFER_STATE)oldb);
378  //yy_flush_buffer((YY_BUFFER_STATE)oldb);
379}
Note: See TracBrowser for help on using the repository browser.