Chapter 2-2 A Simple One-Pass Compiler

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Chapter 5 Test Review Sections 5-1 through 5-4.
Addition 1’s to 20.
25 seconds left…...
Week 1.
1 Week 9 Questions / Concerns Hand back Test#2 What’s due: Final Project due next Thursday June 5. Final Project check-off on Friday June 6 in class. Next.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
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 :=
Chapter 5 Syntax Directed Translation. Outline Syntax Directed Definitions Evaluation Orders of SDD’s Applications of Syntax Directed Translation Syntax.
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.
Top-Down Parsing.
Chapter 2 Chang Chi-Chung rev.1. A Simple Syntax-Directed Translator This chapter contains introductory material to Chapters 3 to 8  To create.
Abstract Syntax Trees Lecture 14 Wed, Mar 3, 2004.
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 Directed Definitions Synthesized Attributes
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
Topic #2: Infix to Postfix EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
1 November 19, November 19, 2015November 19, 2015November 19, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Simple One-Pass Compiler
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Chapter 5. Syntax-Directed Translation. 2 Fig Syntax-directed definition of a simple desk calculator ProductionSemantic Rules L  E n print ( E.val.
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.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007.
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)
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
2-1. LEX & YACC. 2 Overview  Syntax  What its program looks like –Context-free grammar, BNF  Syntax-directed translation –A grammar-oriented compiling.
Chapter 2: A Simple One Pass Compiler
Chapter4 Syntax-Directed Translation Introduction : 1.In the lexical analysis step, each token has its attribute , e.g., the attribute of an id is a pointer.
C HAPTER 2. A S IMPLE S YNTAX -D IRECTED T RANSLATOR DR. NIDJO SANDJOJO, M.Sc.
Lecture 9 Symbol Table and Attributed Grammars
A Simple Syntax-Directed Translator
Constructing Precedence Table
Lecture #12 Parsing Types.
Compiler Construction
Chapter 5 Syntax Directed Translation
Parsing with Context Free Grammars
Syntax-Directed Translation Part I
CS 3304 Comparative Languages
Syntax-Directed Translation Part I
Syntax-Directed Translation Part I
Syntax-Directed Definition
Chapter 2: A Simple One Pass Compiler
CSE 3302 Programming Languages
CSE401 Introduction to Compiler Construction
Chapter 2: A Simple One Pass Compiler
R.Rajkumar Asst.Professor CSE
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Designing a Predictive Parser
Syntax-Directed Translation Part I
SYNTAX DIRECTED DEFINITION
BNF 9-Apr-19.
Predictive Parsing Program
Syntax-Directed Translation Part I
Chapter 5 Syntax Directed Translation
Faculty of Computer Science and Information System
Presentation transcript:

Chapter 2-2 A Simple One-Pass Compiler

Syntax-directed translation Syntax-directed definition  translation of a construct in terms of attributes associated with its syntactic components Syntactic structure :: context-free grammar Grammar symbol :: a set of attributes and with each production, a set of semantic rules for computing values of the attributes associated with the symbols appearing in the production Translation  input-output mapping Annotated parse tree  a parse tree showing the attribute values at each node

Synthesized Attributes An attribute is said to be synthesized if its value at a parse-tree node is determined from attribute values at the children of node 예제 설명

Syntax-directed definition for infix to postfix translation. PRODUCTION SEMANTIC RULE expr  expr1 + term expr  expr1 - term expr  term term  0 term  1 . . . term  9 expr.t := expr1.t || term.t || ‘+’ expr.t := expr1.t || term.t || ‘-’ expr.t := term.t term.t := ‘0’ term.t := ‘1’ term.t := ‘9’ Syntax-directed definition for infix to postfix translation.

Attribute values at nodes in a parse tree.

Depth first traversals Robot positioning :: seq  seq instr | begin instr  east | north | west | south Depth-first traversals Translation Schemes Context-free grammar in which program fragments called semantic actions are embedded Emitting a Translation

Annotated parse tree for begin west south.

Syntax-directed definition of the robot’s position. PRODUCTION SEMANTIC RULES seq  begin seq.x := 0 seq.y := 0 seq  seq1 instr seq.x := seq.x1 + instr.dx seq.y := seq.y1 + instr.dy instr  east instr.dx := 1 instr.dy := 0 instr  north instr.dx := 0 instr.dy := 1 instr  west instr.dx := -1 instr  south instr.dy := -1 Syntax-directed definition of the robot’s position.

Actions translating expressions into postfix notation. expr  expr1 + term expr  expr1 - term expr  term term  0 term  1 . . . term  9 { print (‘+’) } { print (‘-’) } { print (‘0’) } { print (‘1’) } { print (‘9’) } Actions translating expressions into postfix notation.

Actions translating 9-5+2 into 95-2+

Top-down parsing Lookahead … the current token being scanned Array[ num dotdot num ] of integer 과정 설명

type  simple |  id | array [ simple ] of type Simple  integer | char | num dotdot num (2.8)

Steps in the top-down construction of a parse tree (a) type type Array [ simple ] of type (c) Array [ simple ] of type num dotdot num (d) Array [ simple ] of type num dotdot num simple (e) Array [ simple ] of type num dotdot num simple integer Steps in the top-down construction of a parse tree

Predictive parsing First Designing a Predictive Parser Left Recursion 예제로 설명 ∈-production Designing a Predictive Parser Left Recursion expr  expr + term 일반화

expr  term rest rest  + term { print(‘+’) } rest | - term { print(‘-’) } rest | term  0 { print (‘0’) } term  1 { print (‘1’) } . . . term  9 { print (‘9’) } (2.14)

Translation of 9 – 5 + 2 into 95 – 2 +. expr term rest 9 {print(‘9’)} - term {print(‘-’)} rest 5 {print(‘5’)} + term {print(‘+’)} rest 2 {print(‘2’)} Translation of 9 – 5 + 2 into 95 – 2 +.

Fig. 2. 22. Functions for the nonterminals expr, rest, and term. { term(); rest(); } rest() if(lookahead == ‘+’) { match(‘+’); term(); putchar(‘+’); rest(); else if (lookahead == ‘-’) { match(‘-’); term(); putchar(‘-’); rest(); else; term() if (isdigit(lookahead)) { putchar(lookahead); match(lookahead); else error; Fig. 2. 22. Functions for the nonterminals expr, rest, and term.

Replacement for functions expr and rest of Fig. 2.22. { term(); while(1) if(lookahead == ‘+’) { match(‘+’); term(); putchar(‘+’); } else if (lookahead == ‘-’) { match(‘-’); term(); putchar(‘-’); else break; Replacement for functions expr and rest of Fig. 2.22.

Implementing the interactions in Fig. 2. 25. uses getchar() return token to read character to caller pushes back c using ungetc(c, stdin) sets global variable to attribute value lexan() lexical analyzer tokenval Implementing the interactions in Fig. 2. 25.

Symbol table and array for storing strings.

Code layout for conditional and while statements. stmt  if expr then stmt1 { out := newlable; stmt.t := expr.t || ‘gofalse’ out || stmt1.t || ‘label’ out (2.18) IF WHILE label test code for expr gofalse out code for stmt1 goto test label out code for expr gofalse out code for stmt1 label out Code layout for conditional and while statements.

Fig. 4.15. Parsing table M for grammar (4.11) NONTER-MINAL INPUT SYMBOL id + * ( ) $ E E' T T' F E  TE' T  FT' F  id E'  +TE' T'   T'  *FT' F  (E) E'   Fig. 4.15. Parsing table M for grammar (4.11)