Commit 825af57d authored by Akim Demaille's avatar Akim Demaille Committed by Alexandre Duret-Lutz
Browse files

parser: simplifications.

* lib/src/krat_exp/krat_exp_bison.yy: Require Bison 2.5.
Require locations, for upward compatibility with 2.6.
Use a couple typedefs for clarity.
Factor vcsnyy::... with a namespace vcsnyy {...}.
Use code requires/provides for clarity.
parent e621afaa
%require "2.5"
%skeleton "lalr1.cc"
%defines
%define "parser_class_name" "krat_exp_bison"
%locations
//%debug
%error-verbose
%name-prefix="vcsnyy"
%{
%code requires
{
# include <vaucanson/algebra/implementation/series/krat_exp_parser_private.hh>
# include <map>
# include <queue>
......@@ -13,12 +16,28 @@
# include <cassert>
//_DEBUG_
# include <iostream>
%}
}
%code
{
namespace vcsnyy
{
typedef std::pair<krat_exp_bison::token_type,
krat_exp_bison::semantic_type>
token_t;
typedef std::queue<token_t > tokens_t;
}
int
yylex(vcsnyy::krat_exp_bison::semantic_type* yylval,
vcsnyy::krat_exp_bison::location_type* yylloc,
vcsnyy::tokens_t& tok_q);
}
%parse-param { std::queue<std::pair<krat_exp_bison::token_type, krat_exp_bison::semantic_type> >& tok_q }
%parse-param { vcsn::algebra::krat_exp_virtual& exp }
%parse-param { std::string& error_ }
%lex-param { std::queue<std::pair<krat_exp_bison::token_type, krat_exp_bison::semantic_type> >& tok_q }
%lex-param { tokens_t& tok_q }
%printer { debug_stream() << *$$; } <str>
......@@ -29,12 +48,6 @@
std::string * str;
}
%{
int
yylex(vcsnyy::krat_exp_bison::semantic_type* yylval,
std::queue<std::pair<vcsnyy::krat_exp_bison::token_type, vcsnyy::krat_exp_bison::semantic_type> >& tok_q);
%}
%type <rexp> rexp
%token <str> OPAR CPAR
......@@ -78,101 +91,111 @@ namespace vcsnyy
<
krat_exp_bison::token_type,
krat_exp_bison::semantic_type
>
> self;
>
> self;
};
} // vcsnyy
vcsnyy::krat_exp_parser::krat_exp_parser()
{
tok_q_ = new token_queue;
}
krat_exp_parser::krat_exp_parser()
{
tok_q_ = new token_queue;
}
vcsnyy::krat_exp_parser::~krat_exp_parser()
{
delete tok_q_;
}
krat_exp_parser::~krat_exp_parser()
{
delete tok_q_;
}
void
vcsnyy::krat_exp_parser::insert_word(vcsn::algebra::krat_exp_virtual* rexp)
{
krat_exp_bison::semantic_type tmp;
tmp.rexp = rexp;
tok_q_->self.push(std::make_pair(krat_exp_bison::token::WORD, tmp));
}
void
krat_exp_parser::insert_word(vcsn::algebra::krat_exp_virtual* rexp)
{
krat_exp_bison::semantic_type tmp;
tmp.rexp = rexp;
tok_q_->self.push(std::make_pair(krat_exp_bison::token::WORD, tmp));
}
void
vcsnyy::krat_exp_parser::insert_weight(vcsn::algebra::semiring_virtual* sem)
{
krat_exp_bison::semantic_type tmp;
tmp.sem = sem;
tok_q_->self.push(std::make_pair(krat_exp_bison::token::WEIGHT, tmp));
}
void
krat_exp_parser::insert_weight(vcsn::algebra::semiring_virtual* sem)
{
krat_exp_bison::semantic_type tmp;
tmp.sem = sem;
tok_q_->self.push(std::make_pair(krat_exp_bison::token::WEIGHT, tmp));
}
void
vcsnyy::krat_exp_parser::insert_zero(vcsn::algebra::krat_exp_virtual* rexp)
{
krat_exp_bison::semantic_type tmp;
tmp.rexp = rexp;
tok_q_->self.push(std::make_pair(krat_exp_bison::token::ZERO, tmp));
}
void
krat_exp_parser::insert_zero(vcsn::algebra::krat_exp_virtual* rexp)
{
krat_exp_bison::semantic_type tmp;
tmp.rexp = rexp;
tok_q_->self.push(std::make_pair(krat_exp_bison::token::ZERO, tmp));
}
void
vcsnyy::krat_exp_parser::insert_token(int i, std::string* str)
{
krat_exp_bison::semantic_type tmp;
tmp.str = str;
switch (i)
void
krat_exp_parser::insert_token(int i, std::string* str)
{
case 0 :
tok_q_->self.push(std::make_pair(krat_exp_bison::token::OPAR, tmp));
break;
case 1 :
tok_q_->self.push(std::make_pair(krat_exp_bison::token::CPAR, tmp));
break;
case 2 :
tok_q_->self.push(std::make_pair(krat_exp_bison::token::PLUS, tmp));
break;
case 3 :
tok_q_->self.push(std::make_pair(krat_exp_bison::token::TIMES, tmp));
break;
case 4 :
tok_q_->self.push(std::make_pair(krat_exp_bison::token::STAR, tmp));
break;
default : // this step should never append
assert(false);
break;
krat_exp_bison::semantic_type tmp;
tmp.str = str;
switch (i)
{
case 0 :
tok_q_->self.push(std::make_pair(krat_exp_bison::token::OPAR, tmp));
break;
case 1 :
tok_q_->self.push(std::make_pair(krat_exp_bison::token::CPAR, tmp));
break;
case 2 :
tok_q_->self.push(std::make_pair(krat_exp_bison::token::PLUS, tmp));
break;
case 3 :
tok_q_->self.push(std::make_pair(krat_exp_bison::token::TIMES, tmp));
break;
case 4 :
tok_q_->self.push(std::make_pair(krat_exp_bison::token::STAR, tmp));
break;
default : // this step should never append
assert(false);
break;
}
}
}
int
vcsnyy::krat_exp_parser::parse(vcsn::algebra::krat_exp_virtual& rexp_, std::string& error_)
{
vcsnyy::krat_exp_bison parser(tok_q_->self , rexp_, error_);
#if YYDEBUG
parser.set_debug_level(true);
#endif
return parser.parse();
}
int
krat_exp_parser::parse(vcsn::algebra::krat_exp_virtual& rexp_,
std::string& error_)
{
krat_exp_bison parser(tok_q_->self , rexp_, error_);
#if YYDEBUG
parser.set_debug_level(true);
#endif
return parser.parse();
}
void
krat_exp_bison::error(const krat_exp_bison::location_type& loc,
const std::string& s)
{
error_ += s + '\n';
}
} // namespace vcsnyy
int
vcsnyylex(vcsnyy::krat_exp_bison::semantic_type* yylval,
std::queue<std::pair<vcsnyy::krat_exp_bison::token_type, vcsnyy::krat_exp_bison::semantic_type> >& tok_q)
vcsnyy::krat_exp_bison::location_type*,
vcsnyy::tokens_t& tok_q)
{
vcsnyy::krat_exp_bison::token_type res;
if (tok_q.empty())
return 0;
else
{
*yylval = tok_q.front().second;
res = tok_q.front().first;
tok_q.pop();
return res;
}
{
*yylval = tok_q.front().second;
res = tok_q.front().first;
tok_q.pop();
return res;
}
}
void
vcsnyy::krat_exp_bison::error(const vcsnyy::krat_exp_bison::location_type& loc, const std::string& s)
{
error_ += s + '\n';
}
// Local Variables:
// mode: c++
// End:
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment