📄 lexer.c
字号:
/*************************************************************
Copyright (C) 1990, 1991, 1993 Andy C. Hung, all rights reserved.
PUBLIC DOMAIN LICENSE: Stanford University Portable Video Research
Group. If you use this software, you agree to the following: This
program package is purely experimental, and is licensed "as is".
Permission is granted to use, modify, and distribute this program
without charge for any purpose, provided this license/ disclaimer
notice appears in the copies. No warranty or maintenance is given,
either expressed or implied. In no event shall the author(s) be
liable to you or a third party for any special, incidental,
consequential, or other damages, arising out of the use or inability
to use the program for any purpose (or the loss of data), even if we
have been advised of such possibilities. Any public reference or
advertisement of this source code should refer to it as the Portable
Video Research Group (PVRG) code, and not by any author(s) (or
Stanford University) name.
*************************************************************/
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# define U(x) ((x)&0377)
# define NLSTATE yyprevious=YYNEWLINE
# define BEGIN yybgin = yysvec + 1 +
# define INITIAL 0
# define YYLERR yysvec
# define YYSTATE (yyestate-yysvec-1)
# define YYOPTIM 1
# define YYLMAX 200
# define output(c) putc(c,yyout)
# define input() (((yytchar=yysptr>yysbuf?U(*--yysptr):getc(yyin))==10?(yylineno++,yytchar):yytchar)==EOF?0:yytchar)
# define unput(c) {yytchar= (c);if(yytchar=='\n')yylineno--;*yysptr++=yytchar;}
# define yymore() (yymorfg=1)
# define ECHO fprintf(yyout, "%s",yytext)
//# define REJECT { nstr = yyreject(); goto yyfussy;}
int yyleng; extern char yytext[];
int yymorfg;
extern char *yysptr, yysbuf[];
int yytchar;
FILE *yyin, *yyout;
//FILE *yyin ={"stdin"}, *yyout ={"stdout"};
extern int yylineno;
struct yysvf {
struct yywork *yystoff;
struct yysvf *yyother;
int *yystops;};
struct yysvf *yyestate;
extern struct yysvf yysvec[], *yybgin;
/*LABEL lexer.c */
/* Redefine the yywrap so that we don't have
to worry about lex library */
# define yywrap() (1)
static char *ReservedWords[] = {
"ADD",
"SUB",
"MUL",
"DIV",
"NOT",
"AND",
"OR",
"XOR",
"LT",
"LTE",
"EQ",
"GT",
"GTE",
"NEG",
"SQRT",
"ABS",
"FLOOR",
"CEIL",
"ROUND",
"DUP",
"POP",
"EXCH",
"COPY",
"ROLL",
"INDEX",
"CLEAR",
"STO",
"RCL",
"GOTO",
"IFG",
"IFNG",
"EXIT",
"EXE",
"ABORT",
"PRINTSTACK",
"PRINTPROGRAM",
"PRINTIMAGE",
"PRINTFRAME",
"ECHO",
"OPEN",
"CLOSE",
"EQU",
"VAL",
"STREAMNAME",
"COMPONENT",
"FRAMERATE",
"FRAMESKIP",
"QUANTIZATION",
"SEARCHLIMIT",
"NTSC",
"CIF",
"QCIF",
""};
#define R_ADD 1
#define R_SUB 2
#define R_MUL 3
#define R_DIV 4
#define R_NOT 5
#define R_AND 6
#define R_OR 7
#define R_XOR 8
#define R_LT 9
#define R_LTE 10
#define R_EQ 11
#define R_GT 12
#define R_GTE 13
#define R_NEG 14
#define R_SQRT 15
#define R_ABS 16
#define R_FLOOR 17
#define R_CEIL 18
#define R_ROUND 19
#define R_DUP 20
#define R_POP 21
#define R_EXCH 22
#define R_COPY 23
#define R_ROLL 24
#define R_INDEX 25
#define R_CLEAR 26
#define R_STO 27
#define R_RCL 28
#define R_GOTO 29
#define R_IFG 30
#define R_IFNG 31
#define R_EXIT 32
#define R_EXE 33
#define R_ABORT 34
#define R_PRINTSTACK 35
#define R_PRINTPROGRAM 36
#define R_PRINTIMAGE 37
#define R_PRINTFRAME 38
#define R_ECHO 39
#define R_OPEN 40
#define R_CLOSE 41
#define R_EQU 42
#define R_VAL 43
#define R_STREAMNAME 44
#define R_COMPONENT 45
#define R_FRAMERATE 46
#define R_FRAMESKIP 47
#define R_QUANTIZATION 48
#define R_SEARCHLIMIT 49
#define R_NTSC 50
#define R_CIF 51
#define R_QCIF 52
#define R_INTEGER 1000
#define R_LBRACKET 1001
#define R_RBRACKET 1002
#define R_ID 1003
#define R_STRING 1004
#define R_REAL 1005
static char *EquLabels[] = {
"GQUANT",
"MQUANT",
"MQUANTENABLE",
"MTYPE",
"BD",
"DBD",
"VAROR",
"VAR",
"MWOR",
"RATE",
"BUFFERSIZE",
"BUFFERCONTENTS",
"QDFACT",
"QOFFS",
""};
#define L_GQUANT 1
#define L_MQUANT 2
#define L_MQUANTENABLE 3
#define L_MTYPE 4
#define L_BD 5
#define L_DBD 6
#define L_VAROR 7
#define L_VAR 8
#define L_MWOR 9
#define L_RATE 10
#define L_BUFFERSIZE 11
#define L_BUFFERCONTENTS 12
#define L_QDFACT 13
#define L_QOFFS 14
int CommentDepth = 0; /* depth of comment nesting */
int yyint=0; /* Return value for integers */
int LexDebug=0; /* Status of lex debugging */
#define PRIME 211
#define EOS '\0'
//#define MakeStructure(S) (S *) malloc(sizeof(S))
#define MakeStructure(S) (S *) calloc(sizeof(S))
#define InsertLink(link,list){\
if(!list){list=link;}else{link->next=list;list=link;}}
#define ID struct id
#define LINK struct link_def
ID { /* Default id structure */
char *name;
int tokentype;
int count;
int value;
};
LINK { /* A link for the hash buckets */
ID *lid;
LINK *next;
};
ID *Cid=NULL;
/*PUBLIC*/
//extern void initparser();
////extern void parser();
//extern void Execute();
//static int hashpjw();
//static LINK * MakeLink();
//static ID * enter();
//static char * getstr();
//static void PrintProgram();
//static void MakeProgram();
//static void CompileProgram();
//static int mylex();
//int yylook();
//int yyback(int *p, int m);
//void equname(int number,char *name);
/*PRIVATE*/
/*NOPROTO*/
# define NORMAL 2
# define COMMENT 4
# define YYNEWLINE 10
//yylex(){
//int nstr; extern int yyprevious;
//while((nstr = yylook()) >= 0)
//
///*yyfussy:*/ switch(nstr){
//case 0:
//if(yywrap()) return(0); break;
//case 1:
//{}
//break;
//case 2:
//{Cid = enter(0,yytext,yyleng);
// if (LexDebug)
// {
// printf("%s : %s (%d)\n",
// yytext,
// ((Cid->tokentype) ? "RESERVED" : "IDENTIFIER"),
// Cid->count);
// }
// if (Cid->tokentype)
// {
// return(Cid->tokentype);
// }
// else
// {
// yyint = Cid->value;
// return(R_ID);
// }
// }
//break;
//case 3:
// {if (LexDebug)
// {
// printf("%s : %s\n", yytext, "REAL");
// }
// return(R_REAL);
// }
//break;
//case 4:
//{if (LexDebug)
// {
// printf("%s : %s\n", yytext, "INTEGER");
// }
// yyint = atoi(yytext);
// return(R_INTEGER);}
//break;
//case 5:
//{if (LexDebug)
// {
// printf("%s : %s\n", yytext, "(HEX)INTEGER");
// }
// yyint = strtol(yytext+2,NULL,16);
// return(R_INTEGER);}
//break;
//case 6:
//{if (LexDebug)
// {
// printf("%s : %s\n", yytext, "(HEX)INTEGER");
// }
// yyint = strtol(yytext,NULL,16);
// return(R_INTEGER);}
//break;
//case 7:
//{if (LexDebug)
// {
// printf("%s : %s\n", yytext, "(OCT)INTEGER");
// }
// yyint = strtol(yytext+2,NULL,8);
// return(R_INTEGER);}
//break;
//case 8:
//{if (LexDebug)
// {
// printf("%s : %s\n", yytext, "(OCT)INTEGER");
// }
// yyint = strtol(yytext,NULL,8);
// return(R_INTEGER);}
//break;
//case 9:
//{if (LexDebug)
// {
// printf("%s : %s\n", yytext, "(CHAR)INTEGER");
// }
// if (yyleng>4)
// {
// yyint = strtol(yytext+2,NULL,8);
// }
// else
// {
// if (*(yytext+1)=='\\')
// {
// switch(*(yytext+2))
// {
// case '0':
// yyint=0;
// break;
// case 'b':
// yyint = 0x8;
// break;
// case 'i':
// yyint = 0x9;
// break;
// case 'n':
// yyint = 0xa;
// break;
// case 'v':
// yyint = 0xb;
// break;
// case 'f':
// yyint = 0xc;
// break;
// case 'r':
// yyint = 0xd;
// break;
// default:
// yyint=(*yytext+2);
// break;
// }
// }
// else
// {
// yyint = *(yytext+1);
// }
// }
// return(R_INTEGER);}
//break;
//case 10:
// {if (LexDebug)
// {
// printf("%s : %s\n", yytext, "LBRACKET");
// }
// return(R_LBRACKET);}
//break;
//case 11:
// {if (LexDebug)
// {
// printf("%s : %s\n", yytext, "RBRACKET");
// }
// return(R_RBRACKET);}
//break;
//case 12:
//{if (LexDebug)
// {
// printf("%s : %s\n", yytext, "STRING");
// }
// return(R_STRING);}
//break;
//case 13:
//{CommentDepth++; BEGIN COMMENT;}
//break;
//case 14:
// {CommentDepth--;if(!CommentDepth) BEGIN NORMAL;}
//break;
//case 15:
// {
// /* None of the above rules applicable, so
// it's a bad symbol. */
// printf("Bad input char '%c' on line %d\n",
// yytext[0],
// yylineno);
// }
//break;
//case 16:
// {}
//break;
//case -1:
//break;
//default:
//fprintf(yyout,"bad switch yylook %d",nstr);
//} return(0); }
/* end of yylex */
/*PROTO*/
#define NUMBER_PROGRAMS 10
#define MEMTOP 1024
#define MAXIMUM_LINES 2000
LINK *HashTable[PRIME]; /* My little hash table */
int DataLevel = 0; /* pointer within stack */
double DataStack[MEMTOP]; /* The data stack */
double *DataPtr;
int NextVal=0; /* the number of values to load directly
from the program */
double Memory[MEMTOP];
int LocalLevel=0;
int CommandLevel=0;
double *LocalStack;
int *CommandStack;
int CurrentLine=0;
int CurrentProgram=0;
double ProgramLocalStack[NUMBER_PROGRAMS][MAXIMUM_LINES];
int ProgramCommandStack[NUMBER_PROGRAMS][MAXIMUM_LINES];
int ProgramLevel[NUMBER_PROGRAMS];
int ProgramLocalLevel[NUMBER_PROGRAMS];
int PProgram=0;
int PLevel=0;
int PLLevel=0;
int *PCStack=NULL;
double *PLStack=NULL;
int LabelLevel=0;
ID *LabelStack[1000];
int SourceLevel=0;
int SourceProgramStack[16];
int SourceLineStack[16];
/*START*/
/*BFUNC
initparser() is used to place the Reserved Words into the hash table.
It must be called before the parser command is called.
EFUNC*/
//void initparser()
//{
// char i,**sptr;
// BEGIN NORMAL;
//
// for(i=1,sptr=ReservedWords;**sptr!='\0';i++,sptr++)
// { /* Add Reserved Words */
// enter(i,*sptr,strlen(*sptr));
// }
// for(i=1,sptr=EquLabels;**sptr!='\0';i++,sptr++)
// { /* Add defined labels */
// equname((int)i,*sptr);
// }
// for(i=0;i<NUMBER_PROGRAMS;i++)
// {
// ProgramLevel[i]=0;
// }
// DataLevel=0;
// DataPtr = DataStack;
//}
#undef BEGIN
#undef MakeStructure
#include "globals.h"
#include "stream.h"
#include <math.h>
#define pushdata(val) *(DataPtr++) = (double) (val); DataLevel++;
extern FRAME *CFrame;
extern IMAGE *CImage;
extern int ErrorValue;
extern int FrameRate;
extern int FrameSkip;
extern int SearchLimit;
extern int ImageType;
extern int InitialQuant;
/*BFUNC
hashpjw() returns a hash value for a string input.
EFUNC*/
//static int hashpjw(s)
// char *s;
//static int hashpjw(char *s)
//{
// BEGIN("hashpjw");
// char *p;
// unsigned int h=0,g;
//
// for(p=s;*p!=EOS;p++)
// {
// h = (h << 4) + *p;
// if (g = h&0xf0000000)
// {
// h = h ^(g >> 24);
// h = h ^ g;
// }
// }
// return(h % PRIME);
//}
/*BFUNC
MakeLink() is used to construct a link object. The link
is used for the hash table construct.
EFUNC*/
//static LINK *MakeLink(tokentype,str,len)
// int tokentype;
// char *str;
// int len;
//static LINK *MakeLink(int tokentype,char *str,int len)
//{
// BEGIN("MakeLink");
// LINK *temp;
//
// if (!(temp = MakeStructure(LINK)))
// {
// WHEREAMI();
// printf("Cannot make a LINK.\n");
// exit(ERROR_MEMORY);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -