summaryrefslogtreecommitdiff
path: root/tests/linear
blob: bf21f418a9eea3f67813237eaf3b954dd2b014b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#! /usr/bin/env ruby

# Build a grammar whose LALR(1) parser has a given number of states.
# Useful to test edge cases (e.g., 256 and 257 states, etc.).

# Copyright (C) 2020-2022 Free Software Foundation, Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
# Written by Akim Demaille.

class Linear
  def initialize(states)
    @states = states - 4
    @cols = Math.sqrt(@states).to_i
    @lines = @states / @cols
    @rest = @states % @cols

    @n = @lines * (@cols - 1) + (@rest == 0 ? 1 : @rest == 1 ? 2 : @rest)
  end

  def nterms
    last = @lines + ([0, 1].include?(@rest) ? 0 : 1)
    (0...last).map { |i| "t#{i}" }.join(' ')
  end

  def rules
    res = (0...@lines).map { |i| "t#{i}:#{' N' * (@cols - 1)}" }.join("\n")
    case @rest
    when 0
      res += ' N'
    when 1
      res += ' N N'
    else
      res += "\nt#{@lines}:#{" N" * @rest}"
    end
    res
  end

  def to_s
    puts <<~EOF
      // states: #{@states}
      // cols: #{@cols}
      // lines: #{@lines}
      // rest: #{@rest}
      // n: #{@n}

      %code {
        #include <stdio.h>
        #include <stdlib.h>

        static int yylex (void);
        static void yyerror (const char *msg);
       }

      %debug
      %define api.value.type union
      %define parse.lac full
      %define parse.error verbose
      %printer { fprintf (yyo, "%ld", $$); } <long>
      %token <long> N

      %%

      exp: #{nterms}

      #{rules}

      %%

      static
      int yylex (void)
      {
        static long count = 0;
        if (count++ < #{@n})
          {
            yylval.N = count;
            return N;
          }
        else
          return 0;
      }

      static
      void yyerror (const char *msg)
      {
        fprintf (stderr, "%s\\n", msg);
      }

      int
      main (void)
      {
        yydebug = !!getenv ("YYDEBUG");
        return yyparse ();
      }
      EOF
  end
end

puts Linear.new(ARGV[0].to_i).to_s