gcompile.cc

来自「Gambit 是一个游戏库理论软件」· CC 代码 · 共 2,005 行 · 第 1/5 页

CC
2,005 行
字号
    {      /* if just tried and failed to reuse lookahead token after an error, discard it.  */      /* return failure if at end of input */      if (yychar == YYEOF)	YYABORT;#if YYDEBUG != 0      if (yydebug)	fprintf(stderr, "Discarding token %d (%s).\n", yychar, yytname[yychar1]);#endif      yychar = YYEMPTY;    }  /* Else will try to reuse lookahead token     after shifting the error token.  */  yyerrstatus = 3;		/* Each real token shifted decrements this */  goto yyerrhandle;yyerrdefault:  /* current state does not do anything special for the error token. */#if 0  /* This is wrong; only states that explicitly want error tokens     should shift them.  */  yyn = yydefact[yystate];  /* If its default is to accept any token, ok.  Otherwise pop it.*/  if (yyn) goto yydefault;#endifyyerrpop:   /* pop the current state because it cannot handle the error token */  if (yyssp == yyss) YYABORT;  yyvsp--;  yystate = *--yyssp;#ifdef YYLSP_NEEDED  yylsp--;#endif#if YYDEBUG != 0  if (yydebug)    {      short *ssp1 = yyss - 1;      fprintf (stderr, "Error: state stack now");      while (ssp1 != yyssp)	fprintf (stderr, " %d", *++ssp1);      fprintf (stderr, "\n");    }#endifyyerrhandle:  yyn = yypact[yystate];  if (yyn == YYFLAG)    goto yyerrdefault;  yyn += YYTERROR;  if (yyn < 0 || yyn > YYLAST || yycheck[yyn] != YYTERROR)    goto yyerrdefault;  yyn = yytable[yyn];  if (yyn < 0)    {      if (yyn == YYFLAG)	goto yyerrpop;      yyn = -yyn;      goto yyreduce;    }  else if (yyn == 0)    goto yyerrpop;  if (yyn == YYFINAL)    YYACCEPT;#if YYDEBUG != 0  if (yydebug)    fprintf(stderr, "Shifting error token, ");#endif  *++yyvsp = yylval;#ifdef YYLSP_NEEDED  *++yylsp = yylloc;#endif  yystate = yyn;  goto yynewstate; yyacceptlab:  /* YYACCEPT comes here.  */  if (yyfree_stacks)    {      free (yyss);      free (yyvs);#ifdef YYLSP_NEEDED      free (yyls);#endif    }  return 0; yyabortlab:  /* YYABORT comes here.  */  if (yyfree_stacks)    {      free (yyss);      free (yyvs);#ifdef YYLSP_NEEDED      free (yyls);#endif    }  return 1;}#line 382 "gcompile.yy"const char CR = (char) 10;char nextchar(void){  char c = current_expr[current_char];  if( c == '\r' || c == '\n' )    ++current_line;  ++current_char;  return c;}void ungetchar(char /*c*/){  char c = current_expr[current_char-1];  if( (current_char > 0) && (c == '\r' || c == '\n') )    --current_line;  --current_char;}typedef struct tokens  { long tok; char *name; } TOKENS_T;void gcl_yyerror(char *s){static struct tokens toktable[] ={ { LOR, "OR or ||" },  { LAND, "AND or &&" }, { LNOT, "NOT or !" },    { EQU, "=" }, { NEQ, "!=" }, { LTN, "<" }, { LEQ, "<=" },    { GTN, ">" }, { GEQ, ">=" }, { PLUS, "+" }, { MINUS, "-" },    { STAR, "*" }, { SLASH, "/" }, { ASSIGN, ":=" }, { SEMI, ";" },    { LBRACK, "[" }, { DBLLBRACK, "[[" }, { RBRACK, "]" },    { LBRACE, "{" }, { RBRACE, "}" }, { RARROW, "->" },    { LARROW, "<-" }, { DBLARROW, "<->" }, { COMMA, "," }, { HASH, "#" },    { DOT, "." }, { CARET, "^" }, { UNDERSCORE, "_" },    { AMPER, "&" }, { WRITE, "<<" }, { READ, ">>" }, { DOLLAR, "$" },    { IF, "If" }, { WHILE, "While" }, { FOR, "For" },    { NEWFUNC, "NewFunction" }, { DELFUNC, "DeleteFunction" },    { TYPEDEF, "=:" }, { INCLUDE, "Include" },    { PERCENT, "%" }, { DIV, "DIV" }, { LPAREN, "(" }, { RPAREN, ")" },    { CRLF, "carriage return" }, { EOC, "carriage return" },    { FLOATPREC, "Float" }, { RATIONALPREC, "Rational" }, { 0, 0 }};  gsm->ErrorStream() << s << " at line " << current_line << " in file " << current_file       << ": ";  for (int i = 0; toktable[i].tok != 0; i++)    if (toktable[i].tok == gcl_yychar)   {      gsm->ErrorStream() << toktable[i].name << '\n';      return;    }  switch (gcl_yychar)   {    case NAME:      gsm->ErrorStream() << "identifier " << tval << '\n';      break;    case BOOLEAN:      if (bval == triTRUE)     	gsm->ErrorStream() << "True\n";      else if (bval == triFALSE)        gsm->ErrorStream() << "False\n";      else  /* (bval == triUNKNOWN) */        gsm->ErrorStream() << "Unknown\n";      break;    case FLOAT:      gsm->ErrorStream() << "floating-point constant " << dval << '\n';      break;    case INTEGER:      gsm->ErrorStream() << "integer constant " << ival << '\n';      break;    case TEXT:      gsm->ErrorStream() << "text string " << tval << '\n';      break;    case STDOUT:      gsm->ErrorStream() << "StdOut\n";      break;    case gNULL:      gsm->ErrorStream() << "NullOut\n";      break;    default:      if (isprint(gcl_yychar) && !isspace(gcl_yychar))        gsm->ErrorStream() << ((char) gcl_yychar) << '\n';      else         gsm->ErrorStream() << "nonprinting character " << gcl_yychar << '\n';      break;  }    }int gcl_yylex(void){  char c;    do  {    c = nextchar();  }  while (isspace(c) || c == '\r' || c == '\n');  if (isalpha(c))  {    gText s(c);    c = nextchar();    while (isalpha(c) || isdigit(c))   {      s += c;      c = nextchar();    }    ungetchar(c);    if (s == "True")   {      bval = triTRUE;      return BOOLEAN;    }    else if (s == "False")  {      bval = triFALSE;      return BOOLEAN;    }    else if (s == "Unknown") {      bval = triUNKNOWN;      return BOOLEAN;    }    else if (s == "StdOut") return STDOUT;    else if (s == "NullOut")   return gNULL;    else if (s == "AND")    return LAND;    else if (s == "OR")     return LOR;    else if (s == "NOT")    return LNOT;    else if (s == "DIV")    return DIV;    else if (s == "MOD")    return PERCENT;    else if (s == "If")     return IF;    else if (s == "While")  return WHILE;    else if (s == "For")    return FOR;    else if (s == "NewFunction")   return NEWFUNC;    else if (s == "DeleteFunction")   return DELFUNC;    else if (s == "Float")   return FLOATPREC;    else if (s == "Rational")  return RATIONALPREC;    else if (s == "Include")   return INCLUDE;    else  { tval = s; return NAME; }  }  if (c == '"')   {    tval = "";    bool quote = true;    bool check_digraph = true;    while( quote )    {      c = nextchar();      tval += c;            if( check_digraph &&           tval.Length() >= 2 &&           tval[ tval.Length() - 2 ] == '\\' )      {        switch( c )        {	case '\'':	case '\"':	case '\?':	case '\\':          tval = tval.Left( tval.Length() - 2 ) + gText(c);          check_digraph = false;          break;        case 'a':          tval = tval.Left( tval.Length() - 2 ) + gText('\a');          check_digraph = false;          break;                    case 'b':          tval = tval.Left( tval.Length() - 2 ) + gText('\b');          check_digraph = false;          break;                    case 'f':          tval = tval.Left( tval.Length() - 2 ) + gText('\f');          check_digraph = false;          break;                    case 'n':          tval = tval.Left( tval.Length() - 2 ) + gText('\n');          check_digraph = false;          break;                    case 'r':          tval = tval.Left( tval.Length() - 2 ) + gText('\r');          check_digraph = false;          break;                    case 't':          tval = tval.Left( tval.Length() - 2 ) + gText('\t');          check_digraph = false;          break;                    case 'v':          tval = tval.Left( tval.Length() - 2 ) + gText('\v');          check_digraph = false;          break;                    } // switch( c )      }      else      {        check_digraph = true;        if( c == '\"' )        {          tval = tval.Left( tval.Length() - 1 );          quote = false;        }      }    } // while( quote )    return TEXT;  }  if (isdigit(c))   {    gText s(c);    c = nextchar();    while (isdigit(c))   {      s += c;      c = nextchar();    }    if (c == '.')   {      s += c;      c = nextchar();      while (isdigit(c))  {	s += c;	c = nextchar();      }      ungetchar(c);      dval = atof((char *) s);      return FLOAT;    }    else  {      ungetchar(c);      ival = atoI((char *) s);      return INTEGER;    }  }  switch (c)  {    case ',':   return COMMA;    case '.':   c = nextchar();      if (c < '0' || c > '9')  { ungetchar(c);  return DOT; }      else  {	gText s(".");	s += c;        c = nextchar();        while (isdigit(c))  {	  s += c;	  c = nextchar();        }        ungetchar(c);        dval = atof((char *) s);        return FLOAT;      }    case ';':   return SEMI;    case '_':   return UNDERSCORE;    case '(':   return LPAREN;    case ')':   return RPAREN;    case '{':   return LBRACE;    case '}':   return RBRACE;    case '+':   return PLUS;    case '-':   c = nextchar();                if (c == '>')  return RARROW;                else  { ungetchar(c);  return MINUS; }    case '*':   return STAR;    case '/':   return SLASH;    case '%':   return PERCENT;    case '=':   c = nextchar();                if (c == ':')  return TYPEDEF;                else   { ungetchar(c);  return EQU; }      case '#':   return HASH;    case '^':   return CARET;    case '[':   c = nextchar();                if (c == '[')  return DBLLBRACK;                else   {		  ungetchar(c);		  return LBRACK;		}    case ']':   return RBRACK;    case ':':   c = nextchar();                if (c == '=')  return ASSIGN;                else   { ungetchar(c);  return ':'; }      case '!':   c = nextchar();                if (c == '=')  return NEQ;		else   { ungetchar(c);  return LNOT; }    case '<':   c = nextchar();                if (c == '=')  return LEQ;	        else if (c == '<')  return WRITE;                 else if (c != '-')  { ungetchar(c);  return LTN; }                else   { 		  c = nextchar();		  if (c == '>')   return DBLARROW;		  ungetchar(c);		  return LARROW;		}    case '>'

⌨️ 快捷键说明

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