Introduction to the 1st Obligatory Exercise

Similar documents
Theory of Compilation

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

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

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

JFlex User s Manual. Version 1.2.2, August 23, Gerwin Klein. 1 Introduction Design goals About this manual...

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

Compiler Construction

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

Lecture 9. Semantic Analysis Scoping and Symbol Table

A TOOL FOR DATA STRUCTURE VISUALIZATION AND USER-DEFINED ALGORITHM ANIMATION

Language Specification & Compiler Construction

Semantic Analysis: Types and Type Checking

Bottom-Up Parsing. An Introductory Example

Antlr ANother TutoRiaL

Some Scanner Class Methods

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

03 - Lexical Analysis

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

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

Programming Languages CIS 443

Context free grammars and predictive parsing

Compiler Construction

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

C Compiler Targeting the Java Virtual Machine

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

Parsing Technology and its role in Legacy Modernization. A Metaware White Paper

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

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

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

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

LAB4 Making Classes and Objects

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

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.

Programming Project 1: Lexical Analyzer (Scanner)

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Introduction to Java. CS 3: Computer Programming in Java

Yacc: Yet Another Compiler-Compiler

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Explain the relationship between a class and an object. Which is general and which is specific?

csce4313 Programming Languages Scanner (pass/fail)

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

Xtext Documentation. September 26, 2014

CSCI 3136 Principles of Programming Languages

Install Java Development Kit (JDK) 1.8

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

How to Improve Database Connectivity With the Data Tools Platform. John Graham (Sybase Data Tooling) Brian Payton (IBM Information Management)

Chapter 2: Elements of Java

Pemrograman Dasar. Basic Elements Of Java

CS170 Lab 11 Abstract Data Types & Objects

Example of a Java program

If-Then-Else Problem (a motivating example for LR grammars)

Introduction to Java

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

A Lex Tutorial. Victor Eijkhout. July Introduction. 2 Structure of a lex file

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Objects for lexical analysis

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Lexical Analysis and Scanning. Honors Compilers Feb 5 th 2001 Robert Dewar

J a v a Quiz (Unit 3, Test 0 Practice)

The C Programming Language course syllabus associate level

Textual Modeling Languages

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

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

Lecture 5: Java Fundamentals III

JAVA ARRAY EXAMPLE PDF

A Framework for Extensible Languages

Oracle Endeca Information Discovery Integrator

Implementing a Web Service Client using Java

How To Write A Database In Java With A New Data Type In Itunes.Com

Programming Languages

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

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

02 B The Java Virtual Machine

CS1002: COMPUTER SCIENCE OO MODELLING & DESIGN: WEEK 5

Developer's Guide: Driving Tivoli Workload Automation

Implementing Programming Languages. Aarne Ranta

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Redesign and Enhancement of the Katja System

Chapter 4 Parsing Using Java CUP Page 1 of 21

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

Using Eclipse CDT/PTP for Static Analysis

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Chapter 3. Input and output. 3.1 The System class

Moving from CS 61A Scheme to CS 61B Java

WIRIS quizzes web services Getting started with PHP and Java

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

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

Transcription:

