depager 0.2.3 → 0.3.0.b20160729

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. checksums.yaml +7 -0
  2. data/README.en +4 -19
  3. data/README.ja +42 -79
  4. data/bin/depager +42 -45
  5. data/examples/action_pl0d/pl0d.action.dr +421 -0
  6. data/examples/action_pl0d/test.pl0ds +49 -0
  7. data/examples/c89/c89.dr +493 -496
  8. data/examples/c89/test.c89 +10 -10
  9. data/examples/extension/astdf.rb +10 -0
  10. data/examples/extension/atree.dr +55 -0
  11. data/examples/{sample_calc → extension}/calc.atree.dr +42 -43
  12. data/examples/extension/calc.simple_action.dr +33 -0
  13. data/examples/extension/paction.dr +16 -15
  14. data/examples/extension/pactiontest.dr +14 -14
  15. data/examples/extension/simple_action.rb +44 -0
  16. data/examples/pl0d/pl0ds.dr +337 -334
  17. data/examples/pl0d/test.pl0ds +33 -33
  18. data/examples/rie_calc/calc.rie.dr +57 -0
  19. data/examples/rie_calc/test.calc +4 -0
  20. data/examples/rie_dcuse/dcuse.rie.dr +71 -0
  21. data/examples/rie_dcuse/test.dcuse +1 -0
  22. data/examples/rie_pl0/orig_ex/exerrdg.pl0 +44 -0
  23. data/examples/rie_pl0/orig_ex/exerrm.pl0 +19 -0
  24. data/examples/rie_pl0/orig_ex/exerrmre.pl0 +20 -0
  25. data/examples/rie_pl0/orig_ex/exerrtok.pl0 +18 -0
  26. data/examples/rie_pl0/orig_ex/exmdg.pl0 +40 -0
  27. data/examples/rie_pl0/orig_ex/exmdgwwl.pl0 +43 -0
  28. data/examples/rie_pl0/orig_ex/exmrw.pl0 +22 -0
  29. data/examples/rie_pl0/orig_ex/exmwwl.pl0 +18 -0
  30. data/examples/rie_pl0/orig_ex/exnorw.pl0 +17 -0
  31. data/examples/rie_pl0/pl0.rie.dr +450 -0
  32. data/examples/rie_pl0/test.pl0 +10 -0
  33. data/examples/sample_calc/calc.action.dr +33 -33
  34. data/examples/sample_calc/calc.ast.action.dr +65 -66
  35. data/examples/sample_calc/calc.ast.dr +55 -55
  36. data/examples/sample_calc/calc.cst.dr +45 -45
  37. data/examples/sample_calc/calc.dr +43 -43
  38. data/examples/sample_calc/calc.lex.dr +29 -29
  39. data/examples/sample_calc/{calc_prec.nvaction.dr → calc_prec.action.dr} +31 -31
  40. data/examples/slex_test/divreg.slex.dr +29 -29
  41. data/examples/slex_test/ljoin.slex.dr +36 -36
  42. data/examples/slex_test/test.divreg +1 -1
  43. data/examples/slex_test/test.ljoin +3 -3
  44. data/lib/depager.rb +582 -670
  45. data/lib/depager/grammar.rb +256 -291
  46. data/lib/depager/lr.rb +574 -579
  47. data/lib/depager/parser.rb +282 -277
  48. data/lib/depager/ruby/plugins/_rie_debug.rb +35 -0
  49. data/lib/depager/ruby/plugins/action.rb +53 -43
  50. data/lib/depager/ruby/plugins/ast.dr +364 -269
  51. data/lib/depager/ruby/plugins/ast.rb +1367 -1308
  52. data/lib/depager/ruby/plugins/cst.dr +172 -180
  53. data/lib/depager/ruby/plugins/cst.rb +587 -626
  54. data/lib/depager/ruby/plugins/lex.dr +85 -89
  55. data/lib/depager/ruby/plugins/lex.rb +310 -336
  56. data/lib/depager/ruby/plugins/rie.dr +723 -0
  57. data/lib/depager/ruby/plugins/rie.rb +1653 -0
  58. data/lib/depager/ruby/plugins/slex.dr +202 -200
  59. data/lib/depager/ruby/plugins/slex.rb +780 -817
  60. data/lib/depager/ruby/plugins/srp.rb +56 -51
  61. data/lib/depager/ruby/templates/extension_lalr_master.erb +46 -51
  62. data/lib/depager/ruby/templates/extension_lalr_slave.erb +99 -107
  63. data/lib/depager/ruby/templates/single_lalr_parser.erb +115 -117
  64. data/lib/depager/utils.rb +148 -318
  65. data/lib/depager/version.rb +4 -3
  66. metadata +52 -60
  67. data/ChangeLog +0 -16
  68. data/data/depager/pre-setup.rb +0 -3
  69. data/examples/c89/c89.tab.rb +0 -7127
  70. data/examples/pl0d/pl0ds.tab.rb +0 -2698
  71. data/examples/sample_calc/calc.action.tab.rb +0 -457
  72. data/examples/sample_calc/calc.ast.action.tab.rb +0 -749
  73. data/examples/sample_calc/calc.ast.tab.rb +0 -665
  74. data/examples/sample_calc/calc.astdf.dr +0 -54
  75. data/examples/sample_calc/calc.astdf.tab.rb +0 -672
  76. data/examples/sample_calc/calc.atree.tab.rb +0 -451
  77. data/examples/sample_calc/calc.cst.tab.rb +0 -644
  78. data/examples/sample_calc/calc.lex.tab.rb +0 -374
  79. data/examples/sample_calc/calc.nvaction.dr +0 -33
  80. data/examples/sample_calc/calc.nvaction.tab.rb +0 -465
  81. data/examples/sample_calc/calc.tab.rb +0 -365
  82. data/examples/sample_calc/calc_prec.nvaction.tab.rb +0 -431
  83. data/examples/slex_test/divreg.slex.tab.rb +0 -303
  84. data/examples/slex_test/ljoin.slex.tab.rb +0 -370
  85. data/lib/depager/ruby/plugins/_ast_tmpl.rb +0 -73
  86. data/lib/depager/ruby/plugins/astdf.rb +0 -6
  87. data/lib/depager/ruby/plugins/atree.dr +0 -55
  88. data/lib/depager/ruby/plugins/atree.rb +0 -347
  89. data/lib/depager/ruby/plugins/nvaction.rb +0 -19
  90. data/lib/depager/ruby/templates/simple.erb +0 -23
  91. data/setup.rb +0 -1585
