Jan23 - CSE302: Compiler Design Instructor: Dr. Liang Cheng...

Info iconThis preview shows pages 1–11. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University January 23, 2007
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Instructor: Dr. Liang Cheng CSE302: Compiler Design 01/23/07 Today’s Outline ± Recap ± Introduction (Section 2.1) ± Syntax definition (Section 2.2) ± Parsing (Section 2.4) ± A simple syntax-directed translator (Chapter 2) ± Parsing (Section 2.4.5) ± Syntax directed translation (Section 2.3) ± A translator for simple expressions (Section 2.5) ± Summary and homework
Background image of page 2
Instructor: Dr. Liang Cheng CSE302: Compiler Design 01/23/07 BNF Grammar and Parse Trees <program> <stmts> <stmt> const a <var> = <expr> <var> b <term> + <term> BNF Functionality ± Describing Lists ± Grammar & Derivation ± Parse Trees ± Avoiding Ambiguity <program> => <stmts> => <stmt> => <var> = <expr> => a = <expr> => a = <term> + <term> => a = <var> + <term> => a = b + <term> => a = b + const <program> <stmts> <stmts> <stmt> | <stmt> ; <stmts> <stmt> <var> = <expr> <var> a | b | c | d <expr> <term> + <term> | <term> - <term> <term> <var> | const
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Instructor: Dr. Liang Cheng CSE302: Compiler Design 01/23/07 Grammar Ambiguity ± A grammar is ambiguous iff it generates a sentential form that has two or more distinct parse trees ± Use BNF to specify operator precedence and associativity BNF Functionality ± Describing Lists ± Grammar & Derivation ± Parse Trees ± Avoiding Ambiguity
Background image of page 4
Instructor: Dr. Liang Cheng CSE302: Compiler Design 01/23/07 Language Design ± Design a BNF grammar for a language that could express a one-digit number, an addition of two one-digit numbers, or a subtraction of two one-digit numbers ± <expr> <term> + <term> | <term> - <term> | <term> ± <term> 0 | 1 | 2 | … | 9
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Instructor: Dr. Liang Cheng CSE302: Compiler Design 01/23/07 Language Implementation ± A recursive-descent parser ± Language implementation directly following the BNF grammar ± <expr> <term> + <term> | <term> - <term> | <term> ± <term> 0 | 1 | 2 | … | 9 ± Pseudo code void expr() { void term() { term(); match( int_literal ); if( token== plus_op } or token== minus_op ) { match(token); void match(expectedToken) { term(); if(token==expectedToken) } getNextToken(); else error(); else error(); }}
Background image of page 6
Instructor: Dr. Liang Cheng CSE302: Compiler Design 01/23/07 Outline ± Recap ± A simple syntax-directed translator (Chapter 2) ± Parsing (Section 2.4.5) ± Syntax directed translation (Section 2.3) ± A translator for simple expressions (Section 2.5) ± Summary and homework
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Instructor: Dr. Liang Cheng CSE302: Compiler Design 01/23/07 Remove Left Recursion ± What are the languages defined by the following two BNF grammars? A A α | β A →β R R →α R | ε
Background image of page 8
Instructor: Dr. Liang Cheng CSE302: Compiler Design 01/23/07 Remove Left Recursion ± BNF: <expr> <expr> + <term> ± Left-recursion to right-recursion A A α | β A →β R R →α R | ε ± <expr> <term> rest ± rest + <term> rest | - <term> rest | ε ± EBNF: <expr> <term> { + <term>}
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Instructor: Dr. Liang Cheng CSE302: Compiler Design 01/23/07 Outline ±
Background image of page 10
Image of page 11
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 08/06/2008 for the course CSE 302 taught by Professor Cheng during the Spring '07 term at Lehigh University .

Page1 / 34

Jan23 - CSE302: Compiler Design Instructor: Dr. Liang Cheng...

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online