dhaka 2.0.0 → 2.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/dhaka.rb +24 -22
- data/lib/evaluator/evaluator.rb +42 -44
- data/lib/grammar/closure_hash.rb +4 -3
- data/lib/grammar/grammar.rb +113 -110
- data/lib/grammar/grammar_symbol.rb +6 -3
- data/lib/grammar/precedence.rb +3 -2
- data/lib/grammar/production.rb +5 -6
- data/lib/parser/action.rb +16 -11
- data/lib/parser/channel.rb +22 -16
- data/lib/parser/compiled_parser.rb +28 -22
- data/lib/parser/conflict.rb +54 -0
- data/lib/parser/item.rb +19 -19
- data/lib/parser/parse_result.rb +16 -1
- data/lib/parser/parse_tree.rb +15 -9
- data/lib/parser/parser.rb +51 -80
- data/lib/parser/parser_run.rb +6 -6
- data/lib/parser/parser_state.rb +16 -18
- data/lib/parser/token.rb +6 -4
- data/lib/tokenizer/tokenizer.rb +34 -31
- data/test/all_tests.rb +4 -18
- data/test/another_lalr_but_not_slr_grammar.rb +9 -5
- data/test/{arithmetic_evaluator.rb → arithmetic/arithmetic_evaluator.rb} +1 -2
- data/test/{arithmetic_evaluator_test.rb → arithmetic/arithmetic_evaluator_test.rb} +9 -20
- data/test/arithmetic/arithmetic_grammar.rb +41 -0
- data/test/{arithmetic_grammar_test.rb → arithmetic/arithmetic_grammar_test.rb} +2 -4
- data/test/{arithmetic_test_methods.rb → arithmetic/arithmetic_test_methods.rb} +1 -3
- data/test/{arithmetic_tokenizer.rb → arithmetic/arithmetic_tokenizer.rb} +8 -10
- data/test/{arithmetic_tokenizer_test.rb → arithmetic/arithmetic_tokenizer_test.rb} +4 -2
- data/test/{arithmetic_precedence_evaluator.rb → arithmetic_precedence/arithmetic_precedence_evaluator.rb} +1 -2
- data/test/arithmetic_precedence/arithmetic_precedence_grammar.rb +24 -0
- data/test/{arithmetic_precedence_grammar_test.rb → arithmetic_precedence/arithmetic_precedence_grammar_test.rb} +2 -3
- data/test/arithmetic_precedence/arithmetic_precedence_parser_test.rb +31 -0
- data/test/{arithmetic_precedence_tokenizer.rb → arithmetic_precedence/arithmetic_precedence_tokenizer.rb} +8 -10
- data/test/brackets/bracket_grammar.rb +23 -0
- data/test/{bracket_tokenizer.rb → brackets/bracket_tokenizer.rb} +2 -4
- data/test/{brackets_test.rb → brackets/brackets_test.rb} +3 -4
- data/test/chittagong/chittagong_driver.rb +47 -0
- data/test/{chittagong_driver_test.rb → chittagong/chittagong_driver_test.rb} +66 -58
- data/test/{chittagong_evaluator.rb → chittagong/chittagong_evaluator.rb} +28 -13
- data/test/{chittagong_evaluator_test.rb → chittagong/chittagong_evaluator_test.rb} +6 -10
- data/test/chittagong/chittagong_grammar.rb +110 -0
- data/test/{chittagong_parser_test.rb → chittagong/chittagong_parser_test.rb} +5 -7
- data/test/{chittagong_test.rb → chittagong/chittagong_test.rb} +27 -36
- data/test/{chittagong_tokenizer.rb → chittagong/chittagong_tokenizer.rb} +17 -17
- data/test/{chittagong_tokenizer_test.rb → chittagong/chittagong_tokenizer_test.rb} +2 -3
- data/test/compiled_parser_test.rb +9 -42
- data/test/dhaka_test_helper.rb +17 -0
- data/test/evaluator_test.rb +18 -3
- data/test/grammar_test.rb +10 -15
- data/test/lalr_but_not_slr_grammar.rb +10 -8
- data/test/malformed_grammar.rb +2 -4
- data/test/malformed_grammar_test.rb +2 -3
- data/test/nullable_grammar.rb +11 -8
- data/test/parse_result_test.rb +44 -0
- data/test/parser_state_test.rb +36 -0
- data/test/parser_test.rb +53 -103
- data/test/precedence_grammar.rb +6 -6
- data/test/precedence_grammar_test.rb +2 -3
- data/test/rr_conflict_grammar.rb +5 -7
- data/test/simple_grammar.rb +6 -8
- data/test/sr_conflict_grammar.rb +6 -6
- metadata +30 -26
- data/test/arithmetic_grammar.rb +0 -35
- data/test/arithmetic_precedence_grammar.rb +0 -24
- data/test/arithmetic_precedence_parser_test.rb +0 -33
- data/test/bracket_grammar.rb +0 -25
- data/test/chittagong_grammar.rb +0 -104
- data/test/incomplete_arithmetic_evaluator.rb +0 -60
data/test/precedence_grammar.rb
CHANGED
@@ -1,17 +1,17 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../lib/dhaka'
|
2
|
-
|
3
1
|
class PrecedenceGrammar < Dhaka::Grammar
|
2
|
+
|
4
3
|
precedences do
|
5
|
-
left
|
4
|
+
left %w| * |
|
6
5
|
end
|
7
6
|
|
8
7
|
for_symbol(Dhaka::START_SYMBOL_NAME) do
|
9
|
-
expression
|
10
|
-
whatever
|
8
|
+
expression %w| E * F |
|
9
|
+
whatever %w| E F |, :prec => '*'
|
11
10
|
end
|
12
11
|
|
13
12
|
for_symbol('F') do
|
14
|
-
something
|
13
|
+
something %w| foo |
|
15
14
|
end
|
15
|
+
|
16
16
|
end
|
17
17
|
|
@@ -1,6 +1,5 @@
|
|
1
|
-
require
|
2
|
-
|
3
|
-
require "precedence_grammar"
|
1
|
+
require File.dirname(__FILE__) + '/dhaka_test_helper'
|
2
|
+
require File.dirname(__FILE__) + "/precedence_grammar"
|
4
3
|
|
5
4
|
class TestPrecedenceGrammar < Test::Unit::TestCase
|
6
5
|
def test_precedences_are_computed_correctly
|
data/test/rr_conflict_grammar.rb
CHANGED
@@ -1,22 +1,20 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../lib/dhaka'
|
2
|
-
|
3
1
|
class RRConflictGrammar < Dhaka::Grammar
|
4
2
|
|
5
3
|
for_symbol(Dhaka::START_SYMBOL_NAME) do
|
6
|
-
start
|
4
|
+
start %w| S |
|
7
5
|
end
|
8
6
|
|
9
7
|
for_symbol('S') do
|
10
|
-
a_expansion
|
11
|
-
b_expansion
|
8
|
+
a_expansion %w| A c d |
|
9
|
+
b_expansion %w| B c e |
|
12
10
|
end
|
13
11
|
|
14
12
|
for_symbol('A') do
|
15
|
-
xy
|
13
|
+
xy %w| x y |
|
16
14
|
end
|
17
15
|
|
18
16
|
for_symbol('B') do
|
19
|
-
xy_again
|
17
|
+
xy_again %w| x y |
|
20
18
|
end
|
21
19
|
|
22
20
|
end
|
data/test/simple_grammar.rb
CHANGED
@@ -1,23 +1,21 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../lib/dhaka'
|
2
|
-
|
3
1
|
class SimpleGrammar < Dhaka::Grammar
|
4
2
|
|
5
3
|
for_symbol(Dhaka::START_SYMBOL_NAME) do
|
6
|
-
start
|
4
|
+
start %w| S # |
|
7
5
|
end
|
8
6
|
|
9
7
|
for_symbol('S') do
|
10
|
-
expression
|
8
|
+
expression %w| E |
|
11
9
|
end
|
12
10
|
|
13
11
|
for_symbol('E') do
|
14
|
-
subtraction
|
15
|
-
term
|
12
|
+
subtraction %w| E - T |
|
13
|
+
term %w| T |
|
16
14
|
end
|
17
15
|
|
18
16
|
for_symbol('T') do
|
19
|
-
literal
|
20
|
-
parenthetized_expression
|
17
|
+
literal %w| n |
|
18
|
+
parenthetized_expression %w| ( E ) |
|
21
19
|
end
|
22
20
|
|
23
21
|
end
|
data/test/sr_conflict_grammar.rb
CHANGED
@@ -1,16 +1,16 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../lib/dhaka'
|
2
|
-
|
3
1
|
class SRConflictGrammar < Dhaka::Grammar
|
4
2
|
|
5
3
|
for_symbol(Dhaka::START_SYMBOL_NAME) do
|
6
|
-
statement
|
4
|
+
statement %w| statement |
|
7
5
|
end
|
6
|
+
|
8
7
|
for_symbol('statement') do
|
9
|
-
if_statement
|
8
|
+
if_statement %w| if_statement |
|
10
9
|
end
|
10
|
+
|
11
11
|
for_symbol('if_statement') do
|
12
|
-
if_then_statement
|
13
|
-
if_then_else_statement
|
12
|
+
if_then_statement %w| IF expr THEN statement |
|
13
|
+
if_then_else_statement %w| IF expr THEN statement ELSE statement END |
|
14
14
|
end
|
15
15
|
|
16
16
|
end
|
metadata
CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.0
|
|
3
3
|
specification_version: 1
|
4
4
|
name: dhaka
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 2.0.
|
7
|
-
date: 2007-
|
6
|
+
version: 2.0.1
|
7
|
+
date: 2007-02-11 00:00:00 -05:00
|
8
8
|
summary: An LALR1 parser generator written in Ruby
|
9
9
|
require_paths:
|
10
10
|
- lib
|
@@ -39,6 +39,7 @@ files:
|
|
39
39
|
- lib/parser/action.rb
|
40
40
|
- lib/parser/channel.rb
|
41
41
|
- lib/parser/compiled_parser.rb
|
42
|
+
- lib/parser/conflict.rb
|
42
43
|
- lib/parser/item.rb
|
43
44
|
- lib/parser/parse_result.rb
|
44
45
|
- lib/parser/parse_tree.rb
|
@@ -50,44 +51,47 @@ files:
|
|
50
51
|
- lib/tokenizer/tokenizer.rb
|
51
52
|
- test/all_tests.rb
|
52
53
|
- test/another_lalr_but_not_slr_grammar.rb
|
53
|
-
- test/arithmetic_evaluator.rb
|
54
|
-
- test/arithmetic_evaluator_test.rb
|
55
|
-
- test/arithmetic_grammar.rb
|
56
|
-
- test/arithmetic_grammar_test.rb
|
57
|
-
- test/arithmetic_precedence_evaluator.rb
|
58
|
-
- test/arithmetic_precedence_grammar.rb
|
59
|
-
- test/arithmetic_precedence_grammar_test.rb
|
60
|
-
- test/arithmetic_precedence_parser_test.rb
|
61
|
-
- test/arithmetic_precedence_tokenizer.rb
|
62
|
-
- test/arithmetic_test_methods.rb
|
63
|
-
- test/arithmetic_tokenizer.rb
|
64
|
-
- test/arithmetic_tokenizer_test.rb
|
65
|
-
- test/bracket_grammar.rb
|
66
|
-
- test/bracket_tokenizer.rb
|
67
|
-
- test/brackets_test.rb
|
68
|
-
- test/chittagong_driver_test.rb
|
69
|
-
- test/chittagong_evaluator.rb
|
70
|
-
- test/chittagong_evaluator_test.rb
|
71
|
-
- test/chittagong_grammar.rb
|
72
|
-
- test/chittagong_parser_test.rb
|
73
|
-
- test/chittagong_test.rb
|
74
|
-
- test/chittagong_tokenizer.rb
|
75
|
-
- test/chittagong_tokenizer_test.rb
|
76
54
|
- test/compiled_parser_test.rb
|
55
|
+
- test/dhaka_test_helper.rb
|
77
56
|
- test/evaluator_test.rb
|
78
57
|
- test/fake_logger.rb
|
79
58
|
- test/grammar_test.rb
|
80
|
-
- test/incomplete_arithmetic_evaluator.rb
|
81
59
|
- test/lalr_but_not_slr_grammar.rb
|
82
60
|
- test/malformed_grammar.rb
|
83
61
|
- test/malformed_grammar_test.rb
|
84
62
|
- test/nullable_grammar.rb
|
63
|
+
- test/parse_result_test.rb
|
64
|
+
- test/parser_state_test.rb
|
85
65
|
- test/parser_test.rb
|
86
66
|
- test/precedence_grammar.rb
|
87
67
|
- test/precedence_grammar_test.rb
|
88
68
|
- test/rr_conflict_grammar.rb
|
89
69
|
- test/simple_grammar.rb
|
90
70
|
- test/sr_conflict_grammar.rb
|
71
|
+
- test/arithmetic/arithmetic_evaluator.rb
|
72
|
+
- test/arithmetic/arithmetic_evaluator_test.rb
|
73
|
+
- test/arithmetic/arithmetic_grammar.rb
|
74
|
+
- test/arithmetic/arithmetic_grammar_test.rb
|
75
|
+
- test/arithmetic/arithmetic_test_methods.rb
|
76
|
+
- test/arithmetic/arithmetic_tokenizer.rb
|
77
|
+
- test/arithmetic/arithmetic_tokenizer_test.rb
|
78
|
+
- test/arithmetic_precedence/arithmetic_precedence_evaluator.rb
|
79
|
+
- test/arithmetic_precedence/arithmetic_precedence_grammar.rb
|
80
|
+
- test/arithmetic_precedence/arithmetic_precedence_grammar_test.rb
|
81
|
+
- test/arithmetic_precedence/arithmetic_precedence_parser_test.rb
|
82
|
+
- test/arithmetic_precedence/arithmetic_precedence_tokenizer.rb
|
83
|
+
- test/brackets/bracket_grammar.rb
|
84
|
+
- test/brackets/bracket_tokenizer.rb
|
85
|
+
- test/brackets/brackets_test.rb
|
86
|
+
- test/chittagong/chittagong_driver.rb
|
87
|
+
- test/chittagong/chittagong_driver_test.rb
|
88
|
+
- test/chittagong/chittagong_evaluator.rb
|
89
|
+
- test/chittagong/chittagong_evaluator_test.rb
|
90
|
+
- test/chittagong/chittagong_grammar.rb
|
91
|
+
- test/chittagong/chittagong_parser_test.rb
|
92
|
+
- test/chittagong/chittagong_test.rb
|
93
|
+
- test/chittagong/chittagong_tokenizer.rb
|
94
|
+
- test/chittagong/chittagong_tokenizer_test.rb
|
91
95
|
test_files: []
|
92
96
|
|
93
97
|
rdoc_options: []
|
data/test/arithmetic_grammar.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../lib/dhaka'
|
2
|
-
|
3
|
-
class ArithmeticGrammar < Dhaka::Grammar
|
4
|
-
for_symbol(Dhaka::START_SYMBOL_NAME) do
|
5
|
-
expression ['E']
|
6
|
-
end
|
7
|
-
for_symbol('E') do
|
8
|
-
subtraction ['E', '-', 'T']
|
9
|
-
addition ['E', '+', 'T']
|
10
|
-
term ['T']
|
11
|
-
end
|
12
|
-
for_symbol('T') do
|
13
|
-
factor ['F']
|
14
|
-
division ['T', '/', 'F']
|
15
|
-
multiplication ['T', '*', 'F']
|
16
|
-
end
|
17
|
-
for_symbol('F') do
|
18
|
-
getting_literals ['n']
|
19
|
-
unpacking_parenthetized_expression ['(', 'E', ')']
|
20
|
-
function ['Function']
|
21
|
-
end
|
22
|
-
for_symbol('Function') do
|
23
|
-
evaluating_function ['FunctionName', '(', 'Args', ')']
|
24
|
-
end
|
25
|
-
for_symbol('FunctionName') do
|
26
|
-
max_function ['h']
|
27
|
-
min_function ['l']
|
28
|
-
end
|
29
|
-
for_symbol('Args') do
|
30
|
-
empty_args []
|
31
|
-
single_args ['E']
|
32
|
-
concatenating_args ['E', ',', 'Args']
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
@@ -1,24 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../lib/dhaka'
|
2
|
-
|
3
|
-
class ArithmeticPrecedenceGrammar < Dhaka::Grammar
|
4
|
-
precedences do
|
5
|
-
left ['+', '-']
|
6
|
-
left ['*', '/']
|
7
|
-
nonassoc ['^']
|
8
|
-
end
|
9
|
-
|
10
|
-
for_symbol(Dhaka::START_SYMBOL_NAME) do
|
11
|
-
expression ['E']
|
12
|
-
end
|
13
|
-
|
14
|
-
for_symbol('E') do
|
15
|
-
addition ['E', '+', 'E']
|
16
|
-
subtraction ['E', '-', 'E']
|
17
|
-
multiplication ['E', '*', 'E']
|
18
|
-
division ['E', '/', 'E']
|
19
|
-
power ['E', '^', 'E']
|
20
|
-
literal ['n']
|
21
|
-
parenthetized_expression ['(', 'E', ')']
|
22
|
-
negated_expression ['-', 'E'], :prec => '*'
|
23
|
-
end
|
24
|
-
end
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require "test/unit"
|
2
|
-
require "arithmetic_precedence_grammar"
|
3
|
-
require "arithmetic_precedence_tokenizer"
|
4
|
-
require "arithmetic_precedence_evaluator"
|
5
|
-
require "fake_logger"
|
6
|
-
|
7
|
-
class TestArithmeticPrecedenceParser < Test::Unit::TestCase
|
8
|
-
|
9
|
-
def test_parses_arithmetic_expressions
|
10
|
-
fake_logger = FakeLogger.new
|
11
|
-
parser = Dhaka::Parser.new(ArithmeticPrecedenceGrammar, fake_logger)
|
12
|
-
eval(parser.compile_to_ruby_source_as(:ArithmeticPrecedenceParser))
|
13
|
-
assert_equal(30, fake_logger.warnings.size)
|
14
|
-
assert_equal(0, fake_logger.errors.size)
|
15
|
-
|
16
|
-
assert_equal(-8, evaluate(parse("5 * -14/(2*7 - 7) + 2").parse_tree))
|
17
|
-
assert_equal(-4, evaluate(parse("-2^2").parse_tree))
|
18
|
-
assert_equal(10, evaluate(parse("2+2^3").parse_tree))
|
19
|
-
assert_equal(64, evaluate(parse("(2+2)^3").parse_tree))
|
20
|
-
assert_equal(128, evaluate(parse("(2+2)^3*2").parse_tree))
|
21
|
-
assert(parse("(2+2)^3^2").has_error?)
|
22
|
-
end
|
23
|
-
|
24
|
-
def parse(input)
|
25
|
-
ArithmeticPrecedenceParser.parse(ArithmeticPrecedenceTokenizer.tokenize(input))
|
26
|
-
end
|
27
|
-
|
28
|
-
def evaluate(parse_tree)
|
29
|
-
ArithmeticPrecedenceEvaluator.new.evaluate(parse_tree)
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
|
data/test/bracket_grammar.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../lib/dhaka'
|
2
|
-
|
3
|
-
class BracketGrammar < Dhaka::Grammar
|
4
|
-
|
5
|
-
for_symbol(Dhaka::START_SYMBOL_NAME) do
|
6
|
-
start ['Package']
|
7
|
-
end
|
8
|
-
|
9
|
-
for_symbol('Package') do
|
10
|
-
soft_wrapped_package ['(', 'Contents', ')']
|
11
|
-
cardboard_package ['[', 'Contents', ']']
|
12
|
-
wooden_package ['{', 'Contents', '}']
|
13
|
-
end
|
14
|
-
|
15
|
-
for_symbol('Contents') do
|
16
|
-
bracket ['B']
|
17
|
-
set_of_packages ['SetOfPackages']
|
18
|
-
end
|
19
|
-
|
20
|
-
for_symbol('SetOfPackages') do
|
21
|
-
one_package ['Package']
|
22
|
-
multiple_packages ['SetOfPackages', 'Package']
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
data/test/chittagong_grammar.rb
DELETED
@@ -1,104 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../lib/dhaka'
|
2
|
-
|
3
|
-
class ChittagongGrammar < Dhaka::Grammar
|
4
|
-
|
5
|
-
precedences do
|
6
|
-
nonassoc ['==']
|
7
|
-
nonassoc ['<', '>']
|
8
|
-
left ['+', '-']
|
9
|
-
left ['*', '/']
|
10
|
-
nonassoc ['^']
|
11
|
-
nonassoc ['!']
|
12
|
-
end
|
13
|
-
|
14
|
-
for_symbol(Dhaka::START_SYMBOL_NAME) do
|
15
|
-
program ['opt_terms', 'main_body', 'opt_terms']
|
16
|
-
end
|
17
|
-
|
18
|
-
for_symbol('main_body') do
|
19
|
-
single_main_body_statement ['main_body_statement']
|
20
|
-
multiple_main_body_statements ['main_body', 'terms', 'main_body_statement']
|
21
|
-
end
|
22
|
-
|
23
|
-
for_symbol('main_body_statement') do
|
24
|
-
main_body_simple_statement ['simple_statement']
|
25
|
-
function_definition ['def', 'function_name', '(', 'arg_declarations', ')', 'terms',
|
26
|
-
'function_body', 'terms', 'end']
|
27
|
-
main_body_if_statement ['if', 'expression', 'terms', 'main_body', 'terms', 'end']
|
28
|
-
main_body_if_else_statement ['if', 'expression', 'terms', 'main_body',
|
29
|
-
'terms', 'else', 'terms', 'main_body', 'terms', 'end']
|
30
|
-
main_body_while_statement ['while', 'expression', 'terms', 'main_body', 'terms',
|
31
|
-
'end']
|
32
|
-
end
|
33
|
-
|
34
|
-
for_symbol('simple_statement') do
|
35
|
-
assignment_statement ['var_name', '=', 'expression']
|
36
|
-
print_statement ['print', 'expression']
|
37
|
-
function_call_statement ['function_name', '(', 'arg_list', ')']
|
38
|
-
end
|
39
|
-
|
40
|
-
for_symbol('function_body') do
|
41
|
-
single_function_body_statement ['function_body_statement']
|
42
|
-
multiple_function_body_statements ['function_body', 'terms', 'function_body_statement']
|
43
|
-
end
|
44
|
-
|
45
|
-
for_symbol('function_body_statement') do
|
46
|
-
function_body_simple_statement ['simple_statement']
|
47
|
-
return_statement ['return', 'expression']
|
48
|
-
function_body_if_statement ['if', 'expression', 'terms', 'function_body', 'terms', 'end']
|
49
|
-
function_body_if_else_statement ['if', 'expression', 'terms', 'function_body', 'terms', 'else', 'terms', 'function_body', 'terms', 'end']
|
50
|
-
function_body_while_statement ['while', 'expression', 'terms', 'function_body', 'terms', 'end']
|
51
|
-
end
|
52
|
-
|
53
|
-
for_symbol('function_name') do
|
54
|
-
function_name ['word_literal']
|
55
|
-
end
|
56
|
-
|
57
|
-
for_symbol('var_name') do
|
58
|
-
variable_name ['word_literal']
|
59
|
-
end
|
60
|
-
|
61
|
-
for_symbol('arg_declarations') do
|
62
|
-
no_arg_decl []
|
63
|
-
single_arg_declaration ['arg_decl']
|
64
|
-
multiple_arg_declarations ['arg_declarations', ',', 'arg_decl']
|
65
|
-
end
|
66
|
-
|
67
|
-
for_symbol('arg_decl') do
|
68
|
-
arg_declaration ['word_literal']
|
69
|
-
end
|
70
|
-
|
71
|
-
for_symbol('arg_list') do
|
72
|
-
no_args []
|
73
|
-
single_arg ['expression']
|
74
|
-
multiple_args ['arg_list', ',', 'expression']
|
75
|
-
end
|
76
|
-
|
77
|
-
for_symbol('expression') do
|
78
|
-
negation ['!', 'expression']
|
79
|
-
equality_comparison ['expression', '==', 'expression']
|
80
|
-
greater_than_comparison ['expression', '>', 'expression']
|
81
|
-
less_than_comparison ['expression', '<', 'expression']
|
82
|
-
addition ['expression', '+', 'expression']
|
83
|
-
subtraction ['expression', '-', 'expression']
|
84
|
-
multiplication ['expression', '*', 'expression']
|
85
|
-
division ['expression', '/', 'expression']
|
86
|
-
power ['expression', '^', 'expression']
|
87
|
-
literal ['int_literal']
|
88
|
-
function_call_expression ['function_name', '(', 'arg_list', ')']
|
89
|
-
variable_reference ['var_name']
|
90
|
-
parenthetized_expression ['(', 'expression', ')']
|
91
|
-
negated_expression ['-', 'expression'], :prec => '*'
|
92
|
-
end
|
93
|
-
|
94
|
-
for_symbol('terms') do
|
95
|
-
single_term ['newline']
|
96
|
-
multiple_terms ['terms', 'newline']
|
97
|
-
end
|
98
|
-
|
99
|
-
for_symbol('opt_terms') do
|
100
|
-
some_terms ['terms']
|
101
|
-
no_terms []
|
102
|
-
end
|
103
|
-
|
104
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
require File.dirname(__FILE__)+'/../lib/dhaka'
|
2
|
-
require 'arithmetic_grammar'
|
3
|
-
|
4
|
-
class ArithmeticEvaluator < Dhaka::Evaluator
|
5
|
-
|
6
|
-
self.grammar = ArithmeticGrammar
|
7
|
-
|
8
|
-
define_evaluation_rules do
|
9
|
-
|
10
|
-
for_subtraction do
|
11
|
-
child_nodes[0] - child_nodes[2]
|
12
|
-
end
|
13
|
-
|
14
|
-
for_addition do
|
15
|
-
child_nodes[0] + child_nodes[2]
|
16
|
-
end
|
17
|
-
|
18
|
-
for_division do
|
19
|
-
child_nodes[0].to_f/child_nodes[2]
|
20
|
-
end
|
21
|
-
|
22
|
-
for_multiplication do
|
23
|
-
child_nodes[0] * child_nodes[2]
|
24
|
-
end
|
25
|
-
|
26
|
-
for_getting_literals do
|
27
|
-
child_nodes[0].token.value
|
28
|
-
end
|
29
|
-
|
30
|
-
for_start_production do
|
31
|
-
child_nodes[0]
|
32
|
-
end
|
33
|
-
|
34
|
-
for_empty_args do
|
35
|
-
[]
|
36
|
-
end
|
37
|
-
|
38
|
-
for_evaluating_function do
|
39
|
-
child_nodes[0].call child_nodes[2]
|
40
|
-
end
|
41
|
-
|
42
|
-
for_concatenating_args do
|
43
|
-
[child_nodes[0]]+child_nodes[2]
|
44
|
-
end
|
45
|
-
|
46
|
-
for_single_args do
|
47
|
-
[child_nodes[0]]
|
48
|
-
end
|
49
|
-
|
50
|
-
for_min_function do
|
51
|
-
Proc.new {|args| args.inject {|min, elem| min = (elem < min ? elem : min)}}
|
52
|
-
end
|
53
|
-
|
54
|
-
for_max_function do
|
55
|
-
Proc.new {|args| args.inject {|max, elem| max = (elem > max ? elem : max)}}
|
56
|
-
end
|
57
|
-
|
58
|
-
end
|
59
|
-
|
60
|
-
end
|