Theory of Compilation



Similar documents
Introduction to the 1st Obligatory Exercise

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

CUP User's Manual. Scott E. Hudson Graphics Visualization and Usability Center Georgia Institute of Technology. Table of Contents.

Programming Project 1: Lexical Analyzer (Scanner)

Compiler Construction

Antlr ANother TutoRiaL

Flex/Bison Tutorial. Aaron Myles Landwehr CAPSL 2/17/2012

CSCI 3136 Principles of Programming Languages

How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer

Introduction to Java

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Compiler I: Syntax Analysis Human Thought

ANTLR - Introduction. Overview of Lecture 4. ANTLR Introduction (1) ANTLR Introduction (2) Introduction

JFlex User s Manual. The Fast Lexical Analyser Generator. Contents. Copyright c by Gerwin Klein, Steve Rowe, and Régis Décamps.

Install Java Development Kit (JDK) 1.8

Programming Assignment II Due Date: See online CISC 672 schedule Individual Assignment

Compiler Construction

Programming Languages CIS 443

7. Building Compilers with Coco/R. 7.1 Overview 7.2 Scanner Specification 7.3 Parser Specification 7.4 Error Handling 7.5 LL(1) Conflicts 7.

Semantic Analysis: Types and Type Checking

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Introduction to Lex. General Description Input file Output file How matching is done Regular expressions Local names Using Lex

Scanning and parsing. Topics. Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm

Evaluation of JFlex Scanner Generator Using Form Fields Validity Checking

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Compilers Lexical Analysis

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Static Analyzers. Context. Learning Objectives

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

Example of a Java program

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Textual Modeling Languages

Basics of Java Programming Input and the Scanner class

Lecture 9. Semantic Analysis Scoping and Symbol Table

sveltest: A testing language

Introduction. Compiler Design CSE 504. Overview. Programming problems are easier to solve in high-level languages

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

Part I. Multiple Choice Questions (2 points each):

5HFDOO &RPSLOHU 6WUXFWXUH

Moving from CS 61A Scheme to CS 61B Java

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Java Crash Course Part I

The following program is aiming to extract from a simple text file an analysis of the content such as:

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Using Files as Input/Output in Java 5.0 Applications

Chapter 2: Elements of Java

C Compiler Targeting the Java Virtual Machine

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Overview of Web Services API

Consuming a Web Service(SOAP and RESTful) in Java. Cheat Sheet For Consuming Services in Java

Free Java textbook available online. Introduction to the Java programming language. Compilation. A simple java program

Introduction to formal semantics -

Language Specification & Compiler Construction

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Compilation 2012 Domain-Specific Languages and Syntax Extensions

SABLECC, AN OBJECT-ORIENTED COMPILER FRAMEWORK

Objects for lexical analysis

Lex et Yacc, exemples introductifs

CSE 308. Coding Conventions. Reference

How to use the Eclipse IDE for Java Application Development

Smooks Dev Tools Reference Guide. Version: GA

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Master of Sciences in Informatics Engineering Programming Paradigms 2005/2006. Final Examination. January 24 th, 2006

Introduction to Python

Getting Started with the Internet Communications Engine

Programming Languages

Topic 11 Scanner object, conditional execution

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Building a Multi-Threaded Web Server

Chapter 2 Introduction to Java programming

Java Programming Fundamentals

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. AVRO Tutorial

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Pemrograman Dasar. Basic Elements Of Java

Introduction to Programming

AP Computer Science Java Subset

CS170 Lab 11 Abstract Data Types & Objects

A Framework for Extensible Languages

Hadoop + Clojure. Hadoop World NYC Friday, October 2, Stuart Sierra, AltLaw.org

Object Oriented Software Design

Lexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

02 B The Java Virtual Machine

Tutorial: Getting Started

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

How To Write A Program In Java (Programming) On A Microsoft Macbook Or Ipad (For Pc) Or Ipa (For Mac) (For Microsoft) (Programmer) (Or Mac) Or Macbook (For

Sample CSE8A midterm Multiple Choice (circle one)

Continuous Integration Part 2

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

CS 111 Classes I 1. Software Organization View to this point:

5 HDFS - Hadoop Distributed System

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

Analyse et Conception Formelle. Lesson 5. Crash Course on Scala

public static void main(string[] args) { System.out.println("hello, world"); } }

