~ubuntu-branches/ubuntu/precise/code-saturne/precise

« back to all changes in this revision

Viewing changes to src/mei/mei_scanner.l

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2011-11-24 00:00:08 UTC
  • mfrom: (6.1.9 sid)
  • Revision ID: package-import@ubuntu.com-20111124000008-2vo99e38267942q5
Tags: 2.1.0-3
Install a missing file

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%{
 
2
/*============================================================================
 
3
 * Define the scanner for the mathematical expression
 
4
 *============================================================================*/
 
5
 
 
6
/*
 
7
  This file is part of the "Mathematical Expression Interpreter" library.
 
8
 
 
9
  Copyright (C) 2008-2010  EDF
 
10
 
 
11
  This library is free software; you can redistribute it and/or
 
12
  modify it under the terms of the GNU Lesser General Public
 
13
  License as published by the Free Software Foundation; either
 
14
  version 2.1 of the License, or (at your option) any later version.
 
15
 
 
16
  This library is distributed in the hope that it will be useful,
 
17
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
18
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
19
  Lesser General Public License for more details.
 
20
 
 
21
  You should have received a copy of the GNU Lesser General Public
 
22
  License along with this library; if not, write to the Free Software
 
23
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
24
*/
 
25
 
 
26
/*----------------------------------------------------------------------------
 
27
 * Standard C library headers
 
28
 *----------------------------------------------------------------------------*/
 
29
 
 
30
#include <stdio.h>
 
31
#include <stdlib.h>
 
32
#include <string.h>
 
33
 
 
34
/*----------------------------------------------------------------------------
 
35
 * BFT library headers
 
36
 *----------------------------------------------------------------------------*/
 
37
 
 
38
#include <bft_mem.h>
 
39
#include <bft_error.h>
 
40
 
 
41
/*----------------------------------------------------------------------------
 
42
 * Local headers
 
43
 *----------------------------------------------------------------------------*/
 
44
 
 
45
#include "mei_hash_table.h"
 
46
#include "mei_evaluate.h"
 
47
#include "mei_node.h"
 
48
#include "mei_parser.h"
 
49
#include "mei_parser_glob.h"
 
50
 
 
51
/* Redefine macro to redirect scanner input from string instead of standard input. */
 
52
 
 
53
#undef YY_INPUT
 
54
#define YY_INPUT(b, r, ms) ( r = my_yyinput(b, ms) )
 
55
 
 
56
int my_yyinput(char *buffer, int max_size);
 
57
 
 
58
char *buff = NULL;
 
59
size_t len;
 
60
 
 
61
%}
 
62
 
 
63
%%
 
64
" "         {mei_glob_column++;                  } /* skip whitespace */
 
65
"\t"        {mei_glob_column+=8;                 } /* skip whitespace */
 
66
"\n"        {mei_glob_line++; mei_glob_column=0; } /* skip whitespace */
 
67
"#".*\n     {mei_glob_line++; mei_glob_column=0; } /* discard commentary (other possibility: "#"[^\n]*"\n" ) */
 
68
 
 
69
 
 
70
([0-9]+|[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)([Ee][+\-]?[0-9]+)? {
 
71
    yylval.iValue = strtod(yytext, NULL);
 
72
    mei_glob_column += yyleng;
 
73
    return NUMBER;
 
74
}
 
75
 
 
76
 
 
77
"e"|"pi" {
 
78
    strncpy(yylval.sIndex, yytext, yyleng+1);
 
79
    mei_glob_column += yyleng;
 
80
    return VAR;
 
81
}
 
82
 
 
83
 
 
84
"min"|"max"|"atan2"|"mod" {
 
85
    strncpy(yylval.sIndex, yytext, yyleng+1);
 
86
    mei_glob_column += yyleng;
 
87
    return FUN2;
 
88
}
 
89
 
 
90
"exp"|"log"|"sqrt"|"sin"|"cos"|"tan"|"asin"|"acos"|"atan"|"sinh"|"cosh"|"tanh"|"abs"|"int" {
 
91
    strncpy(yylval.sIndex, yytext, yyleng+1);
 
92
    mei_glob_column += yyleng;
 
93
    return FUN1;
 
94
}
 
95
 
 
96
"while"         {mei_glob_column+=5; return WHILE; }
 
97
"if"            {mei_glob_column+=2; return IF;    }
 
98
"else"          {mei_glob_column+=4; return ELSE;  }
 
99
"print"         {mei_glob_column+=5; return PRINT; }
 
100
 
 
101
 
 
102
[_a-zA-Z][_a-zA-Z0-9]* {
 
103
    strncpy(yylval.sIndex, yytext, yyleng+1);
 
104
    if (yyleng+1 > 200)
 
105
        bft_error(__FILE__, __LINE__, 0, "Error: identifier is to long\n");
 
106
    mei_glob_column += yyleng;
 
107
    return VAR;
 
108
}
 
109
 
 
110
 
 
111
">="             {mei_glob_column+=2; return GE;  }
 
112
"<="             {mei_glob_column+=2; return LE;  }
 
113
"=="             {mei_glob_column+=2; return EQ;  }
 
114
"!="             {mei_glob_column+=2; return NE;  }
 
115
"||"             {mei_glob_column+=2; return OR;  }
 
116
"&&"             {mei_glob_column+=2; return AND; }
 
117
"!"              {mei_glob_column++;  return *yytext;}
 
118
 
 
119
 
 
120
[-()<>=+^*/;{},]  {mei_glob_column++; return *yytext;}
 
121
 
 
122
 
 
123
. {
 
124
    mei_glob_column++;
 
125
    len = strlen("unknown character: ") +1;
 
126
    BFT_MALLOC(buff, len, char);
 
127
    strncpy(buff, "unknown character: ", len);
 
128
    BFT_REALLOC(buff, len+yyleng, char);
 
129
    strncat(buff, yytext, yyleng);
 
130
    yyerror(buff);
 
131
    BFT_FREE(buff);
 
132
}
 
133
 
 
134
%%
 
135
 
 
136
int yywrap(void)
 
137
{
 
138
    return 1;
 
139
}
 
140
 
 
141
 
 
142
/* The calling sequence for 'YY_INPUT()' is 'YY_INPUT(buff,result,max_size)'.
 
143
   Its action is to place up to 'max_size' characters in the character
 
144
   array 'buff' and return in the integer variable 'buffer_length' either
 
145
   the number of characters read or the constant 'YY_NULL' to indicate 'EOF'.
 
146
   The default 'YY_INPUT' reads from the global file-pointer 'yyin'. */
 
147
 
 
148
/* max_sise = 8192 bytes. One can change this default
 
149
   value with YY_BUF_SIZE macro */
 
150
 
 
151
 
 
152
int
 
153
my_yyinput(char *buffer, int max_size)
 
154
{
 
155
    int buffer_length;
 
156
 
 
157
    if (max_size < mei_glob_string_end - mei_glob_string_begin)
 
158
    {
 
159
        buffer_length = max_size;
 
160
    }
 
161
    else
 
162
    {
 
163
        buffer_length = mei_glob_string_end - mei_glob_string_begin;
 
164
    }
 
165
 
 
166
    if (buffer_length > 0)
 
167
    {
 
168
        memcpy(buffer, mei_glob_string_begin, buffer_length);
 
169
        mei_glob_string_begin +=buffer_length;
 
170
    }
 
171
 
 
172
    return buffer_length;
 
173
}