156 lines
2.8 KiB
Text
156 lines
2.8 KiB
Text
%type SYMBOL {std::string *} str
|
|
%type SYMBOL_UC {std::string *} str
|
|
%type SYMBOL_LC {std::string *} str
|
|
%type BLOCK {std::string *} str
|
|
%type STRING {std::string *} str
|
|
%type CHAR {char} c
|
|
|
|
%type rule {solar::rule_t *}
|
|
%type rhs {std::pair<std::vector<solar::symbol_t>, std::vector<std::string>> *}
|
|
%type action {std::string *}
|
|
%type symbol {solar::symbol_t *}
|
|
%type term {solar::symbol_t *}
|
|
%type varname {std::string *}
|
|
|
|
|
|
%header {
|
|
#include "grammar.hpp"
|
|
}
|
|
|
|
%extra_arg {__attribute__((unused)) solar::grammar_t *} grammar
|
|
|
|
|
|
grammar |=;
|
|
grammar |= grammar directive;
|
|
|
|
directive |= rule(rule) {
|
|
if (grammar->rules.empty()) {
|
|
solar::item_t init("");
|
|
init.get_rhs().push_back(solar::symbol_t::make_nonterm(rule->item.get_lhs().c_str()));
|
|
grammar->rules.emplace_back(solar::rule_t {std::move(init), std::vector<std::string>(), std::string()});
|
|
}
|
|
|
|
grammar->rules.push_back(*rule);
|
|
delete rule;
|
|
}
|
|
|
|
directive |= "%type" SYMBOL_LC(nonterm) BLOCK(type) {
|
|
grammar->nonterm_types.insert(std::make_pair(*nonterm, *type));
|
|
|
|
delete nonterm;
|
|
delete type;
|
|
}
|
|
|
|
directive |= "%type" term(term) BLOCK(type) varname(name) {
|
|
grammar->term_types.insert(std::make_pair(*term, std::make_pair(*type, *name)));
|
|
|
|
delete term;
|
|
delete type;
|
|
delete name;
|
|
}
|
|
|
|
directive |= "%source" BLOCK(block) {
|
|
grammar->source_block = *block;
|
|
delete block;
|
|
}
|
|
|
|
directive |= "%header" BLOCK(block) {
|
|
grammar->header_block = *block;
|
|
delete block;
|
|
}
|
|
|
|
directive |= "%extra_arg" BLOCK(type) varname(name) {
|
|
grammar->extra_args.push_back(std::make_pair(*type, *name));
|
|
|
|
delete type;
|
|
delete name;
|
|
}
|
|
|
|
|
|
rule |= SYMBOL_LC(lhs) "|=" rhs(rhs) action(action) {
|
|
auto *ret = new solar::rule_t {solar::item_t(*lhs, rhs->first), rhs->second, *action};
|
|
|
|
delete lhs;
|
|
delete rhs;
|
|
delete action;
|
|
|
|
return ret;
|
|
}
|
|
|
|
|
|
rhs |= {
|
|
return new std::pair<std::vector<solar::symbol_t>, std::vector<std::string>>();
|
|
}
|
|
|
|
rhs |= rhs(rhs) symbol(sym) {
|
|
rhs->first.push_back(*sym);
|
|
rhs->second.emplace_back();
|
|
delete sym;
|
|
|
|
return rhs;
|
|
}
|
|
|
|
rhs |= rhs(rhs) symbol(sym) '(' varname(var) ')' {
|
|
rhs->first.push_back(*sym);
|
|
rhs->second.push_back(*var);
|
|
|
|
delete sym;
|
|
delete var;
|
|
|
|
return rhs;
|
|
}
|
|
|
|
rhs |= rhs(rhs) STRING(str) {
|
|
for (char c : *str) {
|
|
rhs->first.push_back(solar::symbol_t::make_char(c));
|
|
rhs->second.emplace_back();
|
|
}
|
|
|
|
delete str;
|
|
|
|
return rhs;
|
|
}
|
|
|
|
|
|
action |= ';' {
|
|
return new std::string;
|
|
}
|
|
|
|
action |= BLOCK(v) {
|
|
return v;
|
|
}
|
|
|
|
|
|
symbol |= term(v) {
|
|
return v;
|
|
}
|
|
|
|
symbol |= SYMBOL_LC(v) {
|
|
solar::symbol_t *ret = new solar::symbol_t(solar::symbol_t::make_nonterm(*v));
|
|
delete v;
|
|
return ret;
|
|
}
|
|
|
|
|
|
term |= SYMBOL_UC(v) {
|
|
solar::symbol_t *ret = new solar::symbol_t(solar::symbol_t::make_term(*v));
|
|
delete v;
|
|
return ret;
|
|
}
|
|
|
|
term |= CHAR(v) {
|
|
return new solar::symbol_t(solar::symbol_t::make_char(v));
|
|
}
|
|
|
|
|
|
varname |= SYMBOL_LC(v) {
|
|
return v;
|
|
}
|
|
|
|
varname |= SYMBOL_UC(v) {
|
|
return v;
|
|
}
|
|
|
|
varname |= SYMBOL(v) {
|
|
return v;
|
|
}
|