📄 parse.c
字号:
/** * @file parse.c * @brief 语法分析器的实现 * @author Shiquan Ye, yeshiquan@gmail.com * @date 2008-11-13 *//* * Copyright (C) 2008 - Shiquan Ye, yeshiquan@gmail.com * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */#include "globals.h"#include "util.h"#include "scan.h"#include "parse.h"static TokenType token; /* 保存当前的token */TreeNode * var_dec();TreeNode * program(); /** * @brief 打印语法错误的提示信息 * * @param message */static void syntaxError(char * message) { fprintf(listing,"\n>>>"); fprintf(listing,"Syntax error at line %d: %s",lineno,message); Error = TRUE;}/** * @brief 检查当前token是否是期望的token,如果是会获取下一个token * * @param expected */static void match(TokenType expected) { if (token == expected) token = getToken(); else { syntaxError("unexpected token -->"); printToken(token,tokenString); fprintf(listing," "); }}/** * @brief 打印token的类型和对应的string * * @param token * @param tokenString */void printToken(TokenType token, const char * tokenString) { printf("%s\n",tokenString);}/** * @brief 语法分析函数 * * @returns 新生成的语法树 */TreeNode * parse(void) { TreeNode * t; token = getToken(); t = program(); if (token != ENDFILE) { syntaxError("Code ends before file\n"); } return t;}TreeNode * program() { TreeNode * t; t = var_dec(); return t;}TreeNode * var_dec() { TreeNode * t = newStmtNode(VarDecK); if (token == INT) { t->child[0] = newOneTokenNode(IntK); t->child[0]->attr.type = INT; } else if (token == REAL) { t->child[0] = newOneTokenNode(RealK); t->child[0]->attr.type = REAL; } else { syntaxError("unexpected token -> "); printToken(token,tokenString); } token = getToken(); match(ID); if (token = LBRACKET) token = getToken(); match(RBRACKET); match(ASSIGN); t->child[2] = var_init();}TreeNode * var_id() { TreeNode * t = newVarIdNode();}TreeNode * var_init() {}TreeNode * array_init() {}TreeNode * exp() {}TreeNode * item() {}TreeNode * factor() {}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -