Ch2 - 1 " A Simple One-Pass Compiler! to Generate...

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

View Full Document Right Arrow Icon
A Simple One-Pass Compiler to Generate Bytecode for the JVM Chapter 2 COP5621 Compiler Construction, Fall 2011 Copyright Robert van Engelen, Florida State University, 2007-2011 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Overview • This chapter contains introductory material to Chapters 3 to 8 of the Dragon book • Combined with material on the JVM to prepare for the laboratory assignments
Background image of page 2
3 Building a Simple Compiler • Building our compiler involves: – Defning the syntax oF a programming language – Develop a source code parser: For our compiler we will use predictive parsing – Implementing syntax directed translation to generate intermediate code: our target is the JVM abstract stack machine – Generating Java bytecode For the JVM – Optimize the Java bytecode (optional)
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 The Structure of our Compiler Lexical analyzer Syntax-directed translator Character stream Token stream Java bytecode Syntax deFnition (BN± grammar) Develop parser and code generator for translator JVM speciFcation
Background image of page 4
5 Syntax Defnition • Context-Free grammar is a 4-tuple with – A set oF tokens ( terminal symbols) – A set oF nonterminals – A set oF productions – A designated start symbol
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Example Grammar list ! list + digit list ! list - digit list ! digit digit ! 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 G = <{ list , digit }, { + , - , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }, P , list > with productions P = Context-free grammar for simple expressions:
Background image of page 6
7 Derivation • Given a CF grammar we can determine the set of all strings (sequences of tokens) generated by the grammar using derivation – We begin with the start symbol – In each step, we replace one nonterminal in the current sentential form with one of the right- hand sides of a production for that nonterminal
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 Derivation for the Example Grammar list " list + digit " list - digit + digit " digit - digit + digit " 9 - digit + digit " 9 - 5 + digit " 9 - 5 + 2 This is an example leftmost derivation , because we replaced the leftmost nonterminal (underlined) in each step. Likewise, a rightmost derivation replaces the rightmost nonterminal in each step
Background image of page 8
9 Parse Trees • The root of the tree is labeled by the start symbol • Each leaf of the tree is labeled by a terminal (=token) or # • Each interior node is labeled by a nonterminal • If A ! X 1 X 2 … X n is a production, then node A has immediate children X 1 , X 2 , …, X n where X i is a (non)terminal or # ( # denotes the empty string )
Background image of page 9

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

View Full DocumentRight Arrow Icon
10 Parse Tree for the Example Grammar Parse tree of the string 9-5+2 using grammar G list digit 9 - 5 + 2 list list digit digit The sequence of leafs is called the yield of the parse tree
Background image of page 10
11 Ambiguity string ! string + string | string - string | 0 | 1 | … | 9 G = <{ string }, { + , - , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 }, P , string > with production P = Consider the following context-free grammar: This grammar is ambiguous , because more than one parse tree represents the string 9-5+2
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 Ambiguity (cont’d) string string 9 - 5 + 2 string string string string string 9 - 5 + 2 string string string
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/01/2012 for the course COP 5621 taught by Professor Vanengelen during the Spring '11 term at FSU.

Page1 / 71

Ch2 - 1 &quot; A Simple One-Pass Compiler! to Generate...

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

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