Programming Languages. Dr. Philip Cannata 1

Similar documents
Programming Languages. Dr. Philip Cannata 1

Functions and Recursion

JavaCC: SimpleExamples

High Level Languages. Java (Object Oriented) This Course. Jython in Java. Relation. ASP RDF (Horn Clause Deduction, Semantic Web) Dr.

fjyswan Dr. Philip Cannata 1

COMP3131/9102: Programming Languages and Compilers

Programming Languages. Dr. Philip Cannata 1

Lecture 12: Parser-Generating Tools

Names and Types. standard hue names. Dr. Philip Cannata 1

SML-SYNTAX-LANGUAGE INTERPRETER IN JAVA. Jiahao Yuan Supervisor: Dr. Vijay Gehlot

Chapter 4. Abstract Syntax

Functions and Recursion. Dr. Philip Cannata 1

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Reminder About Functions

Last Time. What do we want? When do we want it? An AST. Now!

Build your own languages with

Automated Tools. The Compilation Task. Automated? Automated? Easier ways to create parsers. The final stages of compilation are language dependant

Name EID. (calc (parse '{+ {with {x {+ 5 5}} {with {y {- x 3}} {+ y y} } } z } ) )

Syntax Analysis MIF08. Laure Gonnord

COP4020 Programming Assignment 6

Computational Expression

Chapter 2: Basic Elements of Java

Parser Combinators 11/3/2003 IPT, ICS 1

Java Bytecode (binary file)

CMPT 379 Compilers. Parse trees

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

CSE302: Compiler Design

CS 536 Midterm Exam Spring 2013

Exercise ANTLRv4. Patryk Kiepas. March 25, 2017

Name SOLUTIONS EID NOTICE: CHEATING ON THE MIDTERM WILL RESULT IN AN F FOR THE COURSE.

Fall Compiler Principles Lecture 4: Parsing part 3. Roman Manevich Ben-Gurion University of the Negev

Compilation: a bit about the target architecture

Lexical and Syntax Analysis

JavaCUP. There are also many parser generators written in Java

Course Overview. Introduction (Chapter 1) Compiler Frontend: Today. Compiler Backend:

AP Computer Science Unit 1. Programs

HW8 Use Lex/Yacc to Turn this: Into this: Lex and Yacc. Lex / Yacc History. A Quick Tour. if myvar == 6.02e23**2 then f(..!

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Lex and Yacc. A Quick Tour

LECTURE 3. Compiler Phases

RYERSON UNIVERSITY DEPARTMENT OF COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 2016

Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Agenda. Previously. Tentative syllabus. Fall Compiler Principles Lecture 5: Parsing part 4 12/2/2015. Roman Manevich Ben-Gurion University

Semantic actions for declarations and expressions

Semantic actions for declarations and expressions. Monday, September 28, 15

Lexing, Parsing. Laure Gonnord sept Master 1, ENS de Lyon

1. Download the JDK 6, from

A simple syntax-directed

A Simple Syntax-Directed Translator

Getting It Right COMS W4115. Prof. Stephen A. Edwards Spring 2007 Columbia University Department of Computer Science

Marcin Luckner Warsaw University of Technology Faculty of Mathematics and Information Science

Tree visitors. Context classes. CS664 Compiler Theory and Design LIU 1 of 11. Christopher League* 24 February 2016

Install and Configure ANTLR 4 on Eclipse and Ubuntu

Fall Compiler Principles Lecture 5: Parsing part 4. Roman Manevich Ben-Gurion University

PROGRAMMING FUNDAMENTALS

Compilers and Language Processing Tools

Introducing legacy program scripting to molecular biology toolkit (MBT)

Lexical Analysis. Introduction

Program Representations

Question Points Score

CPS 506 Comparative Programming Languages. Syntax Specification

Semantic actions for declarations and expressions

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

Reading Input from Text File

Building Compilers with Phoenix

Compiler Compiler Tutorial

CS 553 Compiler Construction Fall 2009 Project #1 Adding doubles to MiniJava Due September 8, 2009

Recitation: Loop Jul 7, 2008

Project 3 - Parsing. Misc Details. Parser.java Recursive Descent Parser... Using Lexer.java

Compilers CS S-01 Compiler Basics & Lexical Analysis

Compilers CS S-01 Compiler Basics & Lexical Analysis

CS 553 Compiler Construction Fall 2007 Project #1 Adding floats to MiniJava Due August 31, 2005

Compiling Regular Expressions COMP360

Syntax and Grammars 1 / 21

Programming Project #3: Syntax Analysis

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Project Compiler. CS031 TA Help Session November 28, 2011

COMP455: COMPILER AND LANGUAGE DESIGN. Dr. Alaa Aljanaby University of Nizwa Spring 2013

CS S-01 Compiler Basics & Lexical Analysis 1

CSEP 501 Compilers. Languages, Automata, Regular Expressions & Scanners Hal Perkins Winter /8/ Hal Perkins & UW CSE B-1

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Semantic actions for expressions

CS664 Compiler Theory and Design LIU 1 of 16 ANTLR. Christopher League* 17 February Figure 1: ANTLR plugin installer

CMSC424: Programming Project

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

ECE251 Midterm practice questions, Fall 2010

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 19: NOV. 15TH INSTRUCTOR: JIAYIN WANG

Inside OpenCCM. The ObjectWeb Consortium. Developer guide. Released: Mars Version: 1.0

CA4003 Compiler Construction Assignment Language Definition

Classes Basic Overview

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

Last time. What are compilers? Phases of a compiler. Scanner. Parser. Semantic Routines. Optimizer. Code Generation. Sunday, August 29, 2010

Lab 2 Tutorial (An Informative Guide)

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

CSE 401 Midterm Exam Sample Solution 11/4/11

Grammars and Parsing, second week

Programming Languages & Translators PARSING. Baishakhi Ray. Fall These slides are motivated from Prof. Alex Aiken: Compilers (Stanford)

Syntax Analysis The Parser Generator (BYacc/J)

Parser Generation. Prof. Dr. Ralf Lämmel Universität Koblenz-Landau Software Languages Team. Mainly with applications to! language processing

Transcription:

Programming Languages Dr. Philip Cannata

0 High Level Languages This Course Jython in Java Java (Object Oriented) ACL (Propositional Induction) Relation Algorithmic Information Theory (Information Compression and Randomness) - Kolmogorov Complexity Orc (Parallel Computing) GpH (Parallel Computing) RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata

int main () { We ll be starting with javacc moving to ANTLR later Instance of a Programming Language: } return 0 ; Internal Parse Tree Program (abstract syntax): Function = main; Return type = int params = Block: Return: Variable: return#main, LOCAL addr=0 IntValue: 0 Abstract Syntax Dr. Philip Cannata 3

We ll be starting with javacc moving to ANTLR later Dr. Philip Cannata 4

Abstract Syntax for Parser built with javacc Dr. Philip Cannata 5

Parser Files javacc demo similar to Chapter in the Textbook but in java instead of scheme $ ls Makefile Parser.jj test Dr. Philip Cannata 6

Syntax and Grammar Parser.jj PARSER_BEGIN(Parser) import java.io.*; import java.util.*; public class Parser { public static void main(string args[]) throws ParseException { Parser parser = new Parser (System.in); parser.ae(); } } Parser PARSER_END(Parser ) SKIP : { " " "\t" "\n" "\r" <"//" (~["\n","\r"])* ("\n" "\r")> } TOKEN: { < LCURLY: "{" > < RCURLY: "}" > < MINUS: "-" > < PLUS: "+" > } TOKEN: /* Literals */ { < INTEGER: (["0"-"9"])+ > } TOKEN: { <ERROR: ~[] > } Tokens, Terminals Grammar Production Rules void ae() : { Token n; } { n = <INTEGER> { System.out.print("(num " + n +")"); } list() } void list() : {} { LOOKAHEAD() <LCURLY> <PLUS> { System.out.print(" (add ");} ( ae() )* <RCURLY> { System.out.print(") "); } <LCURLY> <MINUS> { System.out.print(" (sub ");} ( ae() )* <RCURLY> { System.out.print(") "); } } Dr. Philip Cannata 7

Parser Makefile - Makefile $ cat Makefile Parser.class: Parser.java javac *java Parser.java: Parser.jj javacc Parser.jj clean: rm *.class ParseException.java Parser.java ParserConstants.java ParserTokenManager.java SimpleCharStream.java Token.java TokenMgrError.java Dr. Philip Cannata 8

Making the Parser $ make javacc Parser.jj Java Compiler Compiler Version 4.0 (Parser Generator) (type "javacc" with no arguments for help) Reading from file Parser.jj... File "TokenMgrError.java" does not exist. Will create one. File "ParseException.java" does not exist. Will create one. File "Token.java" does not exist. Will create one. File "SimpleCharStream.java" does not exist. Will create one. Parser generated successfully. javac *java Note: Parser.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Dr. Philip Cannata 9

Testing the Parser $ cat test; cat test java -cp "." Parser {+ 4 5 {- {+ 3 } 6} 0 {+ 0}} (add (num 4)(num 5) (sub (add (num )(num )(num 3)) (num 6)) (num 0) (add (num 0)) ) Dr. Philip Cannata 0

Parser Files javacc demo similar to Chapter in the Textbook but in java instead of scheme $ ls AbstractSyntax.java Makefile Parser.jj calc.sh test New $ cat calc.sh cat test cat test java Parser cat test java Parser sed -e "s/add/+/g" -e "s/sub/-/g" -e "s/(num//g" -e "s/\([0-9]\))/\/g" tr -s " " " " sed "s/^ *//" cat test java Parser sed -e "s/add/+/g" -e "s/sub/-/g" -e "s/(num//g" -e "s/\([0-9]\))/\/g" clisp --silent grep -v ">" $./calc.sh {+ 4 5 {- {+ 3} 6} 0 {+ 0}} (add (num 4) (num 5) (sub (add (num ) (num ) (num 3)) (num 6)) (num 0) (add (num 0))) (+ 4 5 (- (+ 3) 6) 0 (+ 0)) Dr. Philip Cannata

Parse {+ 3 {+ 4 5 } 6} Beginning top = sub = Abstract Syntax Tree op: nodestack Dr. Philip Cannata

After recognizing {+ top = sub = Abstract Syntax Tree op: nodestack Dr. Philip Cannata 3

After recognizing {+ 3 Abstract Syntax Tree top = sub = op: nodestack 3 3 intval: 3 Dr. Philip Cannata 4

After recognizing {+ 3 {+ Abstract Syntax Tree top = sub = 4 op: nodestack 4 3, 4 3 intval: 3 4 Dr. Philip Cannata 5

After recognizing {+ 3 {+ 4 Abstract Syntax Tree top = sub = 4 op: nodestack 4 3, 4 3 intval: 3 4 5 5 intval: 4 Dr. Philip Cannata 6

After recognizing {+ 3 {+ 4 5 Abstract Syntax Tree top = sub = 4 op: nodestack 4 3, 4 3 intval: 3 4 5, 6 5 intval: 4 6 intval: 5 Dr. Philip Cannata 7

After recognizing {+ 3 {+ 4 5 } Abstract Syntax Tree top = sub = op: nodestack 3, 4 3 intval: 3 4 5, 6 5 intval: 4 6 intval: 5 Dr. Philip Cannata 8

After recognizing {+ 3 {+ 4 5 } 6 Abstract Syntax Tree top = sub = op: nodestack 3, 4, 7 3 intval: 3 4 5, 6 5 intval: 4 6 intval: 5 7 intval: 6 Dr. Philip Cannata 9

After recognizing {+ 3 {+ 4 5 } 6 } Abstract Syntax Tree top = sub = op: 3, 4, 7 nodestack Now print the Abstract Syntax Tree starting with top 3 intval: 3 4 5, 6 5 intval: 4 6 intval: 5 7 intval: 6 Dr. Philip Cannata 0

Parser Files javacc demo3 Building a simple AST $ ls AbstractSyntax.java Makefile Parser.jj calc.sh test $ cat calc.sh cat test cat test java Parser Different $./calc.sh {+ 4 5 {- {+ 3} {+ 00 300 400} 6} 0 {+ 0}} Binary: top Binary: + 4 Binary: + 5 Binary: - Binary: + Binary: + Binary: + 3 Binary: + 00 Binary: + 300 Binary: + 400 Binary: - 6 Binary: + 0 Binary: + 0 Dr. Philip Cannata

Dr. Philip Cannata