[GRASS5] sql expression weirdnesses

Glynn Clements glynn at gclements.plus.com
Sat Jun 11 00:15:55 EDT 2005


Daniel Calvelo Aros wrote:

> > The SQL parser doesn't implement the normal precedence rules for
> > arithmetic operators, so e.g.:

[snip]

> Thanks for looking into it. I feared it would be a parser bug. IMHO a serious
> one... Well, time to dig back into yacc. Anybody feeling like doing it in my
> not-fully-acquainted-with-dbmi place?

Try the attached patch. It compiles OK (on Linux), but I haven't
tested it.

-- 
Glynn Clements <glynn at gclements.plus.com>

-------------- next part --------------
Index: lib/db/sqlp/lex.l
===================================================================
RCS file: /grassrepository/grass51/lib/db/sqlp/lex.l,v
retrieving revision 1.19
diff -u -r1.19 lex.l
--- lib/db/sqlp/lex.l	9 Sep 2004 20:02:23 -0000	1.19
+++ lib/db/sqlp/lex.l	11 Jun 2005 04:13:57 -0000
@@ -82,18 +82,6 @@
 		}
  
  /***************************************
-  * ARITHMETICAL OPERATOR
-  * Conflict of  * as operator with * as all columns, what to do?    
-  ***************************************/
-
-"+" 	|
-"-" 	|
-"/" 		{
-			yylval.strval = (char*)strdup(yytext);
-			return ARITHMETICAL_OPERATOR;
-		}
-
- /***************************************
   * COMPARISON OPERATOR
   ***************************************/
 
Index: lib/db/sqlp/yac.y
===================================================================
RCS file: /grassrepository/grass51/lib/db/sqlp/yac.y,v
retrieving revision 1.20
diff -u -r1.20 yac.y
--- lib/db/sqlp/yac.y	9 Sep 2004 20:02:23 -0000	1.20
+++ lib/db/sqlp/yac.y	11 Jun 2005 04:13:57 -0000
@@ -41,13 +41,14 @@
 	/* operators */
 %type <node>	y_column
 %type <node>	y_value
+%type <node>	y_atom
+%type <node>	y_product
 %type <node>	y_expression
 %type <node>	y_comparison
 %type <node>	y_condition
 %type <node>	y_sub_condition
 
 	/* literal keyword tokens */
-%token <strval> ARITHMETICAL_OPERATOR
 %token <strval> COMPARISON_OPERATOR
 %token <strval> NAME
 %token <strval> STRING
@@ -220,14 +221,29 @@
 
 /* Mathematical expression */
 y_expression:
-		y_value				{ $$ = $1; }
-	|	y_column			{ $$ = $1; }
-	|	y_expression ARITHMETICAL_OPERATOR y_expression {
-		    $$ = sqpNewExpressionNode ( sqpOperatorCode($2), $1, $3 );
+		y_product			{ $$ = $1; }
+	|	y_expression '+' y_product {
+		    $$ = sqpNewExpressionNode ( sqpOperatorCode("+"), $1, $3 );
+		}
+	|	y_expression '-' y_product {
+		    $$ = sqpNewExpressionNode ( sqpOperatorCode("-"), $1, $3 );
 		}
-	|	y_expression '*' y_expression {
+	;
+
+y_product:
+		y_atom				{ $$ = $1; }
+	|	y_expression '*' y_atom {
 		    $$ = sqpNewExpressionNode ( sqpOperatorCode("*"), $1, $3 );
 		}
+	|	y_expression '/' y_atom {
+		    $$ = sqpNewExpressionNode ( sqpOperatorCode("/"), $1, $3 );
+		}
+	;
+
+y_atom:
+		y_value				{ $$ = $1; }
+	|	y_column			{ $$ = $1; }
+	|	'(' y_expression ')'		{ $$ = $2; }
 	;
 
 /* Value used in expressions */ 


More information about the grass-dev mailing list