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 testable (i.e. easily generable) grammar for arithmetic 37// expressions. 38// 39// @copyright Copyright © 2007-2016 Hoa community. 40// @license New BSD License 41// 42 43 44%skip space [\x20\x09]+ 45%token bracket_ \( 46%token _bracket \) 47%token comma , 48%token number ([1-9]\d*)(\.\d+)? 49%token plus \+ 50%token minus \- 51%token times \* 52%token div / 53 54expression: 55 primary() ( ::plus:: #addition expression() )? 56 57primary: 58 secondary() ( ::minus:: #substraction expression() )? 59 60secondary: 61 ternary() ( ::times:: #multiplication expression() )? 62 63ternary: 64 term() ( ::div:: #division expression() )? 65 66term: 67 ( ::bracket_:: expression() ::_bracket:: #group ) 68 | number() 69 | ( ::minus:: #negative | ::plus:: ) term() 70 71number: 72 <number> 73