summaryrefslogtreecommitdiffstats
path: root/src/output_lr0.cpp
blob: b492744e92e3d6b678a9cc6ac1fb798d0e124d0b (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
  Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#include "output_lr0.hpp"


namespace solar {

void output_lr0_t::emit_state_shift(unsigned state) {
	std::fprintf(source_file, "\t\t\tswitch (token) {\n");

	if (generator->get_shifts().find(std::make_pair(state, symbol_t::make_nonterm(""))) != generator->get_shifts().end()) {
		std::fprintf(source_file, "\t\t\tcase 0:\n");
		std::fprintf(source_file, "\t\t\t\treturn 0;\n\n");
	}

	for (const auto &token : generator->get_terminals()) {
		auto it = generator->get_shifts().find(std::make_pair(state, token));
		if (it == generator->get_shifts().end())
			continue;

		std::fprintf(source_file, "\t\t\tcase %s:\n", symbol_case(token).c_str());
		std::fprintf(source_file, "\t\t\t\tparser->stack[++parser->top].state = %u;\n", unsigned(it->second));
		std::fprintf(source_file, "\t\t\t\treturn 1;\n\n");
	}

	std::fprintf(source_file, "\t\t\tdefault:\n");
	std::fprintf(source_file, "\t\t\t\treturn -1;\n");

	std::fprintf(source_file, "\t\t\t}\n");
}

void output_lr0_t::emit_state_reduce(const item_t &item, int rule_id) {
	const auto &rhs = item.get_rhs();
	if (rhs.size())
		std::fprintf(source_file, "\t\t\tparser->top -= %u;\n", unsigned(rhs.size()));

	if (rule_id >= 0) {
		const std::string &type = generator->get_grammar().get_nonterm_type(item.get_lhs());

		std::fprintf(source_file, "\t\t\t");
		if (!type.empty())
			std::fprintf(source_file, "result.symbol_%s = ", item.get_lhs().c_str());
		std::fprintf(source_file, "%sreduce_%i(", prefix(), rule_id);

		bool empty = true;
		const auto &vars = generator->get_grammar().rules[rule_id].variables;
		for (unsigned i = 0; i < vars.size(); i++) {
			if (vars[i].first.empty())
				continue;

			if (!empty)
				std::fprintf(source_file, ", ");

			std::fprintf(source_file, "parser->stack[parser->top + %u].value.%s", i, symbol_values[rhs[i]].c_str());
			empty = false;
		}

		for (const auto &arg : generator->get_grammar().extra_args) {
			if (!empty)
				std::fprintf(source_file, ", ");

			std::fprintf(source_file, "%s", arg.second.c_str());

			empty = false;
		}

		std::fprintf(source_file, ");\n");

		for (unsigned i = 0; i < vars.size(); i++) {
			if (!vars[i].second)
				continue;

			auto it = generator->get_grammar().destructors.find(rhs[i]);
			if (it == generator->get_grammar().destructors.end())
				continue;

			std::fprintf(source_file, "\t\t\t%s(parser->stack[parser->top + %u].value.%s);\n", it->second.c_str(), i, symbol_values[rhs[i]].c_str());
		}

		if (!type.empty())
			std::fprintf(source_file, "\t\t\tparser->stack[parser->top].value.symbol_%s = result.symbol_%s;\n", item.get_lhs().c_str(), item.get_lhs().c_str());
	}

	emit_gotos(item.get_lhs());
}

void output_lr0_t::emit_state(unsigned state) {
	std::fprintf(source_file, "\t\tcase %u:\n", state);

	auto it = generator->get_reductions().find(state);
	if (it == generator->get_reductions().end()) {
		emit_state_shift(state);
	}
	else {
		const rule_t &rule = generator->get_grammar().rules[it->second];
		emit_state_reduce(rule.item, rule.action.empty() ? -1 : it->second);
	}

	std::fprintf(source_file, "\t\t\tbreak;\n\n");
}

}