?? parse.c
字號:
/********************************************************
* Porject Name:
* Little C Interpretes
* Version & Revision:
* 1.0.1
* Creation Date:
* 2005-02-02
* Author:
* Herbet Schildt & Yock Yu
* Description:
* Recursive descent parser for integer expressions
* which may include variable and function calls.
********************************************************/
#include "setjmp.h"
#include "math.h"
#include "ctype.h"
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
#define NUM_FUNC 100
#define NUM_GLOBAL_VARS 100
#define NUM_LOCAL_VARS 200
#define ID_LEN 31
#define FUNC_CALLS 31
#define PROG_SIZE 10000
#define FOR_NEST 31
enum tok_types{DELIMITER, IDENTIFIER, NUMBER, KEYWORD, TEMP, STRING, BLOCK};
enum tokens{ARG, CHAR, INT, IF, ELSE, FOR, DO, WHILE,
SWITCH, CASE, BREAK, CONTINUE, /*Rev 1.05 yock.yu 05-02-18*/
SIZEOF,/*Rev1.06 yock.yu 05-03-01*/
RETURN, EOL, FINISHED, END};
enum double_ops{LT = 1, LE, GT, GE, EQ, NE};
/*Rev 1.02 yock.yu 05-02-09*/
enum self_ops{AD = 0x11, DE, AQ, DQ, MQ, VQ, OQ, RQ};
/* ++, --, +=, -=, *=, /=, %=, ^=*/
/*
These are the constants used to all sntx_err()
when a syntax error occurs. Add more if you like.
NOTE:SYNTAX is generic error message used when
noting else seems appropriate.
*/
enum error_msg{SYNTAX, UNBAL_PARENS, NO_EXP, EQUALS_EXPECTED, NOT_VAR, PARAM_ERR,
SEMI_EXPECTED, UNBAL_BRACES, FUNC_UNDEF, TYPE_EXPECTED, NEST_FUNC,
RET_NOCALL, PAREN_EXPECTED, WHILE_EXPECTED, QUOTE_EXPECTED,
NO_TEMP, TOO_MANY_LVARS, DIV_BY_ZERO};
extern char *prog; /* current location in source code */
extern char *p_buf; /* points to start of program buffer */
extern jmp_buf e_buf; /* hold enviorment for longjmp() */
/*
An array of these structures will hold
the info associated with global variables.
*/
extern struct var_type{
char var_name[32];
int v_type;
int value;
}global_vars[NUM_GLOBAL_VARS];
/*
This is the function call stack
*/
extern struct func_type{
char func_name[32];
int ret_type;
char *loc; /* location of function entry point in file */
}func_stack[NUM_FUNC];
/*
Keyword table
*/
extern struct commands{
char command[20];
char tok;
}table[];
/*
Sizeof variable
*/
/*Rev1.06 yock.yu 05-03-01 start*/
extern struct size_of_var{
int v_type;
int size;
}size_var[];
/*Rev1.06 yock.yu 05-03-01 end*/
/*
"Standard library" functions are declared here so
they can be put into the internal function table
that follows.
*/
int call_getche(void), call_putch(void);
int call_puts(void), print(void), getnum(void);
struct intern_func_type{
char *f_name; /* function name */
int (*p)(); /* pointer to the function */
}intern_func[]={
"getche", call_getche,
"putch", call_putch,
"puts", call_puts,
"print", print,
"getnum", getnum,
"", 0 /* null terminate the list */
};
extern char token[80]; /* string representation of token */
extern char token_type; /* contains type of token */
extern char tok; /* internal representation of token */
extern int ret_value; /* function return value */
void eval_exp(int *value);
void eval_exp0(int *value), eval_exp1(int *value);
void eval_exp2(int *value), eval_exp3(int *value);
void eval_exp4(int *value), eval_exp5(int *value);
void atom(int *value);
void sntx_err(int error), putback(void);
void assign_var(char *var_name, int value);
int isdelim(char c), look_up(char *s), iswhite(char c);
int find_var(char *s), find_type(char *s), get_token(void);
int internal_func(char *s);
extern int is_var(char *s);
extern char *find_func(char *name);
extern void call(void);
int is_alpha(char c);
/* Entry point into parser */
void eval_exp(int *value)
{
get_token();
if(!*token){
sntx_err(NO_EXP);
return;
}
if(*token == ';'){
*value = 0; /* empty expression */
return;
}
eval_exp0(value);
putback();/* return last token read to input stream */
}
/* Process an assignment expression */
void eval_exp0(int *value)
{
char temp[ID_LEN]; /* holds name of var receiving the assignment*/
register int temp_tok;
if(token_type == IDENTIFIER){
if(is_var(token)){ /* if a var, see if assignment */
strcpy(temp, token);
temp_tok = token_type;
get_token();
if(*token == '='){ /* is an assignment */
get_token();
eval_exp0(value); /* get value to assignment */
assign_var(temp, *value); /* assign the value */
return;
}
else{ /* not an assignment */
putback(); /* restore original token */
strcpy(token, temp);
token_type = temp_tok;
}
}
}
eval_exp1(value);
}
/* Process relatonal operators. */
void eval_exp1(int *value)
{
int partial_value;
int t;/*Rev 1.03 yock.yu 05-02-12*/
char temp[ID_LEN];/*Rev 1.03 yock.yu 05-02-12*/
register char op;
char relops[7]={LT, LE, GT, GE, EQ, NE, 0};
char dualops[7]={AQ, DQ, MQ, VQ, OQ, RQ, 0};/* Rev 1.03 yock.yu 05-02-12*/
strcpy(temp, token);
eval_exp2(value);
op = *token;
/* Rev 1.03 yock.yu 05-02-12 start*/
if(strchr(dualops, op)){
get_token();
eval_exp2(&partial_value);
switch(op){
case AQ:
*value = *value + partial_value;
break;
case DQ:
*value = *value - partial_value;
break;
case MQ:
*value = *value * partial_value;
break;
case VQ:
if(partial_value == 0)
sntx_err(DIV_BY_ZERO);
*value = *value / partial_value;
break;
case OQ:
break;
case RQ:
t = (*value) / partial_value;
*value = *value - (t * partial_value);
break;
}
assign_var(temp, *value);
return;
}
/* Rev 1.03 yock.yu 05-02-12 end*/
if(strchr(relops, op)){
get_token();
eval_exp2(&partial_value);
switch(op){ /* perform the relation operation */
case LT:
*value = *value < partial_value;
break;
case LE:
*value = *value <= partial_value;
break;
case GT:
*value = *value > partial_value;
break;
case GE:
*value = *value >= partial_value;
break;
case EQ:
*value = *value == partial_value;
break;
case NE:
*value = *value != partial_value;
break;
}
}
}
/* Add or subtract two terms. */
void eval_exp2(int *value)
{
register char op;
int partial_value;
eval_exp3(value);
while((op = *token) == '+' || op == '-'){
get_token();
eval_exp3(&partial_value);
switch(op){ /* add or subtract */
case '+':
*value = *value + partial_value;
break;
case '-':
*value = *value - partial_value;
break;
}
}
}
/* Multiply or divide two factors. */
void eval_exp3(int *value)
{
register char op;
int partial_value, t;
eval_exp4(value);
while((op = *token) == '*' || op == '/' || op == '%'){
get_token();
eval_exp0(&partial_value);
switch(op){ /* mul, div, or modulus */
case '*':
*value = *value * partial_value;
break;
case '/':
if(partial_value == 0)
sntx_err(DIV_BY_ZERO);
*value = *value / partial_value;
break;
case '%':
t = (*value) / partial_value;
*value = *value - (t * partial_value);
break;
}
}
}
/* Is a unary + or -. */
void eval_exp4(int *value)
{
register char op;
char tmp;/*Rev1.02 yock.yu 05-02-11*/
op = '\0';
if(*token == '+' || *token == '-'){
op = *token;
get_token();
}
/*Rev1.02 yock.yu 05-02-11 start*/
if(*token == AD || *token == DE){
tmp = *token;
get_token();
eval_exp5(value);
if(tmp == AD) ++*value;
else if(tmp == DE) --*value;
}
else
/*Rev1.02 yock.yu 05-02-11 end*/
eval_exp5(value);
if(op)
if(op == '-')
*value = -(*value);
}
/* Process parenthesized expression. */
void eval_exp5(int *value)
{
char temp[ID_LEN];/*Rev 1.02 yock.yu 05-02-11*/
if(*token == '('){
get_token();
eval_exp0(value); /* get subexpressoin */
if(*token != ')')
sntx_err(PAREN_EXPECTED);
get_token();
}
else{
strcpy(temp, token);/*Rev 1.02 yock.yu 05-02-11*/
atom(value);
/*Rev 1.02 yock.yu 05-02-11 start*/
if(*token == AD || *token == DE){
if(*token == AD) ++(*value);
else if(*token == DE) --(*value);
assign_var(temp, *value);
get_token();
}
/*Rev 1.02 yock.yu 05-02-11 end*/
}
}
/* Find value of number, variable, or function. */
void atom(int *value)
{
int i;
switch(token_type){
case IDENTIFIER:
i = internal_func(token);
if(i != -1){ /* call "standard library" function */
*value = (*intern_func[i].p)();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -