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

📄 crp.c

📁 COCO類似C的編譯器
💻 C
📖 第 1 页 / 共 2 页
字号:
/**********************************************************
**   PARSER_C.FRM
**   Coco/R C Support Frames.
**   Author: Frankie Arzu <farzu@uvg.edu.gt>
**
**   Jun 12, 1996   Version 1.06
**      Many fixes and suggestions thanks to
**      Pat Terry <p.terry@ru.ac.za>
**********************************************************/

#include "crs.h"
#include "crp.h"

#include "crt.h"
#include "crf.h"
#include "cra.h"
#include "crp.h"
#include "crs.h"
#include <string.h>
#include <stdlib.h>

static void FixString(char *name)
{
  int i, j, len, spaces, start;
  len = strlen(name);
  if (len == 2) { SemError(129); return; }
  if (ignore_case) upcase(name);
  spaces = FALSE; start = name[0];
  for (i = 1; i <= len-2; i++) {
    if (name[i] > 0 && name[i] <= ' ') spaces = TRUE;
    if (name[i] == '\\') {
      if (name[i+1] == '\\' || name[i+1] == '\'' || name[i+1] == '\"') {
        for (j = i; j < len; j++) name[j] = name[j+1]; len--;
      }
    }
  }
  if (spaces) SemError(124);
}

static void MatchLiteral (int sp)
/* store string either as token or as literal */
{
  PTermNode sn, sn1;
  int matched_sp;

  sn = GetTermP(sp);
  matched_sp = MatchDFA((unsigned char *) sn->name, sp);
  if (matched_sp != 0) {
    sn1 = GetTermP(matched_sp);
    sn1->type = T_CLASSLITTOKEN;
    sn->type  = T_LITTOKEN;
  } else sn->type= T_CLASSTOKEN;
}

static void SetCtx (int gp)
/* set transition code to contextTrans */
{
  PGraphNode gn;
  while (gp > 0) {
    gn = GetGraphP(gp);
    if (gn->type == T_CHAR || gn->type == T_CLASS)
      gn->CONTEXT = T_CONTEXT;
    else
      if (gn->type == T_OPT || gn->type == T_REP)
      SetCtx(gn->INNER);
      else
      if (gn->type == T_ALT) {
      SetCtx(gn->INNER); SetCtx(gn->ALT);
      }
    gp = gn->next;
  }
}

static void StringClass(char *s, Set *items)
{
  s[strlen(s)-1]=0;  s++;  /* Ignore First and Last character */
  while (*s) Set_AddItem(items, *s++);
}

/**************************************************************************/



Error_Func Custom_Error = 0L;

static int Sym;
static int errors = 0;                /*number of detected errors*/
static int ErrDist = MinErrDist;

#define MAXSYM		3


/* Production prototypes */

static void CR(void);
static void Ident(char *s);
static void Declaration(int *startedDFA);
static void Attribs(int *n);
static void SemText(int *n);
static void Expression(int *n);
static void SetDecl(void);
static void TokenDecl(int sec_type);
static void NameDecl(void);
static void TokenExpr(int *n);
static void CompSet(PSet items);
static void SimSet(PSet items);
static void String(char *s);
static void ChrSet(int *n);
static void Term(int *n);
static void Factor(int *n);
static void Symbol(char *name);
static void TokenTerm(int *n);
static void TokenFactor(int *n);


#define NSETBITS        16

static unsigned short int SymSet[][MAXSYM] = {
  /*EOF_Sym identSym stringSym PRODUCTIONSSym EqualSym ENDSym CHARACTERSSym TOKENSSym NAMESSym PRAGMASSym COMMENTSSym IGNORESym LparenPointSym */
  {0x7EC7,0x4,0x80},
  /*identSym stringSym PointSym ANYSym LparenSym BarSym WEAKSym LbrackSym LbraceSym SYNCSym LparenPointSym */
  {0x106,0xBA80,0x82},
  /*identSym ENDSym */
  {0x202,0x0,0x0},
  /*PointSym RparenSym RbrackSym RbraceSym */
  {0x100,0x4400,0x1},
  /*identSym stringSym PointSym ANYSym LparenSym RparenSym BarSym WEAKSym LbrackSym RbrackSym LbraceSym RbraceSym SYNCSym LparenPointSym */
  {0x106,0xFE80,0x83},
  /*PRODUCTIONSSym PointSym CHARACTERSSym TOKENSSym NAMESSym PRAGMASSym COMMENTSSym TOSym NESTEDSym IGNORESym RparenSym RbrackSym RbraceSym */
  {0x7D40,0x4407,0x1},
  /*identSym stringSym LparenSym LbrackSym LbraceSym */
  {0x6,0xA200,0x0},
  {0x0}
};

