void LL1Parsing::LL1() { OpenFile(); Production(); FisrtInit(); //FIRST集合初始化 //l.PrintVariFst(); GetVariFst(); //求FIRST集 //l.PrintVariFst(); GetVariFol(); //求FOLLOW集 //l.PrintVariFol(); GetProFst(); //求产生式的FIRST集合 //l.PrintProFst(); GetFAATable(); //构造预测分析表 //l.PrintFAATable(); stack<int> s; enum tokenType token; s.push(99); s.push(50); int top; token = GetNewToken(); do{ top = s.top(); if (top < t_len || top == 99){ if (top != 99 && top == GetCode(token)){ cout << "pop " << top << endl; s.pop(); token = GetNewToken(); } else { cerr << "error" << endl; } } else{ if (faaTable[top - 50][GetCode(token)].size()>0){ long long int code = faaTable[top - 50][GetCode(token)][0]; cout << top << "-->" << code << endl; s.pop(); if (code > 0){ for (size_t j = (int)((GetLength(code) + 1) / 2); j > 0; --j){ s.push(Get2Code(code, j)); } } } else { cerr << "error" << endl; } } } while (s.top() != 99 && !s.empty()); }
void CServerBrowser::Refresh(int RefreshFlags) { m_RefreshFlags = RefreshFlags; if(RefreshFlags&IServerBrowser::REFRESHFLAG_LAN) { // clear out everything m_aServerlist[IServerBrowser::TYPE_LAN].Clear(); if(m_ActServerlistType == IServerBrowser::TYPE_LAN) m_ServerBrowserFilter.Clear(); // next token m_CurrentLanToken = GetNewToken(); CPacker Packer; Packer.Reset(); Packer.AddRaw(SERVERBROWSE_GETINFO, sizeof(SERVERBROWSE_GETINFO)); Packer.AddInt(m_CurrentLanToken); /* do the broadcast version */ CNetChunk Packet; mem_zero(&Packet, sizeof(Packet)); Packet.m_Address.type = m_pNetClient->NetType()|NETTYPE_LINK_BROADCAST; Packet.m_ClientID = -1; Packet.m_Flags = NETSENDFLAG_CONNLESS; Packet.m_DataSize = Packer.Size(); Packet.m_pData = Packer.Data(); m_BroadcastTime = time_get(); for(int i = 8303; i <= 8310; i++) { Packet.m_Address.port = i; m_pNetClient->Send(&Packet); } if(g_Config.m_Debug) m_pConsole->Print(IConsole::OUTPUT_LEVEL_DEBUG, "client_srvbrowse", "broadcasting for servers"); } if(RefreshFlags&IServerBrowser::REFRESHFLAG_INTERNET) { // clear out everything for(CServerEntry *pEntry = m_pFirstReqServer; pEntry; pEntry = pEntry->m_pNextReq) { m_pNetClient->PurgeStoredPacket(pEntry->m_TrackID); } m_aServerlist[IServerBrowser::TYPE_INTERNET].Clear(); if(m_ActServerlistType == IServerBrowser::TYPE_INTERNET) m_ServerBrowserFilter.Clear(); m_pFirstReqServer = 0; m_pLastReqServer = 0; m_NumRequests = 0; m_NeedRefresh = 1; for(int i = 0; i < m_ServerBrowserFavorites.m_NumFavoriteServers; i++) if(m_ServerBrowserFavorites.m_aFavoriteServers[i].m_State >= CServerBrowserFavorites::FAVSTATE_ADDR) Set(m_ServerBrowserFavorites.m_aFavoriteServers[i].m_Addr, SET_FAV_ADD, -1, 0); } }
// manipulate entries CServerEntry *CServerBrowser::Add(int ServerlistType, const NETADDR &Addr) { // create new pEntry CServerEntry *pEntry = (CServerEntry *)m_aServerlist[ServerlistType].m_ServerlistHeap.Allocate(sizeof(CServerEntry)); mem_zero(pEntry, sizeof(CServerEntry)); // set the info pEntry->m_Addr = Addr; pEntry->m_InfoState = CServerEntry::STATE_INVALID; pEntry->m_CurrentToken = GetNewToken(); pEntry->m_Info.m_NetAddr = Addr; pEntry->m_Info.m_Latency = 999; net_addr_str(&Addr, pEntry->m_Info.m_aAddress, sizeof(pEntry->m_Info.m_aAddress), true); str_copy(pEntry->m_Info.m_aName, pEntry->m_Info.m_aAddress, sizeof(pEntry->m_Info.m_aName)); str_copy(pEntry->m_Info.m_aHostname, pEntry->m_Info.m_aAddress, sizeof(pEntry->m_Info.m_aHostname)); // check if it's a favorite if(m_ServerBrowserFavorites.FindFavoriteByAddr(Addr, 0)) pEntry->m_Info.m_Favorite = 1; // add to the hash list int Hash = AddrHash(&Addr); pEntry->m_pNextIp = m_aServerlist[ServerlistType].m_aServerlistIp[Hash]; m_aServerlist[ServerlistType].m_aServerlistIp[Hash] = pEntry; if(m_aServerlist[ServerlistType].m_NumServers == m_aServerlist[ServerlistType].m_NumServerCapacity) { if(m_aServerlist[ServerlistType].m_NumServerCapacity == 0) { // alloc start size m_aServerlist[ServerlistType].m_NumServerCapacity = 1000; m_aServerlist[ServerlistType].m_ppServerlist = (CServerEntry **)mem_alloc(m_aServerlist[ServerlistType].m_NumServerCapacity*sizeof(CServerEntry*), 1); } else { // increase size m_aServerlist[ServerlistType].m_NumServerCapacity += 100; CServerEntry **ppNewlist = (CServerEntry **)mem_alloc(m_aServerlist[ServerlistType].m_NumServerCapacity*sizeof(CServerEntry*), 1); mem_copy(ppNewlist, m_aServerlist[ServerlistType].m_ppServerlist, m_aServerlist[ServerlistType].m_NumServers*sizeof(CServerEntry*)); mem_free(m_aServerlist[ServerlistType].m_ppServerlist); m_aServerlist[ServerlistType].m_ppServerlist = ppNewlist; } } // add to list m_aServerlist[ServerlistType].m_ppServerlist[m_aServerlist[ServerlistType].m_NumServers] = pEntry; pEntry->m_Info.m_ServerIndex = m_aServerlist[ServerlistType].m_NumServers; m_aServerlist[ServerlistType].m_NumServers++; return pEntry; }
int GetToken( char *pcToken) /******************************************************************************/ /* get a token from the input file. Return a put back token if available */ /******************************************************************************/ { int iResult; if (bPutBackTokenValid == TRUE) { strcpy((char *) pcToken, (char *) pcPutBackToken); bPutBackTokenValid = FALSE; iResult = iPutBackTokenType; } else { iResult = GetNewToken(pcToken); while (iResult == PRECOMPILER_TOKEN) { iResult = PreProcess(pcToken); if (iResult == NO_SCANNER_ERROR) { iResult = GetNewToken(pcToken); } } if (iResult == IDENTIFIER_TOKEN) { iResult = EvaluateMacroList(pcToken); } iPutBackTokenType = iResult; } strcpy((char *) pcPutBackToken, (char *) pcToken); return iResult; }
int PreProcessDefine( char *pcToken) { int iResult; int iTokenType; char *pcExpansion; pcExpansion = malloc(TOKENSIZE); if (pcExpansion == NULL) { iResult = OUT_OF_MEMORY; } else { iTokenType = GetNewToken(pcToken); if (iTokenType != IDENTIFIER_TOKEN) { /* Identifier expected but not found. Skip next token as an attempt to */ /* keep in sync. */ iResult = GetNewToken(pcToken); /* hide last read token, and report the error */ *pcToken = 0; iResult = BAD_MACRO_DEFINITION; } else { iTokenType = GetNewToken(pcExpansion); AddOrReplaceDefine(pcToken, pcExpansion, iTokenType); iResult = NO_SCANNER_ERROR; } free(pcExpansion); } return iResult; }
int PreProcessInclude( char *pcToken) { int16 iResult; char *pcDrive; char *pcDir; char *pcNewDrive; char *pcNewDir; char *pcNewFilename; char *pcNewExtension; char *pcNewFullFilename; iResult = GetNewToken(pcToken); if (iResult == STRING_TOKEN) { /* switch to the included file. First add a record in the IncludedBy */ /* list, fill this record with the current filename, and position of the */ /* read pointer. Then close the current file and open the include file. */ /* Continue with the newly opened file. */ iResult = AddIncludeRecord(); if (iResult == NO_SCANNER_ERROR) { fclose(InputStream); /* Now do some difficult things to retrieve the current directory, */ /* which is the directory in which the current file is found. */ /* This is to prevent errors when the include file is in the same */ /* directory as the main file; the path to the main file is absolute */ /* and the path to the include file is relative. When the current */ /* directory of the OS is another directory, the wrong directory is */ /* searched for the include file. */ pcDrive = (char *) malloc(_MAX_DRIVE+1); pcDir = (char *) malloc(_MAX_DIR+1); pcNewDrive = (char *) malloc(_MAX_DRIVE+1); pcNewDir = (char *) malloc(_MAX_DIR+1); pcNewFilename = (char *) malloc(_MAX_FNAME+1); pcNewExtension = (char *) malloc(_MAX_EXT+1); splitpath(pcCurrentFileName, pcDrive, pcDir, NULL, NULL); splitpath(pcToken, pcNewDrive, pcNewDir, pcNewFilename, pcNewExtension); /* Both the current filename and the include file name are split into */ /* interesting parts now. */ /* When the include file has no specified drive, use the drive of the */ /* current file. */ if (*pcNewDir == 0) { free(pcNewDir); pcNewDir = pcDir; } else { free(pcDir); } /* When the include file has no specified directory, use the directory */ /* of the current file. */ if (*pcNewDrive == 0) { free(pcNewDrive); pcNewDrive = pcDrive; } else { free(pcDrive); } /* Now merge the four components into a full file path. When parts are */ /* not known, their string will be empty. */ pcNewFullFilename = malloc(_MAX_DRIVE + _MAX_DIR + _MAX_FNAME + _MAX_EXT); strcpy(pcNewFullFilename, pcNewDrive); strcat(pcNewFullFilename, pcNewDir); strcat(pcNewFullFilename, pcNewFilename); strcat(pcNewFullFilename, pcNewExtension); InputStream = fopen(pcNewFullFilename, "r"); free(pcNewDrive); free(pcNewDir); free(pcNewFilename); free(pcNewExtension); if (InputStream == NULL) { /* failed. Report the problem. Requesting the next token will result */ /* in detecting the current file to be closed. Because of that, the */ /* list of IncludeRecords will be consulted, and the last closed */ /* file will be re-opened. */ iResult = INCLUDE_FILE_PROBLEM; } else { /* Succeeded. Update the administration for the new file */ strcpy(pcCurrentFileName, pcNewFullFilename); iCurrentLineNumber = 1; } free(pcNewFullFilename); } else { /* internal error, or out of memory problem. There is no need to */ /* assign an error-code to iResult, because this has been done */ /* already. */ } } else { iResult = BAD_MACRO_DEFINITION; } return iResult; }