Simple One-Pass Compiler

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
Chapter 8 Intermediate Code Generation. Intermediate languages: Syntax trees, three-address code, quadruples. Types of Three – Address Statements: x :=
1 Compiler Construction Intermediate Code Generation.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Pushdown Automata Consists of –Pushdown stack (can have terminals and nonterminals) –Finite state automaton control Can do one of three actions (based.
Mooly Sagiv and Roman Manevich School of Computer Science
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #7 Parsing.
1 Chapter 5 Compilers Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
1 Terminology l Statement ( 敘述 ) »declaration, assignment containing expression ( 運算式 ) l Grammar ( 文法 ) »a set of rules specify the form of legal statements.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
Chapter 2 A Simple Compiler
Top-Down Parsing.
Chapter 6: Top-Down Parser1 Compiler Designs and Constructions Chapter 6: Top-Down Parser Objectives: Types of Parsers Backtracking Vs. Non-backtracking.
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Syntax and Semantics Structure of programming languages.
Parsing Chapter 4 Parsing2 Outline Top-down v.s. Bottom-up Top-down parsing Recursive-descent parsing LL(1) parsing LL(1) parsing algorithm First.
Chapter 9 Syntax Analysis Winter 2007 SEG2101 Chapter 9.
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
Chapter 5 Top-Down Parsing.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
A Simple One-Pass Compiler to Generate Bytecode for the JVM Chapter 2 COP5621 Compiler Construction, Fall 2013 Copyright Robert van Engelen, Florida State.
1 A Simple One-Pass Compiler (to Generate Code for the JVM) Chapter 2 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University,
CSE 3302 Programming Languages Chengkai Li, Weimin He Spring 2008 Syntax (cont.) Lecture 4 – Syntax (Cont.), Spring CSE3302 Programming Languages,
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Lesson 7 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Compiler Construction Syntax Analysis Top-down parsing.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Syntax and Semantics Structure of programming languages.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Simple One-Pass Compiler part II Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Top-down Parsing Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
1 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Chapter 2: A Simple One Pass Compiler
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Syntax and Semantics Structure of programming languages.
C HAPTER 2. A S IMPLE S YNTAX -D IRECTED T RANSLATOR DR. NIDJO SANDJOJO, M.Sc.
CSE 3302 Programming Languages
Programming Languages Translator
Context free grammars Terminals Nonterminals Start symbol productions
Lecture #12 Parsing Types.
Parsing with Context Free Grammars
A Simple One-Pass Compiler to Generate Bytecode for the JVM
Syntax-Directed Definition
Chapter 2: A Simple One Pass Compiler
CSE401 Introduction to Compiler Construction
Chapter 2: A Simple One Pass Compiler
Designing a Predictive Parser
Presentation transcript:

Simple One-Pass Compiler Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

Outline Translation Scheme. Annotated Parse Tree. Parsing Fundamental. Top-Down Parsers. Abstract Stack Machine. Simple Code Generation.

Simple One-Pass Compiler Scanner Source program (text stream) m a i n ( ) { Parser Object Code (text stream)

Sample Grammar expr  expr + term expr  expr - term expr  term

Derivation String: 9 – 5 + 2 expr  expr + term  expr – term + term  term – term + term  9 – term + term  9 – 5 + term  9 – 5 + 2 leftmost/rightmost derivation

Parse Tree expr expr term expr term term 9 - 5 + 2

Translation Scheme expr ::= expr + term expr ::= expr – term Context-free Grammar with Embedded Semantic Actions. expr ::= expr + term expr ::= expr – term expr ::= term term ::= 0 term ::= 1 ... term ::= 9 { print(‘+’); } { print(‘-’); } { print(‘0’); } { print(‘1’); } { print(‘9’); } emitting (พ่น) a translation

Parse Tree with Semantic Actions expr + { print(‘+’) } expr term - { print(‘-’) } 2 { print(‘2’) } expr term term 5 { print(‘5’) } 9 { print(‘9’) } Depth-first traversal Input: 9 – 5 + 2 Output: 9 5 - 2 +

Location of Semantic Actions Semantic Actions can be placed anywhere on the RHS. expr ::= {print(‘+’);} expr + term expr ::= {print(‘-’);} expr – term expr ::= term term ::= 0 {print(‘0’);} term ::= 1 {print(‘1’);} ... term ::= 9 {print(‘9’);}

Parsing Approaches Top-down parsing Bottom-up parsing build parse tree from start symbol match result terminal string with input stream simple but limit in power Bottom-up parsing start from input token stream build parse tree from terminal until get start symbol complex but powerful

Top Down vs. Bottom Up start here result match start here result input token stream input token stream Top-down Parsing Bottom-up Parsing

Example type ::= simple | ^id | array [ simple ] of type simple ::= integer | char | num dotdot num Input Token String array [ num dotdot num ] of integer

Top-Down Parsing with Left-to-right Scanning of Input Stream type array [ simple ] of type Input array [ num dotdot num ] of integer lookahead token

Backtracking (Recursive-Descent Parsing) simple integer char num Input array [ num dotdot num ] of integer lookahead token

Predictive Parsing type array [ simple ] of type type ::= simple | ^id | array [ simple ] of type simple ::= integer | char | num dotdot num type array [ simple ] of type Input array [ num dotdot num ] of integer lookahead token

The Program for Predictive Parser match (scanner) Input (text stream) a r r a y [ OK match(‘array’) Predictive Parser Output

The Program for Predictive Parsing procedure match ( t : token ); procedure simple; begin begin if lookahead = t then if lookahead = integer then lookahead := nexttoken match ( integer ) else error else if lookahead = char then end; match ( char ) else if lookahead = num then begin procedure type; match ( num ) match ( dotdot ) match ( num ) begin end if lookahead is in { integer, char, num } then else error simple end; else if lookahead = ‘ ^ ‘ then begin match ( ‘ ^ ’ ); match ( id ) end else if lookahead = array then begin match ( array ); match ( ‘ [ ‘ ); simple; match ( ‘ ] ‘ ); match ( of ); type else error end;

Mapping Between Production and Parser Codes type -> arrary [ simple ] of type match(array); match(‘[‘); simple; match(‘]’); match(of); type scanner parser parsing (recognition) of simple

Lookahead Symbols A -> a FIRST( a ) = set of fist token in strings generated from a FIRST(simple) = { integer, char, num } FIRST( ^id ) = { ^ } FIRST(array [ simple ] of type) = { array }

Rules for Predictive Parser If A -> a and A -> b then FIRST(a) and FIRST(b) are disjoint e-production stmt -> begin opt_stmts end opt_stmts -> stmt_list opt_stmts | e

expr -> expr + term | term Left Recursion Left Recursion => Parser loops forever A -> Aa | b expr -> expr + term | term Rewriting... A -> b R R -> a R | e

Example expr  expr + term expr  expr - term expr  term expr  term rest rest  + term rest | - term rest | e term  0 | 1 | 2 | ... | 9

Semantic Actions expr  term rest rest  + term {print(‘+’);} rest ...

rest  + term {print(‘+’);} rest | - term {print(‘-’);} rest | e expr  term rest rest  + term {print(‘+’);} rest | - term {print(‘-’);} rest | e term  0 {print(‘0’);} ... procedure expr; begin term(); rest(); end; procedure rest; begin if lookahead = ‘+’ then match(‘+’); term(); print(‘+’); rest(); else if lookahead = ‘-’ then begin match(‘-’); print(‘-’); end;

Abstract Stack Machines Primitive Arithmetic Operator 1 3 + 5 * Stack 1 Stack 3 Add Stack 5 Multiply L-values & R-values i := i + 1 ; ADDRESS OF i VALUE OF i p^ := q^

Stack Manipulation push v push v on top of stack pop v pop v from top of stack rvalue l push content of data l lvalue l push address of data l := put r-value on top to l-value below and pop both from top copy push copy of top

Translation of Expression day := (1461 * y) div 4 + (153 * m + 2) div 5 + d lvalue day push 153 div push 1461 rvalue m + rvalue y * rvalue d * push 2 + push 4 + := div push 5

Translation Scheme for Assignment Statement stmt -> id := { emit(‘lvalue’, id.lexeme); } expr { emit(‘:=‘); }

Control Flow label l target for jump goto l jump to lable l gofalse l pop top; jump if zero gotrue l pop top; jump if nonzero

Translation of if Statement stmt -> if expr { out := newlabel; emit ( ‘gofalse’, out ); } then stmt1 { emit ( ‘label’, out ); } code for expr gofalse out code for stmt1 label out