void GenError(int errno)
{ if (ErrDist >= MinErrDist) {
    if (Custom_Error != 0L)
      (*Custom_Error) (errno, S_NextLine, S_NextCol, S_NextPos);
    errors++;
  }
  ErrDist = 0;
}

void SynError(int errno)
{ if (errno <= MAXERROR) errno = MAXERROR;
  if (ErrDist >= MinErrDist) {
    if (Custom_Error != 0L)
      (*Custom_Error) (errno, S_NextLine, S_NextCol, S_NextPos);
    errors++;
  }
  ErrDist = 0;
}

void SemError(int errno)
{ if (errno <= MAXERROR) errno = MAXERROR;
  if (ErrDist >= MinErrDist) {
    if (Custom_Error != 0L)
      (*Custom_Error) (errno, S_Line, S_Col, S_Pos);
    errors++;
  }
  ErrDist = 0;
}

static void Get(void)
{ do {
    Sym = S_Get();
    if (Sym <= MAXT) ErrDist ++;
    else {
      if (Sym == OptionsSym) { /*42*/
      	char s[100];
      	  LookAheadString(s, sizeof(s)-1);
      	  SetOptions(s);
      } else
      /* Empty Stmt */ ;
      S_NextPos  = S_Pos;  S_NextCol = S_Col;
      S_NextLine = S_Line; S_NextLen = S_Len;
    }
  } while (Sym > MAXT);
}

static int In (unsigned short int *SymbolSet, int i)
{ return SymbolSet[i / NSETBITS] & (1 << (i % NSETBITS)); }

static void Expect (int n)
{ if (Sym == n) Get(); else GenError(n); }

static void ExpectWeak (int n, int follow)
{ if (Sym == n) Get();
  else {
    GenError(n);
    while (!(In(SymSet[follow], Sym) || In(SymSet[0], Sym))) Get();
  }
}

static int WeakSeparator (int n, int syFol, int repFol)
{ unsigned short int s[MAXSYM];
  int i;

  if (Sym == n) { Get(); return 1; }
  if (In(SymSet[repFol], Sym)) return 0;
  for (i = 0; i < MAXSYM; i++)
    s[i] = SymSet[0][i] | SymSet[syFol][i] | SymSet[repFol][i];
  GenError(n);
  while (!In(s, Sym)) Get();
  return In(SymSet[syFol], Sym);
}

int Successful(void)
{ return errors == 0; }

/* Productions */

static void CR(void)
{
	Name name1;
	int  attr, sem, exp, is_new, sp, type;
	int startedDFA = FALSE;
	PNTermNode sn;
	Expect(COMPILERSym);
	Ident(compiler_name);
	global_defs.pos  = S_NextPos;
	global_defs.line = S_NextLine;
	while (Sym >= identSym && Sym <= COMPILERSym ||
	       Sym >= EqualSym && Sym <= ENDSym ||
	       Sym >= FROMSym && Sym <= NESTEDSym ||
	       Sym >= CASESym && Sym <= No_Sym) {
		Get();
	}
	global_defs.len  =
	  (int) (S_NextPos-global_defs.pos);
	while (Sym >= CHARACTERSSym && Sym <= COMMENTSSym ||
	       Sym == IGNORESym) {
		Declaration(&startedDFA);
	}
	while (!(Sym == EOF_Sym ||
	         Sym == PRODUCTIONSSym)) { GenError(43); Get(); }
	if (Successful()) { /* No Errors so far */
	  if (!MakeDeterministic()) SemError(127);
	};
	Expect(PRODUCTIONSSym);
	while (Sym == identSym) {
		attr = NIL; sem = NIL;
		Ident(name1);
		if ((sp = FindSym(name1, &type)) != UNDEF) {
		  is_new = FALSE;
		  if (type != T_NT) { SemError(108); return; }
		  else {
		    sn = GetNTermP(sp);
		    if (sn->graph) SemError(107);
		    sn->line_dec = S_Line;
		  }
		} else {
		  sp = NewSym(name1, T_NT);
		  sn = GetNTermP(sp); is_new = TRUE;
		  sn->line_dec = S_Line;
		};
		if (Sym == LessSym ||
		    Sym == LessPointSym) {
			Attribs(&attr);
		}
		if (!is_new) {
		  if (sn->has_attr && !attr) SemError(105);
		  if (!sn->has_attr && attr) SemError(105);
		}
		if (attr) {
		  sn->attr = attr; sn->has_attr = TRUE;
		};
		ExpectWeak(EqualSym,1);
		if (Sym == LparenPointSym) {
			SemText(&sem);
		}
		Expression(&exp);
		if (sem) {
		  (void) LinkGraph(sem, exp); exp = sem;
		};
		ExpectWeak(PointSym,2);
		sn = GetNTermP(sp); /* reload */
		sn->graph = exp;
		while (!(Sym >= EOF_Sym && Sym <= identSym ||
		         Sym == ENDSym)) { GenError(44); Get(); }
	}
	Expect(ENDSym);
	Ident(name1);
	if (strcmp(name1, compiler_name)) SemError(117);
	if((sp = FindSym(compiler_name, &type)) != UNDEF) {
	  if (type!=T_NT) SemError(108);
	  else {
	    sn = GetNTermP(sp);
	    if (sn->has_attr) SemError(112);
	    sn->reachable=TRUE;
	  }
	} else SemError(111);
	no_sym = NewSym("not", T_T);
	Expect(PointSym);
	if (dirty_DFA && !MakeDeterministic()) SemError(127);
}

