1 / 22

Programming Languages

Programming Languages. 10. High Level Languages. Java (Object Oriented) ASP RDF (Horn Clause Deduction, Semantic Web). This Course. Jython in Java. Relation. High Level Overview of Grammar Concepts. { } a series of zero or more ( ) must pick one from a list

kiri
Télécharger la présentation

Programming Languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Languages

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

  3. High Level Overview of Grammar Concepts { } 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 

  4. We’ll be starting with javacc  moving to ANTLR later Instance of a Programming Language: int main () { 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

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

  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_END(Parser ) SKIP : { " " | "\t" | "\n" | "\r" | <"//" (~["\n","\r"])* ("\n"|"\r")> } TOKEN: { < LCURLY: "{" > | < RCURLY: "}" > | < MINUS: "-" > | < PLUS: "+" > } TOKEN: /* Literals */ { < INTEGER: (["0"-"9"])+ > } TOKEN: { <ERROR: ~[] > } Parser Grammar Production Rules 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(") "); } } Tokens, Terminals

  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

  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.

  9. Testing the Parser $ 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)) )

  10. Parser Files – javacc demo2 – similar to Chapter 2 in the Textbook but in java instead of scheme $ 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

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

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

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

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

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

  16. After recognizing {+ 3 {+ 4 5 Abstract Syntax Tree nodeStack 4 2 1 top = 1 sub = 4 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children: 3, 4 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:

  17. After recognizing {+ 3 {+ 4 5 } Abstract Syntax Tree nodeStack 2 1 top = 1 sub = 2 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children:3, 4 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. After recognizing {+ 3 {+ 4 5 } 6 Abstract Syntax Tree nodeStack 2 1 top = 1 sub = 2 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children: 3, 4, 7 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:

  19. After recognizing {+ 3 {+ 4 5 } 6 } Abstract Syntax Tree nodeStack 1 top = 1 sub = 1 1 op: “” intval: 0 children: 2 Now print the Abstract Syntax Tree starting with top 2 op: “+” intval: 0 children: 3, 4, 7 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. 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 $ ./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

More Related