Introduction to the 1st Obligatory Exercise Scanning, parsing, constructing the abstract syntax tree Department of Informatics University of Oslo Compiler technique, Friday 22 2013 (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 1 / 17

Scanner and Parser Generators JFlex JFlex Is specified in.lex file Specification consists of three parts: User code Options and macros Lexical rules (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 2 / 17

Scanner and Parser Generators JFlex USER CODE package oblig1parser import java_cup.runtime.* OPTIONS / MACROS %% %unicode %cup %{ private Symbol symbol(int type) { return new Symbol(type, yyline, yycolumn) % Identifier = [:jletter:] [:jletterdigit:]* %% Code copied to Lexer.java Lexical State LEXICAL RULES <YYINITIAL> { "class" { return symbol(sym.class) {Identifier { return symbol(sym.id, yytext()) (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 3 / 17

Scanner and Parser Generators CUP Java Based Constructor of Useful Parsers (CUP) Is specified in.cup file Specification consists of five parts: Package and import specifications User code components Symbol list Prcedence declarations Grammar (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 4 / 17

Scanner and Parser Generators CUP PACKAGE / IMPORTS USER CODE SYMBOL LIST PRECEDENCE DECLARATIONS package oblig1parser import java_cup.runtime.* import syntaxtree.* parser code {: : terminal terminal String non terminal ClassDecl precedence left OR precedence left AND CLASS ID class_decl, decl program::= decl_list:dl {: RESULT = new Program(dl) : Code copied to parser.java GRAMMAR decl_list ::= decl:d {: List<ClassDecl> l = new LinkedList<ClassDecl>() l.add(d) RESULT = l : decl_list:dl decl:d {: dl.add(d) RESULT = dl : decl ::= class_decl:sd {: RESULT = sd : class_decl ::= CLASS ID:name LBRACK RBRACK {: RESULT = new ClassDecl(name) : (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 5 / 17

Scanner and Parser Generators CUP PACKAGE / IMPORTS USER CODE SYMBOL LIST PRECEDENCE DECLARATIONS GRAMMAR package oblig1parser import java_cup.runtime.* import syntaxtree.* parser code {: : terminal terminal String non terminal ClassDecl precedence left OR precedence left AND CLASS ID class_decl, decl program::= decl_list:dl {: RESULT = new Program(dl) : 5. 3. 4. decl_list ::= decl:d {: List<ClassDecl> l = new LinkedList<ClassDecl>() l.add(d) RESULT = l : decl_list:dl decl:d {: dl.add(d) RESULT = dl : decl ::= class_decl:sd {: RESULT = sd : class_decl ::= CLASS ID:name LBRACK RBRACK {: RESULT = new ClassDecl(name) : 2. 1. (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 6 / 17

Scanner and Parser Generators CUP Conflicts Two types of conflicts may occur: Shift-Reduce in which CUP chooses to reduce Reduce-Reduce in which CUP chooses first rule (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 7 / 17

Scanner and Parser Generators Integration JFLEX Symbol CUP "class" { return symbol(sym.class) public interface sym { public static final int NOT = 36 public static final int AND = 21 public static final int RPAR = 42 public static final int COMMA = 37 public static final int CLASS = 2 public static final int REFERENCE = 5 (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 8 / 17

Abstract syntax tree model Using Java Each required node in the tree model is represented by a Java class package syntaxtree.nonterminals import java.util.list import utils.utils public class ClassDecl extends Decl { private String name private List<VarDecl> vardecls public ClassDecl( String name, List<VarDecl> vardecls ) { this.name = name this.vardecls = vardecls public void extractast( StringBuilder sb, String indent ) { sb.append( "\n" + indent + "(CLASS_DECL (NAME " + name + ")" )... (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 9 / 17

Overview of provided code Class files for compiler, lexer, parser, syntaxtree, etc. Oblila source code build oblila-code Three pairs of.lex/.cup files Java source code for compiler, syntax tree, etc. grammars src Test file for example parser Java source code example syntax tree input-examples lib JFlex and CUP libs src-examples src-gen Generated Java source code for lexer and parser Generated abstract syntax tree oblila-ast (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 10 / 17

Overview of provided code grammars Three pairs of.lex/.cup files grammars Three grammar pairs expression-eval (example expression language) expression-par (example language handling parentheses) oblig1 (oblig grammars) (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 11 / 17

Overview of provided code oblila-ast Generated abstract syntax tree oblila-ast One abstract syntax tree Oblila.ast (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 12 / 17

Overview of provided code oblila-code Oblila source code oblila-code One Oblila source file Oblila.obl (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 13 / 17

Overview of provided code src src Java source code for compiler, syntax tree, etc. Two folders compiler syntaxtree (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 14 / 17

Using build script Ant Ant targets build.xml contains an Ant-based build script with targets and task definitions $ ant clean $ ant build $ ant run... (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 15 / 17

The tasks of this assignment Three main parts Create JFlex specification Create CUP specification (two versions) Define abstract syntax tree model using Java USER CODE OPTIONS / MACROS package oblig1parser import java_cup.runtime.* %% %unicode %cup %{ private Symbol symbol(int type) { return new Symbol(type, yyline, yycolumn) % Code copied to Lexer.java PACKAGE / IMPORTS USER CODE SYMBOL LIST PRECEDENCE DECLARATIONS package oblig1parser import java_cup.runtime.* import syntaxtree.* parser code {: : terminal CLASS terminal String ID non terminal ClassDecl class_decl, decl precedence left OR precedence left AND Code copied to parser.java package syntaxtree.nonterminals import java.util.list import utils.utils public class ClassDecl extends Decl { private String name private List<VarDecl> vardecls LEXICAL RULES Identifier = [:jletter:] [:jletterdigit:]* %% Lexical State <YYINITIAL> { "class" { return symbol(sym.class) {Identifier { return symbol(sym.id, yytext()) GRAMMAR program::= decl_list:dl {: RESULT = new Program(dl) : decl_list ::= decl:d {: List<ClassDecl> l = new LinkedList<ClassDecl>() l.add(d) RESULT = l : decl_list:dl decl:d {: dl.add(d) RESULT = dl : decl ::= class_decl:sd {: RESULT = sd : class_decl ::= CLASS ID:name LBRACK RBRACK {: RESULT = new ClassDecl(name) : public ClassDecl( String name, List<VarDecl> vardecls ) { this.name = name this.vardecls = vardecls public void extractast( StringBuilder sb, String indent ) { sb.append( "\n" + indent + "(CLASS_DECL (NAME " + name + ")" )... (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 16 / 17

Additional information More information Exercise and Oblila descriptions JFlex manual CUP manual (Department of Informatics, UiO) Introduction to the 1st Obligatory Exercise INF5110/9110 2013 17 / 17