Sha256: 31290c794afe986f530f71d7db65c1b515fa9f8f2e2b179d1a02a059d0b4b3c6
Contents?: true
Size: 933 Bytes
Versions: 10
Compression:
Stored size: 933 Bytes
Contents
# frozen_string_literal: true # Grammar for simple arithmetical expressions require 'rley' # Load the gem ######################################## # Define a grammar for basic arithmetical expressions builder = Rley::grammar_builder do add_terminals('NUMBER') add_terminals('LPAREN', 'RPAREN') # For '(', ')' delimiters add_terminals('PLUS', 'MINUS') # For '+', '-' operators add_terminals('STAR', 'DIVIDE') # For '*', '/' operators rule 'expression' => 'simple_expression' rule 'simple_expression' => 'term' rule 'simple_expression' => 'simple_expression add_operator term' rule 'term' => 'factor' rule 'term' => 'term mul_operator factor' rule 'factor' => 'NUMBER' rule 'factor' => 'LPAREN expression RPAREN' rule 'add_operator' => 'PLUS' rule 'add_operator' => 'MINUS' rule 'mul_operator' => 'STAR' rule 'mul_operator' => 'DIVIDE' end # And now build the grammar... CalcGrammar = builder.grammar
Version data entries
10 entries across 10 versions & 1 rubygems