From: Alexei Sheplyakov Date: Sun, 14 Sep 2008 02:24:29 +0000 (+0400) Subject: Use the new parser in the ex(const string&, lst&) ctor. X-Git-Tag: release_1-5-0~59^2~4 X-Git-Url: https://www.ginac.de/ginac.git//ginac.git?p=ginac.git;a=commitdiff_plain;h=95bec2b8af126412b2e27b51e6bfd8d9ab18d5d7 Use the new parser in the ex(const string&, lst&) ctor. Note: this is certainly not the optimal way to use the parser. It's provided for backward compatibility only. --- diff --git a/ginac/Makefile.am b/ginac/Makefile.am index 17b272f2..2614c457 100644 --- a/ginac/Makefile.am +++ b/ginac/Makefile.am @@ -16,6 +16,7 @@ libginac_la_SOURCES = add.cpp archive.cpp basic.cpp clifford.cpp color.cpp \ parser/builtin_fcns.cpp \ parser/lexer.cpp \ parser/lexer.hpp \ + parser/parser_compat.cpp \ parser/debug.hpp libginac_la_LDFLAGS = -version-info $(LT_VERSION_INFO) -release $(LT_RELEASE) diff --git a/ginac/ex.cpp b/ginac/ex.cpp index 377b15b4..7becb3e1 100644 --- a/ginac/ex.cpp +++ b/ginac/ex.cpp @@ -553,17 +553,6 @@ basic & ex::construct_from_double(double d) return *bp; } -ptr ex::construct_from_string_and_lst(const std::string &s, const ex &l) -{ - set_lexer_string(s); - set_lexer_symbols(l); - ginac_yyrestart(NULL); - if (ginac_yyparse()) - throw (std::runtime_error(get_parser_error())); - else - return parsed_ex.bp; -} - ////////// // static member variables ////////// diff --git a/ginac/parser/parser_compat.cpp b/ginac/parser/parser_compat.cpp new file mode 100644 index 00000000..d0723eed --- /dev/null +++ b/ginac/parser/parser_compat.cpp @@ -0,0 +1,49 @@ +/// @file parser_compat.cpp Parser interface compatible with the old +/// (bison/flex based) parser. +#include "ex.h" +#include "idx.h" +#include "lst.h" +#include "parser.hpp" +#include +#include + +namespace GiNaC +{ +static symtab make_symtab(const ex& l); + +ptr ex::construct_from_string_and_lst(const std::string &s, const ex &l) +{ + static const bool strict = true; + symtab syms = make_symtab(l); + parser reader(syms, strict); + ex parsed_ex = reader(s); + return parsed_ex.bp; +} + +static std::string get_symbol_name(const ex & s); + +static symtab make_symtab(const ex& l) +{ + symtab syms; + if (is_exactly_a(l)) { + for (std::size_t i = 0; i < l.nops(); i++) { + const ex &o = l.op(i); + if (is_a(o) || (is_a(o) && is_a(o.op(0)))) + syms[get_symbol_name(o)] = o; + } + } + return syms; +} + +static std::string get_symbol_name(const ex & s) +{ + if (is_a(s)) + return ex_to(s).get_name(); + else if (is_a(s) && is_a(s.op(0))) + return ex_to(s.op(0)).get_name(); + else + throw (std::invalid_argument("get_symbol_name(): unexpected expression type")); +} + +} +