The TINY Language A sequence of statements no

  • Slides: 12
Download presentation

The TINY Language • • • A sequence of statements no procedures, no declarations

The TINY Language • • • A sequence of statements no procedures, no declarations integer variable only if-statement (with an optional else part) (must be terminated by end) repeat-statement read/write statement comments: { … } - nesting is not allowed integer arithmetic expressions : +, -, *, / (integer division) Boolean arithmetic expressions : <, = – no Boolean variables, assignment, or I/O 3

Sample program in TINY read x; {input an integer} if x > 0 then

Sample program in TINY read x; {input an integer} if x > 0 then {don’t compute if x <= 0 } fact : = 1; repeat fact : = fact * x; x : = x - 1; until x = 0; write fact {output factorial of x } end 4

TINY의 Scanner • TINY 토큰 – 예약어: if, then, else, end, repeat, until, read,

TINY의 Scanner • TINY 토큰 – 예약어: if, then, else, end, repeat, until, read, write – 특수심볼: + - * / = < ( ) ; “: =” – 기타심볼: number (one or more digits) identifier (one or more letters) • Some lexical convention – – comments: { } nesting is not allowed free format white space: blank, tab, newline the principle of longest substring

TINY 토큰 인식을 위한 DFA digit in_num digit white space letter start letter :

TINY 토큰 인식을 위한 DFA digit in_num digit white space letter start letter : { } in_comment other [other] in_id digit in_assign finish = [other] +-*/=<(); other ERROR • 예약어 식별? 7

TINY Scanner의 구현 • Appendix B 참조 • scan. h (줄 550 -571) +

TINY Scanner의 구현 • Appendix B 참조 • scan. h (줄 550 -571) + scan. c (줄 600 -793) • 토큰의 정의 - globals. h (줄 174 -186) • get. Token 루틴 – consumes input characters – returns next token • 테스트 – 샘플 TINY 코드 (Fig. 2. 11) – 실행 결과(Fig. 2. 12)

예제: get. Token - TINY ID 식별 코드 state = start; advance(input); while (state

예제: get. Token - TINY ID 식별 코드 state = start; advance(input); while (state != finish && state != error) switch (state) { case start: if (isalpha(input)) { advance(input); state = in_id; } else state = error; break; case in_id: if (!isalpha(input)) state = finish; else advance(input); break; default: break; } } if (state == finish) return ID; else return ERROR; 11