Ejemplo n.º 1
0
void trieInsert(trieNodePointer root, char *token, char *inputFilePath)
{
	trieNodePointer currentNode = root;

	for (int i = 0; i < strlen(token); i++)
	{
		if (currentNode->children[parseChar(token[i])] == NULL)
		{
			currentNode->children[parseChar(token[i])] = createNewTrieNode();
			currentNode = currentNode->children[parseChar(token[i])];
		}
		else
		{
			currentNode = currentNode->children[parseChar(token[i])];
		}
		if (i == (strlen(token) - 1))
		{
			if (currentNode->token == NULL)
			{
				currentNode->token = (char *)malloc(strlen(token)+1);
				strcpy(currentNode->token, token);
			}
			if (currentNode->files == NULL)
			{
				currentNode->files = createNewFileNode(inputFilePath);
			}
			else
			{
				bool newNode = true;
				fileNodePointer tempFiles = currentNode->files;
				while (tempFiles->next != NULL)
				{
					if (strcmp(tempFiles->fileName, inputFilePath) == 0)
					{
						tempFiles->count++;
						newNode = false;
						break;
					}
					tempFiles = tempFiles->next;
				}
				if (strcmp(tempFiles->fileName, inputFilePath) == 0)
				{
					tempFiles->count++;
				}
				else if (newNode == true)
				{
					tempFiles->next = createNewFileNode(inputFilePath);
				}
			}
		}
	}
}
Ejemplo n.º 2
0
int main(void) {

    SudokuArray instance;
    std::vector<int> unknownEntries;

    std::cout << "Type the sudoku (use '.' for unknown entries):" << std::endl;

    // Read from stdin
    int counter = 0;
    char c;
    while (counter < SIZE * SIZE && std::cin >> c) {
        int val = parseChar(c);
        if (val > -1) {
            instance[counter] = val;
            if (val == 0) unknownEntries.push_back(counter);
            counter++;
        }
    }

    if (counter != SIZE * SIZE) {
        std::cerr << "Error reading input data" << std::endl;
        exit(-1);
    }

    cspsolve::solve(instance, unknownEntries.begin(), unknownEntries.end(), valid, firstValue, nextValue, output);

    return 0;

}
Ejemplo n.º 3
0
// Scans a line of tokens using yylex
token * BlarbVM_scanLine(BlarbVM *vm) {
    BlarbVM_WORD tokenCount = 0;
    // 1024 tokens *should* be way more than enough.
    token *line = malloc(sizeof(token) * 1024);
    token_t tokenType;

    int label_call_present = 0;
    int newline_present = 0;

    while ((tokenType = yylex())) {
        token *t = &line[tokenCount++];
        // In general, we just care about the type.
        t->type = tokenType;

        if (tokenType == NEWLINE) {
            newline_present = 1;
            break;
        // No tokens (except newlines) are allowed after a function call
        } else if (label_call_present) {
            fprintf(stderr, "Parse error: "
                    "No tokens are allowed after calls to labels.\n"
                    "Error on line %d in %s\n",
                    yylineno, yyfilename);
            exit(1);
        } else if (tokenType == INTEGER) {
            t->val = strtoull(yytext, 0, 10);
        } else if (tokenType == LABEL_CALL) {
            addStringToToken(t, yytext, strlen(yytext));
            label_call_present = 1;
        } else if (tokenType == LABEL) {
            // Exclude the hash
            addStringToToken(t, yytext + 1, strlen(yytext) - 1);
            BlarbVM_addLabelPointer(vm, t->str, vm->lineCount);
        } else if (tokenType == STR) {
            addStringLiteralToToken(t, yytext);
        } else if (tokenType == CHR) {
            t->type = INTEGER;
            char *s = yytext + 1;
            t->val = parseChar(&s);
        }
    }

    // If this is the EOF (no more tokens)
    if (tokenCount == 0) {
        free(line);
        return NULL;
    }

    // In case we hit an EOF on a valid line (we use newlines for terminators)
    if ( ! newline_present) {
        token *t = &line[tokenCount++];
        t->type = NEWLINE;
    }

    line = BlarbVM_optimizeLine(line, &tokenCount);

    // Resize the memory chunk so we don't waste memory on small lines
    return realloc(line, sizeof(token) * tokenCount);
}
Ejemplo n.º 4
0
bool ControlSeqParser::next() {
	if (!m_seq) return false;
	m_token = CS_UNKNOWN;

	while (nextChar()) {
		if (parseChar()) return true;
	}
	return false;
}
Ejemplo n.º 5
0
void addStringLiteralToToken(token *t, char *str) {
    str++; // Cut out first double quote
    *(strrchr(str, '\"')) = 0; // Cut out second double quote

    t->str = malloc(strlen(str) + 1);

    int i;
    char *itr;
    for (i = 0, itr = str; *itr; i++) {
        t->str[i] = parseChar(&itr);
    }
    t->str[i] = '\0';
}
Ejemplo n.º 6
0
PARSENODE_PTR SQLParser::parseLiteral() {
    if (nowReading == INTEGER) {
        return parseInt();
    }
    else if (nowReading == CHAR) {
        return parseChar();
    }
    else if (nowReading == FLOAT) {
        return parseFloat();
    }
    else {
        syntaxError(nowReading, "expect literal!");
        return nullptr;
    }
}
Ejemplo n.º 7
0
char Node::ephemerisType() const
{
    if (!m_ephemerisType)
    {
        m_ephemerisType = new char('\0');
        if (m_line2)
        {
            ErrorCode error = NoError;
            *m_ephemerisType = parseChar(m_line2, 62, error);
            if (error != NoError)
            {
                m_lastError = error;
                *m_ephemerisType = '\0';
            }
        }
    }

    return *m_ephemerisType;
}
Ejemplo n.º 8
0
char Node::classification() const
{
    if (!m_classification)
    {
        m_classification = new char('\0');
        if (m_line2)
        {
            ErrorCode error = NoError;
            *m_classification = parseChar(m_line2, 7, error);
            if (error != NoError)
            {
                m_lastError = error;
                *m_classification = '\0';
            }
        }
    }

    return *m_classification;
}
Ejemplo n.º 9
0
void UploadService()
{
    vTimerStart(UPLOAD_TIMEOUT);
    quit = 0;

    resetVar();
    while(!quit)
    {
        if(vTimerTimeOut()) break; //超时跳出

        if(!u8UARTReceive(&ch)) continue; //没有收到数据

        if(!parseChar(ch)) continue; //不是协议包

        pkt = readPacket(&pktLen); //读出一条数据
        if(parseUpload(pkt,pktLen)) //正确的升级数据
        {
            //只要收到正确的升级数据就复位计数器
            vTimerStart(UPLOAD_TIMEOUT);
        }

    }
}
Ejemplo n.º 10
0
void
Board::parseLine(std::string line, int & index)
{
    unsigned int i = 0;
    while (i < line.length()) {
        char c = line.at(i);
        parseChar(c, index);
        index++;
        i++;

        // discard the whitespaces
        while (i < line.length() && (line.at(i) == ' '))
            i++;
    }

    // Error checking
    if (index % DIMENSION != 0) {
        std::cerr << "Error in input file, line:\n";
        std::cerr << "   " << line << "\n";
        std::cerr << "Index = " << index << ", should be a multiple of " << DIMENSION << "\n";
        exit(EXIT_FAILURE);
    }
}
Ejemplo n.º 11
0
bool ControlSeqParser::parseChar() {
	switch (m_currentChar) {
	case 0x00: // ^@ NUL
	case 0x01: // ^A SOH
	case 0x02: // ^B STX
	case 0x03: // ^C ETX
	case 0x04: // ^D EOT
		return false; /* ignore */
	case 0x05: // ^E ENQ
		return true; // return character
	case 0x06: // ^F ACK
		return false; /* ignore */
	case 0x07: // ^G BEL
		if (m_state != ST_OSC && m_state != ST_OSC_ESC)
		{
			m_token = CS_ASCII_BEL;
			return true;
		}
		break; // terminator for OSC
	case 0x08: // ^H BS \b backspace
		m_token = CS_ASCII_BS;
		return true;
	case 0x09: // ^I HT \t
		m_token = CS_ASCII_TAB;
		return true;
	case 0x0A: // ^J LF \n
		m_token = CS_ASCII_LF;
		return true;
	case 0x0B: // ^K VT \v
		m_token = CS_ASCII_VT;
		return true;
	case 0x0C: // ^L FF \f
		m_token = CS_ASCII_FF;
		return true;
	case 0x0D: // ^M CR \r
		m_token = CS_ASCII_CR;
		return true; // return character
	case 0x0E: // ^N SO (shift out)
		m_token = CS_CHARSET_USE_G1;
		return true;
	case 0x0F: // ^O SI (shift in)
		m_token = CS_CHARSET_USE_G0;
		return true;
	case 0x10: // ^P DLE
	case 0x11: // ^Q DC1
	case 0x12: // ^R DC2
	case 0x13: // ^S DC3
	case 0x14: // ^T DC4
	case 0x15: // ^U NAK
	case 0x16: // ^B SYN
	case 0x17: // ^W ETB
		return false;  /* ignore */
	case 0x18: // ^X CAN
		m_state = ST_START;
		return false;
	case 0x19: // ^Y EM
		return false;  /* ignore */
	case 0x1A: // ^Z SUB
		m_state = ST_START;
		m_currentChar = 0x2592;
		return true;
	case 0x1B: // ^[ ESC
		m_state = ST_START;
		break;
	case 0x1C: // ^\\ FS
	case 0x1D: // ^] GS
	case 0x1E: // ^^ RS
	case 0x1F: // ^_ US
	case 0x7F: // DEL
		return false;  /* ignore */
	}
	switch (m_state) {
	case ST_START:
		if (ESC_CHAR == m_currentChar) {
			m_state = ST_ESCAPE;
			m_prefixlen = 0;
			m_numValues = 0;
			m_suffix = 0;
			return false;
		}
		if (MODE_7BIT != m_mode && m_currentChar >= 0x80 && m_currentChar < 0xA0) {
			m_state = ST_ESCAPE;
			m_prefixlen = 0;
			m_numValues = 0;
			m_suffix = 0;
			m_currentChar = m_currentChar - 0x40;
			return parseChar();
		}
		return true;
	case ST_ESCAPE:
		if (m_currentChar >= 0x100) {
			/* cancel sequence, ignore previous part */
			m_state = ST_START;
			return true;
		} else if ('[' == m_currentChar) {
			m_state = ST_CSI;
			return false;
		} else if (']' == m_currentChar) {
			m_state = ST_OSC;
			return false;
		} else if (m_vt52 && 'Y' == m_currentChar) {
			m_state = ST_VT52_ESCY;
			return false;
		} else {
			m_state = ST_ESCAPE_TRIE;
			return parseChar();
		}
		break;
	case ST_CSI:
		if (m_currentChar >= 0x3C && m_currentChar <= 0x3F) {
			/* "private" extension */
			if (m_prefixlen >= 1) {
				m_state = ST_CSI_INVALID;
			} else {
				m_prefix[m_prefixlen++] = m_currentChar;
			}
		} else if ((m_currentChar >= 0x30 && m_currentChar <= 0x39) || 0x3B == m_currentChar) {
			/* digit */
			m_state = ST_CSI_VALUES;
			parseCSIValue();
			return false;
		} else if (m_currentChar >= 0x20 && m_currentChar <= 0x2F) {
			/* intermediate bytes */
			m_state = ST_CSI_INVALID;
		} else if (m_currentChar >= 0x40 && m_currentChar <= 0x7E) {
			/* final char */
			return matchCSI();
		} else {
			m_state = ST_CSI_INVALID;
		}
		return false;
	case ST_CSI_VALUES:
		if ((m_currentChar >= 0x30 && m_currentChar <= 0x39) || 0x3B == m_currentChar) {
			/* digit */
			parseCSIValue();
		} else if (m_currentChar >= 0x20 && m_currentChar <= 0x2F) {
			/* intermediate bytes */
			m_suffix = m_currentChar;
			m_state = ST_CSI_SUFFIX;
		} else if (m_currentChar >= 0x40 && m_currentChar <= 0x7E) {
			/* final char */
			return matchCSI();
		} else {
			m_state = ST_CSI_INVALID;
		}
		return false;
	case ST_CSI_SUFFIX:
		if (m_currentChar >= 0x40 && m_currentChar <= 0x7E) {
			/* final char */
			return matchCSI();
		}
		return false;
	case ST_CSI_INVALID:
		if (m_currentChar >= 0x40 && m_currentChar <= 0x7E) {
			/* final char */
			m_state = ST_START;
		}
		return false;
	case ST_OSC:
		if (m_numValues < 2) m_oscString.clear();
		if (m_currentChar == 0x98 || m_currentChar == 0x9C || m_currentChar == 0x07) {
			m_state = ST_START;
			if (2 == m_numValues) {
				m_token = CS_OSC;
				m_numValues = 1;
				return true;
			}
		} else if (ESC_CHAR == m_currentChar) {
			m_state = ST_OSC_ESC;
		} else {
			if (m_numValues < 2) {
				/* no semicolon yet */
				if ((m_currentChar >= 0x30 && m_currentChar <= 0x39) || 0x3B == m_currentChar) {
					/* digit */
					parseCSIValue();
				} else {
					m_numValues = 3; /* "invalid" marker */
				}
			} else {
				appendUtf8Char(m_oscString, m_currentChar);
			}
		}
		return false;
	case ST_OSC_ESC:
		if (m_currentChar == 0x98 || m_currentChar == 0x9C || m_currentChar == 0x07 || m_currentChar == 'X' || m_currentChar == '\\') {
			m_state = ST_START;
			if (2 == m_numValues) {
				m_token = CS_OSC;
				m_numValues = 1;
				return true;
			}
		} else {
			/* throw away old state, start with new escape seq */
			m_state = ST_ESCAPE;
			m_prefixlen = 0;
			m_numValues = 0;
			return parseChar();
		}
		return false;
	case ST_ESCAPE_TRIE:
		if (m_currentChar >= 0x100) {
			/* cancel sequence, ignore previous part */
			m_state = ST_START;
			return true;
		}
		m_prefix[m_prefixlen++] = m_currentChar;
		return tryFixedEscape();
	case ST_VT52_ESCY:
		if (m_currentChar < 0x20) {
			/* cancel sequence, ignore previous part */
			m_state = ST_START;
			return true;
		}
		m_values[m_numValues++] = m_currentChar - 0x1F;
		if (2 == m_numValues) {
			m_state = ST_START;
			m_token = CS_VT52_CURSOR_POSITION;
			return true;
		}
		return false;
	default:
		m_state = ST_START;
		return true;
	}
}