static void Ident(char *s)
{
	Expect(identSym);
	LexString(s, MAX_ID_LEN-1);
}

static void Declaration(int *startedDFA)
{
	Set ignore;
	int n1, n2, nested = FALSE;
	switch (Sym) {
		case CHARACTERSSym:  
			Get();
			while (Sym == identSym) {
				SetDecl();
			}
			break;
		case TOKENSSym:  
			Get();
			while (Sym >= identSym && Sym <= stringSym) {
				TokenDecl(T_T);
			}
			break;
		case NAMESSym:  
			Get();
			while (Sym == identSym) {
				NameDecl();
			}
			break;
		case PRAGMASSym:  
			Get();
			while (Sym >= identSym && Sym <= stringSym) {
				TokenDecl(T_P);
			}
			break;
		case COMMENTSSym:  
			Get();
			Expect(FROMSym);
			TokenExpr(&n1);
			Expect(TOSym);
			TokenExpr(&n2);
			if (Sym == NESTEDSym) {
				Get();
				nested = TRUE;
			}
			if (n1 * n2) NewComment(n1, n2, nested);
			break;
		case IGNORESym:  
			Get();
			if (Sym == CASESym) {
				Get();
				if (*startedDFA) SemError(130);
				ignore_case = TRUE;
			} else if (Sym >= identSym && Sym <= stringSym ||
			           Sym >= ANYSym && Sym <= CHRSym) {
				Set_Init(&ignore);
				CompSet(&ignore);
				AddIgnore(&ignore);
				if (Set_IsItem(&ignore,0)) SemError(119);
				Set_Done(&ignore);
			} else GenError(45);
			break;
		default :GenError(46); break;
	}
	*startedDFA = TRUE;
}

static void Attribs(int *n)
{
	long P;
	int Len, Line, Col;
	if (Sym == LessSym) {
		Get();
		P = S_Pos+1; Line = S_Line; Col = S_Col;
		while (Sym >= identSym && Sym <= LessSym ||
		       Sym >= LessPointSym && Sym <= No_Sym) {
			if (Sym >= identSym && Sym <= stringSym ||
			    Sym >= numberSym && Sym <= LessSym ||
			    Sym >= LessPointSym && Sym <= No_Sym) {
				Get();
			} else if (Sym == badstringSym) {
				Get();
				SemError(102);
			} else GenError(47);
		}
		Expect(GreaterSym);
		Len = (int) (S_Pos - P);
		*n = MakeSemGraph(T_ATTR, P, Len, Line, Col);
	} else if (Sym == LessPointSym) {
		Get();
		P = S_Pos+2; Line = S_Line; Col = S_Col;
		while (Sym >= identSym && Sym <= LessPointSym ||
		       Sym >= LparenPointSym && Sym <= No_Sym) {
			if (Sym >= identSym && Sym <= stringSym ||
			    Sym >= numberSym && Sym <= LessPointSym ||
			    Sym >= LparenPointSym && Sym <= No_Sym) {
				Get();
			} else if (Sym == badstringSym) {
				Get();
				SemError(102);
			} else GenError(48);
		}
		Expect(PointGreaterSym);
		Len = (int) (S_Pos - P);
		*n = MakeSemGraph(T_ATTR, P, Len, Line, Col);
	} else GenError(49);
}

⌨️ 快捷键说明

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