230 likes | 371 Views
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
E N D
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 [ ] 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
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
Parser Files – javacc demo1 – similar to Chapter 1 in the Textbook but in java instead of scheme $ ls Makefile Parser.jj test
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
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
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.
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)) )
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
Parse {+ 3 {+ 4 5 } 6} Beginning Abstract Syntax Tree nodeStack 1 top = sub = 1 1 op: “” intval: 0 children:
After recognizing {+ Abstract Syntax Tree nodeStack 2 1 top = 1 sub = 2 1 op: “” intval: 0 children: 2 2 op: “+” intval: 0 children:
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:
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:
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:
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:
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:
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:
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:
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