Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Dr. Philip Cannata 1 Programming Languages. Dr. Philip Cannata 2 10 Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web) Relation Jython."— Presentation transcript:

1 Dr. Philip Cannata 1 Programming Languages

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

3 Dr. Philip Cannata 3

4 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 ) / 3 + 100  High Level Overview of Grammar Concepts

5 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

6 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

7 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

8 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

9 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

10 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

11 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 {- {+ 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

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

13 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

14 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:

15 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 4 2 1 Abstract Syntax Tree 3 op: “int” intval: 3 children: 4 op: “+” intval: 0 children:

16 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 4 2 1 Abstract Syntax Tree 3 op: “int” intval: 3 children: 4 op: “+” intval: 0 children: 5 5 op: “int” intval: 4 children:

17 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 4 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:

18 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:

19 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:

20 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

21 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 {- {+ 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

22 Dr. Philip Cannata 22


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

Similar presentations


Ads by Google