⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vtc.c

📁 使用BorlandC++4.5编译的一个MUD客户端程序
💻 C
📖 第 1 页 / 共 4 页
字号:
# line 2 "vtc.y"/* vtc.y: VTC compiler and lexical analyzer */#include "vt.h"String ptext;int debug = 0;extern int iwidth[];#define ERR_PRMTNAME	"Primitive name used as function name"#define ERR_RESERVED	"Reserved word used as identifier"#define ERR_DUPGOTO	"Duplicate goto label"#define ERR_MISBREAK	"Misplaced break statement"#define ERR_MISCONT	"Misplaced continue statement"#define ERR_DUPTAG	"Duplicate parameter or local variable name"#define ERR_UDGOTO	"One or more goto labels not defined"#define ERR_TOOMANY	"Too many parameters or local variables"#define ERR_UNTERMCHAR	"Unterminated character constant"#define ERR_UNTERMSTR	"Unterminated string constant"#define ERR_BADBRACE	"Illegal placement of right brace"#define ERR_ILLFUNC	"Illegal placement of reserved word func"/* The intermediate program */#define INIT_CODE 256static Instr *icode;static int loc = 0, codesize;#define Check_code(n) Check(icode, Instr, codesize, loc + (n))#define Hexdigitvalue(a) (isdigit(a) ? a - '0' : ucase(a) - 'A' + 10)#define Code(t) if (1) { Check_code(1); icode[loc++].type = t; } else#define Code2(t, elem, val) if (1) { Check_code(2); icode[loc++].type = t; \				     icode[loc++].elem = val; } else#define Code_iconst(val) Code2(I_ICONST, iconst, val)#define Code_pcall(pn, ac) if (1) { Check_code(2); \				    icode[loc++].type = I_PCALL; \				    icode[loc].pc.pnum = pn; \				    icode[loc++].pc.argc = ac; } else#define Code_fcall(id, ac) if (1) { Check_code(2); \				    icode[loc++].type = I_FCALL; \				    icode[loc++].argc = ac; \				    icode[loc++].ident = id; } elsetypedef struct label {	char *name;	int offset;	struct label *next;} Label;#define MAXILEN 32static Label *lhead = NULL;#ifdef PROTOTYPESstatic void code_var(char *);static void code_goto(char *, int);static void free_labels(void);static void add_tag(char **, int *, char *);static int find_tag(char **, int, char *);static char *parse_token(char *);static int read_char(char **);static int enter_sconst(Cstr);static char *read_string(char *);static char *read_comment(char *);static void compile(void);static void scan(void);static void cleanup_parse();static void lexerror(char *);static void yyerror(char *);static int yylex(void);#elsestatic void compile(), scan(), cleanup_parse(), lexerror(), yyerror();static void code_var(), free_labels(), add_tag(), code_goto();static char *parse_token(), *read_string(), *read_comment();#endif#define INIT_JMP 64#define INIT_COND 16int *jmptab, jmpsize, jmppos;int *condstack, condsize, condpos;int *loopstack, loopsize, looppos;#define Incjmp(n)	Check(jmptab, int, jmpsize, jmppos += (n))#define Pushcond(x)	Push(condstack, int, condsize, condpos, x)#define Pushloop(x)	Push(loopstack, int, loopsize, looppos, x)#define Peekloop	(loopstack[looppos - 1])#define Incloop(n)	if (1) { Pushloop(jmppos); Incjmp(n); } else#define Decloop		looppos--#define Ljmp(l, t)	Code2(t, offset, Peekloop + (l))#define Ldest(l)	(jmptab[Peekloop + (l)] = loc)#define Break		if (looppos) Code2(I_JMP, offset, Peekloop + 1); \			else yyerror(ERR_MISBREAK)#define Continue	if (looppos) Code2(I_JMP, offset, Peekloop); \			else yyerror(ERR_MISCONT)#define Jmp(t)		if (1) { Pushcond(jmppos); Code2(t, offset, jmppos); \				 Incjmp(1); } else#define Dest		(jmptab[condstack[--condpos]] = loc)#define Destnext	(jmptab[condstack[--condpos]] = loc + 2)#define Return		Code2(I_JMP, offset, 0)/* Local vars and array vars */#define MAX_TAGS 32static char *lvars[MAX_TAGS], *avars[MAX_TAGS];static int lvarc = 0, avarc = 0, reqargs = 0;static int lexline = -1;		/* Line being analyzed	     */static int parseline = -1;		/* Line being parsed	     */static int errflag;			/* Error flag		     */static char *curfunc = "";		/* Name of current function  */static char *curfile = "";		/* Name of current file	     */extern Prmt prmtab[];#define OP_BOR	     0#define OP_BXOR	     1#define OP_BAND	     2#define OP_EQ	     3#define OP_NE	     4#define OP_LT	     5#define OP_LE	     6#define OP_GT	     7#define OP_GE	     8#define OP_SL	     9#define OP_SR	    10#define OP_ADD	    11#define OP_SUB	    12#define OP_MULT	    13#define OP_DIV	    14#define OP_MOD	    15#define OP_POSTINC  16#define OP_POSTDEC  17#define OP_PREINC   18#define OP_PREDEC   19#define OP_NOT	    20#define OP_COMPL    21#define OP_NEG	    22#define OP_ASN	    23#define PR_LOOKUP   102# line 136 "vtc.y"typedef union  {	int num;	int index;	void (*bobj)();	int str;	char *s;} YYSTYPE;#ifdef __cplusplus#  include <stdio.h>#  include <yacc.h>#endif	/* __cplusplus */ # define ICONST 257# define SCONST 258# define BOBJ 259# define IDENT 260# define FUNC 261# define FASSIGN 262# define DO 263# define WHILE 264# define IF 265# define ELSE 266# define FOR 267# define GOTO 268# define BREAK 269# define CONTINUE 270# define RETURN 271# define TA 272# define DA 273# define MA 274# define AA 275# define SA 276# define SLA 277# define SRA 278# define BAA 279# define BXA 280# define BOA 281# define CONDA 282# define DEREF 283# define OR 284# define AND 285# define EQ 286# define NE 287# define LE 288# define GE 289# define SL 290# define SR 291# define INC 292# define DEC 293#define yyclearin yychar = -1#define yyerrok yyerrflag = 0extern int yychar;#ifndef YYMAXDEPTH#define YYMAXDEPTH 150#endif/* __YYSCLASS defines the scoping/storage class for global objects * that are NOT renamed by the -p option.  By default these names * are going to be 'static' so that multi-definition errors * will not occur with multiple parsers. * If you want (unsupported) access to internal names you need * to define this to be null so it implies 'extern' scope. * This should not be used in conjunction with -p. */#ifndef __YYSCLASS# define __YYSCLASS static#endifYYSTYPE yylval;__YYSCLASS YYSTYPE yyval;typedef int yytabelem;# define YYERRCODE 256# line 361 "vtc.y"/* Auxiliary compiler functions */static void code_var(ident)	char *ident;{	int tag;	if ((tag = find_tag(avars, avarc, ident)) != -1)		Code2(I_AVAR, tnum, tag);	else if ((tag = find_tag(lvars, lvarc, ident)) != -1)		Code2(I_LVAR, tnum, tag);	else		Code2(I_GVAR, ident, ident);}static void code_goto(name, to)	char *name;	int to;{	Label *label;	for (label = lhead; label; label = label->next) {		if (streq(label->name, name)) {			if (to)				Code2(I_JMP, offset, label->offset);			else if (jmptab[label->offset] == -1)				jmptab[label->offset] = loc;			else				yyerror(ERR_DUPGOTO);			return;		}	}	label = New(Label);	label->name = name;	label->offset = jmppos;	Push(jmptab, int, jmpsize, jmppos, to ? -1 : loc);	if (to)		Code2(I_JMP, offset, label->offset);	label->next = lhead;	lhead = label;}static void free_labels(){	Label *label = lhead, *next;	for (label = lhead; label; label = next) {		next = label->next;		if (jmptab[label->offset] == -1)			yyerror(ERR_UDGOTO);		Discard(label, Label);	}	lhead = NULL;}static void add_tag(table, pos, tag)	char *table[], *tag;	int *pos;{	int i;	for (i = 0; i < *pos; i++) {		if (!strcmp(table[i], tag)) {			yyerror(ERR_DUPTAG);			return;		}	}	if (*pos == MAX_TAGS)		yyerror(ERR_TOOMANY);	else		table[(*pos)++] = tag;}static int find_tag(table, num, ident)	char *table[], *ident;	int num;{	int i;	for (i = 0; i < num; i++) {		if (streq(table[i], ident))			return i;	}	return -1;}/* The lexical analyzer */typedef struct token {	int type;	YYSTYPE val;} Token;Token *tokbuf;int tokpos = 0, toksize;#define INIT_TOKENS 128#define Textend Checkinc(tokbuf, Token, toksize, tokpos)#define Add_vtoken(t, elem, v) if (1) { Textend; tokbuf[tokpos].type = (t); \					tokbuf[tokpos++].val.elem = (v); } else#define Add_ntoken(t) if (1) { Textend; tokbuf[tokpos++].type = (t); } else#define INIT_SCONST 32#define INIT_IDENTS 64static Cstr *sconsts;			/* Allocated string constants */static char **idents;			/* Allocated identifiers */static int sconstpos = 0, identpos = 0, sconstsize, identsize;static int braces = 0, incomment = 0, instring = 0, infunc = 0, tokindex;static String sbuf;static char *ecodes = "ntvbrf\\'\"";static char *results = "\n\t\v\b\r\f\\'\"";static int begin[128], end[128];	/* Lookup table for words */static char transtab[128];		/* Table for escape codes *//* Table of reserved words and compound operators *//* These do not need to be in alphabetical order, but words with the** same first character must be adjacent.  Among compound operators** with the same first character, longer operators should come first. */static struct word {	char *s;	int val;} words[] = {	{ ""		, 0		},	{ "do"		, DO		},	{ "while"	, WHILE		},	{ "if"		, IF		},	{ "else"	, ELSE		},	{ "for"		, FOR		},	{ "func"	, FUNC		},	{ "goto"	, GOTO		},	{ "break"	, BREAK		},	{ "continue"	, CONTINUE	},	{ "return"	, RETURN	},	{ "^="		, BXA		},	{ "*="		, TA		},	{ "/="		, DA		},	{ "%="		, MA		},	{ "+="		, AA		},	{ "++"		, INC		},	{ "-="		, SA		},	{ "-->"		, FASSIGN	},	{ "--"		, DEC		},	{ "->"		, DEREF		},	{ "<<="		, SLA		},	{ "<<"		, SL		},	{ "<="		, LE		},	{ ">>="		, SRA		},	{ ">>"		, SR		},	{ ">="		, GE		},	{ "&="		, BAA		},	{ "&&"		, AND		},	{ "|="		, BOA		},	{ "||"		, OR		},	{ "=="		, EQ		},	{ "!="		, NE		},	{ "?:="		, CONDA		},	{ ""		, 0		}};#define NWORDS (sizeof(words) / sizeof(struct word) - 1)/* Set up data */void init_compile(){	int i;	bzero(begin, sizeof(begin));	bzero(end, sizeof(end));	for (i = 0; i < NWORDS; i++) {		begin[*words[i].s] = i;		while (*words[i + 1].s == *words[i].s)			i++;		end[*words[i].s] = i;	}	bzero(transtab, sizeof(transtab));	for (i = 0; ecodes[i]; i++)		transtab[ecodes[i]] = results[i];	tokbuf = Newarray(Token, toksize = INIT_TOKENS);	icode = Newarray(Instr, codesize = INIT_CODE);	sconsts = Newarray(Cstr, sconstsize = INIT_SCONST);	idents = Newarray(char *, identsize = INIT_IDENTS);	jmptab = Newarray(int, jmpsize = INIT_JMP);	condstack = Newarray(int, condsize = INIT_COND);	loopstack = Newarray(int, loopsize = INIT_COND);	sbuf = empty_string;}/* Analyze text and place tokens in buffer */void parse(text)	char *text;{	while (*text) {		text = parse_token(text);		if (!text)			return;	}	if (!infunc && !braces && !incomment && !instring && tokpos)		compile();}static char *parse_token(s)	char *s;{	char *ptr, buf[MAXILEN + 1];	int i;	void (*bobj)();	Const *cp;	if (incomment)		return read_comment(s);	if (instring)		return read_string(s);	for (; isspace(*s); s++);	if (!*s || *s == '/' && s[1] == '/')		return "";	if (*s == '/' && s[1] == '*')		return read_comment(s + 2);	if (*s == '"') {		s_term(&sbuf, 0);		return read_string(s + 1);	}	if (*s == '\'') {		s++;		Add_vtoken(ICONST, num, read_char(&s));		if (*s++ != '\'')			lexerror(ERR_UNTERMCHAR);		return s;	}	if (isdigit(*s)) {		i = 0;		if (*s == '0' && lcase(s[1]) == 'x' && isxdigit(s[2])) {			s++;			while (isxdigit(*++s))				i = i * 0x10 + Hexdigitvalue(*s);			Add_vtoken(ICONST, num, i);			return s;		}		if (*s == '0' && s[1] >= '0' && s[1] <= '8') {			while (*++s >= '0' && *s <= '8')				i = i * 010 + *s - '0';			Add_vtoken(ICONST, num, i);			return s;		}		Add_vtoken(ICONST, num, atoi(s));		while (isdigit(*++s));		return s;	}	if (*s == '\\' && !s[1])		return NULL;	if (isalpha(*s) || *s == '_') {		for (ptr = s; isalnum(*ptr) || *ptr == '_'; ptr++);		strncpy(buf, s, min(ptr - s, MAXILEN));		buf[min(ptr - s, MAXILEN)] = '\0';		if (!(*buf & ~0x7f) && begin[*buf]) {			for (i = begin[*buf]; i <= end[*buf]; i++) {				if (streq(buf + 1, words[i].s + 1))					break;			}			if (i <= end[*buf]) {				if (words[i].val == FUNC) {					if (tokpos) {						lexerror(ERR_ILLFUNC);						braces = 0;					}					infunc = 1;				}				Add_ntoken(words[i].val);				return ptr;			}		}		bobj = find_bobj(buf);		if (bobj) {			Add_vtoken(BOBJ, bobj, bobj);			return ptr;		}		cp = find_const(buf);		if (cp) {			Add_vtoken(ICONST, num, cp->val);			return ptr;		}		Push(idents, char *, identsize, identpos, vtstrdup(buf));		Add_vtoken(IDENT, s, idents[identpos - 1]);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -