slike bài giảng môn chương trình dịch chương 5 syntax-directed translation

60 554 0
slike bài giảng môn chương trình dịch chương 5 syntax-directed translation

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Syntax-Directed Translation Dr. Nguyen Hua Phung Falcuty of CSE – HCMUT 2007 Outline • Review • Syntax-directed definition • Attributes • Top-down translation • Bottom-up translation CSE - HCMUT Syntax-directed Translation 2 Compilation Phases CSE - HCMUT Syntax-directed Translation 3 Source code Scanner Parser Semantic analyzer Intermediate code generation Code optimization Code generation Sequence of tokens AST or Parse tree front end Symbol table AST tree Intermediate code Optimized code back end Machine code Pass • A pass consists of reading an input file and writing an output file. • A pass may include many phases • One-pass or multi-pass compilation – internal form – information dependence CSE - HCMUT Syntax-directed Translation 4 One-pass Compiler CSE - HCMUT Syntax-directed Translation 5 BKIT code Scanner √ Parser Semantic analyzer Code generation Sequence of tokens Parse tree Symbol table Parse tree Jasmin code Multi-pass Compiler BKIT code Scanner √ CSE - HCMUT Syntax-directed Translation 6 Parser Semantic analyzer Code generation Sequence of tokens AST Symbol table AST Jasmin code Semantic Analyzer • enforce semantic requirements: – is an identifier a scalar, array, struct or a method ? – is an identifier declared before used? – is an expression type-consistent? – is a method called with right number and type of args? – which declaration of a reference is? – does the dimension of an array match with the declaration? –etc. These requirement does not enforced by a CFG CSE - HCMUT Syntax-directed Translation 7 Outline • Review √ • Syntax-directed definition • Attributes • Top-down translation • Bottom-up translation CSE - HCMUT Syntax-directed Translation 8 Syntax-directed Definition • Each grammar symbol associates with it a set of attributes • Each grammar production associates with it a set of semantics rules which evaluate and refer only to attributes associated with the symbols in the production. CSE - HCMUT Syntax-directed Translation 9 Example 1 Production Semantic Rules L → E $ print(E.val) E → E 1 + T E.val = E 1 .val add T.val E → T E.val = T.val T → T 1 * F T.val = T 1 .val mul F.val T → F T.val = F.val F → ( E ) F.val = E.val F → digit F.val = IntVal(digit.lexeme) CSE - HCMUT Syntax-directed Translation 10 [...]... 8 +5* 4$ E→ E + T E→ T L→ E $ {print(E.val)} L {E.val = E1.val + T.val} E.val = 28 {E.val = T.val} 28 $ E E.val = 8 E + T T.val = 20 T.val = 8 T T * F F.val = 8 F F F.val = 5 4 8 5 T→ F {T.val = F.val} F→digit {F.val=IntVal(digit.lexeme)} CSE - HCMUT T→T + F Syntax-directed Translation F.val = 4 {T.val = T1.val * F.val} 11 Example 2 • 8 +5* 4 E E T * F CSE - HCMUT T T Parse Tree + F 8 5 Syntax-directed Translation. .. HCMUT Syntax-directed Translation 14 Example 2 E.node=new BinExp(E1.node,+,T.node) T.node=new BinExp(T1.node,*,F.node) E.node 8 +5* 4 + E.node T.node parser tree T.node T.node E.node=T.node F.node F.node 8 5 T.node=F.node F.node=new LitExp(num) * F.node 4 BinExp AST LitExp(8) F.node=new LitExp(num) + LitExp (5) CSE - HCMUT Syntax-directed Translation BinExp * LitExp(4) 15 Outline • • • • • Review Syntax-directed. .. CSE - HCMUT Syntax-directed Translation 20 Example E.val = E’.val = 17 E T T.val =T’.val E’.in =T.val = 8 T + T’.in = F.val = 8 F T’ F T’.in = 5 E’.val = E1’.val = 17 T.val = 5 T’ E’ T + T’.val =T’.in T’.val =T’.in 8 E’ 8 +5+ 4 5 E1’.in =E’.in add T.val = 13 F T’.in = 4 E’.val = E1’.val=17 T.val = 4 E’ E’.val =E’.in T’ T’.val = 4 4 E1’.in =E’.in add T.val = 17 CSE - HCMUT Syntax-directed Translation. .. IntVal(digit.lexeme)} CSE - HCMUT Syntax-directed Translation 26 Top-down Translation • Read Algorithm 5. 2 page 306 CSE - HCMUT Syntax-directed Translation 27 Imperative Approach • Create a function for each nonterminal • Synthesized attributes ⇒ output parameter • Inherited attributes ⇒ input parameters • Semantics actions are inserted into corresponding places in the function CSE - HCMUT Syntax-directed Translation 28... mul F.val; T’.val = T’1.val T’ → ∈ T’.val = T’.in F→(E) F.val = E.val F → digit F.val = IntVal(digit.lexeme) CSE - HCMUT Syntax-directed Translation 23 Outline • • • • • Review Syntax-directed definition Attributes Top-down translation Bottom-up translation CSE - HCMUT Syntax-directed Translation √ √ √ 24 Implementation • Graph-based methods: – create a dependence graph – topological sort – calculate... computed from the values of the attributes of the grammar symbols in RHS L 8 +5* 4$ E.val = 28 28 $ E E.val = 8 E + T T.val = 20 T.val = 8 T T * F F.val = 8 F F F.val = 5 4 8 5 F.val = 4 • A syntax-directed definition that consists of only synthesized attributes is called a S-attributed definition CSE - HCMUT Syntax-directed Translation 18 Inherited Attribute • An attribute of a grammar symbol in RHS... Outline • • • • • Review Syntax-directed definition Attributes Top-down translation Bottom-up translation CSE - HCMUT Syntax-directed Translation √ √ 16 Attribute • An attribute can represent – a string, – a number, – a type, – a memory location, – a code fragment, – etc • An attribute has its name and value CSE - HCMUT Syntax-directed Translation 17 Synthesized Attribute • An attribute of a grammar symbol... parse is called CSE - HCMUT Syntax-directed Translation 34 Example E → T E’ E’ → + T E’ | ∈ T → F T’ T’ → * F T’ | ∈ F → ( E ) | digit class E { } class E’ { } class T { } class T’ { } class F { } CSE - HCMUT class E { void parse() { switch (lookahead) { case ‘(‘: case ‘digit’: (new T()).parse(); (new E’()).parse(); break; default: error(“ ”); } } } Syntax-directed Translation 35 Example (cont’d) E’ →... graph – topological sort – calculate all attibutes – many passes • Rule-based methods: – the order in which the attributes are evaluated is predetermined – one-pass evaluation CSE - HCMUT Syntax-directed Translation 25 Translation Schemes • Semantic actions are inserted within RHS of productions E.g.: L→E$ {print(E.val)} E → T {E’.in = T.val} E’ {E.val = E’.val} E’ → + T {E’1.in = E’.in add T.val} E’1... F.val = 4 {T.val = T1.val * F.val} 11 Example 2 • 8 +5* 4 E E T * F CSE - HCMUT T T Parse Tree + F 8 5 Syntax-directed Translation F 4 12 Example 2 8 +5* 4 BinExp LitExp(8) + BinExp LitExp (5) * LitExp(4) Abstract Syntax Tree (AST) CSE - HCMUT Syntax-directed Translation 13 Example 2 Production Semantic Rules E → E1 + T E.node = new BinExp(E1.node,+, T.node) E→T E.node = T.node T → T1 * F T.node = new BinExp(T1.node,*, . HCMUT Syntax-directed Translation 7 Outline • Review √ • Syntax-directed definition • Attributes • Top-down translation • Bottom-up translation CSE - HCMUT Syntax-directed Translation 8 Syntax-directed. BinExp(E1.node,+,T.node) CSE - HCMUT Syntax-directed Translation 15 Outline • Review √ • Syntax-directed definition √ • Attributes • Top-down translation • Bottom-up translation CSE - HCMUT Syntax-directed Translation. T 1 .val * F.val} Example 2 • 8 + 5 * 4 E E T + CSE - HCMUT Syntax-directed Translation 12 T F * T F F 8 5 4 Parse Tree Example 2 8 + 5 * 4 BinExp LitExp(8) + BinExp LitExp (5) LitExp(4) * Abstract Syntax

Ngày đăng: 23/10/2014, 17:33

Từ khóa liên quan

Mục lục

  • Syntax-Directed Translation

  • Outline

  • Compilation Phases

  • Pass

  • One-pass Compiler

  • Multi-pass Compiler

  • Semantic Analyzer

  • Outline

  • Syntax-directed Definition

  • Example 1

  • Example 1 (cont’d)

  • Example 2

  • Example 2

  • Example 2

  • Example 2

  • Outline

  • Attribute

  • Synthesized Attribute

  • Inherited Attribute

  • Example

Tài liệu cùng người dùng

Tài liệu liên quan