dr. philip cannata 1 programming languages. dr. philip cannata 2 10 java (object oriented) asp rdf...

Post on 22-Dec-2015

225 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Dr. Philip Cannata 1

Programming Languages

Dr. Philip Cannata 2

10

Java (Object Oriented)Java (Object Oriented)

ASPASP

RDF (Horn Clause Deduction, RDF (Horn Clause Deduction, Semantic Web)Semantic Web)

RelationRelation

Jython in JavaJython in Java

This CourseThis Course

High LevelHigh LevelLanguagesLanguages

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 EBNFnumber -> { 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 }

6 * ( 11 – 7 ) / 3 + 100

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

$ lsMakefile 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" | <"//" (~["\n","\r"])* ("\n"|"\r")>}

TOKEN:{ < LCURLY: "{" > | < RCURLY: "}" > | < MINUS: "-" > | < PLUS: "+" >

}

TOKEN: /* Literals */{ < INTEGER: (["0"-"9"])+ >}

TOKEN:{ <ERROR: ~[] >}

void ae() :{ Token n; }{ n = <INTEGER> { System.out.print("(num " + n +")"); } | list()}

void list() :{}{ LOOKAHEAD(2) <LCURLY> <PLUS> { System.out.print(" (add ");}

( ae() )* <RCURLY> { System.out.print(") "); } | <LCURLY> <MINUS> { System.out.print(" (sub ");} ( ae() )*

<RCURLY> { System.out.print(") "); }}

Parser

Grammar Production Rules

Tokens, Terminals

Syntax and Grammar – Parser.jj

Dr. Philip Cannata 8

$ cat MakefileParser.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

$ makejavacc Parser.jjJava 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 *javaNote: 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 {- {+ 1 2 3 } 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

$ lsAbstractSyntax.java Makefile Parser.jj calc.sh test

$ cat calc.shcat testcat test | java Parsercat 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 {- {+ 1 2 3} 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 (- (+ 1 2 3) 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

1op: “”intval: 0children:

top = sub = 1

nodeStack

1

Parse {+ 3 {+ 4 5 } 6}

Abstract Syntax TreeBeginning

Dr. Philip Cannata 13

After recognizing {+

top = 1

sub = 2

11op: “”intval: 0children: 2

22op: “+”intval: 0children:

nodeStack

2

1

Abstract Syntax Tree

Dr. Philip Cannata 14

After recognizing {+ 3

top = 1

sub = 2

1op: “”intval: 0children: 2

2op: “+”intval: 0children: 3

nodeStack

2

1

Abstract Syntax Tree

3op: “int”intval: 3children:

Dr. Philip Cannata 15

After recognizing {+ 3 {+

top = 1

sub = 4

1op: “”intval: 0children: 2

2op: “+”intval: 0children: 3, 4

nodeStack

4

2

1

Abstract Syntax Tree

3op: “int”intval: 3children:

4op: “+”intval: 0children:

Dr. Philip Cannata 16

After recognizing {+ 3 {+ 4

top = 1

sub = 4

1op: “”intval: 0children: 2

2op: “+”intval: 0children: 3, 4

nodeStack

4

2

1

Abstract Syntax Tree

3op: “int”intval: 3children:

4op: “+”intval: 0children: 5

5op: “int”intval: 4children:

Dr. Philip Cannata 17

After recognizing {+ 3 {+ 4 5

top = 1

sub = 4

1op: “”intval: 0children: 2

2op: “+”intval: 0children: 3, 4

nodeStack

4

2

1

Abstract Syntax Tree

3op: “int”intval: 3children:

4op: “+”intval: 0children: 5, 6

5op: “int”intval: 4children:

6op: “int”intval: 5children:

Dr. Philip Cannata 18

After recognizing {+ 3 {+ 4 5 }

top = 1

sub = 2

1op: “”intval: 0children: 2

2op: “+”intval: 0children:3, 4

nodeStack

2

1

Abstract Syntax Tree

3op: “int”intval: 3children:

4op: “+”intval: 0children: 5, 6

5op: “int”intval: 4children:

6op: “int”intval: 5children:

Dr. Philip Cannata 19

After recognizing {+ 3 {+ 4 5 } 6

top = 1

sub = 2

1op: “”intval: 0children: 2

2op: “+”intval: 0children: 3, 4, 7

nodeStack

2

1

Abstract Syntax Tree

3op: “int”intval: 3children:

4op: “+”intval: 0children: 5, 6

5op: “int”intval: 4children:

6op: “int”intval: 5children:

7op: “int”intval: 6children:

Dr. Philip Cannata 20

After recognizing {+ 3 {+ 4 5 } 6 }

top = 1

sub = 1

1op: “”intval: 0children: 2

2op: “+”intval: 0children: 3, 4, 7

nodeStack

1

Abstract Syntax Tree

3op: “int”intval: 3children:

4op: “+”intval: 0children: 5, 6

5op: “int”intval: 4children:

6op: “int”intval: 5children:

7op: “int”intval: 6children:

Now print the Abstract Syntax Tree starting with top

Dr. Philip Cannata 21

$ ls

AbstractSyntax.java Makefile Parser.jj calc.sh test

$ cat calc.shcat testcat test | java Parser

$ ./calc.sh{+ 4 5 {- {+ 1 2 3} {+ 200 300 400} 6} 101 {+ 102}}

Binary: top Binary: + 4 Binary: + 5 Binary: - Binary: + 1 Binary: + 2 Binary: + 3 Binary: + 200 Binary: + 300 Binary: + 400 Binary: - 6 Binary: + 101 Binary: + 102

Different

Parser Files – javacc demo3 – Building a simple AST

Dr. Philip Cannata 22

top related