Download presentation
Presentation is loading. Please wait.
Published byGeorge Richards Modified over 8 years ago
1
CHAPTER 4 CFG & SYNTACTIC ANALYSIS SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY
2
SYNTACTIC ANALYSIS Parsing Input: token stream Output: parse tree (sentence structure) Production rules described by context-free grammar (CFG) (2013-1) Compiler 2
3
DERIVATION (1) Steps to generate string From start symbol to string Application of production rules on every derivation step EX: a + a * a Rule order: 1 3 2 3 3 (2013-1) Compiler 3 1. E → E + E 2. E → E * E 3. E → a E ⇒ E + E (1 번 생성규칙 적용 ) ⇒ a + E (3 번 생성규칙 적용 ) ⇒ a + E * E (2 번 생성규칙 적용 ) ⇒ a + a * E (3 번 생성규칙 적용 ) ⇒ a + a * a (3 번 생성규칙 적용 )
4
DERIVATION (2) Sentential form Intermediate form from S to string More than 2 non-terminals Leftmost derivation Apply production rule from the leftmost nonterminal in the sentential form Left parse = rule order in leftmost derivation (ex: 1 3 2 3 3) Rightmost derivation Apply production rule from the rightmost nonterminal in the sentential form (2013-1) Compiler 4
5
DERIVATION (3) Example of rightmost derivation (2013-1) Compiler 5 E ⇒ E + E(1 번 생성규칙 적용 ) ⇒ E + E * E(2 번 생성규칙 적용 ) ⇒ E + E * a(3 번 생성규칙 적용 ) ⇒ E + a * a(3 번 생성규칙 적용 ) ⇒ a + a * a(3 번 생성규칙 적용 )
6
DERIVATION (4) Rightmost reverse derivation Reverse order of rightmost derivation Right parse: rule order in rightmost reverse derivation EX: 3 3 3 2 1 (2013-1) Compiler 6 E ⇐ E + E (1 번 생성규칙 적용 ) ⇐ E + E * E (2 번 생성규칙 적용 ) ⇐ E + E * a (3 번 생성규칙 적용 ) ⇐ E + a * a (3 번 생성규칙 적용 ) ⇐ a + a * a (3 번 생성규칙 적용 )
7
PARSE TREE EX Root node = start symbol Internal node: nonterminal Leaf note: terminal E EE + EE* a a a E EE * EE+ a a a (2013-1) Compiler 7
8
AMBIGUITY (1) For an input string, more than 2 possible derivation steps (parse trees) Ambiguous production rule: A → AαA LHS 에 기술된 nonterminal 이 RHS 에 2 회 이상 사용되는 경우 (2013-1) Compiler 8 E ⇒ E * E (2 번 생성규칙 적용 ) ⇒ E + E * E (1 번 생성규칙 적용 ) ⇒ a + E * E (3 번 생성규칙 적용 ) ⇒ a + a * E (3 번 생성규칙 적용 ) ⇒ a + a * a (3 번 생성규칙 적용 )
9
AMBIGUITY (2) Unambiguous rules (2013-1) Compiler 9 E → E + F | F F → F * G | G G → a Leftmost derivation: E ⇒ E + F (1) ⇒ F + F (2) ⇒ G + F (4) ⇒ a + F (5) ⇒ a + F * G (3) ⇒ a + G * G (4) ⇒ a + a * G (5) ⇒ a + a * a (5) Rightmost derivation: E ⇒ E + F(1) ⇒ E + F * G(3) ⇒ E + F * a (5) ⇒ E + G * a(4) ⇒ E + a * a(5) ⇒ F + a * a(2) ⇒ G + a * a(4) ⇒ a + a * a(5)
10
ABSTRACT SYNTAX TREE (AST) Object code 로 생성될 필요가 없는 sub-tree 는 parse tree 로 생 성하지 않아도 된다 Object code 생성에 영향을 미치지 않는 불필요한 부분을 제거 하고 목적 코드 생성에 필요한 요소들만 parse tree 로 구성 EX: single production rule Internal node: sub-tree operation Instruction in object code add mula aa (2013-1) Compiler 10
11
SYNTACTIC ANALYSIS METHOD (1) By leftmost derivation From starting symbol to string: stack for storing sentential forms Stack’s initial state = starting symbol Replace nonterminal on top of the stack with RHS of the production rules Selection problem: more than 2 productions rules with same LHS Try to match terminal on top of the stack with the symbol in the string When stack is empty, syntactic analysis completes Sub-tree construction: in order of production rule application (2013-1) Compiler 11
12
SYNTACTIC ANALYSIS METHOD (2) Summary Initial state of the stack: starting symbol Nonterminal RHS Terminal matching with symbol in input string Stack empty completion (2013-1) Compiler 12
13
EXAMPLE (1) (2013-1) Compiler 13 (a+a*a, E) ⇒ (a+a*a, E+F) (1 번 생성규칙 적용 ) ⇒ (a+a*a, F+F) (2 번 생성규칙 적용 ) ⇒ (a+a*a, G+F) (4 번 생성규칙 적용 ) ⇒ (a+a*a, a+F) (5 번 생성규칙 적용 ) ⇒ (+a*a, +F) pop a ⇒ (a*a, F) pop + ⇒ (a*a, F*G) (3 번 생성규칙 적용 ) ⇒ (a*a, G*G) (4 번 생성규칙 적용 ) ⇒ (a*a, a*G) (5 번 생성규칙 적용 ) ⇒ (*a, *G) pop a ⇒ (a, G) pop * ⇒ (a, a) (5 번 생성규칙 적용 ) ⇒ (ε, ε) pop a
14
EXAMPLE (2) Operations: pop, expand a + a * a $ E F + E (2013-1) Compiler 14
15
SYNTACTIC ANALYSIS METHOD (3) By rightmost reverse derivation From input string to starting symbol Stack’s initial state = empty Input buffer = input string Operations Shift: move terminal symbol from input buffer to stack Reduce: replace RHS on stack top with LHS Completion Stack: starting symbol Input buffer: empty (2013-1) Compiler 15
16
EXAMPLE (3) (2013-1) Compiler 16 (ε, a+a*a) ⇒ (a, +a*a)shift a ⇒ (G, +a*a)(reduce 5) ⇒ (F, +a*a)(reduce 4) ⇒ (E, +a*a)(reduce 2) ⇒ (E+, a*a)shift + ⇒ (E+a, *a)shift a ⇒ (E+G, *a)(reduce 5) ⇒ (E+F, *a)(reduce 4) ⇒ (E+F*, a)shift * ⇒ (E+F*a, ε)shift a ⇒ (E+F*G, ε)(reduce 5) ⇒ (E+F, ε)(reduce 3) ⇒ (E, ε)(reduce 1) a + a * a $ + a * a $ a
17
KINDS OF PARSING METHODS Top-down parsing By leftmost derivation From starting symbol (= root node) From root node to leaf nodes Bottom-up parsing By rightmost reverse derivation From input string to root node From leaf node to root node (2013-1) Compiler 17
18
NONDETERMINISTIC PROPERTY (1) Top-down parsing More than 2 productions: backtracking Ex) A → α | β | γ Deterministic parsing: 각 단계에서 항상 어떤 생성규칙을 적용 하면 input string 을 생성할 수 있는지 미리 알 수 있을 때 Ex) A → aα | bβ | cγ (2013-1) Compiler 18
19
NONDETERMINISTIC PROPERTY (2) Bottom-up parsing Shift-reduce conflict Reduce-reduce conflict 부분 문자열 크기에 따라 2 가지 이상의 생성규칙 적용 가능 Handle 이 α 또는 βα : A→α, B→ βα 부분 문자열 α 가 일치되는 경우 : A→α, B→α, C→α Deterministic parsing: 각 단계에서 shift 할지 reduce 할지, reduce 할 경우에 어떤 생성규칙을 적용해야 parsing 을 성공할 지 미리 알 수 있을 때 (2013-1) Compiler 19
20
CFG NOTATION (1) BNF (Backus-Naur Form) Nonterminal symbol: Terminal symbol: ‘, ’ : ::= Example 17 (2013-1) Compiler 20 E E+T | T T T*F | F F (E) | a ::= ‘+’ | ::= ‘*’ | ::= ‘(‘ ‘)’ | ‘a’
21
CFG NOTATION (2) EBNF (extended BNF) Easy to read and simple Meta symbol: simply represent the repetitive part and alternative part (2013-1) Compiler 21 { } [ ]
22
CFG NOTATION (3) ::=, | ::= {, } Max/min # of repetition ::= if then [else ] BNF: ::= | ‘[‘ ‘]’ EBNF: ::= [ ‘[‘ ‘]’ ] (2013-1) Compiler 22
23
CFG NOTATION (4) Parenthesis and alternation: simple representation (2013-1) Compiler 23 ::= + | - | * | / ::= (+|-|*|/)
24
CFG NOTATION (5) Syntax diagram Show grammar by figure: easy to understand the syntactic structure Notation Nonterminal: rectangle Terminal: circle, ellipse Arc: link (2013-1) Compiler 24 A a
25
CFG NOTATION (6) Example A ::= X 1 X 2 … Xn (2013-1) Compiler 25... X1X1 X2X2 XnXn X1X1 X2X2 XnXn
26
CFG NOTATION (7) A ::= 1 | 2 |...| n A ::= { } A ::= [ ] (2013-1) Compiler 26 A.. 11 22 A A
27
CFG NOTATION (8) A ::= ( 1 | 2 | ) Example (2013-1) Compiler 27 A 11 22 A ::= a | (B) B ::= AC C ::= {+A} C B A C A B a () A +
28
CFG NOTATION (9) Synthesis (2013-1) Compiler 28 A A A a () +
29
CFG NOTATION (10) Example: integer variable declaration in C Format: keyword int variable list (comma) semi-colon BNF ::= int ; ::=, | EBNF ::= int {, } ; (2013-1) Compiler 29
30
CFG NOTATION (11) Syntax diagram (2013-1) Compiler 30 int; id, int_dcl
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.