[GiNaC-devel] [PATCH] parser: handle abbreviations as advertized in the manual.

Alexei Sheplyakov alexei.sheplyakov at gmail.com
Wed Jun 30 15:13:06 CEST 2010


The following example from the tutorial

GiNaC::symbol x, y;
GiNaC::symtab table;
table["x"] = x+log(y)+1;
GiNaC::parser reader(table);
GiNaC::ex e = reader("5*x^3 - x^2");

fails with the following exception:

terminate called after throwing an instance of 'std::invalid_argument'
what(): find_or_insert_symbol: name "x" does not correspond to a symbol

Remove silly checks from find_or_insert_symbol, and fix its return value
(should be ex, not symbol).

Note: theoretically this change breaks the binary compatibility, since
the return value of the function has been changed. However, the function
in question is internal, and should be used only by GiNaC itself. Hence
the patch is OK even for the stable (1.5) branch.

Thanks to Felipe Bordeu for reporting this bug.
---
 ginac/parser/parse_context.cpp |   13 +++----------
 ginac/parser/parse_context.h   |    4 ++--
 2 files changed, 5 insertions(+), 12 deletions(-)

diff --git a/ginac/parser/parse_context.cpp b/ginac/parser/parse_context.cpp
index 7a7867a..2230d61 100644
--- a/ginac/parser/parse_context.cpp
+++ b/ginac/parser/parse_context.cpp
@@ -27,19 +27,12 @@
 
 namespace GiNaC {
 
-symbol
+ex
 find_or_insert_symbol(const std::string& name, symtab& syms, const bool strict)
 {
 	symtab::const_iterator p = syms.find(name);
-	if (p != syms.end()) {
-		if (is_a<symbol>(p->second))
-			return ex_to<symbol>(p->second);
-		else
-			throw std::invalid_argument(
-				std::string("find_or_insert_symbol: name \"")
-				+ name + "\" does not correspond to a symbol");
-	}
-
+	if (p != syms.end())
+		return p->second;
 
 	if (strict)
 		throw std::invalid_argument(
diff --git a/ginac/parser/parse_context.h b/ginac/parser/parse_context.h
index 6e0dff9..3516320 100644
--- a/ginac/parser/parse_context.h
+++ b/ginac/parser/parse_context.h
@@ -41,12 +41,12 @@ namespace GiNaC {
 typedef std::map<std::string, ex> symtab;
 
 /**
- * Find the symbol with the @a name in the symbol table @a syms.
+ * Find the symbol (or abbreviation) with the @a name in the symbol table @a syms.
  *
  * If symbol is missing and @a strict = false, insert it, otherwise
  * throw an exception.
  */
-extern symbol
+extern ex 
 find_or_insert_symbol(const std::string& name, symtab& syms,
 	              const bool strict);
 
-- 
1.7.1



More information about the GiNaC-devel mailing list