Commit fc07c575 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

expr: improve ternary operator, no brackets needed anymore.

parent 9823c656
......@@ -3,6 +3,10 @@
* Using CDI library version 1.9.0
* Version 1.9.0 release
2017-06-28 Uwe Schulzweida
* expr: improve ternary operator, no brackets needed anymore.
2017-06-27 Uwe Schulzweida
* expr: added support for clev in ifthenelse (bug fix).
......
......@@ -347,8 +347,8 @@ static void yynoreturn yy_fatal_error (yyconst char* msg ,yyscan_t yyscanner );
*yy_cp = '\0'; \
yyg->yy_c_buf_p = yy_cp;
#define YY_NUM_RULES 23
#define YY_END_OF_BUFFER 24
#define YY_NUM_RULES 25
#define YY_END_OF_BUFFER 26
/* This struct is not used in this scanner,
but its presence is necessary. */
struct yy_trans_info
......@@ -356,37 +356,38 @@ struct yy_trans_info
flex_int32_t yy_verify;
flex_int32_t yy_nxt;
};
static yyconst flex_int16_t yy_acclist[158] =
static yyconst flex_int16_t yy_acclist[164] =
{ 0,
4, 4, 24, 22, 23, 21, 22, 23, 21, 23,
22, 23, 1, 22, 23, 22, 23, 11, 22, 23,
4, 11, 22, 23, 4, 22, 23, 16, 22, 23,
11, 22, 23, 15, 22, 23, 9, 10, 22, 23,
16391, 4, 9, 10, 22, 23,16391, 9, 10, 22,
23,16391, 9, 10, 22, 23,16391, 9, 10, 22,
23,16391, 9, 10, 22, 23,16391, 22, 23, 21,
18, 1, 19, 4, 4, 4, 4, 10, 4, 10,
10, 14, 17, 13, 8199, 8, 9, 10,16391, 9,
10,16391, 4, 9, 10,16391, 9, 10,16391, 9,
10,16391, 9, 10,16391, 20, 4, 10, 10, 4,
10, 12, 8, 4, 4, 9, 10,16391, 3, 9,
4, 4, 26, 24, 25, 23, 24, 25, 23, 25,
24, 25, 1, 24, 25, 24, 25, 11, 24, 25,
4, 11, 24, 25, 4, 24, 25, 22, 24, 25,
16, 24, 25, 11, 24, 25, 15, 24, 25, 21,
24, 25, 9, 10, 24, 25,16391, 4, 9, 10,
24, 25,16391, 9, 10, 24, 25,16391, 9, 10,
24, 25,16391, 9, 10, 24, 25,16391, 9, 10,
24, 25,16391, 24, 25, 23, 18, 1, 19, 4,
4, 4, 4, 10, 4, 10, 10, 14, 17, 13,
8199, 8, 9, 10,16391, 9, 10,16391, 4, 9,
10,16391, 9, 10,16391, 9, 10,16391, 9, 10,
16391, 4, 10, 4, 2, 9, 10,16391, 9, 10,
16391, 9, 10,16391, 9, 10,16391, 9, 10,16391,
6, 8199, 9, 10,16391, 5, 8199
16391, 20, 4, 10, 10, 4, 10, 12, 8, 4,
4, 9, 10,16391, 3, 9, 10,16391, 9, 10,
16391, 9, 10,16391, 9, 10,16391, 4, 10, 4,
2, 9, 10,16391, 9, 10,16391, 9, 10,16391,
9, 10,16391, 9, 10,16391, 6, 8199, 9, 10,
16391, 5, 8199
} ;
static yyconst flex_int16_t yy_accept[74] =
static yyconst flex_int16_t yy_accept[76] =
{ 0,
1, 2, 3, 4, 6, 9, 11, 13, 16, 18,
21, 25, 28, 31, 34, 37, 42, 48, 53, 58,
63, 68, 70, 71, 72, 73, 74, 75, 76, 76,
77, 78, 79, 81, 82, 83, 84, 85, 85, 86,
87, 90, 93, 93, 97, 100, 103, 106, 107, 107,
108, 109, 110, 112, 113, 114, 115, 119, 123, 126,
129, 132, 134, 135, 139, 142, 145, 148, 151, 153,
156, 158, 158
21, 25, 28, 31, 34, 37, 40, 43, 48, 54,
59, 64, 69, 74, 76, 77, 78, 79, 80, 81,
82, 82, 83, 84, 85, 87, 88, 89, 90, 91,
91, 92, 93, 96, 99, 99, 103, 106, 109, 112,
113, 113, 114, 115, 116, 118, 119, 120, 121, 125,
129, 132, 135, 138, 140, 141, 145, 148, 151, 154,
157, 159, 162, 164, 164
} ;
static yyconst YY_CHAR yy_ec[256] =
......@@ -396,15 +397,15 @@ static yyconst YY_CHAR yy_ec[256] =
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 4, 5, 1, 6, 1, 1, 7, 1, 8,
9, 9, 10, 9, 10, 11, 9, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 9, 9, 13,
14, 15, 9, 1, 16, 16, 16, 17, 18, 16,
16, 16, 19, 16, 16, 17, 20, 16, 16, 21,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
1, 1, 1, 9, 22, 1, 16, 16, 16, 17,
23, 16, 16, 16, 24, 16, 16, 17, 25, 26,
27, 28, 16, 29, 16, 30, 16, 31, 16, 16,
16, 16, 9, 32, 9, 1, 1, 1, 1, 1,
12, 12, 12, 12, 12, 12, 12, 13, 9, 14,
15, 16, 17, 1, 18, 18, 18, 19, 20, 18,
18, 18, 21, 18, 18, 19, 22, 18, 18, 23,
18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
1, 1, 1, 9, 24, 1, 18, 18, 18, 19,
25, 18, 18, 18, 26, 18, 18, 19, 27, 28,
29, 30, 18, 31, 18, 32, 18, 33, 18, 18,
18, 18, 9, 34, 9, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
......@@ -421,80 +422,80 @@ static yyconst YY_CHAR yy_ec[256] =
1, 1, 1, 1, 1
} ;
static yyconst YY_CHAR yy_meta[33] =
static yyconst YY_CHAR yy_meta[35] =
{ 0,
1, 1, 2, 3, 1, 1, 1, 3, 1, 1,
4, 5, 1, 1, 1, 6, 6, 6, 6, 6,
4, 5, 1, 1, 1, 1, 1, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 1
6, 6, 6, 1
} ;
static yyconst flex_uint16_t yy_base[78] =
static yyconst flex_uint16_t yy_base[80] =
{ 0,
0, 0, 132, 133, 31, 34, 117, 0, 123, 133,
27, 40, 115, 114, 113, 56, 58, 30, 104, 96,
101, 91, 44, 133, 0, 133, 59, 133, 31, 61,
74, 110, 109, 43, 105, 133, 133, 57, 133, 0,
0, 0, 107, 37, 38, 94, 92, 133, 104, 63,
103, 102, 71, 133, 0, 77, 0, 0, 94, 73,
69, 83, 133, 0, 63, 59, 79, 58, 133, 66,
133, 133, 97, 100, 104, 106, 66
0, 0, 134, 135, 33, 36, 118, 0, 125, 135,
29, 31, 135, 116, 115, 114, 135, 49, 51, 34,
104, 96, 101, 91, 62, 135, 0, 135, 55, 135,
35, 57, 59, 112, 111, 71, 106, 135, 135, 64,
135, 0, 0, 0, 109, 73, 66, 94, 92, 135,
106, 75, 105, 104, 76, 135, 0, 78, 0, 0,
94, 86, 84, 87, 135, 0, 64, 60, 83, 48,
135, 50, 135, 135, 97, 100, 104, 106, 46
} ;
static yyconst flex_int16_t yy_def[78] =
static yyconst flex_int16_t yy_def[80] =
{ 0,
72, 1, 72, 72, 72, 72, 72, 73, 72, 72,
72, 74, 72, 72, 72, 75, 75, 17, 17, 17,
17, 72, 72, 72, 73, 72, 72, 72, 72, 72,
74, 76, 76, 76, 72, 72, 72, 72, 72, 77,
17, 17, 72, 17, 17, 17, 17, 72, 72, 72,
76, 76, 76, 72, 77, 72, 17, 17, 17, 17,
17, 76, 72, 17, 17, 17, 17, 17, 72, 17,
72, 0, 72, 72, 72, 72, 72
74, 1, 74, 74, 74, 74, 74, 75, 74, 74,
74, 76, 74, 74, 74, 74, 74, 77, 77, 19,
19, 19, 19, 74, 74, 74, 75, 74, 74, 74,
74, 74, 76, 78, 78, 78, 74, 74, 74, 74,
74, 79, 19, 19, 74, 19, 19, 19, 19, 74,
74, 74, 78, 78, 78, 74, 79, 74, 19, 19,
19, 19, 19, 78, 74, 19, 19, 19, 19, 19,
74, 19, 74, 0, 74, 74, 74, 74, 74
} ;
static yyconst flex_uint16_t yy_nxt[166] =
static yyconst flex_uint16_t yy_nxt[170] =
{ 0,
4, 5, 6, 5, 7, 8, 9, 10, 10, 10,
11, 12, 13, 14, 15, 16, 17, 18, 16, 19,
16, 16, 18, 16, 16, 16, 16, 20, 21, 16,
16, 22, 23, 23, 23, 23, 23, 23, 27, 43,
49, 44, 50, 28, 29, 23, 23, 23, 44, 29,
30, 31, 43, 57, 53, 58, 33, 34, 59, 38,
38, 38, 34, 39, 39, 39, 40, 41, 40, 41,
27, 55, 27, 71, 50, 28, 29, 28, 29, 28,
70, 29, 53, 29, 30, 31, 69, 62, 56, 68,
33, 34, 67, 63, 51, 66, 34, 25, 65, 25,
25, 25, 25, 32, 32, 32, 42, 42, 42, 42,
52, 52, 64, 51, 51, 50, 61, 60, 56, 54,
51, 51, 48, 47, 46, 45, 37, 36, 35, 26,
24, 72, 3, 72, 72, 72, 72, 72, 72, 72,
72, 72, 72, 72, 72, 72, 72, 72, 72, 72,
72, 72, 72, 72, 72, 72, 72, 72, 72, 72,
72, 72, 72, 72, 72
11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
18, 21, 18, 18, 20, 18, 18, 18, 18, 22,
23, 18, 18, 24, 25, 25, 25, 25, 25, 25,
29, 32, 33, 45, 51, 46, 52, 30, 31, 35,
36, 57, 40, 31, 40, 36, 41, 73, 41, 42,
43, 42, 43, 25, 25, 25, 29, 40, 29, 32,
33, 41, 72, 30, 31, 30, 31, 35, 36, 31,
45, 31, 55, 36, 46, 60, 52, 55, 61, 58,
71, 59, 70, 30, 64, 69, 65, 27, 53, 27,
27, 27, 27, 34, 34, 34, 44, 44, 44, 44,
54, 54, 68, 67, 66, 53, 53, 52, 63, 62,
58, 56, 53, 53, 50, 49, 48, 47, 39, 38,
37, 28, 26, 74, 3, 74, 74, 74, 74, 74,
74, 74, 74, 74, 74, 74, 74, 74, 74, 74,
74, 74, 74, 74, 74, 74, 74, 74, 74, 74,
74, 74, 74, 74, 74, 74, 74, 74, 74
} ;
static yyconst flex_int16_t yy_chk[166] =
static yyconst flex_int16_t yy_chk[170] =
{ 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 5, 5, 5, 6, 6, 6, 11, 18,
29, 18, 29, 11, 11, 23, 23, 23, 44, 11,
12, 12, 34, 44, 34, 45, 12, 12, 45, 16,
38, 17, 12, 16, 38, 17, 16, 16, 17, 17,
27, 77, 30, 70, 50, 27, 27, 30, 30, 50,
68, 27, 53, 30, 31, 31, 67, 53, 56, 66,
31, 31, 65, 56, 62, 61, 31, 73, 60, 73,
73, 73, 73, 74, 74, 74, 75, 75, 75, 75,
76, 76, 59, 52, 51, 49, 47, 46, 43, 35,
33, 32, 22, 21, 20, 19, 15, 14, 13, 9,
7, 3, 72, 72, 72, 72, 72, 72, 72, 72,
72, 72, 72, 72, 72, 72, 72, 72, 72, 72,
72, 72, 72, 72, 72, 72, 72, 72, 72, 72,
72, 72, 72, 72, 72
1, 1, 1, 1, 5, 5, 5, 6, 6, 6,
11, 12, 12, 20, 31, 20, 31, 11, 11, 12,
12, 79, 18, 11, 19, 12, 18, 72, 19, 18,
18, 19, 19, 25, 25, 25, 29, 40, 32, 33,
33, 40, 70, 29, 29, 32, 32, 33, 33, 29,
36, 32, 36, 33, 46, 47, 52, 55, 47, 58,
69, 46, 68, 52, 55, 67, 58, 75, 64, 75,
75, 75, 75, 76, 76, 76, 77, 77, 77, 77,
78, 78, 63, 62, 61, 54, 53, 51, 49, 48,
45, 37, 35, 34, 24, 23, 22, 21, 16, 15,
14, 9, 7, 3, 74, 74, 74, 74, 74, 74,
74, 74, 74, 74, 74, 74, 74, 74, 74, 74,
74, 74, 74, 74, 74, 74, 74, 74, 74, 74,
74, 74, 74, 74, 74, 74, 74, 74, 74
} ;
#define YY_TRAILING_MASK 0x2000
......@@ -538,7 +539,7 @@ goto find_rule; \
LPH [A-Za-z_] Alphabetic character
LPHDGT [A-Za-z0-9_] Alphanumeric character
XPN [eE][+-]?[0-9]+ Real number Exponent */
#line 542 "expr_lex.cc"
#line 543 "expr_lex.cc"
#define INITIAL 0
......@@ -831,7 +832,7 @@ YY_DECL
#line 36 "expr_lex.l"
#line 835 "expr_lex.cc"
#line 836 "expr_lex.cc"
while ( /*CONSTCOND*/1 ) /* loops until end-of-file is reached */
{
......@@ -857,14 +858,14 @@ yy_match:
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 73 )
if ( yy_current_state >= 75 )
yy_c = yy_meta[(unsigned int) yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
*yyg->yy_state_ptr++ = yy_current_state;
++yy_cp;
}
while ( yy_base[yy_current_state] != 133 );
while ( yy_base[yy_current_state] != 135 );
yy_find_action:
yy_current_state = *--yyg->yy_state_ptr;
......@@ -1040,22 +1041,32 @@ YY_RULE_SETUP
return OR;
YY_BREAK
case 21:
/* rule 21 can match eol */
YY_RULE_SETUP
#line 99 "expr_lex.l"
; /* ignore whitespace */
#line 98 "expr_lex.l"
return QUESTION;
YY_BREAK
case 22:
YY_RULE_SETUP
#line 99 "expr_lex.l"
return COLON;
YY_BREAK
case 23:
/* rule 23 can match eol */
YY_RULE_SETUP
#line 101 "expr_lex.l"
; /* ignore whitespace */
YY_BREAK
case 24:
YY_RULE_SETUP
#line 103 "expr_lex.l"
yyerror(NULL, NULL, "Unknown character");
YY_BREAK
case 23:
case 25:
YY_RULE_SETUP
#line 102 "expr_lex.l"
#line 104 "expr_lex.l"
ECHO;
YY_BREAK
#line 1059 "expr_lex.cc"
#line 1070 "expr_lex.cc"
case YY_STATE_EOF(INITIAL):
yyterminate();
......@@ -1319,7 +1330,7 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 73 )
if ( yy_current_state >= 75 )
yy_c = yy_meta[(unsigned int) yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
......@@ -1343,11 +1354,11 @@ static int yy_get_next_buffer (yyscan_t yyscanner)
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 73 )
if ( yy_current_state >= 75 )
yy_c = yy_meta[(unsigned int) yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + (flex_int16_t) yy_c];
yy_is_jam = (yy_current_state == 72);
yy_is_jam = (yy_current_state == 74);
if ( ! yy_is_jam )
*yyg->yy_state_ptr++ = yy_current_state;
......@@ -2172,7 +2183,7 @@ void yyfree (void * ptr , yyscan_t yyscanner)
#define YYTABLES_NAME "yytables"
#line 102 "expr_lex.l"
#line 104 "expr_lex.l"
......@@ -82,7 +82,7 @@ M_E {
}
[-()=+*/;,{}^.?:] {
[-()=+*/;,{}^.] {
return *yytext;
}
......@@ -95,6 +95,8 @@ M_E {
"!=" return NE;
"&&" return AND;
"||" return OR;
"?" return QUESTION;
":" return COLON;
[ \t\n\r]+ ; /* ignore whitespace */
......
This diff is collapsed.
......@@ -30,8 +30,8 @@
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
#ifndef YY_YY_EXPR_YACC_H_INCLUDED
# define YY_YY_EXPR_YACC_H_INCLUDED
#ifndef YY_YY_EXPR_YACC_HH_INCLUDED
# define YY_YY_EXPR_YACC_HH_INCLUDED
/* Debug traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
......@@ -48,36 +48,40 @@ extern int yydebug;
CONSTANT = 258,
VARIABLE = 259,
FUNCTION = 260,
REMOVE = 261,
PRINT = 262,
AND = 263,
OR = 264,
LEG = 265,
GE = 266,
LE = 267,
EQ = 268,
NE = 269,
GT = 270,
LT = 271,
UMINUS = 272
QUESTION = 261,
COLON = 262,
REMOVE = 263,
PRINT = 264,
AND = 265,
OR = 266,
LEG = 267,
GE = 268,
LE = 269,
EQ = 270,
NE = 271,
GT = 272,
LT = 273,
UMINUS = 274
};
#endif
/* Tokens. */
#define CONSTANT 258
#define VARIABLE 259
#define FUNCTION 260
#define REMOVE 261
#define PRINT 262
#define AND 263
#define OR 264
#define LEG 265
#define GE 266
#define LE 267
#define EQ 268
#define NE 269
#define GT 270
#define LT 271
#define UMINUS 272
#define QUESTION 261
#define COLON 262
#define REMOVE 263
#define PRINT 264
#define AND 265
#define OR 266
#define LEG 267
#define GE 268
#define LE 269
#define EQ 270
#define NE 271
#define GT 272
#define LT 273
#define UMINUS 274
/* Value type. */
......@@ -85,4 +89,4 @@ extern int yydebug;
int yyparse (parse_param_t *parse_arg, void *scanner);
#endif /* !YY_YY_EXPR_YACC_H_INCLUDED */
#endif /* !YY_YY_EXPR_YACC_HH_INCLUDED */
/* http://epaperpress.com/lexandyacc/download/LexAndYaccTutorial.pdf */
/* bison -y -o expr_yacc.c -d expr_yacc.y */
%{
#include <stdio.h>
......@@ -39,6 +41,7 @@ void freeNode(nodeType *p);
%token <cvalue> CONSTANT
%token <varnm> VARIABLE
%token <fname> FUNCTION
%token QUESTION COLON
%token REMOVE
%token PRINT
......@@ -47,10 +50,9 @@ void freeNode(nodeType *p);
%left '+' '-'
%left '*' '/'
%precedence UMINUS
%right '?' ':'
%right '^'
%type <nPtr> stmt expr stmt_list
%type <nPtr> stmt expr stmt_list ternary
%%
......@@ -67,6 +69,7 @@ stmt:
';' { $$ = expr_opr(';', 2, NULL, NULL); }
| expr ';' { $$ = $1; }
| VARIABLE '=' expr ';' { $$ = expr_opr('=', 2, expr_var($1), $3); }
| VARIABLE '=' ternary ';' { $$ = expr_opr('=', 2, expr_var($1), $3); }
| VARIABLE ';' { $$ = expr_opr('=', 2, expr_var($1), expr_var($1)); } /* conflicts: 1 shift/reduce */
| REMOVE VARIABLE ')' ';' { $$ = expr_com("remove", $2); }
| PRINT VARIABLE ')' ';' { $$ = expr_com("print", $2); }
......@@ -96,13 +99,15 @@ expr:
| expr LEG expr { $$ = expr_opr(LEG, 2, $1, $3); }
| expr AND expr { $$ = expr_opr(AND, 2, $1, $3); }
| expr OR expr { $$ = expr_opr(OR, 2, $1, $3); }
| expr '?' expr ':' expr { $$ = expr_opr('?', 3, $1, $3, $5); }
| '(' expr ')' { $$ = $2; }
| FUNCTION '(' expr ',' '-' CONSTANT ')' { $$ = expr_fun1c($1, $3, - $6); }
| FUNCTION '(' expr ',' CONSTANT ')' { $$ = expr_fun1c($1, $3, $5); }
| FUNCTION '(' expr ')' { $$ = expr_fun($1, $3); }
;
ternary: expr QUESTION expr COLON expr { $$ = expr_opr('?', 3, $1, $3, $5); }
;
%%
#define SIZEOF_NODETYPE ((char *)&p->u.con - (char *)p)
......
# bison version 3.0.4; flex version 2.6.1
bison -W -y -r all -o expr_yacc.cc -d expr_yacc.y
flex -v -oexpr_lex.cc expr_lex.l
mv expr_yacc.hh expr_yacc.h
......@@ -56,7 +56,7 @@ function testfunc()
INSTR="_clev=clev(var130);pottemp=var130*((100000/_clev)^0.287);"
testfunc
#
INSTR="var1=(var129>0)?(var130-273.15):var152;"
INSTR="var1=var129>0?var130-273.15:1*var152;"
testfunc
#
rm -f $CDOOUT $CDOERR
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment