Решение систем нелинейных уравнений https://www.mapleprimes.com/users/one%20man/posts?page=1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lexer.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /** @file lexer.cpp
  2. *
  3. * Implementation of GiNaC's lexer. */
  4. /*
  5. * GiNaC Copyright (C) 1999-2014 Johannes Gutenberg University Mainz, Germany
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "lexer.h"
  22. #include "compiler.h"
  23. #include <iostream>
  24. #include <sstream>
  25. #include <string>
  26. #include <cstdio>
  27. namespace GiNaC {
  28. /// Skip to the end of line
  29. static int skipline(std::istream* s);
  30. /// Skip to the next non-whitespace character
  31. static int skipspace(std::istream* s, int c, std::size_t& line);
  32. /// Check if the identifier is predefined literal
  33. static bool literal_p(const std::string& name);
  34. /// gettok - Return the next token from standard input.
  35. int lexer::gettok()
  36. {
  37. // Skip any whitespace.
  38. c = skipspace(input, c, line_num);
  39. // identifier: [a-zA-Z][a-zA-Z0-9_]*
  40. if (isalpha(c)) {
  41. str = c;
  42. do {
  43. c = input->get();
  44. if ( isalnum(c) || c=='_' || c=='[' || c==']')
  45. str += c;
  46. else
  47. break;
  48. } while (true);
  49. if (unlikely(literal_p(str)))
  50. return token_type::literal;
  51. else
  52. return token_type::identifier;
  53. }
  54. // Number: [0-9]+([.][0-9]*(eE[+-][0-9]+)*)*
  55. if (isdigit(c) || c == '.') {
  56. str = "";
  57. do {
  58. str += c;
  59. c = input->get();
  60. } while (isdigit(c) || c == '.');
  61. if (c == 'E' || c == 'e') {
  62. str += 'E';
  63. c = input->get();
  64. if (isdigit(c))
  65. str += '+';
  66. do {
  67. str += c;
  68. c = input->get();
  69. } while (isdigit(c));
  70. }
  71. return token_type::number;
  72. }
  73. // Comment until end of line.
  74. if (c == '#') {
  75. c = skipline(input);
  76. ++line_num;
  77. if (c != EOF)
  78. return gettok();
  79. }
  80. // Check for end of file. Don't eat the EOF.
  81. if (c == EOF)
  82. return token_type::eof;
  83. // Otherwise, just return the character as its ascii value.
  84. int current = c;
  85. c = input->get();
  86. return current;
  87. }
  88. static int skipline(std::istream* s)
  89. {
  90. int c;
  91. do {
  92. c = s->get();
  93. } while (c != EOF && c != '\n' && c != '\r');
  94. return c;
  95. }
  96. static int skipspace(std::istream* s, int c, std::size_t& line)
  97. {
  98. while (isspace(c)) {
  99. if (c == '\n')
  100. ++line;
  101. c = s->get();
  102. }
  103. return c;
  104. }
  105. static bool literal_p(const std::string& name)
  106. {
  107. if (name == "I")
  108. return true;
  109. else if (name == "Pi")
  110. return true;
  111. else if (name == "Euler")
  112. return true;
  113. else if (name == "Catalan")
  114. return true;
  115. else
  116. return false;
  117. }
  118. lexer::lexer(std::istream* in, std::ostream* out, std::ostream* err)
  119. {
  120. if (in)
  121. input = in;
  122. else
  123. in = &std::cin;
  124. if (out)
  125. output = out;
  126. else
  127. output = &std::cout;
  128. if (err)
  129. error = err;
  130. else
  131. error = &std::cerr;
  132. c = ' ';
  133. str = "";
  134. line_num = 0;
  135. column = 0;
  136. }
  137. lexer::~lexer() { }
  138. void lexer::switch_input(std::istream* in)
  139. {
  140. input = in;
  141. line_num = 0;
  142. column = 0;
  143. c = ' ';
  144. }
  145. /// Symbolic name of current token (for error reporting)
  146. std::string lexer::tok2str(const int tok) const
  147. {
  148. switch (tok) {
  149. case lexer::token_type::identifier:
  150. case lexer::token_type::number:
  151. return std::string("\"") + str + "\"";
  152. case lexer::token_type::eof:
  153. return std::string("EOF");
  154. default:
  155. return std::string("\"") + char(tok) + "\"";
  156. }
  157. }
  158. } // namespace GiNaC