๐ 2021-1 Compiler
์๊ตฌ ์ฌํญ
DFA๋ฅผ Hardwired method์ Table-driven method๋ก ๊ตฌํํ๊ณ ๋ค์ 2๊ฐ์ง ์ํฉ์ ํ ์คํธ ํ๋ผ.
์๊ตฌ ์ฌํญ
์ ์ํ special form token๊ณผ general form token์ด ํฌํจ๋ sample program์ ์์ฑํ์ฌ ๊ตฌํํ ์ดํ ๋ถ์๊ธฐ๋ฅผ ์คํํ๊ธฐ- Sample program์ negative example๋ ํฌํจํ๋๋ก ํ์ฌ error๋ฅผ ์ถ๋ ฅํ๋ ๊ฒ์ ํ์ธํ ์ ์์ด์ผ ํจ
- ๋ฐ๋ชจ๋ฅผ ํตํด token์ด ์ฌ๋ฐ๋ฅด๊ฒ ์ธ์๋์์์ ์ฝ๊ฒ ํ์ธํ ์ ์๋๋ก ํ๊ณ , ์ธ์ ๊ณผ์ ์์ ์์ฑํ symbol table๊ณผ literal table์ ๋ณด์ฌ์ฃผ์ด์ผ ํจ
- ์ฒ๋ฆฌ ํญ๋ชฉ - Bold: required, Italic: optional
- Special form tokens
- Keywords
- Special symbols
- General form tokens
- Identifier
- Literal/Constants
- Number
- Decimal
- Octal
- Hexdecimal
- String
์คํ ๋ฐฉ๋ฒ ๋ฐ ํ๋ฉด
- How to run this program
LexicalAnalyzer.exe <input-file> <output-file>
- Sample program: input-file.py
def foo(count): res = 0 for i in range(1, count + 1): res += i print("{} times completed".format(count)) return res # This is comment. โ Because # is not defined, it will cause error! foo(5)
- Analysis result: output-file.txt
==========================[ Token Table ]========================== (21, -) (Token.SPACE, -) (Token.ID, 1) (Token.LPAREN, -) (Token.ID, 2) (Token.RPAREN, -) (Token.COLON, -) (Token.NEWLINE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.ID, 3) (Token.SPACE, -) (Token.ASSIGNMENT2, -) (Token.SPACE, -) (Token.ZERO, -)(Token.NEWLINE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (17, -) (Token.SPACE, -) (Token.ID, 4) (Token.SPACE, -) (8, -) (Token.SPACE, -)(Token.ID, 5) (Token.LPAREN, -) (Token.DECIMAL, -) (Token.COMMA, -) (Token.SPACE, -) (Token.ID, 2) (Token.SPACE, -) (Token.PLUS, -) (Token.SPACE, -)(Token.DECIMAL, -) (Token.RPAREN, -) (Token.COLON, -) (Token.NEWLINE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.ID, 3) (Token.SPACE, -) (Token.ADD_ASSIGNMENT, -) (Token.SPACE, -) (Token.ID, 4) (Token.NEWLINE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.ID, 6) (Token.LPAREN, -) (Token.STRING2, -) (Token.PERIOD, -) (Token.ID, 7) (Token.LPAREN, -) (Token.ID, 2) (Token.RPAREN, -) (Token.RPAREN, -) (Token.NEWLINE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (Token.SPACE, -) (14, -) (Token.SPACE, -) (Token.ID, 3) (Token.NEWLINE, -) (Token.NEWLINE, -) !!! Error occurred because of the symbol # !!! ==========================[ Symbol Table ]========================= (1) foo (2) count (3) res (4) i (5) range (6) print (7) format =========================[ Literal Table ]========================= (1) 0 (2) 1 (3) "{} times completed"
- Run Lexical Analyzer in command line