Chapter 3 Syntactic sugar causes cancer of the

















![Clite 어휘 구문 부류 정규식 정의 • any. Char [ -~] • Letter [a-z. Clite 어휘 구문 부류 정규식 정의 • any. Char [ -~] • Letter [a-z.](https://slidetodoc.com/presentation_image_h/034e710af5e40221ef2dfa1b0ff05318/image-18.jpg)




















































- Slides: 70
프로그래밍언어론 Chapter 3 어휘 및 구문 분석 Syntactic sugar causes cancer of the semicolon. 구문설탕은 세미콜론 암을 일으킨다 A. Perlis Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
목차 Contents 3. 1 촘스키 분류 Chomsky Hierarchy 3. 2 어휘 분석 Lexical Analysis 3. 3 구문 분석 Syntactic Analysis Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
3. 1 촘스키 분류 • 정규 문법 Regular grammar -- least powerful • 문맥자유 문법 Context-free grammar (BNF) • 문맥민감 문법 Context-sensitive grammar • 무제한 문법 Unrestricted grammar Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
정규 문법 Regular Grammar • 가장 간단하고 가장 표현력이 약하다 • 다음과 동일한 표현력을 가진다: – 정규식 Regular expression – 유한상태 오토마타 Finite-state automaton • 오른쪽 정규 문법 Right regular grammar: (여기서 T*, B N) A→ B A→ Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
예제 • Integer → 0 Integer | 1 Integer |. . . | 9 Integer | 0 | 1 |. . . | 9 Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
무제한 문법 Unrestricted Grammar • 동일한 능력(표현력, 계산능력)을 가는 것: – Turing machine – von Neumann machine – C++, Java • 모든 계산가능 함수를 계산할 수 있다. Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
프로그래밍언어론 Chapter 3 어휘 및 구문 분석 Syntactic sugar causes cancer of the semicolon. A. Perlis Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
목차 3. 1 Chomsky Hierarchy 3. 2 Lexical Analysis 3. 3 Syntactic Analysis Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
다른 문자열 • 여백: 공백, 탭 문자 • 주석 // any-char* end-of-line • 줄끝 표시자 End-of-line • 파일끝 표시자 End-of-file Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
정규식 Regular Expressions Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
Clite 어휘 구문 부류 정규식 정의 • any. Char [ -~] • Letter [a-z. A-Z] • Digit [0 -9] • Whitespace [ t] • Eol n • Eof 04 Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
Clite 어휘 구문 부류 • Keyword 정규식 정의 bool | char | else | false | float | if | int | main | true | while • Identifier {Letter}({Letter} | {Digit})* • integer. Lit {Digit}+ • float. Lit {Digit}+. {Digit}+ • char. Lit ‘{any. Char}’ Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
Clite 어휘 구문 부류 • Operator 정규식 정의 = | || | && | == | != | <= | > >= | + | - | * | / |! • Separator : |. |{|}|(|) • Comment // | |[|] ({any. Char} | {Whitespace})* {eol} Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
어휘분석기 생성기 • 입력: 정규식 • 출력: 테이블(배열), 코드 • C/C++: Lex, Flex • Java: JLex Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
예제 • (S, a 2 i$) ├ (I, $) ├ (F, ) 따라서 (S, a 2 i$) ├* (F, ) Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
설계에서 구현으로 private char ch = ‘ ‘; public Token next ( ) { do { switch (ch) {. . . } } while (true); } Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
보조함수 is. Letter private boolean is. Letter(char c) { return ch >= ‘a’ && ch <= ‘z’ || ch >= ‘A’ && ch <= ‘Z’; } Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
보조함수 concat private String concat(String set) { String. Buffer r = new String. Buffer(“”); do { r. append(ch); ch = next. Char( ); } while (set. index. Of(ch) >= 0); return r. to. String( ); } Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
렉서의 토큰 생성 public Token next( ) { do { if (is. Letter(ch) { // ident or keyword String spelling = concat(letters+digits); return Token. keyword(spelling); } else if (is. Digit(ch)) { // int or float literal String number = concat(digits); if (ch != ‘. ’) return Token. mk. Int. Literal(number); number += concat(digits); return Token. mk. Float. Literal(number); Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
렉서의 토큰 생성 } else switch (ch) { case ‘ ‘: case ‘t’: case ‘r’: case eoln. Ch: ch = next. Ch( ); break; case eof. Ch: return Token. eof. Tok; case ‘+’: ch = next. Char( ); return Token. plus. Tok; … case ‘&’: check(‘&’); return Token. and. Tok; case ‘=‘: return chk. Opt(‘=‘, Token. assign. Tok, Token. eqeq. Tok); Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
소스 프로그램 // a first program // with 2 comments int main ( ) { char c; int i; c = 'h'; i = c + 3; } // main 토큰 int main ( ) { char Identifier ; Copyright © 2006 The Mc. Graw-Hill Companies, Inc. c
목차 3. 1 Chomsky Hierarchy 3. 2 Lexical Analysis 3. 3 Syntactic Analysis Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
저장문 문법 • Assignment → Identifier = Expression • Expression → Term { Add. Op Term } • Add. Op → + | - • Term → Factor { Mul. Op Factor } • Mul. Op → * | / • Factor → [ Unary. Op ] Primary • Unary. Op → - | ! • Primary → Identifier | Literal | ( Expression ) Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
Nullable Algorithm • Set nullable = new Set( ); • do { old. Size = nullable. size( ); • for (Production p : grammar. productions( )) • • • if (p. length( ) == 0) nullable. add(p. nonterminal( )); else << check righthand side >> } while (nullable. size( ) > old. Size); Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
널이 될 수 있는 비단말자 집합 구하기 Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
변환된 문법(그림 3. 8) S → A$ A → i=E; E → T E' E' → | AO T E' AO → + | T → F T' T' → MO F T' MO → * | / F → F' P F' → | UO UO → - | ! P → i | l | (E) Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
널이 될 수 있는 비단말자 집합(그림 3. 8에 대하여) Pass Nullable 1 E' T' F' 2 E' T' F' Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
비단말자 First A i UO !- E !-il( P il( E’ +- AO +- T !-il( T’ */ MO */ F !-il( F’ !Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
변환 알고리즘 T(EBNF) = Code (생성규칙 A → w 에 대하여) 4. w 가 w 1 |. . . | wn 이면 다음 코드로 변환, switch (token) { case First(w 1): T(w 1); break; . . . case First(wn): T(wn); break; default: error(token); (wi가 빈 것이 있으면 default: break; ) } 5. w 가 [ w' ] 이면 ( | w' )로 고치고 규칙 4를 적용. 6. w = X 1. . . Xn 이면, T(w) = T(X 1); . . . T(Xn); 를 적용 Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
메소드 match private void match (int t) { if (token. type() == t) token = lexer. next(); else error(t); } Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
메소드 error private void error(int tok) { System. err. println( “Syntax error: expecting” + tok + “; saw: ” + token); System. exit(1); } Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
배정문 파서 private void assignment( ) { // Assignment → Identifier = Expression ; match(Token. Identifier); match(Token. Assign); expression( ); match(Token. Semicolon); } Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
수식 파서 private void expression( ) { // Expression → Term { Add. Op Term } term( ); while (is. Add. Op()) { token = lexer. next( ); term( ); } } Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
추상구문 Assignment = Variable target; Expression source Expression = Variable | Value | Binary | Unary Binary = Operator op; Expression term 1, term 2 Unary = Operator op; Expression term Variable = String id Value = Integer value Operator = + | - | * | / | ! Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
abstract class Expression { } class Binary extends Expression { Operator op; Expression term 1, term 2; } class Unary extends Expression { Operator op; Expression term; } Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
private Assignment assignment( ) { // Assignment → Identifier = Expression ; Variable target = match(Token. Identifier); match(Token. Assign); Expression source = expression( ); match(Token. Semicolon); return new Assignment(target, source); } Copyright © 2006 The Mc. Graw-Hill Companies, Inc.
private String match (int t) { String value = Token. value( ); if (token. type( ) == t) token = lexer. next( ); else error(t); return value; } Copyright © 2006 The Mc. Graw-Hill Companies, Inc.