Introduction to Java. CS 3: Computer Programming in Java

Transcription:

Theory of Compilation JLex, CUP tools CS Department, Haifa University Nov, 2010 By Bilal Saleh 1

Outlines JLex & CUP tutorials and Links JLex & CUP interoperability Structure of JLex specification JLex specification compilation Structure of CUP specification CUP specification compilation AST Nodes Integration in Eclipse workspace 2

Tutorials & links JLex: lexical analyzer generator in Java. Online tutorial http:// home.in.tum.de/~kleing/jflex/manual.html CUP: parser generator in Java. Online tutorial http:// www.cs.princeton.edu/~appel/modern/java/cup/manual.html A ready-to-use JLex_CUP workspace: http://cs.haifa.ac.il/courses/compilers/bilal/jlex_cup.zip 3

JLex & CUP interoperability 4

Interoperability sym.class AST nodes Source code Lexical analyzer tokens Parser Target code Javac: the java compiler Lexical analyzer (*.java) Parser (*.java) sym.java Nodes of the AST (*.java) JLex CUP JLex spec (*.lex) CUP spec (*.cup) 5

JLex specification 6

sym.class AST nodes Source code Lexical analyzer tokens Parser Target code Javac: the java compiler Lexical analyzer (*.java) Parser (*.java) sym.java Nodes of the AST (*.java) JLex CUP JLex spec (*.lex) CUP spec (*.cup) 7

JLex specification Consists of 3 sections 1. User code: Package name Import packages of java 2. Options & declarations: Specifying directives such as %cup, %byacc The code included in %{ %} will be automatically injected inside the generated lexer Defining macros that might be used in the Lexical rules section 3. Lexical rules Contains the regular expressions and actions to be performed Sections are separated by %% 8

JLex specification example package miny_pascal; import java_cup.runtime.symbol; import java.io.fileinputstream; import java.io.inputstream; %% %cup %line %{ private int countlines(string str){ } %} DIGIT = [0-9] LETTER = [a-za-z_] IDE INT %% = {LETTER}({LETTER} {DIGIT})* = {DIGIT}+ "IF" { return new Symbol(sym.IF); } "+" { return new Symbol(sym.ADD); } {INT} { return new Symbol(sym.INTCONST, new Integer(Integer.parseInt(yytext()))); } {IDE} { return new Symbol(sym.IDE, yytext()); } [\n] { ++yyline; } [\r\t\f\ ]+ { } 9

Some notes JLex designates tokens with longest match, for example input: abc rule: [a-z]+ result will be abc (not a, ab) JLex uses the first applicable rule, for example input: FOR rule1: FOR rule2: [a-za-z]+ JLex will choose rule1 10

Compiling JLex specification 11

sym.class AST nodes Source code Lexical analyzer tokens Parser Target code Javac: the java compiler Lexical analyzer (*.java) Parser (*.java) sym.java Nodes of the AST (*.java) JLex CUP JLex spec (*.lex) CUP spec (*.cup) 12

Compiling JLex specification 1. Download JLex_CUP.zip package from the course website. 2. Extract it somewhere in your hard drive (e.g. C:\tmp). Tree view looks like this: C:\ +--tmp\ +--JLex\ +--Main.java +--java_cup\ +--Main.java +--runtime\ +--Yylex.lex +--Parser.cup 3. Modify your Yylex.lex specification as you desire. 4. Compile Yylex.lex: C:\tmp>java JLex.Main Yylex.lex 5. If compiled successfully, it will output file Yylex.lex.java under C:\tmp 6. Rename Yylex.lex.java to Yylex.java 13

CUP specification 14

sym.class AST nodes Source code Lexical analyzer tokens Parser Target code Javac: the java compiler Lexical analyzer (*.java) Parser (*.java) sym.java Nodes of the AST (*.java) JLex CUP JLex spec (*.lex) CUP spec (*.cup) 15

CUP specification Package & import declarations User code components (linking with the lexer) Symbols (terminal & non terminal) lists Precedence declaration Grammar (context-free) 16

Package & import declarations package miny_pascal; import java_cup.runtime.*; import java.io.fileinputstream; import java.io.inputstream; 17

