Dr. Philip Cannata 1 Programming Languages. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython.

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.
Parsing & Scanning Lecture 2 COMP /25/2004 Derek Ruths Office: DH Rm #3010.
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.
Environments and Evaluation
Professor Yihjia Tsai Tamkang University
Syntax Analysis (Chapter 4) 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture.
CS 153: Concepts of Compiler Design October 27 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
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) ACL2 (Propositional Induction) Algorithmic Information Theory.
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.
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,
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
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.
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.
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.
COMPILER OVERVIEW. Compiler Phases  Syntactic Analysis (Lexing, Parsing)  c = (a + b) * (a + b);
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().
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
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
Parse & Syntax Trees Syntax & Semantic Errors Mini-Lecture.
CPS 506 Comparative Programming Languages Syntax Specification.
CS 345 Project Presentation OOH A More Object-Oriented Hmm++ Alysha Behn Jesse Vera.
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.
CS453 LectureIntroduction1 CS453 Compiler Construction Original Design: Michelle Strout Instructor:Wim Bohm
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Bernd Fischer COMP2010: Compiler Engineering Abstract Syntax Trees.
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.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Comp 411 Principles of Programming Languages Lecture 3 Parsing
Chapter 3 – Describing Syntax
COMP261 Lecture 18 Parsing 3 of 4.
Crash course in the Java Programming Language
CS 3304 Comparative Languages
Chapter 3 – Describing Syntax
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Java CUP.
CS 614: Theory and Construction of Compilers
Programming Language Concepts
Compiler Designs and Constructions
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
C H A P T E R T W O Syntax.
Intermediate Representations Hal Perkins Autumn 2005
Programming Languages
Introduction to ANTLR Jin Tianxing
Review for the Midterm. Overview (Chapter 1):
Course Overview PART I: overview material PART II: inside a compiler
Presentation transcript:

Dr. Philip Cannata 1 Programming Languages

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

Dr. Philip Cannata 3

Dr. Philip Cannata 4 { } a series of zero or more ( ) must pick one from a list [ ] pick none or one from a list expression -> term { ( + | - ) term } term -> factor { ( * | / ) factor } factor -> ( expression ) | number // the parenthesis are part of the grammar not the EBNF number -> { 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 } 6 * ( 11 – 7 ) /  High Level Overview of Grammar Concepts

Dr. Philip Cannata 5 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 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