source: git/Singular/scanner.ll @ 0df59c8

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