Dr. Philip Cannata 1 Programming Languages. Dr. Philip Cannata 2 10 Java (Object Oriented) ACL2 (Propositional Induction) Algorithmic Information Theory.

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

JavaCUP JavaCUP (Construct Useful Parser) is a parser generator
Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
C++ Classes & Data Abstraction
1 JavaCUP JavaCUP (Construct Useful Parser) is a parser generator Produce a parser written in java, itself is also written in Java; There are many parser.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Environments and Evaluation
Professor Yihjia Tsai Tamkang University
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
Getting to Know Decaf Goals for Today: Get acquainted with the Decaf language Get experience with ASTs.
Dr. Philip Cannata 1 Programming Languages. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
ANTLR with ASTs. Abstract Syntax Trees ANTLR can be instructed to produce ASTs for the output of the parser ANTLR uses a prefix notation for representing.
ANTLR Andrew Pangborn & Zach Busser. ANTLR in a Nutshell ANother Tool for Language Recognition generates lexers generates parsers (and parse trees)‏ Java-based,
Attribute Grammars They extend context-free grammars to give parameters to non-terminals, have rules to combine attributes Attributes can have any type,
Shorthand operators.
StringTemplate Terence Parr University of San Francisco
Dr. Philip Cannata 1 with ReL. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython in Java This.
Dr. Philip Cannata 1 fJyswan. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython in Java This.
1 Languages and Compilers (SProg og Oversættere) Parsing.
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
10/1/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Autumn 2009.
Dr. Philip Cannata 1 Programming Languages. Dr. Philip Cannata 2 Class Website
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Chapter 5: Preparing Java Programs 1 Chapter 5 Preparing Java Programs.
1 Top Down Parsing. CS 412/413 Spring 2008Introduction to Compilers2 Outline Top-down parsing SLL(1) grammars Transforming a grammar into SLL(1) form.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Dr. Philip Cannata 1 Functions and Recursion. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.
A First Simple Program /* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main().
Dr. Philip Cannata 1 Lexical and Syntactic Analysis Chomsky Grammar Hierarchy Lexical Analysis – Tokenizing Syntactic Analysis – Parsing Hmm Concrete Syntax.
CS453 LectureIntroduction1 CS453 Compiler Construction Instructor:Wim Bohm Computer Science Building 470 TA: tba
COMP Parsing 3 of 4 Lectures 23. Using the Scanner Break input into tokens Use Scanner with delimiter: public void parse(String input ) { Scanner.
Parse & Syntax Trees Syntax & Semantic Errors Mini-Lecture.
Mixing integer and floating point numbers in an arithmetic operation.
CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera.
CS101: Introduction to Computer Science Slides adapted from Sedgewick and Wayne Copyright © Your First Java.
CS 153: Concepts of Compiler Design October 21 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
12/18/2015© Hal Perkins & UW CSEG-1 CSE P 501 – Compilers Intermediate Representations Hal Perkins Winter 2008.
ERRORS. Types of errors: Syntax errors Logical errors.
CS453 LectureIntroduction1 CS453 Compiler Construction Original Design: Michelle Strout Instructor:Wim Bohm
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
Bernd Fischer COMP2010: Compiler Engineering Abstract Syntax Trees.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Syntax-Directed Definitions CS375 Compilers. UT-CS. 1.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
Dr. Philip Cannata 1 Functions and Recursion Programming Languages.
MiniJava Compiler A multi-back-end JIT compiler of Java.
Dr. Philip Cannata 1 Names, Types, and Functions standard hue names.
Introduction to Computer Science What is Computer Science? Getting Started Programming.
Comp 411 Principles of Programming Languages Lecture 3 Parsing
COMP261 Lecture 18 Parsing 3 of 4.
Crash course in the Java Programming Language
CS 3304 Comparative Languages
Java CUP.
CS 614: Theory and Construction of Compilers
Programming Language Concepts
Compiler Designs and Constructions
Syntax-Directed Translation
Intermediate Representations Hal Perkins Autumn 2011
Syntax-Directed Definition
Is everyone signed up on piazza?
Programming Languages
Parsing & Scanning Lecture 2
The Boolean (logical) data type boolean
CSE401 Introduction to Compiler Construction
Intermediate Representations Hal Perkins Autumn 2005
Programming Languages
Introduction to ANTLR Jin Tianxing
Presentation transcript:

Dr. Philip Cannata 1 Programming Languages

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

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

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

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

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

Dr. Philip Cannata 7 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_END(Parser ) SKIP : { " " | "\t" | "\n" | "\r" | } TOKEN: { | } TOKEN: /* Literals */ { } TOKEN: { } void ae() : { Token n; } { n = { System.out.print("(num " + n +")"); } | list() } void list() : {} { LOOKAHEAD(2) { System.out.print(" (add ");} ( ae() )* { System.out.print(") "); } | { System.out.print(" (sub ");} ( ae() )* { System.out.print(") "); } } Parser Grammar Production Rules Tokens, Terminals Syntax and Grammar – Parser.jj