User code components /* Preliminaries to set up and use the scanner. */ parser code {: public Node root = null; public static parser getparser(string ppath) throws Exception { InputStream is = null; is = new FileInputStream(pPath); return new parser(new Yylex(is)); } public Node gettree() throws Exception { if (root == null) { this.parse(); } return root; } public static void main(string args[]) throws Exception { new parser(new Yylex(System.in)).parse(); } :} 18

Terminals & non terminals /* Terminals (tokens returned by the scanner). */ terminal PROGRAM, BEGIN, END, DECLARE, PROCEDURE, FUNCTION, terminal BOOLEAN, ARRAY, OF, ASSIGN, LC, RC, IF, THEN, ELSE, terminal READ, WRITE, TRUE, FALSE, ADD, MIN, MUL, DIV, GOTO; terminal MOD, LES, LEQ, EQU, NEQ, GRE, GEQ, AND, OR; terminal NOT, CASE, FOR, FIN, IDENTICAL, FROM, BY, TO, NEW; terminal UMIN, COLON, SEMI, LPAR, RPAR, LPAR_SQ, RPAR_SQ, DOT, COMMA, PTR; /* Terminals with attached values */ terminal Integer INTCONST; terminal String IDE; terminal Double REALCONST; terminal String STRING; /* Non terminals */ non terminal Node var, assign, program, stat_seq, loop_stat, case_stat, non terminal Node expr, atom, block, stat, nonlable_stat, cond_stat, case, non terminal Node var_decl, type, simple_type, array_type, record_type, non terminal Node record_list, dim, dim_list, proc_decl, formal_list, non terminal Node inout_stat, new_stat; 19

Precedence declaration /* Precedence List */ precedence nonassoc LES, LEQ, EQU, NEQ, GRE, GEQ; precedence left ADD, MIN, OR; precedence left MUL, DIV, AND, MOD; precedence left UMIN; precedence right NOT; precedence right DOT; precedence right PTR;; 20

Grammar (context-free) /* Grammar */ start with program; program ::= PROGRAM IDE:n block:b {: RESULT = new Program(b,n); parser.root=result; :} ; block ::= LC stat_seq:s RC {: RESULT = new Block(s); :} decl_list:d LC stat_seq:s RC {: RESULT = new Block(d,s); :} ; decl_list ::= decl:d {: RESULT = new DeclarationList(d); :} decl:d decl_list:dl {: RESULT = new DeclarationList(dl,d); :} ; decl ::= var_decl:vd {: RESULT = vd; :} proc_decl:pd {: RESULT = pd; :} func_decl:fd {: RESULT = fd; :} ; assign ::= var:v ASSIGN expr:e {: RESULT = new Assign(e,v); :} ; cond_stat ::= IF expr:e THEN stat_seq:ss FI {: RESULT = new ConditionalStatement(e,ss); :} 21

Compiling CUP specification 22

sym.class AST nodes Source code Lexical analyzer tokens Parser Target code Javac: the java compiler Lexical analyzer (*.java) Parser (*.java) sym.java Nodes of the AST (*.java) JLex CUP JLex spec (*.lex) CUP spec (*.cup) 23

Compiling CUP specification 1. Download JLex_CUP.zip package from the course website. 2. Extract it somewhere in your hard drive (e.g. C:\tmp). Tree view looks like this: C:\ +--tmp\ +--JLex\ +--Main.java +--java_cup\ +--Main.java +--Yylex.lex +--Parser.cup +--runtime\ 3. Modify your Parser.cup specification as you desire. 4. Compile Parser.cup: C:\tmp>java java_cup.main expect 1 Parser.cup 5. If compiled successfully, it will output file parser.java & sym.java under C:\tmp 24

AST Nodes 25

sym.class AST nodes Source code Lexical analyzer tokens Parser Target code Javac: the java compiler Lexical analyzer (*.java) Parser (*.java) sym.java Nodes of the AST (*.java) JLex CUP JLex spec (*.lex) CUP spec (*.cup) 26

27

Integration in Eclipse Project 28

Eclipse workspace In HW2 & HW3 you have to implement the Code-Generation part inside Node.java If you need to modify the parser/lexer, follow the instructions from previous slides and then replace Yylex.java, sym.java, parser.java by your own classes. 29