Programming Languages Dr. Philip Cannata
0 High Level Languages This Course Jython in Java Java (Object Oriented) ACL (Propositional Induction) Relation Algorithmic Information Theory (Information Compression and Randomness) - Kolmogorov Complexity Orc (Parallel Computing) GpH (Parallel Computing) RDF (Horn Clause Deduction, Semantic Web) Dr. Philip Cannata
int main () { We ll be starting with javacc moving to ANTLR later Instance of a Programming Language: } 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 Dr. Philip Cannata 3
We ll be starting with javacc moving to ANTLR later Dr. Philip Cannata 4
Abstract Syntax for Parser built with javacc Dr. Philip Cannata 5
Parser Files javacc demo similar to Chapter in the Textbook but in java instead of scheme $ ls Makefile Parser.jj test Dr. Philip Cannata 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 PARSER_END(Parser ) SKIP : { " " "\t" "\n" "\r" <"//" (~["\n","\r"])* ("\n" "\r")> } TOKEN: { < LCURLY: "{" > < RCURLY: "}" > < MINUS: "-" > < PLUS: "+" > } TOKEN: /* Literals */ { < INTEGER: (["0"-"9"])+ > } TOKEN: { <ERROR: ~[] > } Tokens, Terminals Grammar Production Rules void ae() : { Token n; } { n = <INTEGER> { System.out.print("(num " + n +")"); } list() } void list() : {} { LOOKAHEAD() <LCURLY> <PLUS> { System.out.print(" (add ");} ( ae() )* <RCURLY> { System.out.print(") "); } <LCURLY> <MINUS> { System.out.print(" (sub ");} ( ae() )* <RCURLY> { System.out.print(") "); } } Dr. Philip Cannata 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 Dr. Philip Cannata 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. Dr. Philip Cannata 9
Testing the Parser $ cat test; cat test java -cp "." Parser {+ 4 5 {- {+ 3 } 6} 0 {+ 0}} (add (num 4)(num 5) (sub (add (num )(num )(num 3)) (num 6)) (num 0) (add (num 0)) ) Dr. Philip Cannata 0
Parser Files javacc demo similar to Chapter in the Textbook but in java instead of scheme $ ls AbstractSyntax.java Makefile Parser.jj calc.sh test New $ 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]\))/\/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]\))/\/g" clisp --silent grep -v ">" $./calc.sh {+ 4 5 {- {+ 3} 6} 0 {+ 0}} (add (num 4) (num 5) (sub (add (num ) (num ) (num 3)) (num 6)) (num 0) (add (num 0))) (+ 4 5 (- (+ 3) 6) 0 (+ 0)) Dr. Philip Cannata
Parse {+ 3 {+ 4 5 } 6} Beginning top = sub = Abstract Syntax Tree op: nodestack Dr. Philip Cannata
After recognizing {+ top = sub = Abstract Syntax Tree op: nodestack Dr. Philip Cannata 3
After recognizing {+ 3 Abstract Syntax Tree top = sub = op: nodestack 3 3 intval: 3 Dr. Philip Cannata 4
After recognizing {+ 3 {+ Abstract Syntax Tree top = sub = 4 op: nodestack 4 3, 4 3 intval: 3 4 Dr. Philip Cannata 5
After recognizing {+ 3 {+ 4 Abstract Syntax Tree top = sub = 4 op: nodestack 4 3, 4 3 intval: 3 4 5 5 intval: 4 Dr. Philip Cannata 6
After recognizing {+ 3 {+ 4 5 Abstract Syntax Tree top = sub = 4 op: nodestack 4 3, 4 3 intval: 3 4 5, 6 5 intval: 4 6 intval: 5 Dr. Philip Cannata 7
After recognizing {+ 3 {+ 4 5 } Abstract Syntax Tree top = sub = op: nodestack 3, 4 3 intval: 3 4 5, 6 5 intval: 4 6 intval: 5 Dr. Philip Cannata 8
After recognizing {+ 3 {+ 4 5 } 6 Abstract Syntax Tree top = sub = op: nodestack 3, 4, 7 3 intval: 3 4 5, 6 5 intval: 4 6 intval: 5 7 intval: 6 Dr. Philip Cannata 9
After recognizing {+ 3 {+ 4 5 } 6 } Abstract Syntax Tree top = sub = op: 3, 4, 7 nodestack Now print the Abstract Syntax Tree starting with top 3 intval: 3 4 5, 6 5 intval: 4 6 intval: 5 7 intval: 6 Dr. Philip Cannata 0
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 Different $./calc.sh {+ 4 5 {- {+ 3} {+ 00 300 400} 6} 0 {+ 0}} Binary: top Binary: + 4 Binary: + 5 Binary: - Binary: + Binary: + Binary: + 3 Binary: + 00 Binary: + 300 Binary: + 400 Binary: - 6 Binary: + 0 Binary: + 0 Dr. Philip Cannata
Dr. Philip Cannata