%{ #include /* Formal specification of minimum formula syntax, in bison/yacc form. by David A. Wheeler. Released under the "MIT license": Copyright (c) 2005 David A. Wheeler Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Notes: + See OpenOffice specification 22 March 2004, pg 171, section 8.1.3. + Note that the formula syntax used for exchange is NOT necessarily the format displayed to users. E.G., users might see "A1" displayed, meaning cell A1, but that'd be stored and exchanged as "[.A1]". This is perhaps unfortunate, since this means that the exchange format is always different than what is shown the user (beyond locale differences) + Ordinary precedence and parentheses are allowed; 3+2*5 is 13. + You can label cells or cell ranges with IDENTIFIERs, and then refer to them in the spreadsheet; simply use the IDENTIFIER. + Labels may refer to ranges of cells. Currently can't have label:label; that seems like an unnecessary weakness. + Function calls always include the parentheses, and can be given zero or more parameters. If there are parameters, none can be empty. RAND() is legal, and so is SUM(.A1;2;3). RAND is not a legal function call (need the parens). + Note: Parameter separators are semicolons, not commas. This is rather inconsistent with the rest of the world, because Commas are much more common separators for parameters. E.G., C, C++, C#, Java, Ada, Fortran. I presume that the UI uses semicolon so that users with locales that use "," as the decimal point won't get confused. This isn't strictly necessary here, since numbers are C locale, but there IS an advantage to having the internal syntax being similar to what's actually stored and received. But shouldn't the READERS at least understand "," since it's a likely mistake? + Function call names appear to be transformed to all upper case before being transferred, but readers should probably accept either case and NOT treat case as significant (so "sum" and "SUM" are the same). At least, I _think_ that's true - need to confirm. + The logical operators are IF(condition;if-true;if-false), AND(...), OR(...), NOT(x), TRUE(), and FALSE(). + OOo uses stronger typing than other spreadsheets, e.g., OR(1,2) isn't TRUE, but an error. Is that really a good idea, since it will probably impede interoperability and user expectations? + This grammar treats logical operators and function calls as identical, since they have the same syntax. However, I believe the logical operators IF(), AND(), and OR() must short-circuit, e.g., not do excess computational work. As long as there are no functions with side-effects it doesn't matter, but some user-defined functions in some languages COULD have side-effects. An implementation must operate as though it short-circuits, though it may actually compute in parallel as long as only side-effect-free operations are performed (it may not be possible to ensure that some user-defined functions are side-effect free). + Unary minus and plus are allowed, so "-[.B1]" is legal. Here they're handled by the grammar, not the lexer. + Comparison operations (x=y, etc.) return TRUE() or FALSE(). + The operators +,-,*,/,^ convert booleans on either side to a number (false->0, true->1). So (3>2)*5 is 5. */ void process(char *s); void yyerror(char *s); int yydebug=1; %} %token NUMBER STRING CELLRANGEADDRESSLIST IDENTIFIER /* Define precedence and assocation direction */ %left GE LE EQ NE '>' '<' '=' %left '+' '-' '&' %left '*' '/' /* You could argue to reverse the next two, or change to %left UMINUS */ %right '^' %nonassoc UMINUS %start formula %% formula: '=' expr ; parameter: expr {process("Function parameter");} ; nonempty_expr_list: parameter | nonempty_expr_list ';' parameter ; expr_list: /* empty - zero parameters okay. */ | nonempty_expr_list ; /* It's not valid to say IDENTIFIER : IDENTIFIER, but I think it should be. */ /* end_range: | ':' IDENTIFIER ; */ expr: NUMBER {process("Constant number");} | STRING {process("Constant string");} | CELLRANGEADDRESSLIST {process("Cell address(es)");} | IDENTIFIER '(' expr_list ')' {process("Function call");} | IDENTIFIER /* end_range */ {process("Labelled value");} | '-' expr %prec UMINUS {process("Unary minus");} | '+' expr %prec UMINUS {process("Unary plus");} | expr '+' expr {process("add");} | expr '-' expr {process("Subtract");} | expr '&' expr {process("String concatenation");} | expr '*' expr {process("Multiply");} | expr '/' expr {process("Divide");} | expr '^' expr {process("Power");} | expr '<' expr {process("Less-than?");} | expr '>' expr {process("Greater-than?");} | expr GE expr {process("Greater-than-or-equal-to?");} | expr LE expr {process("Less-than-or-equal-to?");} | expr NE expr {process("Not-equal?");} | expr EQ expr {process("Equal?");} | '(' expr ')' {} ; %% void process(char *s) { fprintf(stdout, "%s\n", s); } void yyerror(char *s) { fprintf(stdout, "%s\n", s); } int main(void) { yyparse(); return 0; }