@@ -1,34 +1,34 @@
1
- const m = 7, n = 85;
2
- var x,y;
3
-
4
- function puts10(x)
5
- var i;
6
- begin
7
- i := 0;
8
- while i < 10 do
9
- begin
10
- write x; writeln;
11
- i := i + 1;
12
- end;
13
- return i
14
- end;
15
-
16
- function fact(x)
17
- begin
18
- write x; writeln;
19
- if x = 1 then return 1;
20
- return x * fact(x - 1);
21
- end;
22
-
23
- begin
24
- x := m; y := n;
25
- write x; write y; writeln;
26
- x := 84; y := 36;
27
- write x; write y; writeln;
28
-
29
- y := puts10(5);
30
- write y; writeln;
31
-
32
- y := fact(5);
33
- write y; writeln;
1
+ const m = 7, n = 85;
2
+ var x,y;
3
+
4
+ function puts10(x)
5
+ var i;
6
+ begin
7
+ i := 0;
8
+ while i < 10 do
9
+ begin
10
+ write x; writeln;
11
+ i := i + 1;
12
+ end;
13
+ return i
14
+ end;
15
+
16
+ function fact(x)
17
+ begin
18
+ write x; writeln;
19
+ if x = 1 then return 1;
20
+ return x * fact(x - 1);
21
+ end;
22
+
23
+ begin
24
+ x := m; y := n;
25
+ write x; write y; writeln;
26
+ x := 84; y := 36;
27
+ write x; write y; writeln;
28
+
29
+ y := puts10(5);
30
+ write y; writeln;
31
+
32
+ y := fact(5);
33
+ write y; writeln;
34
34
  end.
