예제 #1
0
파일: util.c 프로젝트: GD14/richman
int find_int(char *str)
{
	while(*str!='\0')
	{
		if(is_digtial(*str)) break;
		str++;
	}
	return str2int(str);
}
예제 #2
0
파일: util.c 프로젝트: GD14/richman
int str2int(const char *str)
{
	int ret=0;
	while((*str)!='\0' )
	{
		if(!is_digtial(*str)) return ret;
		ret=ret*10+*str-'0';
		str++;
	}
	return ret;
}
예제 #3
0
파일: scanner.c 프로젝트: pexcn/mini-c
void Scanner()
{
    int word_pos, num_value, max_word_len;
    char word[100], word_temp[100], input[MAXLEN];
    char *p;

    InitList();
    while( fgets(input,100,fpIn) )
    {
        readRow++;
        strcpy( word, input );
        p = word;
        while( *p != '\n' )
        {
            word_pos = 0;
            while( *p == ' ' || *p == 9 )
                p++;
            if( *p == '\n' )// bug  test file : while( a < 10 {
                break;
            if( isalpha(*p) || *p == '_' )
            {
                CLR(word_temp);
                max_word_len = 0;
                while( isalpha(*p) || is_digtial(*p) || *p == '_' )
                {
                    word_temp[word_pos++] = *p;
                    p++ , max_word_len++;
                }
                if( max_word_len >= 20 )
                {
                    fprintf( fpOut, "word's max len error");
                    exit(0);
                }
                word_temp[word_pos] = '\0';
                if( *(p) == '[' )
                {
                    InsertList( SYN_ARRAY, word_temp );
                }
                else
                    InsertList( IsKeyword(word_temp), word_temp );
            }
            else if( *p >= '0' && *p <= '9' )
            {
                CLR(word_temp);
                num_value = 0;
                while( (*p>='0' && *p<='9') )
                {
                    num_value = num_value * 10 + *p-'0';
                    p++;
                }
                if( isalpha(*p) )
                {
                    fprintf( fpOut, "word wrong can't not begin with number" );
                    exit(0);
                    return ;
                }
                if( num_value >= 32767 )
                {
                    fprintf( fpOut, "int too big" );
                    exit(0);
                    return ;
                }
                else
                {
                    itoa(num_value,word_temp,10);
                    InsertList( SYN_NUM, word_temp );
                }
            }
            else
            { 
                switch( *p )
                {
                    case '=':
                        if( *(p+1) == '=' )
                        {
                            InsertList( SYN_EQ, "==" );
                            p++;
                        }
                        else
                            InsertList( SYN_ASSIGN, "=" );
                        break;
                    case '+':
                        InsertList( SYN_PLUS, "+" );
                        break;
                    case '-':
                        InsertList( SYN_MINUS, "-" );
                        break;
                    case '*':
                        InsertList( SYN_MUL, "*" );
                        break;
                    case '/':
                        InsertList( SYN_DIV, "/" );
                        break;
                    case '<':
                        if( *(p+1) == '=' )
                        {
                            InsertList( SYN_LE, "<=" );
                            p++;
                        }
                        else
                            InsertList( SYN_LT, "<" );
                        break;
                    case '>':
                        if( *(p+1) == '=' )
                        {
                            InsertList( SYN_ME, ">=" );
                            p++;
                        }
                        else
                            InsertList( SYN_LG, ">" );
                        break;
                    case '!':
                        InsertList( SYN_NE, "!=" );
                        break;
                    case ',':
                        InsertList( SYN_COMMA, "," );
                        break;
                    case ':':
                        InsertList( SYN_COLON, ":" );
                        break;
                    case ';':
                        InsertList( SYN_SEMICOLON, ";" );
                        break;
                    case '(':
                        InsertList( SYN_LPAREN, "(" );
                        break;
                    case ')':
                        InsertList( SYN_RPAREN, ")" );
                        break;
                    case '{':
                        InsertList( SYN_LEFTBRACKET2, "{" );
                        break;
                    case '}':
                        InsertList( SYN_RIGHTBRACKET2, "}" );
                        break;
                    case '[':
                        InsertList( SYN_LEFTBRACKET1, "[" );
                        break;
                    case ']':
                        InsertList( SYN_RIGHTBRACKET1, "]" );
                        break;
                    default:
                        return ;
                        break;
                }
                p++;
            }
        }
        }
    }