4 Syntax n Lexical structure n n Syntax

  • Slides: 41
Download presentation
4장 구문(Syntax)

4장 구문(Syntax)

(프로그래밍) 언어의 구성 n 어휘구조(Lexical structure) n n 단어의 구조 구문(Syntax) n n 구문

(프로그래밍) 언어의 구성 n 어휘구조(Lexical structure) n n 단어의 구조 구문(Syntax) n n 구문 구조 문법을 이용해서 기술 n n Context-free grammar in BNF(Backus-Naur Form) 의미(Semantics) n n n 프로그램의 의미 자연어를 이용한 기술 수학적 기술 n n operational semantics, denotational semantics axiomatic semantics

4. 1 어휘구조(Lexical Structure)

4. 1 어휘구조(Lexical Structure)

예제 n if-then-else 문 S if E then S else S | if E

예제 n if-then-else 문 S if E then S else S | if E then S | others n 단순 수식 E | | | N D E*E E+E (E) N ND|D 0|1|2|3|4|5|6|7|8|9

CFG n A CFG consists of: n n n set of terminals T set

CFG n A CFG consists of: n n n set of terminals T set of non-terminals N start symbol S (one of the non-terminals) set of productions X Y 1 Y 2 … Yn where X N and Yi T N { } a a Notational convention n n non-terminals are written upper-case terminals are written lower-case

유도(Derivation) n Direct derivation X 1 …Xi… Xn X 1 … Xi-1 Y 1

유도(Derivation) n Direct derivation X 1 …Xi… Xn X 1 … Xi-1 Y 1 Y 2…Yn Xi+1 … Xn if there is a production Xi Y 1 Y 2…Yn n Derivation * X 1 … Xn * Y 1 … Ym if X 1 … Xn … Y 1 … Ym (0 or more direct derivations from X 1 … Xn to Y 1 … Ym ) n The language L(G) of a CFG G {a 1 … an | S * a 1 … an and every ai is a terminal}

예제 n L(G) is the language of a CFG G. n Strings of balanced

예제 n L(G) is the language of a CFG G. n Strings of balanced parentheses: S (S ) S n The language is ?

유도 및 파스 트리 n A sequence of production applications S … … is

유도 및 파스 트리 n A sequence of production applications S … … is a derivation. n A derivation can be drawn as a tree: n n n S is the tree's root. If the derivation uses production X Y 1 Y 2…Yn, X has children Y 1, …, Yn.

유도에 대한 참조(1) n A parse tree has n n terminals at the leaves.

유도에 대한 참조(1) n A parse tree has n n terminals at the leaves. non-terminals at the interior nodes. An inorder traversal of the leaves is the original input. The parse tree shows association of operations; n 3 + (4 * 5)

C Grammar in BNF translation-unit external-declaration | translation-unit external-declaration function-definition | declaration function-definition declaration-specifiers

C Grammar in BNF translation-unit external-declaration | translation-unit external-declaration function-definition | declaration function-definition declaration-specifiers declarator declaration-list compound-statement | declaration-specifiers declarator compound-statement | declarator declaration-list compound-statement | declarator compound-statement declaration-specifiers ‘; ’ | declaration-specifiers init-declarator-list ‘; ’

C Grammar in BNF init-declarator-list init-declarator | init-declarator-list ‘, ’ init-declarator | init-declarator ‘=‘

C Grammar in BNF init-declarator-list init-declarator | init-declarator-list ‘, ’ init-declarator | init-declarator ‘=‘ initializer declarator pointer direct-declarator | direct-declarator pointer ‘*’ type-qualifier-list pointer | ‘*’ type-qualifier-list | ‘*’ pointer | ‘*’ direct-declarator ID | ‘(‘ declarator ‘)’ | direct_declarator ‘[‘ ‘]’ | direct_declarator ‘[‘ constant_expression ‘]’ | direct_declarator ‘(‘ parameter_type_list ‘)’ | direct_declarator ‘(‘ identifier-list ‘)’ | direct_declarator ‘(‘ ‘)’

요약 n 구문구조 n 문법을 이용하여 표기 n n 유도(derivation) n n n CFG

요약 n 구문구조 n 문법을 이용하여 표기 n n 유도(derivation) n n n CFG in BNF 좌우선 유도(Leftmost derivation) 우우선 유도(Rightmost derivation) 파스트리(parse tree)

모호성 예: The Dangling Else n 모호한 문법 S if E then S |

모호성 예: The Dangling Else n 모호한 문법 S if E then S | if E then S else S n 이 문장에 대한 두 개의 파스 트리 if E 1 then if E 2 then S 1 else S 2 if if E 1 E 2 S 1 S 2 E 2 if S 2 S 1

EBNF n BNF E E+T|T T T*F | F F N | (E) N

EBNF n BNF E E+T|T T T*F | F F N | (E) N ND|D n EBNF E T {+ T} T F {* F} F N | (E) N D {D}