@@ -0,0 +1,57 @@
1
+ %class TinyCalc::Parser
2
+ %extend Depager::Lexer ('plugins/lex.rb')
3
+ %extend Depager::Rie ('plugins/rie.rb')
4
+ #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
5
+ %decorate @Rie
6
+ %%
7
+
8
+ %LEX{
9
+ /\s+/, /\#.*/ { }
10
+ /[A-Za-z]\w*/ { yield token(:ID, $&) }
11
+ /\d+/ { yield token(:NUMBER, $&.to_i) }
12
+ /./ { yield token($&, $&) }
13
+ %}
14
+
15
+ #begin-rule
16
+ program:
17
+ vardecl_list expr
18
+ {
19
+ %thread env
20
+ %except vardecl_list._env = { }
21
+ printf "vars:%s\nresult:%s\n", expr._env.inspect, expr.value
22
+ }
23
+ ;
24
+ vardecl_list [ _env:eq, env ] :
25
+ { %thread env }
26
+ | vardecl_list vardecl { %thread env }
27
+ ;
28
+ vardecl [ _env:eq, env] :
29
+ ID '=' expr ';'
30
+ {
31
+ expr._env = $._env
32
+ $.env = $._env.merge({ ID.value => expr.value })
33
+ $._env[ ID.value ] and printf "'%s' redefined.\n", ID.value
34
+ }
35
+ ;
36
+ expr [ _env:eq, value ] :
37
+ expr '+' term { %transfer _env ; $.value = expr.value + term.value }
38
+ | expr '-' term { %transfer _env ; $.value = expr.value - term.value }
39
+ | term { %transfer _env, value }
40
+ ;
41
+ term [ _env:eq, value ] :
42
+ term '*' fact { %transfer _env ; $.value = term.value * fact.value }
43
+ | term '/' fact { %transfer _env ; $.value = term.value / fact.value }
44
+ | fact { %transfer _env, value }
45
+ ;
46
+ fact [ _env:eq, value ] :
47
+ ID
48
+ {
49
+ $.value = $._env[ ID.value ] || 0
50
+ $._env[ ID.value ] or printf "'%s' undefined.\n", ID.value
51
+ }
52
+ | NUMBER { $.value = NUMBER.value }
53
+ ;
54
+ #end-rule
55
+ %%
56
+ parser = TinyCalc.create_decorated_parser
57
+ r, = parser.parse(STDIN)
@@ -0,0 +1,4 @@
1
+ a=10;
2
+ b=20+x;
3
+ a=10;
4
+ a+b
@@ -0,0 +1,71 @@
1
+ %class DcUse::Parser
2
+ %extend Depager::Lexer ('plugins/lex.rb')
3
+ %extend Depager::Rie ('plugins/rie.rb')
4
+ #%decorate Depager::LALR::ShiftReducePrinter ('plugins/srp.rb')
5
+ %decorate @Rie
6
+ %%
7
+
8
+ %LEX{
9
+ /\s+/, /\#.*/ { }
10
+ /use/ { yield token(:USE) }
11
+ /dcl / { yield token(:DCL) }
12
+ /[A-Za-z_]\w*/ { yield token(:ID, $&) }
13
+ /./ { yield token($&, $&) }
14
+ %}
15
+
16
+ #begin-rule
17
+ program:
18
+ dclist stlist
19
+ { stlist._env = dclist.env }
20
+ ;
21
+ dclist [ env ] :
22
+ dc
23
+ { $.env = newenv([ dc.name ]) }
24
+ | dclist dc
25
+ {
26
+ $.env = dclist.env | [ dc.name ]
27
+ not dclist.env.include? dc.name or
28
+ message "redeclaration '%s'.\n", dc.name
29
+ }
30
+ ;
31
+ dc [ name ] :
32
+ DCL ID ';'
33
+ { $.name = ID.value }
34
+ ;
35
+ stlist [ _env ] :
36
+ stlist st
37
+ { %transfer _env }
38
+ | st
39
+ { %transfer _env }
40
+ ;
41
+ st [ _env ] :
42
+ USE ID ';'
43
+ {
44
+ $._env.include? ID.value or
45
+ message "not declared '%s'.\n", ID.value
46
+ }
47
+ ;
48
+ #end-rule
49
+ %%
50
+ class DcUseEnv
51
+ def initialize ary
52
+ @ary = ary
53
+ end
54
+ def | ary
55
+ DcUseEnv.new(@ary | ary)
56
+ end
57
+ def include? i
58
+ @ary.include? i
59
+ end
60
+ def inspect
61
+ "{#{@ary.inspect}}"
62
+ end
63
+ end
64
+ def newenv *args
65
+ DcUseEnv.new(*args)
66
+ end
67
+ def message *args
68
+ printf(*args)
69
+ end
70
+ parser = DcUse.create_decorated_parser
71
+ r, = parser.parse(STDIN)
@@ -0,0 +1 @@
1
+ dcl a; dcl b; use a; use b;
@@ -0,0 +1,44 @@
1
+ const m=7, n=85
2
+ var x,y,z,q,r ;
3
+
4
+ procedure multiply ;
5
+ var a,b
6
+ begin a := u ; b := y ; z := 0
7
+ while b > 0 do
8
+ begin
9
+ if odd b do z := z + a ;
10
+ a := 2a ; b := b/2 ;
11
+ end
12
+ end ;
13
+
14
+ procedure divide
15
+ var w ;
16
+ const two = 2, three := 3;
17
+ begin r = x ; q := 0 ; w := y ;
18
+ while w <= r do w := two*w ;
19
+ while w > y
20
+ begin q := ( 2*q ; w := w/2 ) ;
21
+ if w <= r then
22
+ begin r:= r-w q := q + 1
23
+ end
24
+ end
25
+ end ;
26
+
27
+ procedure gcd ;
28
+ var f,g ;
29
+ begin f := x ; g := y
30
+ while f <> g do
31
+ begin if f < g then g := g-f ;
32
+ if g < f then f := f-g ;
33
+ z := f
34
+ end ;
35
+
36
+ begin
37
+ x := m ; y := n ; call multiply ;
38
+ x := 25 ; y := 3 ; call divide ;
39
+ x := 84 ; y := 36 ; call gcd ;
40
+ call x ; x:= gcd ; gcd = x
41
+ end
42
+
43
+
44
+
@@ -0,0 +1,19 @@
1
+ const m=7,n=85;
2
+ var x,y,z;
3
+
4
+ procedure multiply;
5
+ var a,b;
6
+ begin
7
+ a:=u; b=y; z:=0;
8
+ while b>0 do
9
+ begin
10
+ if odd b then z:=z+a;
11
+ a:=2*a; b:=b/2;
12
+ end
13
+ end;
14
+
15
+ begin
16
+ x:=m; y:=n; call multiply;
17
+ call x;
18
+ x:=multiply; multiply:=x
19
+ end.
@@ -0,0 +1,20 @@
1
+ const m=7,n=85,m=7;
2
+ var x,y,z,x;
3
+
4
+ procedure multiply;
5
+ var a,b;
6
+ begin
7
+ a:=x; b:=y; z:=0;
8
+ while b>0 do
9
+ begin
10
+ if odd b then z:=z+a;
11
+ a:=2048*a; b:=b/2;
12
+ end
13
+ end;
14
+ procedure multiply;
15
+
16
+ begin
17
+ x:=m; y:=n; call multiply;
18
+ call x;
19
+ x:=multiply; multiply:=x
20
+ end.
@@ -0,0 +1,18 @@
1
+ const m=7,n=85;
2
+ var x,y,z;
3
+
4
+ procedure multiply;
5
+ var a,b;
6
+ begin
7
+ a:=x; b:=y; z:=0;
8
+ while b>0 do
9
+ begin
10
+ if odd b then z:=z+a;
11
+ a:=2*a; b:=b/2;
12
+ end
13
+ end;
14
+
15
+ begin
16
+ @x := 0 ; x:=m; y:=n; call multiply;
17
+ !write(x); writeln(y); writeln(z)
18
+ end.
@@ -0,0 +1,40 @@
1
+ const m=7, n=85 ;
2
+ var x,y,z,q,r ;
3
+
4
+ procedure multiply ;
5
+ var a,b ;
6
+ begin a := x ; b := y ; z := 0 ;
7
+ while b > 0 do
8
+ begin
9
+ if odd b then z := z + a ;
10
+ a := 2*a ; b := b/2 ;
11
+ end
12
+ end ;
13
+
14
+ procedure divide ;
15
+ var w ;
16
+ begin r := x ; q := 0 ; w := y ;
17
+ while w <= r do w := 2*w ;
18
+ while w > y do
19
+ begin q := 2*q ; w := w/2 ;
20
+ if w <= r then
21
+ begin r:= r-w ; q := q + 1
22
+ end
23
+ end
24
+ end ;
25
+
26
+ procedure gcd ;
27
+ var f,g ;
28
+ begin f := x ; g := y ;
29
+ while f <> g do
30
+ begin if f < g then g := g-f ;
31
+ if g < f then f := f-g ;
32
+ end ;
33
+ z := f
34
+ end ;
35
+
36
+ begin
37
+ x := m ; y := n ; call multiply ;
38
+ x := 25 ; y := 3 ; call divide ;
39
+ x := 84 ; y := 36 ; call gcd ;
40
+ end .
@@ -0,0 +1,43 @@
1
+ const m=7, n=85 ;
2
+ var x,y,z,q,r ;
3
+
4
+ procedure multiply ;
5
+ var a,b ;
6
+ begin a := x ; b := y ; z := 0 ;
7
+ while b > 0 do
8
+ begin
9
+ if odd b then z := z + a ;
10
+ a := 2*a ; b := b/2 ;
11
+ end
12
+ end ;
13
+
14
+ procedure divide ;
15
+ var w ;
16
+ begin r := x ; q := 0 ; w := y ;
17
+ while w <= r do w := 2*w ;
18
+ while w > y do
19
+ begin q := 2*q ; w := w/2 ;
20
+ if w <= r then
21
+ begin r:= r-w ; q := q + 1
22
+ end
23
+ end
24
+ end ;
25
+
26
+ procedure gcd ;
27
+ var f,g ;
28
+ begin f := x ; g := y ;
29
+ while f <> g do
30
+ begin if f < g then g := g-f ;
31
+ if g < f then f := f-g ;
32
+ end ;
33
+ z := f
34
+ end ;
35
+
36
+ begin
37
+ x := m ; y := n ; call multiply ;
38
+ write(x) ; write(y) ; write(z) ; writeln ;
39
+ x := 25 ; y := 3 ; call divide ;
40
+ write(x) ; write(y) ; write(q) ; write(r) ; writeln ;
41
+ x := 84 ; y := 36 ; call gcd ;
42
+ write(x) ; write(y) ; write(z) ; writeln
43
+ end .
@@ -0,0 +1,22 @@
1
+ var x,y,z;
2
+
3
+ procedure multiply;
4
+ var a,b;
5
+ begin
6
+ a:=x; b:=y; z:=0;
7
+ while b>0 do
8
+ begin
9
+ if odd b then z:=z+a;
10
+ a:=2*a; b:=b/2;
11
+ end
12
+ end;
13
+
14
+ begin
15
+ read(x);
16
+ while x<>0 do
17
+ begin
18
+ read(y); call multiply;
19
+ write( x ); write( y ); writeln( z );
20
+ read(x)
21
+ end
22
+ end.
@@ -0,0 +1,18 @@
1
+ const m=7,n=85;
2
+ var x,y,z;
3
+
4
+ procedure multiply;
5
+ var a,b;
6
+ begin
7
+ a:=x; b:=y; z:=0;
8
+ while b>0 do
9
+ begin
10
+ if odd b then z:=z+a;
11
+ a:=2*a; b:=b/2;
12
+ end
13
+ end;
14
+
15
+ begin
16
+ x:=m; y:=n; call multiply;
17
+ write(x); writeln(y); writeln(z)
18
+ end.
@@ -0,0 +1,17 @@
1
+ const m=7,n=85;
2
+ var x,y,z,q,r;
3
+
4
+ procedure multiply;
5
+ var a,b;
6
+ begin
7
+ a:=x; b:=y; z:=0;
8
+ while b>0 do
9
+ begin
10
+ if odd b then z:=z+a;
11
+ a:=2*a; b:=b/2;
12
+ end
13
+ end;
14
+
15
+ begin
16
+ x:=m; y:=n; call multiply;
17
+ end.