📄 lex.c
字号:
/****************************************************************Copyright 1990, 1992, 1993, 1994 by AT&T Bell Laboratories and Bellcore.Permission to use, copy, modify, and distribute this softwareand its documentation for any purpose and without fee is herebygranted, provided that the above copyright notice appear in allcopies and that both that the copyright notice and thispermission notice and warranty disclaimer appear in supportingdocumentation, and that the names of AT&T Bell Laboratories orBellcore or any of their entities not be used in advertising orpublicity pertaining to distribution of the software withoutspecific, written prior permission.AT&T and Bellcore disclaim all warranties with regard to thissoftware, including all implied warranties of merchantabilityand fitness. In no event shall AT&T or Bellcore be liable forany special, indirect or consequential damages or any damageswhatsoever resulting from loss of use, data or profits, whetherin an action of contract, negligence or other tortious action,arising out of or in connection with the use or performance ofthis software.****************************************************************/#include "defs.h"#include "tokdefs.h"#include "p1defs.h"#ifdef NO_EOF_CHAR_CHECK#undef EOF_CHAR#else#ifndef EOF_CHAR#define EOF_CHAR 26 /* ASCII control-Z */#endif#endif#define BLANK ' '#define MYQUOTE (2)#define SEOF 0/* card types */#define STEOF 1#define STINITIAL 2#define STCONTINUE 3/* lex states */#define NEWSTMT 1#define FIRSTTOKEN 2#define OTHERTOKEN 3#define RETEOS 4LOCAL int stkey; /* Type of the current statement (DO, END, IF, etc) */extern char token[]; /* holds the actual token text */static int needwkey;ftnint yystno;flag intonly;extern int new_dcl;LOCAL long int stno;LOCAL long int nxtstno; /* Statement label */LOCAL int parlev; /* Parentheses level */LOCAL int parseen;LOCAL int expcom;LOCAL int expeql;LOCAL char *nextch;LOCAL char *lastch;LOCAL char *nextcd = NULL;LOCAL char *endcd;LOCAL long prevlin;LOCAL long thislin;LOCAL int code; /* Card type; INITIAL, CONTINUE or EOF */LOCAL int lexstate = NEWSTMT;LOCAL char *sbuf; /* Main buffer for Fortran source input. */LOCAL char *send; /* Was = sbuf+20*66 with sbuf[1390]. */LOCAL int maxcont;LOCAL int nincl = 0; /* Current number of include files */LOCAL long firstline;LOCAL char *laststb, *stb0;extern int addftnsrc;static char **linestart;LOCAL int ncont;LOCAL char comstart[Table_size];#define USC (unsigned char *)static char anum_buf[Table_size];#define isalnum_(x) anum_buf[x]#define isalpha_(x) (anum_buf[x] == 1)#define COMMENT_BUF_STORE 4088typedef struct comment_buf { struct comment_buf *next; char *last; char buf[COMMENT_BUF_STORE]; } comment_buf;static comment_buf *cbfirst, *cbcur;static char *cbinit, *cbnext, *cblast;static void flush_comments Argdcl((void));extern flag use_bs;static char *lastfile = "??", *lastfile0 = "?";static char fbuf[P1_FILENAME_MAX];static long lastline;static void putlineno(Void);/* Comment buffering data Comments are kept in a list until the statement before them has been parsed. This list is implemented with the above comment_buf structure and the pointers cbnext and cblast. The comments are stored with terminating NULL, and no other intervening space. The last few bytes of each block are likely to remain unused.*//* struct Inclfile holds the state information for each include file */struct Inclfile{ struct Inclfile *inclnext; FILEP inclfp; char *inclname; int incllno; char *incllinp; int incllen; int inclcode; ftnint inclstno;};LOCAL struct Inclfile *inclp = NULL;struct Keylist { char *keyname; int keyval; char notinf66;};struct Punctlist { char punchar; int punval;};struct Fmtlist { char fmtchar; int fmtval;};struct Dotlist { char *dotname; int dotval; };LOCAL struct Keylist *keystart[26], *keyend[26];/* KEYWORD AND SPECIAL CHARACTER TABLES*/static struct Punctlist puncts[ ] ={ '(', SLPAR, ')', SRPAR, '=', SEQUALS, ',', SCOMMA, '+', SPLUS, '-', SMINUS, '*', SSTAR, '/', SSLASH, '$', SCURRENCY, ':', SCOLON, '<', SLT, '>', SGT, 0, 0 };LOCAL struct Dotlist dots[ ] ={ "and.", SAND, "or.", SOR, "not.", SNOT, "true.", STRUE, "false.", SFALSE, "eq.", SEQ, "ne.", SNE, "lt.", SLT, "le.", SLE, "gt.", SGT, "ge.", SGE, "neqv.", SNEQV, "eqv.", SEQV, 0, 0 };LOCAL struct Keylist keys[ ] ={ { "assign", SASSIGN }, { "automatic", SAUTOMATIC, YES }, { "backspace", SBACKSPACE }, { "blockdata", SBLOCK }, { "byte", SBYTE }, { "call", SCALL }, { "character", SCHARACTER, YES }, { "close", SCLOSE, YES }, { "common", SCOMMON }, { "complex", SCOMPLEX }, { "continue", SCONTINUE }, { "data", SDATA }, { "dimension", SDIMENSION }, { "doubleprecision", SDOUBLE }, { "doublecomplex", SDCOMPLEX, YES }, { "elseif", SELSEIF, YES }, { "else", SELSE, YES }, { "endfile", SENDFILE }, { "endif", SENDIF, YES }, { "enddo", SENDDO, YES }, { "end", SEND }, { "entry", SENTRY, YES }, { "equivalence", SEQUIV }, { "external", SEXTERNAL }, { "format", SFORMAT }, { "function", SFUNCTION }, { "goto", SGOTO }, { "implicit", SIMPLICIT, YES }, { "include", SINCLUDE, YES }, { "inquire", SINQUIRE, YES }, { "intrinsic", SINTRINSIC, YES }, { "integer", SINTEGER }, { "logical", SLOGICAL }, { "namelist", SNAMELIST, YES }, { "none", SUNDEFINED, YES }, { "open", SOPEN, YES }, { "parameter", SPARAM, YES }, { "pause", SPAUSE }, { "print", SPRINT }, { "program", SPROGRAM, YES }, { "punch", SPUNCH, YES }, { "read", SREAD }, { "real", SREAL }, { "return", SRETURN }, { "rewind", SREWIND }, { "save", SSAVE, YES }, { "static", SSTATIC, YES }, { "stop", SSTOP }, { "subroutine", SSUBROUTINE }, { "then", STHEN, YES }, { "undefined", SUNDEFINED, YES }, { "while", SWHILE, YES }, { "write", SWRITE }, { 0, 0 }};static void analyz Argdcl((void));static void crunch Argdcl((void));static int getcd Argdcl((char*, int));static int getcds Argdcl((void));static int getkwd Argdcl((void));static int gettok Argdcl((void));static void store_comment Argdcl((char*));LOCAL char *stbuf[3]; int#ifdef KR_headersinilex(name) char *name;#elseinilex(char *name)#endif{ stbuf[0] = Alloc(3*P1_STMTBUFSIZE); stbuf[1] = stbuf[0] + P1_STMTBUFSIZE; stbuf[2] = stbuf[1] + P1_STMTBUFSIZE; nincl = 0; inclp = NULL; doinclude(name); lexstate = NEWSTMT; return(NO);}/* throw away the rest of the current line */ voidflline(Void){ lexstate = RETEOS;} char *#ifdef KR_headerslexline(n) int *n;#elselexline(int *n)#endif{ *n = (lastch - nextch) + 1; return(nextch);} void#ifdef KR_headersdoinclude(name) char *name;#elsedoinclude(char *name)#endif{ FILEP fp; struct Inclfile *t; char *name0, *lastslash, *s, *s0, *temp; int j, k; chainp I; extern chainp Iargs; if(inclp) { inclp->incllno = thislin; inclp->inclcode = code; inclp->inclstno = nxtstno; if(nextcd) inclp->incllinp = copyn(inclp->incllen = endcd-nextcd , nextcd); else inclp->incllinp = 0; } nextcd = NULL; if(++nincl >= MAXINCLUDES) Fatal("includes nested too deep"); if(name[0] == '\0') fp = stdin; else if(name[0] == '/' || inclp == NULL#ifdef MSDOS || name[0] == '\\' || name[1] == ':'#endif ) fp = fopen(name, textread); else { lastslash = NULL; s = s0 = inclp->inclname;#ifdef MSDOS if (s[1] == ':') lastslash = s + 1;#endif for(; *s ; ++s) if(*s == '/'#ifdef MSDOS || *s == '\\'#endif ) lastslash = s; name0 = name; if(lastslash) { k = lastslash - s0 + 1; temp = Alloc(k + strlen(name) + 1); strncpy(temp, s0, k); strcpy(temp+k, name); name = temp; } fp = fopen(name, textread); if (!fp && (I = Iargs)) { k = strlen(name0) + 2; for(; I; I = I->nextp) { j = strlen(s = I->datap); name = Alloc(j + k); strcpy(name, s); switch(s[j-1]) { case '/':#ifdef MSDOS case ':': case '\\':#endif break; default: name[j++] = '/'; } strcpy(name+j, name0); if (fp = fopen(name, textread)) { free(name0); goto havefp; } free(name); name = name0; } } } if (fp) { havefp: t = inclp; inclp = ALLOC(Inclfile); inclp->inclnext = t; prevlin = thislin = 0; infname = inclp->inclname = name; infile = inclp->inclfp = fp; lastline = 0; putlineno(); lastline = 0; } else { fprintf(diagfile, "Cannot open file %s\n", name); done(1); }} LOCAL intpopinclude(Void){ struct Inclfile *t; register char *p; register int k; if(infile != stdin) clf(&infile, infname, 1); /* Close the input file */ free(infname); --nincl; t = inclp->inclnext; free( (charptr) inclp); inclp = t; if(inclp == NULL) { infname = 0; return(NO); } infile = inclp->inclfp; infname = inclp->inclname; lineno = prevlin = thislin = inclp->incllno; code = inclp->inclcode; stno = nxtstno = inclp->inclstno; if(inclp->incllinp) { lastline = 0; putlineno(); lastline = lineno; endcd = nextcd = sbuf; k = inclp->incllen; p = inclp->incllinp; while(--k >= 0) *endcd++ = *p++; free( (charptr) (inclp->incllinp) ); } else nextcd = NULL; return(YES);} void#ifdef KR_headersp1_line_number(line_number) long line_number;#elsep1_line_number(long line_number)#endif{ if (lastfile != lastfile0) { p1puts(P1_FILENAME, fbuf); lastfile0 = lastfile; } fprintf(pass1_file, "%d: %ld\n", P1_SET_LINE, line_number); } static voidputlineno(Void){ extern int gflag; register char *s0, *s1; if (gflag) { if (lastline) p1_line_number(lastline); lastline = firstline; if (lastfile != infname) if (lastfile = infname) { strncpy(fbuf, lastfile, sizeof(fbuf)); fbuf[sizeof(fbuf)-1] = 0; } else fbuf[0] = 0; } if (addftnsrc) { if (laststb && *laststb) { for(s1 = laststb; *s1; s1++) { for(s0 = s1; *s1 != '\n'; s1++) if (*s1 == '*' && s1[1] == '/') *s1 = '+'; *s1 = 0; p1puts(P1_FORTRAN, s0); } *laststb = 0; /* prevent trouble after EOF */ } laststb = stb0; } } intyylex(Void){ static int tokno; int retval; switch(lexstate) { case NEWSTMT : /* need a new statement */ retval = getcds(); putlineno(); if(retval == STEOF) { retval = SEOF; break; } /* if getcds() == STEOF */ crunch(); tokno = 0; lexstate = FIRSTTOKEN; yystno = stno; stno = nxtstno; toklen = 0; retval = SLABEL; break;first: case FIRSTTOKEN : /* first step on a statement */ analyz(); lexstate = OTHERTOKEN; tokno = 1; retval = stkey; break; case OTHERTOKEN : /* return next token */ if(nextch > lastch) goto reteos; ++tokno; if( (stkey==SLOGIF || stkey==SELSEIF) && parlev==0 && tokno>3) goto first; if(stkey==SASSIGN && tokno==3 && nextch<lastch && nextch[0]=='t' && nextch[1]=='o') { nextch+=2; retval = STO; break; } retval = gettok(); break;reteos: case RETEOS: lexstate = NEWSTMT; retval = SEOS; break; default: fatali("impossible lexstate %d", lexstate); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -