1 Program exercise -Yacc Use Lex and Yacc to generate a compiler for Micro/Ex Micro/Ex is an extension of Micro. %the beginning of an test data for Micro/Ex.

Slides:



Advertisements
Similar presentations
Intermediate Code Generation
Advertisements

Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 9 Imperative and object-oriented languages 1.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 5 Function Basics.
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
Program Design and Development
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
ISBN Chapter 10 Implementing Subprograms –Semantics of Calls and Returns –Implementing “Simple” Subprograms –Implementing Subprograms with.
Chapter 10 Implementing Subprograms. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Semantics of Call and Return The subprogram call and return.
Hello, world! Dissect HelloWorld.java Compile it Run it.
Final Exam Review Instructor : Yuan Long CSC2010 Introduction to Computer Science Apr. 23, 2013.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
High-Level Programming Languages: C++
ISBN Chapter 10 Implementing Subprograms.
Lesson 3 Two topics 1. Semantic Analysis 2. Translation to intermediate code Presented as separated topics but can be implemented together.
Session One Introduction. Personal Introduction Role of programmers Robot Examination HUD & HID Uploading Code.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design First Edition by Tony Gaddis.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
程式語言結構 Final Project. Goal The student can gain the knowledge of the object operation for the Java programming language. –Class –Interface.
Chapter 8 High-Level Programming Languages. 8-2 Chapter Goals Describe the translation process and distinguish between assembly, compilation, interpretation,
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
I Power Int 2 Computing Software Development High Level Language Constructs.
Machine Independent Macro Processor Features Concatenation of Macro Parameters Generation of Unique Labels Conditional Macro Expansion Keyword Macro.
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Code generation Joey Paquet,
Group 4 Java Compiler Group Members: Atul Singh(Y6127) Manish Agrawal(Y6241) Mayank Sachan(Y6253) Sudeept Sinha(Y6483)
程式語言結構 Final Project 老師: Gwan-Hwan Hwang. Goal The students can gain the knowledge of the object operation for the Java programming language. –Class –Interface.
Looping and Counting Lecture 3 Hartmut Kaiser
C463 / B551 Artificial Intelligence Dana Vrajitoru Python.
© ABB University - 1 Revision C E x t e n d e d A u t o m a t i o n S y s t e m x A Chapter 11 Structured Text Course T314.
1 Using Yacc. 2 Introduction Grammar –CFG –Recursive Rules Shift/Reduce Parsing –See Figure 3-2. –LALR(1) –What Yacc Cannot Parse It cannot deal with.
COMPILER CONSTRUCTION Lesson 1 – TDDD16 TDDB44 Compiler Construction 2010 Kristian Stavåker (Erik Hansson.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
Advanced Arithmetic, Conditionals, and Loops INFSY 535.
Review for Final Exam. Contents 5 questions (20 points each) + 1 bonus question (20 points) – Basic concepts in Chapters 1-4 – Chapters 5-9 – Bonus: Chapter.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Higher Computing Science 2016 Prelim Revision. Topics to revise Computational Constructs parameter passing (value and reference, formal and actual) sub-programs/routines,
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Review on Program Challenge CSc3210 Yuan Long.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
1 Software Project n Design and implement an assembly for SIC/XE The test data look like Figure 2.5. and are generated by MS Notepad. The possible OPCODE.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
K.K. Leung Fall 2008Introductory Pentium Programming1 Pentium Architecture: Introductory Programming Kin K. Leung
PHP – PHP Hypertext Processor A quick overview. How is PHP used? Embedded with HTML, e.g. Not like CGI: PHP files not an executable Used with servers.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Topics to be covered Instruction Execution Characteristics
Implementing Subprograms
The Machine Model Memory
The Java Virtual Machine (JVM)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Programming Language Concepts (CIS 635)
Review for Final Exam.
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Some Common Issues: Iteration
CS 153: Concepts of Compiler Design November 6 Class Meeting
C Programming Language
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Names of variables, functions, classes
General Computer Science for Engineers CISC 106 Lecture 03
C Language B. DHIVYA 17PCA140 II MCA.
CMPE 152: Compiler Design April 16 Class Meeting
Presentation transcript:

1 Program exercise -Yacc Use Lex and Yacc to generate a compiler for Micro/Ex Micro/Ex is an extension of Micro. %the beginning of an test data for Micro/Ex Program testP Begin declare I as integer; declare A,B,C,D, LLL[100] as float; FOR (I:=1 TO 100) A:=-LLL[I]+B*D-C; ENDFOR IF (A>= ) THEN print(1); ELSE print(2,1.4); ENDIF End

2 Micro/Ex is an extension of Micro (Cont’d) Variables must be declared before referenced. FOR construct FOR (I:=1 TO 100) A:=-LLL[I]+B*D-C; ENDFOR FOR (I:=100 DOWNTO 1) A:=-LLL[I]+B*D-C; ENDFOR

3 Micro/Ex is an extension of Micro (Cont’d) IF-ENDIF and IF-ELSE_ENDIF construct IF (A>= ) THEN print(5*3+1); ENDIF IF (A>= ) THEN print(1); ELSE print(2,1.4); ENDIF Only simple Boolean expression.

4 Micro/Ex is an extension of Micro (Cont’d) Subroutine call IF (A>= ) THEN print(5*3+1); ENDIF IF (A>= ) THEN print(1); ELSE print(2,1.4); ENDIF It can have multiple actual parameters. Each actual parameter can be an expression.

5 Program exercise -Yacc Target Language –Three-address machine Variable declaration instruction –Declare A, Integer –Declare A, Integer_array,20 –Declare B, Float –Declare B, Float_array,20 Arithmetic instruction –I_SUB i1,i2,t –I_ADD i1,i2,t –I_DIV i1,i2,t –I_MUL i1,i2,t –I_UMINUS i1,t –INC I »I=I+1 –DEC I »I=I-1

6 Program exercise -Yacc Arithmetic instruction –F_SUB f1,f2,t –F_ADD f1,f2,t –F_DIV f1,f2,t –F_MUL f1,f2,t –F_UMINUS f1,t Assignment –I_Store i1,t –F_Store f1,t Compare instruction –I_CMP i1,i2 –F_CMP f1,f2 Jump instruction –J,JE, JG, JGE, JL, JLE, JNE Subroutine operation –CALL rn,a1,a2 »rn: the name of the subroutine »a1 and a2 could be integer literal, float point literal, or id.

7 Program exercise -Yacc Logical instruction –AND b1,b2,t »t will be 0 or 1 after the execution of this instruction –OR b1,b2,t »t will be 0 or 1 after the execution of this instruction –NOT b, t »b will be 0 or 1 after the execution of this instruction

8 Program exercise -Yacc %the beginning of an test data for Micro/Ex Program testP Begin declare I as integer; declare A,B,C,D, LLL[100] as float; FOR (I:=1 TO 100) A:=-LLL[I]+B*D-C; ENDFOR IF (A>= ) THEN print(A+3.14); ELSE print(2,1.4); ENDIF End START testP Declare I, Integer Declare A, Float Declare B, Float Declare C, Float Declare D, Float Declare LLL, Float_array,100 I_STORE 1,I lb&1:F_MUL B,D,T&1 F_UMINUS LLL[I],T&2 F_ADD T&2, T&1, T&3 F_SUB T&3,C,T&4 F_STORE T&4,A INC I I_CMP I,100 JL lb&1 F_CMP A, JL lb&2 F_ADD A, 3.14, T&5 CALL print, T&5 J lb&3 lb&2:CALL print,2,1.4 lb&3:HALT testP Declare T&1, Float Declare T&2, Float Declare T&3, Float Declare T&4, Float Declare T&5, Float

9 Bonus In case your Micro/Ex compiler can do the code generation of the following constructs

10 Program exercise -Yacc (1) To support more complex FOR construct FOR (I:=1 TO 100*J+6 STEP 5) A:=-LLL[I]+B*D-C; ENDFOR FOR (I:=2*J-4 DOWNTO 5 STEP 4) A:=-LLL[I]+B*D-C; ENDFOR

11 Program exercise -Yacc (2) To support WHILE construct %the beginning of an test data for Micro/Ex Program testP Begin declare I as integer; declare A,B,C,D, LLL[100] as float; I:=1; WHILE (I<=100) A:=-LLL[I]+B*D-C; I:=1+1; ENDWHILE IF (A>= ) THEN print(A+3.14); ELSE print(2,1.4); ENDIF End

12 Program exercise -Yacc (3) To support nested structure %the beginning of an test data for Micro/Ex Program testP Begin declare I,J as integer; declare A,B,C,D, LLL[100] as float; I:=1; WHILE (I<=100) A:=-LLL[I]+B*D-C; I:=1+1; FOR (I:=1 TO 100) A:=A*3.0; ENDFOR ENDWHILE IF (A>= ) THEN IF (B<=0.0) THEN print(A+3.14); ELSE print(A+3.14*10); ENDIF ELSE print(2,1.4); ENDIF End

13 Program exercise -Yacc (4) To support sophisticated logical expressions WHILE ((I 10)) A:=-LLL[I]+B*D-C; I:=1+1; ENDWHILE IF (!((A>=10000)|| (C<100))) THEN print(A+3.14); ELSE print(2,1.4); ENDIF End It adopts the logical expression of C.

14 Program exercise -Yacc (5) To support user-defined function and static type checking

15 %the beginning of an test data for Micro/Ex Program testP Function integer Cal_Something(integer I, float f) Begin declare k as integer; ………. return k; End Begin declare I,J as integer; declare A,B,C,D, LLL[100] as float; FOR (I:=1 TO 100) A:=-LLL[I]+B*D-C; J:=Cal_Something(I,A); ENDFOR End START testP Declare Cal_Something, Function,I,f Declare k, integer;. Return k Declare I, Integer Declare J,Integer Declare A, Float Declare B, Float Declare C, Float Declare D, Float Declare LLL, Float_array,100 I_STORE 1,I lb&1:F_MUL B,D,T&1 F_UMINUS LLL[I],T&2 F_ADD T&2, T&1, T&3 F_SUB T&3,C,T&4 F_STORE T&4,A I_STORE Cal_Something(I,A),J INC I I_CMP I,100 JL lb&1 HALT testP Declare T&1, Float Declare T&2, Float Declare T&3, Float Declare T&4, Float Note that variables declared in functions should not have the same names as variables defined in main program.

16 Demonstration Each student will have ten minutes to make the demonstration of his program. TAs will schedule and watch the demonstrations. –Then, TAs will report to G. H. Hwang.

17 Project Report The student should prepare a report which contains at least the follows: –The source code and execution results If you have your own test data, you can show it. –What you have learned and experienced during the implementation of Micro/Ex compiler. E.g. You could show your daily record of the implementation. –In case you implement more than the required specification, please itemize it. –Copyright Claim Do you make the implementation yourself? –Any thing you would like to let G.H.Hwang know. E.g. Suggestion, … Who will be reading the report? –Not TAs but G. H. Hwang

18 How to hand in your report? Please send a mail to TA with a zip file –Mail title: Compiler final project + your student id –Attached filename: your_student_id.zip –It should have the at least the following items: Electronic files of your report –MS word and (or) pdf Source codes (yacc & lex) Your test data and the corresponding execution results.