1// 2// Hoa 3// 4// 5// @license 6// 7// New BSD License 8// 9// Copyright © 2007-2016, Hoa community. All rights reserved. 10// 11// Redistribution and use in source and binary forms, with or without 12// modification, are permitted provided that the following conditions are met: 13// * Redistributions of source code must retain the above copyright 14// notice, this list of conditions and the following disclaimer. 15// * Redistributions in binary form must reproduce the above copyright 16// notice, this list of conditions and the following disclaimer in the 17// documentation and/or other materials provided with the distribution. 18// * Neither the name of the Hoa nor the names of its contributors may be 19// used to endorse or promote products derived from this software without 20// specific prior written permission. 21// 22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE 26// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32// POSSIBILITY OF SUCH DAMAGE. 33// 34// Grammar \Hoa\Math\Arithmetic. 35// 36// Provide a grammar for arithmetic expressions. 37// 38// @copyright Copyright © 2007-2016 Hoa community. 39// @license New BSD License 40// 41 42 43%skip space [\x20\x09]+ 44%token bracket_ \( 45%token _bracket \) 46%token comma , 47%token number (0|[1-9]\d*)(\.\d+)?([eE][\+\-]?\d+)? 48%token plus \+ 49%token minus \-|− 50%token times \*|× 51%token div /|÷ 52%token constant [A-Z_]+[A-Z0-9_]* 53%token id \w+ 54 55expression: 56 primary() ( ::plus:: #addition expression() )? 57 58primary: 59 secondary() ( ::minus:: #substraction expression() )? 60 61secondary: 62 ternary() ( ::times:: #multiplication expression() )? 63 64ternary: 65 term() ( ::div:: #division expression() )? 66 67term: 68 ( ::bracket_:: expression() ::_bracket:: #group ) 69 | number() 70 | constant() 71 | variable() 72 | ( ::minus:: #negative | ::plus:: ) term() 73 | function() 74 75number: 76 <number> 77 78constant: 79 <constant> 80 81#variable: 82 <id> 83 84#function: 85 <id> ::bracket_:: 86 ( expression() ( ::comma:: expression() )* )? 87 ::_bracket:: 88