# 어휘분석

## Lexical Analyzer

*

## Scanner Source Code (Lexical Analyzer)

```c
#include <stdio.h>

int isAlpha(char x){
    if(x >= 'A' && x <= 'Z') return 1;
    if(x >= 'a' && x <= 'z') return 1;
    return 0;
}

int isUnderbar(char x){
    if(x == '_') return 1;
    return 0;
}

int isNumber(char x){
    if(x >= '0' && x <= '9') return 1;
    return 0;
}

int main(){
    char string[100];
    int i = 0;

    printf("식을 입력하세요: ");
    fgets(string, 100, stdin);
    
    printf("수식: %s\n", string);

    for(int i = 0; string[i]; i++) {
        char tmp = string[i];

        if( isAlpha(tmp) || isUnderbar(tmp)) {
                printf("식별자: %c",tmp);
                
                while(1){
                    i++;
                    char tmp2 = string[i];
                    if(isAlpha(tmp2) || isUnderbar(tmp2) || isNumber(tmp2)) {
                        printf("%c", tmp2);
                    }
                    else
                        break;
                }
                printf("\n");
                printf("token num : 2\n");
            }
        if( isNumber(tmp) ) {
            printf("상수 : %c", tmp);

            while(1) {
                i++;
                char tmp2 = string[i];
                if(!isNumber(tmp2))
                    break;
                printf("%c",tmp2);
            }
            printf("\n");
            printf("token num : 4\n");
        }

        if(tmp == '+') {
            printf("연산자: +\n");
            printf("token num :6\n");
        }

        if(tmp == '-') {
            printf("연산자: -\n");
            printf("token num :7\n");
        }
        
        if(tmp == '*') {
            printf("연산자: *\n");
            printf("token num :8\n");
        }
        
        if(tmp == '/') {
            printf("연산자: /\n");
            printf("token num :9\n");
        }
        
        if(tmp == '=') {
            printf("연산자: =\n");
            printf("token num :10\n");
        }

        if(tmp == ';') {
            printf("구분자: ;\n");
            printf("token num :12\n");
        }

        if(tmp == '(') {
            printf("구분자: (\n");
            printf("token num :13\n");
        }
        if(tmp == ')') {
            printf("구분자: )\n");
            printf("token num :14\n");
        }
    }
}
```

### Run

![scanning \`ni = ba \* po - 60 + ni / (ab12c\_ddd +40);\`](https://2654940563-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MOnbA8vSHqWi_tWvoTv%2F-MP5DvlIBIG0l2cM4uqm%2F-MP5E1zV1EKTrFzJYZUA%2Fimage.png?alt=media\&token=770c253e-d90f-4f96-a8be-be47fe7ff0e8)

Lexical Analyzer(Scanner) gets source program as an input, and make `token stream` as an output for `syntax analyzer`
