일지

인터프리터...11

niamdank 2020. 11. 3. 22:13

어휘 분석 프로그램 예제

다음의 키워드만을 지원하는 어휘 분석 프로그램을 만든다.


if else print ( ) + - * / == != < <= > >= = , "


먼저, 토큰의 종류와 토큰을 표현할 수 있는 구조체를 작성한다.

 

Tokenizer.h

#pragma region 토큰 설정
enum class TokenKind : char
{
    LeftParenthesis = '(', RightParenthesis = ')',
    Plus = '+', Minus = '-', Multiply = '*', Divide = '/',

    Assign = 1, Comma, DoubleQuotes,
    Equal, NotEqual, Less, LessEqual, Grater, GraterEqual,
    If, Else, End, Print, Identifier, Int, String, Letter, Digit,
    EndOfList, Others,
    MAX = CHAR_MAX
};

struct Token
{
    Token()
        :tokenKind(TokenKind::Others), text(""), intValue(0) {}
    Token(TokenKind tokenKind, const string& text, int intValue)
        :tokenKind(tokenKind), text(text), intValue(intValue) {}

    TokenKind tokenKind;
    string text;
    int intValue;
};
#pragma endregion