📄 parser.c
字号:
/* Z80Sim - A simulator/debugger for the Zilog Z80 processor Copyright (C) 2003 Lorenzo J. Lucchini 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 "parser.h"#include "types.h"#include "sizes.h"#include "symbols.h"#include "tokens.h"#include <stdlib.h>#include <string.h>#include <ctype.h>#include <assert.h>#define MAX_OPERATORS 256#define MAX_VALUES 256#define MAX_TOKEN 64// A terminal is simply the internal representation of a token; thus it can be an operator// or a value. TERM_NONE is used to return error condition, while TERM_EMPTY indicates that// a new token should be parsed and a new terminal representation generated.typedef struct { enum { TERM_NONE, TERM_EMPTY, TERM_OPERATOR, TERM_VALUE } Type; union { operator Operator; value Value; } Contents;} terminal;// Actions are chosen depending on the last fetched operator and the operator on top of the// stack. Actions for every combination are defined in OperandsTable. ACT_NONE is an error.typedef enum { ACT_NONE, ACT_SHIFT, ACT_REDUCE, ACT_ACCEPT} action;// When the action to perform on an operator is ACT_SHIFT, the representation of an// operator is pushed on top of OperatorsStack. The operator on top of it is popped out// when an ACT_REDUCE is performed.static struct { operator Value[MAX_OPERATORS]; int Top;} OperatorsStack;// As soon as it has been estabilished that a token is a value, its representation is// pushed on top of ValuesStack. The (two) value(s) on top of ValuesStack are replaced// by a result when an ACT_REDUCE is performed.static struct { value Value[MAX_VALUES]; int Top;} ValuesStack;// Operations defined on the two stacks// Initialize an empty stackstatic void InitOperatorsStack(void);static void InitValuesStack(void);// Push an operator on the stackstatic void PushOperatorsStack(operator);static void PushValuesStack(value);// Pop an operator from the stackstatic operator PopOperatorsStack(void);static value PopValuesStack(void);// Return the operator on top of the stackstatic operator PeekOperatorsStack(void);static value PeekValuesStack(void);// Operations on expressions// Reduce the operation on top of the stacks to its representationstatic operation* Reduce(void);// Parse the first operator or value found in *Expression, and strip it out from *Expressionstatic terminal ParseToken(const char** Expression);// When we encounter an operator (Input) during parsing, we can decide to// 1) shift it into the operators stack, and leave it there for the moment// 2) reduce the (two) value(s) we have on top of the values stack to their result// 3) accept the value on top of the values stack (hopefully the only value there) as our result// 4) leave both stacks alone and think hard about how to tell the user that she's done a mistake// We take this decision based on what operator (Stack) is on top of the operator stack.#define N ACT_NONE#define S ACT_SHIFT#define R ACT_REDUCE#define A ACT_ACCEPTstatic action OperatorsTable[OperatorCount][OperatorCount]={/////////////////////////////////////////////////////////////////////////////////// ?
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -