|
这才是真神
- #include <iostream>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <stdexcept>
- // 定义 Token 类型
- enum TokenType {
- LEFT_PAREN,
- RIGHT_PAREN,
- NUMBER,
- SYMBOL
- };
- // 定义 Token 结构体
- struct Token {
- TokenType type;
- std::string value;
- };
- // 判断字符串是否全为数字的辅助函数
- bool isAllDigits(const std::string& str) {
- for (std::string::size_type i = 0; i < str.length(); ++i) {
- if (!isdigit(str[i])) {
- return false;
- }
- }
- return true;
- }
- // 词法分析函数,添加调试输出
- std::vector<Token> tokenize(const std::string& input) {
- std::vector<Token> tokens;
- std::string tokenStr;
- std::cout << "Tokenizing input: " << input << std::endl;
- for (size_t i = 0; i < input.length(); ++i) {
- char c = input[i];
- if (c == ' ') {
- if (!tokenStr.empty()) {
- Token token;
- if (isAllDigits(tokenStr)) {
- token.type = NUMBER;
- token.value = tokenStr;
- } else {
- token.type = SYMBOL;
- token.value = tokenStr;
- }
- tokens.push_back(token);
- std::cout << "Found token: type=";
- switch (token.type) {
- case LEFT_PAREN: std::cout << "LEFT_PAREN"; break;
- case RIGHT_PAREN: std::cout << "RIGHT_PAREN"; break;
- case NUMBER: std::cout << "NUMBER"; break;
- case SYMBOL: std::cout << "SYMBOL"; break;
- }
- std::cout << ", value=" << token.value << std::endl;
- tokenStr.clear();
- }
- } else if (c == '(') {
- if (!tokenStr.empty()) {
- Token token;
- if (isAllDigits(tokenStr)) {
- token.type = NUMBER;
- token.value = tokenStr;
- } else {
- token.type = SYMBOL;
- token.value = tokenStr;
- }
- tokens.push_back(token);
- std::cout << "Found token: type=";
- switch (token.type) {
- case LEFT_PAREN: std::cout << "LEFT_PAREN"; break;
- case RIGHT_PAREN: std::cout << "RIGHT_PAREN"; break;
- case NUMBER: std::cout << "NUMBER"; break;
- case SYMBOL: std::cout << "SYMBOL"; break;
- }
- std::cout << ", value=" << token.value << std::endl;
- tokenStr.clear();
- }
- Token token;
- token.type = LEFT_PAREN;
- token.value = "(";
- tokens.push_back(token);
- std::cout << "Found token: type=LEFT_PAREN, value=(" << std::endl;
- } else if (c == ')') {
- if (!tokenStr.empty()) {
- Token token;
- if (isAllDigits(tokenStr)) {
- token.type = NUMBER;
- token.value = tokenStr;
- } else {
- token.type = SYMBOL;
- token.value = tokenStr;
- }
- tokens.push_back(token);
- std::cout << "Found token: type=";
- switch (token.type) {
- case LEFT_PAREN: std::cout << "LEFT_PAREN"; break;
- case RIGHT_PAREN: std::cout << "RIGHT_PAREN"; break;
- case NUMBER: std::cout << "NUMBER"; break;
- case SYMBOL: std::cout << "SYMBOL"; break;
- }
- std::cout << ", value=" << token.value << std::endl;
- tokenStr.clear();
- }
- Token token;
- token.type = RIGHT_PAREN;
- token.value = ")";
- tokens.push_back(token);
- std::cout << "Found token: type=RIGHT_PAREN, value=)" << std::endl;
- } else {
- tokenStr += c;
- }
- }
- if (!tokenStr.empty()) {
- Token token;
- if (isAllDigits(tokenStr)) {
- token.type = NUMBER;
- token.value = tokenStr;
- } else {
- token.type = SYMBOL;
- token.value = tokenStr;
- }
- tokens.push_back(token);
- std::cout << "Found token: type=";
- switch (token.type) {
- case LEFT_PAREN: std::cout << "LEFT_PAREN"; break;
- case RIGHT_PAREN: std::cout << "RIGHT_PAREN"; break;
- case NUMBER: std::cout << "NUMBER"; break;
- case SYMBOL: std::cout << "SYMBOL"; break;
- }
- std::cout << ", value=" << token.value << std::endl;
- }
- return tokens;
- }
- // 定义 AST 节点类型
- struct ASTNode {
- std::string value;
- std::vector<ASTNode> children;
- };
- // 语法分析函数,添加调试输出
- ASTNode parse(const std::vector<Token>& tokens, size_t& pos, int depth = 0) {
- std::string indent(depth * 2, ' ');
- std::cout << indent << "Parsing at position " << pos << std::endl;
- ASTNode node;
- const Token& token = tokens[pos++];
- if (token.type == LEFT_PAREN) {
- std::cout << indent << "Found left parenthesis, starting sub - expression" << std::endl;
- while (tokens[pos].type != RIGHT_PAREN) {
- node.children.push_back(parse(tokens, pos, depth + 1));
- }
- std::cout << indent << "Found right parenthesis, ending sub - expression" << std::endl;
- pos++; // 跳过右括号
- } else if (token.type == NUMBER || token.type == SYMBOL) {
- node.value = token.value;
- std::cout << indent << "Found " << (token.type == NUMBER ? "number" : "symbol") << ": " << node.value << std::endl;
- }
- return node;
- }
- // 将字符串转换为整数的辅助函数
- int stringToInt(const std::string& str) {
- int result = 0;
- std::istringstream iss(str);
- iss >> result;
- return result;
- }
- // 解释执行函数,添加调试输出
- int evaluate(const ASTNode& node, int depth = 0) {
- std::string indent(depth * 2, ' ');
- std::cout << indent << "Evaluating node: ";
- if (node.children.empty()) {
- std::cout << "Leaf node with value " << node.value << std::endl;
- try {
- int val = stringToInt(node.value);
- std::cout << indent << "Value of leaf node: " << val << std::endl;
- return val;
- } catch (...) {
- throw std::runtime_error("Invalid number: " + node.value);
- }
- }
- std::string op = node.children[0].value;
- std::cout << "Operator node with op " << op << std::endl;
- if (op == "+") {
- int result = 0;
- std::cout << indent << "Performing addition" << std::endl;
- for (size_t i = 1; i < node.children.size(); ++i) {
- int childResult = evaluate(node.children[i], depth + 1);
- result += childResult;
- std::cout << indent << "Adding child result " << childResult << ", current sum: " << result << std::endl;
- }
- std::cout << indent << "Final result of addition: " << result << std::endl;
- return result;
- } else if (op == "-") {
- std::cout << indent << "Performing subtraction" << std::endl;
- if (node.children.size() == 2) {
- int childResult = evaluate(node.children[1], depth + 1);
- std::cout << indent << "Result of subtraction: " << childResult << std::endl;
- return childResult;
- } else {
- int result = evaluate(node.children[1], depth + 1);
- std::cout << indent << "Initial value for subtraction: " << result << std::endl;
- for (size_t i = 2; i < node.children.size(); ++i) {
- int childResult = evaluate(node.children[i], depth + 1);
- result -= childResult;
- std::cout << indent << "Subtracting child result " << childResult << ", current result: " << result << std::endl;
- }
- std::cout << indent << "Final result of subtraction: " << result << std::endl;
- return result;
- }
- } else {
- throw std::runtime_error("Unsupported operator: " + op);
- }
- }
- int main() {
- std::string input;
- std::cout << "Enter a LISP expression: ";
- std::getline(std::cin, input);
- try {
- std::vector<Token> tokens = tokenize(input);
- size_t pos = 0;
- std::cout << "Starting parsing..." << std::endl;
- ASTNode ast = parse(tokens, pos);
- std::cout << "Starting evaluation..." << std::endl;
- int result = evaluate(ast);
- std::cout << "Result: " << result << std::endl;
- } catch (const std::exception& e) {
- std::cerr << "Error: " << e.what() << std::endl;
- }
- return 0;
- }
复制代码
代码生成/调试过程
https://www.doubao.com/thread/wcd00bbdbbc68a282 |
评分
-
2
查看全部评分
-
|