Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 94 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
94
Dung lượng
201,72 KB
Nội dung
Syntax Analysis Quan Thanh Tho (qttho@) Nguyen Hua Phung (phung@) cse.hcmut.edu.vn Objectives • Context-free grammar for designing programming languages syntax • Parsing methods typically used in compilers • Error recovery from commonly occurring syntax errors CSE-HCMUT Syntax Analysis I 2 Outline • The role of syntax analysis (parser) • Language syntax specification • Parsing Techniques • Error Recovery CSE-HCMUT Syntax Analysis I 3 The role of syntax analysis • Receive tokens from lexical analyzer •Verifyif the received tokens conform to the language grammar or not • Generate a parsing representation (usually a parse tree) • Handle syntax error (report and recover) Lexical Analyzer Syntax Analyzer token get next token CSE-HCMUT Syntax Analysis I 4 Outline • The role of syntax analysis (parser) • Language syntax specification – Syntax and Grammar – Context-free Grammar • Derivation • Parse Tree – Grammar Construction for Programming Language: • Language construct definition • Operators precedence and associativity •Ambiguity • Parsing Techniques • Error Recovery CSE-HCMUT Syntax Analysis I 5 Syntax and Grammar • Syntax (programming language sense): – Define structure of a program – Not reflect the meaning (semantic) of the program • Grammar: – Rule-based formalism to specify a language syntax CSE-HCMUT Syntax Analysis I 6 Why Grammar? • Capable of specifying language syntax precisely • Rule-based representation supported by grammar is natural and easy to understand for human • Effectively support language modification and extension • Provide fundamental basic to develop parsers systematically CSE-HCMUT Syntax Analysis I 7 Context-Free Grammar (CFG) • A kind of grammar • Not as complex as context-sensitive and phase-structure grammar • More powerful than regular grammar CSE-HCMUT Syntax Analysis I 8 Formal Definition of CFG • G = (V N ,V T ,S, P) •V N : finite set of nonterminal symbols V T : finite set of tokens (V T ∩V N =∅) S∈V N : start symbol P: finite set of rules (or productions) of BNF (Backus – Naur Form) form AÆ (a)* where A ∈ V N ,a∈(V T ∪V N ) CSE-HCMUT Syntax Analysis I 9 Example 1 • G = ({exp,op},{+,-,*,/,id} ,exp,P) where P is the following exp Æ exp op exp exp Æ id op Æ +|-|*|/ CSE-HCMUT Syntax Analysis I 10 [...]... CSE-HCMUT Syntax Analysis I 30 Operator Precedence exp exp op id + exp op id CSE-HCMUT exp * Syntax Analysis I exp id 31 Operator Precedence exp exp exp op exp id + op * exp id id CSE-HCMUT Syntax Analysis I 32 Operator Precedence exp exp + term term term factor factor id CSE-HCMUT * factor id id Syntax Analysis I 33 Operator Precedence • (id+id) *id exp term term * factor ( CSE-HCMUT exp factor id ) Syntax. .. CSE-HCMUT Syntax Analysis I 21 Example 4 • exp ⇒ exp op exp exp exp CSE-HCMUT op Syntax Analysis I exp 22 Example 4 exp ⇒ exp op exp ⇒ id op exp exp exp op exp id CSE-HCMUT Syntax Analysis I 23 Example 4 • exp ⇒ exp op exp ⇒ id op exp ⇒ id + exp exp exp id CSE-HCMUT op + Syntax Analysis I exp 24 Example 4 • exp ⇒ exp op exp ⇒ id op exp ⇒ id + exp ⇒ id + id exp exp exp id CSE-HCMUT op + id Syntax Analysis. .. nonterminal CSE-HCMUT Syntax Analysis I 15 Example 3 – Leftmost Derivation • exp ⇒ exp op exp ⇒ id op exp ⇒ id + exp ⇒ id + id CSE-HCMUT Syntax Analysis I 16 Example 3 – Rightmost Derivation • exp ⇒ exp op exp ⇒ exp op id ⇒ exp + id ⇒ id + id CSE-HCMUT Syntax Analysis I 17 Hands-on Exercise • Find the leftmost derivation and rightmost derivation of id+id*id+id CSE-HCMUT Syntax Analysis I 18 Parsing... CSE-HCMUT Syntax Analysis I 11 Example 2 • exp ⇒ exp op exp ⇒ exp op id ⇒ id op id ⇒ id + id CSE-HCMUT Syntax Analysis I 12 Example 2 (cont’d) • exp ⇒ exp op exp ⇒ id op exp ⇒ id + exp ⇒ id + id CSE-HCMUT Syntax Analysis I 13 Example 2 (cont’d) • exp ⇒ exp op exp ⇒ exp op exp op exp ⇒ id op exp op exp ⇒ id + exp op exp ⇒ id + exp * exp ⇒ id + id * exp ⇒ id + id * exp ⇒ id + id * id CSE-HCMUT Syntax Analysis. .. Analysis I 33 Operator Precedence • (id+id) *id exp term term * factor ( CSE-HCMUT exp factor id ) Syntax Analysis I 34 Operator Associativity exp exp exp op exp id + op + exp id id CSE-HCMUT Syntax Analysis I 35 Operator Associativity exp exp op id + exp op id CSE-HCMUT exp + Syntax Analysis I exp id 36 ... analysis CSE-HCMUT Syntax Analysis I 19 Parse Tree • Tree-based structure representing a derivation – Root node Start symbol – Interior node Nonterminal symbol – Leaf node token or nonterminal – Children of a node from left to right form the right-hand side of a production whose lefthand side is the node – Parse tree is constructed based on the deriving sequence of the derivation CSE-HCMUT Syntax Analysis. .. Hands-on Exercise • Draw the corresponding parse tree for a derivation of id+id*id+id CSE-HCMUT Syntax Analysis I 26 Extended Backus-Naur Form (EBNF) • EBNF = BNF + regular expression – More impact – More readable – More difficult to be processed by computer program exp ⇒ exp (op exp) * CSE-HCMUT Syntax Analysis I 27 Language Construct Definition • Program: prog (declaration)? statements statements... exp THEN statement (ELSE statement)? CSE-HCMUT Syntax Analysis I 28 Hands-on Exercise • Rewrite the EBNF-form productions in the previous slide as BNF-form one • Draw the parse trees for the following sentences: – IF ID+INT THEN INT*INT – IF ID THEN ID ELSE ID – IF ID THEN ID ELSE IF ID THEN ID • Write the grammar for the While statement CSE-HCMUT Syntax Analysis I 29 Classic Expression Grammar exp exp . syntax errors CSE-HCMUT Syntax Analysis I 2 Outline • The role of syntax analysis (parser) • Language syntax specification • Parsing Techniques • Error Recovery CSE-HCMUT Syntax Analysis I 3 The. Handle syntax error (report and recover) Lexical Analyzer Syntax Analyzer token get next token CSE-HCMUT Syntax Analysis I 4 Outline • The role of syntax analysis (parser) • Language syntax specification –. nonterminal CSE-HCMUT Syntax Analysis I 15 Example 3 – Leftmost Derivation •exp ⇒ exp op exp ⇒ id op exp ⇒ id + exp ⇒ id + id CSE-HCMUT Syntax Analysis I 16 Example 3 – Rightmost Derivation •exp