Dr. Philip Cannata 8 $ 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 Parser Makefile - Makefile

Dr. Philip Cannata 9 $ 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. Making the Parser

Dr. Philip Cannata 10 $ cat test; cat test | java -cp "." Parser {+ 4 5 {- { } 6} 101 {+ 102}} (add (num 4)(num 5) (sub (add (num 1)(num 2)(num 3)) (num 6)) (num 101) (add (num 102)) ) Testing the Parser

Dr. Philip Cannata 11 $ ls AbstractSyntax.java Makefile Parser.jj calc.sh test $ 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]\))/\1/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]\))/\1/g" | clisp --silent | grep -v ">" $./calc.sh {+ 4 5 {- { } 6} 101 {+ 102}} (add (num 4) (num 5) (sub (add (num 1) (num 2) (num 3)) (num 6)) (num 101) (add (num 102))) (+ 4 5 (- ( ) 6) 101 (+ 102)) 212 New Parser Files – javacc demo2 – similar to Chapter 2 in the Textbook but in java instead of scheme

Dr. Philip Cannata 12 1 op: “” intval: 0 children: top = sub = 1 nodeStack 1 Parse {+ 3 {+ 4 5 } 6} Abstract Syntax Tree Beginning

Dr. Philip Cannata 13 After recognizing {+ top = 1 sub = 2 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children: nodeStack 2 1 Abstract Syntax Tree

Dr. Philip Cannata 14 After recognizing {+ 3 top = 1 sub = 2 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children: 3 nodeStack 2 1 Abstract Syntax Tree 3 op: “int” intval: 3 children:

Dr. Philip Cannata 15 After recognizing {+ 3 {+ top = 1 sub = 4 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children: 3, 4 nodeStack Abstract Syntax Tree 3 op: “int” intval: 3 children: 4 op: “+” intval: 0 children:

Dr. Philip Cannata 16 After recognizing {+ 3 {+ 4 top = 1 sub = 4 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children: 3, 4 nodeStack Abstract Syntax Tree 3 op: “int” intval: 3 children: 4 op: “+” intval: 0 children: 5 5 op: “int” intval: 4 children:

Dr. Philip Cannata 17 After recognizing {+ 3 {+ 4 5 top = 1 sub = 4 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children: 3, 4 nodeStack Abstract Syntax Tree 3 op: “int” intval: 3 children: 4 op: “+” intval: 0 children: 5, 6 5 op: “int” intval: 4 children: 6 op: “int” intval: 5 children:

Dr. Philip Cannata 18 After recognizing {+ 3 {+ 4 5 } top = 1 sub = 2 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children:3, 4 nodeStack 2 1 Abstract Syntax Tree 3 op: “int” intval: 3 children: 4 op: “+” intval: 0 children: 5, 6 5 op: “int” intval: 4 children: 6 op: “int” intval: 5 children:

Dr. Philip Cannata 19 After recognizing {+ 3 {+ 4 5 } 6 top = 1 sub = 2 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children: 3, 4, 7 nodeStack 2 1 Abstract Syntax Tree 3 op: “int” intval: 3 children: 4 op: “+” intval: 0 children: 5, 6 5 op: “int” intval: 4 children: 6 op: “int” intval: 5 children: 7 op: “int” intval: 6 children:

Dr. Philip Cannata 20 After recognizing {+ 3 {+ 4 5 } 6 } top = 1 sub = 1 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children: 3, 4, 7 nodeStack 1 Abstract Syntax Tree 3 op: “int” intval: 3 children: 4 op: “+” intval: 0 children: 5, 6 5 op: “int” intval: 4 children: 6 op: “int” intval: 5 children: 7 op: “int” intval: 6 children: Now print the Abstract Syntax Tree starting with top

Dr. Philip Cannata 21 $ ls AbstractSyntax.java Makefile Parser.jj calc.sh test $ cat calc.sh cat test cat test | java Parser $./calc.sh {+ 4 5 {- { } { } 6} 101 {+ 102}} Binary: top Binary: + 4 Binary: + 5 Binary: - Binary: + 1 Binary: + 2 Binary: + 3 Binary: Binary: Binary: Binary: - 6 Binary: Binary: Different Parser Files – javacc demo3 – Building a simple AST

Dr. Philip Cannata 22