├── nbproject ├── private │ ├── config.properties │ ├── private.properties │ └── private.xml ├── genfiles.properties ├── project.xml └── project.properties ├── src └── dsllanguage │ ├── jacc │ ├── jacc.bat │ ├── jacc.jar │ ├── DSL_ParserTokens.java │ ├── DSLTokens.java │ ├── README │ ├── LICENSE │ ├── ASTNodes │ ├── ASTNodeType.java │ ├── ASTNode.java │ ├── EqualBinaryASTNode.java │ ├── NotEqualBinaryASTNode.java │ ├── DivisionBinaryASTNode.java │ ├── SubtractionBinaryASTNode.java │ ├── MultiplicationBinaryASTNode.java │ ├── ConditionalASTNode.java │ ├── ConstantASTNode.java │ ├── AdditionBinaryASTNode.java │ ├── NameASTNode.java │ ├── PrintASTNode.java │ ├── ComparisonBinaryASTNode.java │ ├── DefinitionASTNode.java │ ├── BinaryASTNode.java │ ├── AssignmentASTNode.java │ ├── StatementASTNode.java │ ├── WhileConditionalASTNode.java │ ├── IfConditionalASTNode.java │ └── ProgramASTNode.java │ ├── GlobalOutputStream.java │ ├── BasicBlock.java │ ├── DSL_Parser.jacc │ ├── Main.java │ ├── Symbol.java │ ├── SymbolTable.java │ ├── DSL_Parser.output │ ├── CFG.java │ ├── DSLLexer.java │ ├── ASTGraph.java │ ├── DSLPanel.form │ └── DSLPanel.java ├── applet.policy ├── screenshots ├── ast.png ├── cfg.png └── output.png ├── ToC_Presentation.pptx ├── dist ├── DSLLanguage.jar ├── lib │ ├── jgraph-5.13.0.0.jar │ ├── jgraphx-3.4.1.3.jar │ ├── antlr4-runtime-4.7.jar │ ├── jgrapht-core-1.0.1.jar │ └── jgrapht-ext-1.0.1.jar ├── duffs_device.dsl └── README.TXT ├── ToC_DSL_Presentation.pdf ├── lib ├── jgraph-5.13.0.0.jar ├── jgraphx-3.4.1.3.jar ├── antlr4-runtime-4.7.jar ├── jgrapht-core-1.0.1.jar ├── jgrapht-ext-1.0.1.jar ├── CopyLibs │ └── org-netbeans-modules-java-j2seproject-copylibstask.jar └── nblibraries.properties ├── manifest.mf ├── LICENSE └── README.md /nbproject/private/config.properties: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/dsllanguage/jacc: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | java -jar $JACC_PATH/jacc.jar $* 3 | -------------------------------------------------------------------------------- /src/dsllanguage/jacc.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | java -jar %JACC_PATH%\jacc.jar %* 3 | -------------------------------------------------------------------------------- /applet.policy: -------------------------------------------------------------------------------- 1 | grant { 2 | permission java.security.AllPermission; 3 | }; 4 | -------------------------------------------------------------------------------- /screenshots/ast.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/screenshots/ast.png -------------------------------------------------------------------------------- /screenshots/cfg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/screenshots/cfg.png -------------------------------------------------------------------------------- /ToC_Presentation.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/ToC_Presentation.pptx -------------------------------------------------------------------------------- /dist/DSLLanguage.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/dist/DSLLanguage.jar -------------------------------------------------------------------------------- /screenshots/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/screenshots/output.png -------------------------------------------------------------------------------- /ToC_DSL_Presentation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/ToC_DSL_Presentation.pdf -------------------------------------------------------------------------------- /lib/jgraph-5.13.0.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/lib/jgraph-5.13.0.0.jar -------------------------------------------------------------------------------- /lib/jgraphx-3.4.1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/lib/jgraphx-3.4.1.3.jar -------------------------------------------------------------------------------- /src/dsllanguage/jacc.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/src/dsllanguage/jacc.jar -------------------------------------------------------------------------------- /lib/antlr4-runtime-4.7.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/lib/antlr4-runtime-4.7.jar -------------------------------------------------------------------------------- /lib/jgrapht-core-1.0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/lib/jgrapht-core-1.0.1.jar -------------------------------------------------------------------------------- /lib/jgrapht-ext-1.0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/lib/jgrapht-ext-1.0.1.jar -------------------------------------------------------------------------------- /manifest.mf: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | X-COMMENT: Main-Class will be added automatically by build 3 | 4 | -------------------------------------------------------------------------------- /dist/lib/jgraph-5.13.0.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/dist/lib/jgraph-5.13.0.0.jar -------------------------------------------------------------------------------- /dist/lib/jgraphx-3.4.1.3.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/dist/lib/jgraphx-3.4.1.3.jar -------------------------------------------------------------------------------- /dist/lib/antlr4-runtime-4.7.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/dist/lib/antlr4-runtime-4.7.jar -------------------------------------------------------------------------------- /dist/lib/jgrapht-core-1.0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/dist/lib/jgrapht-core-1.0.1.jar -------------------------------------------------------------------------------- /dist/lib/jgrapht-ext-1.0.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/dist/lib/jgrapht-ext-1.0.1.jar -------------------------------------------------------------------------------- /lib/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LouisJenkinsCS/DSL/HEAD/lib/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar -------------------------------------------------------------------------------- /lib/nblibraries.properties: -------------------------------------------------------------------------------- 1 | libs.CopyLibs.classpath=\ 2 | ${base}/CopyLibs/org-netbeans-modules-java-j2seproject-copylibstask.jar 3 | libs.CopyLibs.displayName=CopyLibs Task 4 | libs.CopyLibs.prop-version=2.0 5 | -------------------------------------------------------------------------------- /nbproject/private/private.properties: -------------------------------------------------------------------------------- 1 | compile.on.save=false 2 | do.depend=false 3 | do.jar=true 4 | javac.debug=true 5 | javadoc.preview=true 6 | user.properties.file=C:\\Users\\Louis\\AppData\\Roaming\\NetBeans\\8.1\\build.properties 7 | -------------------------------------------------------------------------------- /src/dsllanguage/DSL_ParserTokens.java: -------------------------------------------------------------------------------- 1 | // Output created by jacc on Sat Apr 08 16:45:12 GMT 2017 2 | 3 | package dsllanguage; 4 | 5 | interface DSL_ParserTokens { 6 | int ENDINPUT = 0; 7 | int INTEGER = 1; 8 | int NAME = 2; 9 | int STRING = 3; 10 | int VAR = 4; 11 | int error = 5; 12 | // ';' (code=59) 13 | // '=' (code=61) 14 | } 15 | -------------------------------------------------------------------------------- /nbproject/genfiles.properties: -------------------------------------------------------------------------------- 1 | build.xml.data.CRC32=b78853ec 2 | build.xml.script.CRC32=52b58b53 3 | build.xml.stylesheet.CRC32=8064a381@1.79.1.48 4 | # This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. 5 | # Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. 6 | nbproject/build-impl.xml.data.CRC32=b78853ec 7 | nbproject/build-impl.xml.script.CRC32=9532462c 8 | nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 9 | -------------------------------------------------------------------------------- /nbproject/project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | org.netbeans.modules.java.j2seproject 4 | 5 | 6 | DSLLanguage 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | .\lib\nblibraries.properties 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/dsllanguage/DSLTokens.java: -------------------------------------------------------------------------------- 1 | // Output created by jacc on Mon May 01 23:23:05 GMT 2017 2 | 3 | package dsllanguage; 4 | 5 | interface DSLTokens { 6 | int ENDINPUT = 0; 7 | int ELSE = 1; 8 | int EQ = 2; 9 | int GE = 3; 10 | int IF = 4; 11 | int INTEGER = 5; 12 | int LE = 6; 13 | int NAME = 7; 14 | int NEQ = 8; 15 | int PRINT = 9; 16 | int STRING = 10; 17 | int VAR = 11; 18 | int WHILE = 12; 19 | int error = 13; 20 | // '(' (code=40) 21 | // ')' (code=41) 22 | // '*' (code=42) 23 | // '+' (code=43) 24 | // '-' (code=45) 25 | // '/' (code=47) 26 | // ';' (code=59) 27 | // '<' (code=60) 28 | // '=' (code=61) 29 | // '>' (code=62) 30 | // '{' (code=123) 31 | // '}' (code=125) 32 | } 33 | -------------------------------------------------------------------------------- /dist/duffs_device.dsl: -------------------------------------------------------------------------------- 1 | var nElements = 15; 2 | var nLeftOver = nElements - ((8 * (nElements / 8))); 3 | var nIterations = nElements / 8; 4 | var tape = ""; 5 | print "nIterations: " + nIterations + ", nLeftOver: " + nLeftOver; 6 | 7 | while (nIterations) { 8 | tape = tape + "A"; 9 | tape = tape + "A"; 10 | tape = tape + "A"; 11 | tape = tape + "A"; 12 | tape = tape + "A"; 13 | tape = tape + "A"; 14 | tape = tape + "A"; 15 | tape = tape + "A"; 16 | 17 | nIterations = nIterations - 1; 18 | } 19 | 20 | if (nLeftOver >= 7) { tape = tape + "A"; } 21 | if (nLeftOver >= 6) { tape = tape + "A"; } 22 | if (nLeftOver >= 5) { tape = tape + "A"; } 23 | if (nLeftOver >= 4) { tape = tape + "A"; } 24 | if (nLeftOver >= 3) { tape = tape + "A"; } 25 | if (nLeftOver >= 2) { tape = tape + "A"; } 26 | if (nLeftOver >= 1) { tape = tape + "A"; } 27 | 28 | print tape; 29 | -------------------------------------------------------------------------------- /dist/README.TXT: -------------------------------------------------------------------------------- 1 | ======================== 2 | BUILD OUTPUT DESCRIPTION 3 | ======================== 4 | 5 | When you build an Java application project that has a main class, the IDE 6 | automatically copies all of the JAR 7 | files on the projects classpath to your projects dist/lib folder. The IDE 8 | also adds each of the JAR files to the Class-Path element in the application 9 | JAR files manifest file (MANIFEST.MF). 10 | 11 | To run the project from the command line, go to the dist folder and 12 | type the following: 13 | 14 | java -jar "DSLLanguage.jar" 15 | 16 | To distribute this project, zip up the dist folder (including the lib folder) 17 | and distribute the ZIP file. 18 | 19 | Notes: 20 | 21 | * If two JAR files on the project classpath have the same name, only the first 22 | JAR file is copied to the lib folder. 23 | * Only JAR files are copied to the lib folder. 24 | If the classpath contains other types of files or folders, these files (folders) 25 | are not copied. 26 | * If a library on the projects classpath also has a Class-Path element 27 | specified in the manifest,the content of the Class-Path element has to be on 28 | the projects runtime path. 29 | * To set a main class in a standard Java project, right-click the project node 30 | in the Projects window and choose Properties. Then click Run and enter the 31 | class name in the Main Class field. Alternatively, you can manually type the 32 | class name in the manifest Main-Class element. 33 | -------------------------------------------------------------------------------- /src/dsllanguage/README: -------------------------------------------------------------------------------- 1 | ------------------------------------------------------------------- 2 | Jacc, a yacc-like parser generator for Java February 2004 3 | ------------------------------------------------------------------- 4 | 5 | This is a preliminary release of the jacc parser generator. See 6 | the file LICENSE for conditions of distribution and use. 7 | 8 | This software requires an installation of a Java 2 runtime, such as 9 | the JDK or JRE from java.sun.com. (I have used jacc with a variety 10 | of different Java versions, from 1.1 to 1.4, and on a variety of 11 | different platforms, but your mileage may vary.) 12 | 13 | To install: 14 | 15 | Copy the file jacc.jar to a suitable directory of your machine. 16 | 17 | On Windows: 18 | Copy the batch file jacc.bat to a directory on your path. 19 | You should edit this file so that the string %JACC_PATH% is 20 | replaced with the absolute path of the directory containing 21 | jacc.jar (or else set the environment variable JACC_PATH to 22 | point to that directory). 23 | 24 | On Unix: 25 | Copy the script file jacc to a directory on your path. 26 | You should edit this file so that the string $JACC_PATH is 27 | replaced with the absolute path of the directory containing 28 | jacc.jar (or else set the environment variable JACC_PATH to 29 | point to that directory). 30 | 31 | ------------------------------------------------------------------- 32 | For more information, please contact mpj@cse.ogi.edu. 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | BSD 3-Clause License 2 | 3 | Copyright (c) 2017, Louis Jenkins 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without 7 | modification, are permitted provided that the following conditions are met: 8 | 9 | * Redistributions of source code must retain the above copyright notice, this 10 | list of conditions and the following disclaimer. 11 | 12 | * Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | * Neither the name of the copyright holder nor the names of its 17 | contributors may be used to endorse or promote products derived from 18 | this software without specific prior written permission. 19 | 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 24 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 27 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 28 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | -------------------------------------------------------------------------------- /src/dsllanguage/LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 1999-2004, Mark P Jones, Oregon Health & Science 2 | University (OGI School of Science & Engineering at OHSU) 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or 6 | without modification, are permitted provided that the following 7 | conditions are met: 8 | 9 | o Redistributions of source code must retain the above copyright 10 | notice, this list of conditions and the following disclaimer. 11 | 12 | o Redistributions in binary form must reproduce the above 13 | copyright notice, this list of conditions and the following 14 | disclaimer in the documentation and/or other materials provided 15 | with the distribution. 16 | 17 | o Neither the name of the Oregon Health & Science University, the 18 | OGI School of Science & Engineering at OHSU nor the names of its 19 | contributors may be used to endorse or promote products derived 20 | from this software without specific prior written permission. 21 | 22 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 24 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 25 | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 26 | COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 27 | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 28 | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 29 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 30 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 32 | ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 33 | POSSIBILITY OF SUCH DAMAGE. 34 | 35 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/ASTNodeType.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | /** 34 | * 35 | * @author Louis Jenkins 36 | */ 37 | public enum ASTNodeType { 38 | INT_LITERAL, 39 | STRING_LITERAL, 40 | NAME, 41 | DEFN, 42 | BINARY_OP, 43 | STATEMENT, 44 | CONDITIONAL 45 | } 46 | -------------------------------------------------------------------------------- /src/dsllanguage/GlobalOutputStream.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage; 32 | 33 | import java.util.function.Consumer; 34 | 35 | /** 36 | * 37 | * @author Louis Jenkins 38 | */ 39 | public class GlobalOutputStream { 40 | 41 | public static Consumer PRINTER = System.out::println; 42 | public static void println(String msg) { 43 | PRINTER.accept(msg); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/dsllanguage/BasicBlock.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package dsllanguage; 7 | 8 | import java.io.Serializable; 9 | import java.util.ArrayList; 10 | import java.util.List; 11 | 12 | /** 13 | * 14 | * @author lpj11535 15 | */ 16 | public class BasicBlock implements Serializable, Comparable { 17 | public String description = ""; 18 | public static final int NOT_REDUCIBLE = 0; 19 | public static final int REDUCIBLE = 1; 20 | public static final int REDUCIBLE_SINGLETON = 2; 21 | public static final int NOT_REDUCIBLE_NOP = 3; 22 | public int reducibility = REDUCIBLE; 23 | public boolean hasDescription = true; 24 | 25 | public List children = new ArrayList<>(); 26 | 27 | public BasicBlock(String description) { 28 | this.description = description; 29 | } 30 | 31 | public BasicBlock(String description, boolean hasDescription) { 32 | this.description = description; 33 | this.hasDescription = hasDescription; 34 | } 35 | 36 | public BasicBlock(String description, int reducibility) { 37 | this.description = description; 38 | this.reducibility = reducibility; 39 | } 40 | 41 | public BasicBlock(String description, int reducibility, boolean hasDescription) { 42 | this.description = description; 43 | this.reducibility = reducibility; 44 | this.hasDescription = hasDescription; 45 | } 46 | 47 | public void addChild(BasicBlock child) { 48 | children.add(child); 49 | if (child.hasDescription) { 50 | description = description == "" ? child.description : description + "\n" + child.description; 51 | } 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return description; 57 | } 58 | 59 | @Override 60 | public int compareTo(BasicBlock o) { 61 | return Integer.compare(this.hashCode(), o.hashCode()); 62 | } 63 | 64 | 65 | } 66 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/ASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.CFG; 34 | import java.util.ArrayList; 35 | import java.util.Collections; 36 | import java.util.List; 37 | import org.jgrapht.Graph; 38 | import org.jgrapht.graph.DefaultEdge; 39 | 40 | /** 41 | * 42 | * @author Louis Jenkins 43 | */ 44 | public interface ASTNode { 45 | 46 | public abstract Object execute(); 47 | 48 | public abstract void addDependency(Graph graph); 49 | 50 | public abstract CFG createCFG(); 51 | 52 | public abstract String stringify(); 53 | } 54 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/EqualBinaryASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.DSLLexer; 34 | 35 | /** 36 | * 37 | * @author Louis Jenkins 38 | */ 39 | public class EqualBinaryASTNode extends BinaryASTNode { 40 | 41 | public EqualBinaryASTNode(ASTNode left, ASTNode right) { 42 | super(left, right); 43 | } 44 | 45 | @Override 46 | protected Object op(Object left, Object right) { 47 | if (left == null || right == null) { 48 | DSLLexer.yyerror("One of Equality Operands are null..."); 49 | } 50 | 51 | return left.equals(right); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "=="; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/NotEqualBinaryASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.DSLLexer; 34 | 35 | /** 36 | * 37 | * @author Louis Jenkins 38 | */ 39 | public class NotEqualBinaryASTNode extends BinaryASTNode { 40 | 41 | public NotEqualBinaryASTNode(ASTNode left, ASTNode right) { 42 | super(left, right); 43 | } 44 | 45 | @Override 46 | protected Object op(Object left, Object right) { 47 | if (left == null || right == null) { 48 | DSLLexer.yyerror("One of Equality Operands are null..."); 49 | } 50 | 51 | return !left.equals(right); 52 | } 53 | 54 | @Override 55 | public String toString() { 56 | return "!="; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/dsllanguage/DSL_Parser.jacc: -------------------------------------------------------------------------------- 1 | %package dsllanguage 2 | %semantic dsllanguage.ASTNodes.ASTNode 3 | %class DSL 4 | %interface DSLTokens 5 | 6 | %{ import java.util.Collections; %} 7 | 8 | %token VAR '=' ';' INTEGER STRING NAME WHILE EQ PRINT IF ELSE NEQ GE LE 9 | 10 | %% 11 | 12 | prog : prog stmt { $$ = $2; $$.execute(); DSLPanel.addNode($$);} 13 | | stmt { $$ = $1; $$.execute(); DSLPanel.addNode($$);} 14 | | /* Empty */ 15 | ; 16 | 17 | conditional : WHILE expr block { $$ = new dsllanguage.ASTNodes.WhileConditionalASTNode($2, $3); } 18 | | IF expr block { $$ = new dsllanguage.ASTNodes.IfConditionalASTNode($2, $3, null); } 19 | | IF expr block ELSE block { $$ = new dsllanguage.ASTNodes.IfConditionalASTNode($2, $3, $5); } 20 | ; 21 | 22 | stmt : VAR NAME '=' expr ';' { $$ = new dsllanguage.ASTNodes.DefinitionASTNode($2, $4); } 23 | | NAME '=' expr ';' { $$ = new dsllanguage.ASTNodes.AssignmentASTNode($1, $3); } 24 | | conditional { $$ = $1; } 25 | | PRINT expr ';' { $$ = new dsllanguage.ASTNodes.PrintASTNode($2); } 26 | | error ';' 27 | ; 28 | 29 | stmt_list : stmt stmt_list { (( dsllanguage.ASTNodes.StatementASTNode)$2).body.add(0, $1); $$ = $2; } 30 | | /* Empty */ { $$ = new dsllanguage.ASTNodes.StatementASTNode(); } 31 | ; 32 | 33 | block : '{' stmt_list '}' { $$ = $2; } 34 | ; 35 | 36 | 37 | expr : expr '+' expr { $$ = new dsllanguage.ASTNodes.AdditionBinaryASTNode($1, $3); } 38 | | expr '-' expr { $$ = new dsllanguage.ASTNodes.SubtractionBinaryASTNode($1, $3); } 39 | | expr '*' expr { $$ = new dsllanguage.ASTNodes.MultiplicationBinaryASTNode($1, $3); } 40 | | expr '/' expr { $$ = new dsllanguage.ASTNodes.DivisionBinaryASTNode($1, $3); } 41 | | expr EQ expr { $$ = new dsllanguage.ASTNodes.ComparisonBinaryASTNode($1, $3, "=="); } 42 | | expr NEQ expr { $$ = new dsllanguage.ASTNodes.ComparisonBinaryASTNode($1, $3, "!="); } 43 | | expr GE expr { $$ = new dsllanguage.ASTNodes.ComparisonBinaryASTNode($1, $3, ">="); } 44 | | expr LE expr { $$ = new dsllanguage.ASTNodes.ComparisonBinaryASTNode($1, $3, "<="); } 45 | | expr '>' expr { $$ = new dsllanguage.ASTNodes.ComparisonBinaryASTNode($1, $3, ">"); } 46 | | expr '<' expr { $$ = new dsllanguage.ASTNodes.ComparisonBinaryASTNode($1, $3, "<"); } 47 | | '(' expr ')' { $$ = $2; } 48 | | INTEGER { $$ = $1; } 49 | | STRING { $$ = $1; } 50 | | NAME { $$ = $1; } 51 | ; 52 | 53 | %% 54 | 55 | private DSLLexer lexer; 56 | DSL(DSLLexer lexer) { this.lexer = lexer; } 57 | private void yyerror(String msg) { lexer.yyerror(msg); } 58 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/DivisionBinaryASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.DSLLexer; 34 | 35 | /** 36 | * 37 | * @author Louis Jenkins 38 | */ 39 | public class DivisionBinaryASTNode extends BinaryASTNode { 40 | 41 | public DivisionBinaryASTNode(ASTNode left, ASTNode right) { 42 | super(left, right); 43 | } 44 | 45 | @Override 46 | protected Object op(Object left, Object right) { 47 | if (left instanceof Integer && right instanceof Integer) { 48 | return (Integer) left / (Integer) right; 49 | } else { 50 | DSLLexer.yyerror("Bad Operand Types: " + left.getClass().getName() + " and " + right.getClass().getName()); 51 | } 52 | 53 | return null; 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "/"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DSL 2 | 3 | DSL is a very minimal domain specific language, with a problem domain being academic research, designed and created for a Theory of Computation course. Bundled with this program is a simple text editor which allows the user to execute (interpret) code written in the language, generate a graph of the Abstract Syntax Tree for the language, and a reduced Control Flow Graph. 4 | 5 | ## Screenshots 6 | 7 | Below are screenshots of the Fibonacci problem 8 | 9 | ### Output from interpretation 10 | 11 | ![output](screenshots/output.png) 12 | 13 | ### Generated Abstract Syntax Tree 14 | 15 | ![ast](screenshots/ast.png) 16 | 17 | ### Generated Control Flow Graph 18 | 19 | ![cfg](screenshots/cfg.png) 20 | 21 | ## Developer's Note and Warning 22 | 23 | I would like to note that this project was hacked together in a very brief time under *extremely* tense time constraints, and as such the code quality, to say the *least* is lacking. As well, the logic is counter-intuitive at times, and some things are still buggy and broken. I am officially done with the project, although it does deserve to be displayed here as a lot of work went into it. 24 | 25 | ### Bugs 26 | 27 | #### Control Flow Graph - Basic Block Reduction 28 | 29 | Control Flow Graph incorrectly reduces the last node of a while loop into its own basic block due to the wonky reduction algorithm I devised. If anyone wants to work on it, that's one of the first things needing to be done; the Control Flow Graph algorithm needs an entirely new overhaul. It should be noted that control flow itself is correct, as it does introduce a back-edge from the incorrectly-constructed singleton basic block. 30 | 31 | ## Grammar 32 | 33 | The BNF grammar can be seen below... 34 | 35 | ``` 36 | prog : prog stmt 37 | | stmt 38 | | /* Empty */ 39 | ; 40 | 41 | conditional : WHILE expr block 42 | | IF expr block 43 | | IF expr block ELSE block 44 | ; 45 | 46 | stmt : VAR NAME '=' expr ';' 47 | | NAME '=' expr ';' 48 | | conditional 49 | | PRINT expr ';' 50 | ; 51 | 52 | stmt_list : stmt stmt_list 53 | | /* Empty */ 54 | ; 55 | 56 | block : '{' stmt_list '}' 57 | ; 58 | 59 | 60 | expr : expr '+' expr 61 | | expr '-' expr 62 | | expr '*' expr 63 | | expr '/' expr 64 | | expr EQ expr 65 | | expr NEQ expr 66 | | expr GE expr 67 | | expr LE expr 68 | | expr '>' expr 69 | | expr '<' expr 70 | | '(' expr ')' 71 | | INTEGER 72 | | STRING 73 | | NAME 74 | ; 75 | 76 | ``` 77 | 78 | 79 | ### Code Example 80 | 81 | ``` 82 | var x = 0; 83 | var y = 100; 84 | while (x < y) { 85 | if (x == 10) { 86 | print "X: " + x; 87 | } 88 | } 89 | ``` 90 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/SubtractionBinaryASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.DSLLexer; 34 | 35 | /** 36 | * 37 | * @author Louis Jenkins 38 | */ 39 | public class SubtractionBinaryASTNode extends BinaryASTNode { 40 | 41 | public SubtractionBinaryASTNode(ASTNode left, ASTNode right) { 42 | super(left, right); 43 | } 44 | 45 | @Override 46 | protected Object op(Object left, Object right) { 47 | if (left instanceof Integer && right instanceof Integer) { 48 | return (Integer) left - (Integer) right; 49 | } else { 50 | DSLLexer.yyerror("Bad Operand Types: " + left.getClass().getName() + " and " + right.getClass().getName()); 51 | } 52 | 53 | return null; 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "-"; 59 | } 60 | 61 | } 62 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/MultiplicationBinaryASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.DSLLexer; 34 | 35 | /** 36 | * 37 | * @author Louis Jenkins 38 | */ 39 | public class MultiplicationBinaryASTNode extends BinaryASTNode { 40 | 41 | public MultiplicationBinaryASTNode(ASTNode left, ASTNode right) { 42 | super(left, right); 43 | } 44 | 45 | @Override 46 | protected Object op(Object left, Object right) { 47 | if (left instanceof Integer && right instanceof Integer) { 48 | return (Integer) left * (Integer) right; 49 | } else { 50 | DSLLexer.yyerror("Bad Operand Types: " + left.getClass().getName() + " and " + right.getClass().getName()); 51 | } 52 | 53 | return null; 54 | } 55 | 56 | @Override 57 | public String toString() { 58 | return "*"; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/dsllanguage/Main.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage; 32 | 33 | import java.io.IOException; 34 | import java.nio.file.Paths; 35 | import java.util.logging.Level; 36 | import java.util.logging.Logger; 37 | 38 | /** 39 | * 40 | * @author Louis Jenkins 41 | */ 42 | public class Main { 43 | public static void main(String[] args) { 44 | try { 45 | DSLLexer lexer = new DSLLexer(Paths.get("Assignment_Example.dsl").toFile()); 46 | lexer.nextToken(); 47 | DSL dsl = new DSL(lexer); 48 | dsl.parse(); 49 | System.out.println("Outputing Symbol Table Contents..."); 50 | SymbolTable.printAll(); 51 | } catch (IOException ex) { 52 | Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 53 | } 54 | } 55 | 56 | static void error(String msg) { 57 | System.out.println("ERROR: " + msg); 58 | System.exit(1); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/dsllanguage/Symbol.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage; 32 | 33 | /** 34 | * 35 | * @author Louis Jenkins 36 | */ 37 | public class Symbol { 38 | private String name; 39 | private long lineNumber; 40 | private Object value; 41 | 42 | public Symbol(String name, long lineNumber, Object value) { 43 | this.name = name; 44 | this.lineNumber = lineNumber; 45 | this.value = value; 46 | } 47 | 48 | 49 | 50 | public String getName() { 51 | return name; 52 | } 53 | 54 | public void setName(String name) { 55 | this.name = name; 56 | } 57 | 58 | public long getLineNumber() { 59 | return lineNumber; 60 | } 61 | 62 | public void setLineNumber(long lineNumber) { 63 | this.lineNumber = lineNumber; 64 | } 65 | 66 | public Object getValue() { 67 | return value; 68 | } 69 | 70 | public void setValue(Object value) { 71 | this.value = value; 72 | } 73 | 74 | @Override 75 | public String toString() { 76 | return "Symbol{" + "name=" + name + ", lineNumber=" + lineNumber + ", value=" + value + '}'; 77 | } 78 | 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/ConditionalASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.DSLLexer; 34 | import org.jgrapht.Graph; 35 | import org.jgrapht.graph.DefaultEdge; 36 | 37 | /** 38 | * 39 | * @author Louis Jenkins 40 | */ 41 | public abstract class ConditionalASTNode implements ASTNode { 42 | 43 | protected ASTNode conditional; 44 | protected ASTNode primaryBlock; 45 | protected ASTNode secondaryBlock; 46 | 47 | public ConditionalASTNode(ASTNode conditional, ASTNode primaryBlock, ASTNode secondaryBlock) { 48 | this.conditional = conditional; 49 | this.primaryBlock = primaryBlock; 50 | this.secondaryBlock = secondaryBlock; 51 | } 52 | 53 | @Override 54 | public void addDependency(Graph graph) { 55 | graph.addVertex(this); 56 | 57 | conditional.addDependency(graph); 58 | graph.addEdge(this, conditional); 59 | 60 | primaryBlock.addDependency(graph); 61 | graph.addEdge(this, primaryBlock); 62 | 63 | if (secondaryBlock != null) { 64 | secondaryBlock.addDependency(graph); 65 | graph.addEdge(this, secondaryBlock); 66 | } 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/ConstantASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.CFG; 34 | import org.jgrapht.Graph; 35 | import org.jgrapht.graph.DefaultEdge; 36 | 37 | /** 38 | * 39 | * @author Louis Jenkins 40 | */ 41 | public class ConstantASTNode implements ASTNode { 42 | public Object value; 43 | 44 | public ConstantASTNode(String value) { 45 | this.value = value; 46 | } 47 | 48 | public ConstantASTNode(int value) { 49 | this.value = value; 50 | } 51 | 52 | 53 | @Override 54 | public Object execute() { 55 | return value; 56 | } 57 | 58 | @Override 59 | public void addDependency(Graph graph) { 60 | graph.addVertex(this); 61 | } 62 | 63 | @Override 64 | public String toString() { 65 | if (value instanceof String) { 66 | return "\"" + value + "\""; 67 | } else { 68 | return value.toString(); 69 | } 70 | } 71 | 72 | @Override 73 | public CFG createCFG() { 74 | return null; 75 | } 76 | 77 | @Override 78 | public String stringify() { 79 | return this.toString(); 80 | } 81 | 82 | 83 | 84 | } 85 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/AdditionBinaryASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.DSLLexer; 34 | 35 | /** 36 | * The addition (+) operator... 37 | * 38 | * String + String => "Hello" + " World" = "Hello World" 39 | * String + Integer => "Hello " + 3 = "Hello 3" 40 | * Integer + Integer => 1 + 1 = 2 41 | * @author Louis Jenkins 42 | */ 43 | public class AdditionBinaryASTNode extends BinaryASTNode { 44 | 45 | 46 | public AdditionBinaryASTNode(ASTNode left, ASTNode right) { 47 | super(left, right); 48 | } 49 | 50 | @Override 51 | protected Object op(Object left, Object right) { 52 | if (left instanceof String) { 53 | return (String) left + right; 54 | } else if (right instanceof String) { 55 | return left + (String) right; 56 | } else if (left instanceof Integer && right instanceof Integer) { 57 | return (Integer) left + (Integer) right; 58 | } else { 59 | DSLLexer.yyerror("Bad Operand Types: " + left.getClass().getName() + " and " + right.getClass().getName()); 60 | } 61 | 62 | return null; 63 | } 64 | 65 | @Override 66 | public String toString() { 67 | return "+"; 68 | } 69 | 70 | 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/dsllanguage/SymbolTable.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage; 32 | 33 | import java.util.ArrayList; 34 | import java.util.List; 35 | import java.util.Map; 36 | import java.util.Optional; 37 | import java.util.TreeMap; 38 | import java.util.stream.Stream; 39 | 40 | /** 41 | * 42 | * @author Louis Jenkins 43 | */ 44 | public class SymbolTable { 45 | private static final List SYMBOL_TABLE = new ArrayList<>(); 46 | 47 | public static Symbol get(int idx) { 48 | return SYMBOL_TABLE.get(idx); 49 | } 50 | 51 | public static Optional get(String name) { 52 | return SYMBOL_TABLE.stream().flatMap(sym -> sym.getName().equals(name) ? Stream.of(sym) : Stream.empty()).findFirst(); 53 | } 54 | 55 | public static boolean exists(String name) { 56 | return SYMBOL_TABLE.stream().map(Symbol::getName).anyMatch(name::equals); 57 | } 58 | 59 | public static void printAll() { 60 | SYMBOL_TABLE.forEach(System.out::println); 61 | } 62 | 63 | public static int create(String name) { 64 | Symbol sym = new Symbol(name, 0, null); 65 | SYMBOL_TABLE.add(sym); 66 | return SYMBOL_TABLE.size() - 1; 67 | } 68 | 69 | public static void clear() { 70 | SYMBOL_TABLE.clear(); 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/NameASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.CFG; 34 | import dsllanguage.DSLLexer; 35 | import dsllanguage.Symbol; 36 | import dsllanguage.SymbolTable; 37 | import java.util.Optional; 38 | import org.jgrapht.Graph; 39 | import org.jgrapht.graph.DefaultEdge; 40 | 41 | /** 42 | * 43 | * @author Louis Jenkins 44 | */ 45 | public class NameASTNode implements ASTNode { 46 | public String name; 47 | 48 | public NameASTNode(String name) { 49 | this.name = name; 50 | } 51 | 52 | @Override 53 | public Object execute() { 54 | Optional optSym = SymbolTable.get(name); 55 | if (!optSym.isPresent()) { 56 | DSLLexer.yyerror("Symbol not found for name: " + name); 57 | } 58 | Symbol sym = optSym.get(); 59 | return sym.getValue(); 60 | } 61 | 62 | @Override 63 | public void addDependency(Graph graph) { 64 | graph.addVertex(this); 65 | } 66 | 67 | @Override 68 | public String toString() { 69 | return name; 70 | } 71 | 72 | @Override 73 | public CFG createCFG() { 74 | return null; 75 | } 76 | 77 | @Override 78 | public String stringify() { 79 | return this.toString(); 80 | } 81 | 82 | 83 | } 84 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/PrintASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.BasicBlock; 34 | import dsllanguage.CFG; 35 | import dsllanguage.GlobalOutputStream; 36 | import org.jgrapht.Graph; 37 | import org.jgrapht.graph.DefaultEdge; 38 | 39 | /** 40 | * 41 | * @author Louis Jenkins 42 | */ 43 | public class PrintASTNode implements ASTNode { 44 | protected ASTNode expr; 45 | 46 | public PrintASTNode(ASTNode expr) { 47 | this.expr = expr; 48 | } 49 | 50 | @Override 51 | public Object execute() { 52 | Object exprValue; 53 | if (expr == null || (exprValue = expr.execute()) == null) { 54 | GlobalOutputStream.println("(NULL)"); 55 | return null; 56 | } 57 | 58 | GlobalOutputStream.println(exprValue.toString()); 59 | return null; 60 | } 61 | 62 | @Override 63 | public void addDependency(Graph graph) { 64 | graph.addVertex(this); 65 | 66 | expr.addDependency(graph); 67 | graph.addEdge(this, expr); 68 | } 69 | 70 | @Override 71 | public String toString() { 72 | return "PRINT"; 73 | } 74 | 75 | @Override 76 | public CFG createCFG() { 77 | CFG cfg = new CFG(null); 78 | BasicBlock bb = new BasicBlock(this.stringify()); 79 | cfg.start = cfg.end = bb; 80 | return cfg; 81 | } 82 | 83 | @Override 84 | public String stringify() { 85 | return "print " + expr.stringify(); 86 | } 87 | 88 | 89 | 90 | } 91 | -------------------------------------------------------------------------------- /nbproject/project.properties: -------------------------------------------------------------------------------- 1 | annotation.processing.enabled=true 2 | annotation.processing.enabled.in.editor=false 3 | annotation.processing.processors.list= 4 | annotation.processing.run.all.processors=true 5 | annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output 6 | application.title=DSLLanguage 7 | application.vendor=Louis 8 | build.classes.dir=${build.dir}/classes 9 | build.classes.excludes=**/*.java,**/*.form 10 | # This directory is removed when the project is cleaned: 11 | build.dir=build 12 | build.generated.dir=${build.dir}/generated 13 | build.generated.sources.dir=${build.dir}/generated-sources 14 | # Only compile against the classpath explicitly listed here: 15 | build.sysclasspath=ignore 16 | build.test.classes.dir=${build.dir}/test/classes 17 | build.test.results.dir=${build.dir}/test/results 18 | # Uncomment to specify the preferred debugger connection transport: 19 | #debug.transport=dt_socket 20 | debug.classpath=\ 21 | ${run.classpath} 22 | debug.test.classpath=\ 23 | ${run.test.classpath} 24 | # Files in build.classes.dir which should be excluded from distribution jar 25 | dist.archive.excludes= 26 | # This directory is removed when the project is cleaned: 27 | dist.dir=dist 28 | dist.jar=${dist.dir}/DSLLanguage.jar 29 | dist.javadoc.dir=${dist.dir}/javadoc 30 | endorsed.classpath= 31 | excludes= 32 | file.reference.antlr4-runtime-4.7.jar=lib/antlr4-runtime-4.7.jar 33 | file.reference.jgraph-5.13.0.0.jar=lib/jgraph-5.13.0.0.jar 34 | file.reference.jgrapht-core-1.0.1.jar=lib/jgrapht-core-1.0.1.jar 35 | file.reference.jgrapht-ext-1.0.1.jar=lib/jgrapht-ext-1.0.1.jar 36 | file.reference.jgraphx-3.4.1.3.jar=lib/jgraphx-3.4.1.3.jar 37 | includes=** 38 | jar.compress=false 39 | javac.classpath=\ 40 | ${file.reference.jgrapht-core-1.0.1.jar}:\ 41 | ${file.reference.antlr4-runtime-4.7.jar}:\ 42 | ${file.reference.jgraphx-3.4.1.3.jar}:\ 43 | ${file.reference.jgrapht-ext-1.0.1.jar}:\ 44 | ${file.reference.jgraph-5.13.0.0.jar} 45 | # Space-separated list of extra javac options 46 | javac.compilerargs= 47 | javac.deprecation=false 48 | javac.external.vm=true 49 | javac.processorpath=\ 50 | ${javac.classpath} 51 | javac.source=1.8 52 | javac.target=1.8 53 | javac.test.classpath=\ 54 | ${javac.classpath}:\ 55 | ${build.classes.dir} 56 | javac.test.processorpath=\ 57 | ${javac.test.classpath} 58 | javadoc.additionalparam= 59 | javadoc.author=false 60 | javadoc.encoding=${source.encoding} 61 | javadoc.noindex=false 62 | javadoc.nonavbar=false 63 | javadoc.notree=false 64 | javadoc.private=false 65 | javadoc.splitindex=true 66 | javadoc.use=true 67 | javadoc.version=false 68 | javadoc.windowtitle= 69 | main.class=dsllanguage.DSLPanel 70 | manifest.file=manifest.mf 71 | meta.inf.dir=${src.dir}/META-INF 72 | mkdist.disabled=false 73 | platform.active=default_platform 74 | run.classpath=\ 75 | ${javac.classpath}:\ 76 | ${build.classes.dir} 77 | # Space-separated list of JVM arguments used when running the project. 78 | # You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. 79 | # To set system properties for unit tests define test-sys-prop.name=value: 80 | run.jvmargs=-Djava.security.policy=applet.policy 81 | run.test.classpath=\ 82 | ${javac.test.classpath}:\ 83 | ${build.test.classes.dir} 84 | source.encoding=UTF-8 85 | src.dir=src 86 | test.src.dir=test 87 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/ComparisonBinaryASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.DSLLexer; 34 | 35 | /** 36 | * 37 | * @author Louis Jenkins 38 | */ 39 | public class ComparisonBinaryASTNode extends BinaryASTNode { 40 | String type; 41 | 42 | public ComparisonBinaryASTNode(ASTNode left, ASTNode right, String type) { 43 | super(left, right); 44 | this.type = type; 45 | } 46 | 47 | @Override 48 | protected Object op(Object left, Object right) { 49 | if (left == null || right == null) { 50 | DSLLexer.yyerror("One of Equality Operands are null..."); 51 | } 52 | 53 | if (type.equals("==")) { 54 | return left.equals(right); 55 | } else if (type.equals("!=")) { 56 | return !left.equals(right); 57 | } else { 58 | if (!(left instanceof Integer) && !(right instanceof Integer)) { 59 | DSLLexer.yyerror("Comparison of wrong types..."); 60 | } 61 | 62 | Integer leftInt = (Integer) left; 63 | Integer rightInt = (Integer) right; 64 | 65 | if (type.equals("<")) { 66 | return leftInt < rightInt; 67 | } else if (type.equals(">")) { 68 | return leftInt < rightInt; 69 | } else if (type.equals("<=")) { 70 | return leftInt <= rightInt; 71 | } else if (type.equals(">=")) { 72 | return leftInt >= rightInt; 73 | } else { 74 | DSLLexer.yyerror("Operator not supported: " + type); 75 | } 76 | } 77 | 78 | return null; 79 | } 80 | 81 | @Override 82 | public String toString() { 83 | return type; 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/DefinitionASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.BasicBlock; 34 | import dsllanguage.CFG; 35 | import dsllanguage.DSLLexer; 36 | import dsllanguage.SymbolTable; 37 | import org.jgrapht.Graph; 38 | import org.jgrapht.graph.DefaultEdge; 39 | 40 | /** 41 | * 42 | * @author Louis Jenkins 43 | */ 44 | public class DefinitionASTNode implements ASTNode { 45 | protected ASTNode name; 46 | protected ASTNode expr; 47 | 48 | public DefinitionASTNode(ASTNode name, ASTNode expr) { 49 | this.name = name; 50 | this.expr = expr; 51 | } 52 | 53 | @Override 54 | public Object execute() { 55 | String nname = ((NameASTNode)name).name; 56 | if (SymbolTable.exists(nname)) { 57 | DSLLexer.yyerror("Name already exists: " + nname); 58 | } 59 | int symIdx = SymbolTable.create(nname); 60 | // Create the value equal to the expression... 61 | SymbolTable.get(symIdx).setValue(expr.execute()); 62 | 63 | return null; 64 | } 65 | 66 | @Override 67 | public void addDependency(Graph graph) { 68 | graph.addVertex(this); 69 | 70 | name.addDependency(graph); 71 | graph.addEdge(this, name); 72 | 73 | expr.addDependency(graph); 74 | graph.addEdge(this, expr); 75 | 76 | } 77 | 78 | @Override 79 | public String toString() { 80 | return "DECL"; 81 | } 82 | 83 | @Override 84 | public CFG createCFG() { 85 | CFG cfg = new CFG(null); 86 | BasicBlock bb = new BasicBlock(this.stringify()); 87 | cfg.start = cfg.end = bb; 88 | 89 | return cfg; 90 | } 91 | 92 | @Override 93 | public String stringify() { 94 | return "var " + name.stringify() + " = " + expr.stringify(); 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/BinaryASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.CFG; 34 | import dsllanguage.DSLLexer; 35 | import dsllanguage.Symbol; 36 | import java.util.Collections; 37 | import org.jgrapht.Graph; 38 | import org.jgrapht.graph.DefaultEdge; 39 | 40 | /** 41 | * 42 | * @author Louis Jenkins 43 | */ 44 | public abstract class BinaryASTNode implements ASTNode { 45 | protected ASTNode left; 46 | protected ASTNode right; 47 | 48 | public BinaryASTNode(ASTNode left, ASTNode right) { 49 | this.left = left; 50 | this.right = right; 51 | } 52 | 53 | @Override 54 | public Object execute() { 55 | Object leftValue = left.execute(); 56 | Object rightValue = right.execute(); 57 | 58 | if (leftValue == null || rightValue == null) { 59 | DSLLexer.yyerror("An addition operand evaluated to NULL!"); 60 | } 61 | 62 | // If either are names, get their actual values... 63 | if (leftValue instanceof Symbol) { 64 | leftValue = ((Symbol) leftValue).getValue(); 65 | } 66 | 67 | if (rightValue instanceof Symbol) { 68 | rightValue = ((Symbol) rightValue).getValue(); 69 | } 70 | 71 | return op(leftValue, rightValue); 72 | } 73 | 74 | @Override 75 | public void addDependency(Graph graph) { 76 | graph.addVertex(this); 77 | 78 | left.addDependency(graph); 79 | graph.addEdge(this, left); 80 | 81 | right.addDependency(graph); 82 | graph.addEdge(this, right); 83 | } 84 | 85 | 86 | protected abstract Object op(Object left, Object right); 87 | 88 | @Override 89 | public CFG createCFG() { 90 | return null; 91 | } 92 | 93 | @Override 94 | public String stringify() { 95 | return left.stringify() + " " + this.toString() + " " + right.stringify(); 96 | } 97 | 98 | 99 | } 100 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/AssignmentASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.BasicBlock; 34 | import dsllanguage.CFG; 35 | import dsllanguage.DSLLexer; 36 | import dsllanguage.SymbolTable; 37 | import org.jgrapht.Graph; 38 | import org.jgrapht.graph.DefaultEdge; 39 | 40 | /** 41 | * 42 | * @author Louis Jenkins 43 | */ 44 | public class AssignmentASTNode implements ASTNode { 45 | 46 | public ASTNode name; 47 | public ASTNode expr; 48 | 49 | public AssignmentASTNode(ASTNode name, ASTNode expr) { 50 | this.name = name; 51 | this.expr = expr; 52 | } 53 | 54 | @Override 55 | public Object execute() { 56 | 57 | if (!(name instanceof NameASTNode)) { 58 | DSLLexer.yyerror("Cannot assign to a non-Symbol..."); 59 | } 60 | 61 | String nname = ((NameASTNode) name).name; 62 | if (!SymbolTable.exists(nname)) { 63 | DSLLexer.yyerror("Undeclared name: " + nname); 64 | } 65 | 66 | // Create the value equal to the expression... 67 | SymbolTable.get(nname).get().setValue(expr.execute()); 68 | 69 | return null; 70 | } 71 | 72 | @Override 73 | public void addDependency(Graph graph) { 74 | graph.addVertex(this); 75 | 76 | name.addDependency(graph); 77 | graph.addEdge(this, name); 78 | 79 | expr.addDependency(graph); 80 | graph.addEdge(this, expr); 81 | } 82 | 83 | @Override 84 | public String toString() { 85 | return "="; 86 | } 87 | 88 | @Override 89 | public CFG createCFG() { 90 | CFG cfg = new CFG(null); 91 | BasicBlock bb = new BasicBlock(this.stringify()); 92 | cfg.start = cfg.end = bb; 93 | 94 | return cfg; 95 | } 96 | 97 | @Override 98 | public String stringify() { 99 | return name.stringify() + " = " + expr.stringify(); 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /nbproject/private/private.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/AssignmentASTNode.java 7 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/NotEqualBinaryASTNode.java 8 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/NameASTNode.java 9 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/ProgramASTNode.java 10 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/AdditionBinaryASTNode.java 11 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/EqualBinaryASTNode.java 12 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/BasicBlock.java 13 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/PrintASTNode.java 14 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTGraph.java 15 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/IfConditionalASTNode.java 16 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/ASTNode.java 17 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/DSLLexer.java 18 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/BinaryASTNode.java 19 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/DSLTokens.java 20 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/DSL_Parser.jacc 21 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/DivisionBinaryASTNode.java 22 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/MultiplicationBinaryASTNode.java 23 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/WhileConditionalASTNode.java 24 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/Main.java 25 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/StatementASTNode.java 26 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/DefinitionASTNode.java 27 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/DSL.java 28 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/CFG.java 29 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/GlobalOutputStream.java 30 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/SubtractionBinaryASTNode.java 31 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/ComparisonBinaryASTNode.java 32 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/SymbolTable.java 33 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/ASTNodes/ConditionalASTNode.java 34 | file:/C:/Github/CourseWork/Theory_Of_Computation/DSLLanguage/src/dsllanguage/DSLPanel.java 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/StatementASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.BasicBlock; 34 | import dsllanguage.CFG; 35 | import java.util.ArrayList; 36 | import java.util.Collections; 37 | import java.util.List; 38 | import org.jgrapht.Graph; 39 | import org.jgrapht.graph.DefaultEdge; 40 | 41 | /** 42 | * 43 | * @author Louis Jenkins 44 | */ 45 | public class StatementASTNode implements ASTNode { 46 | public List body = new ArrayList<>(); 47 | 48 | public StatementASTNode(ASTNode ...nodes) { 49 | Collections.addAll(body, nodes); 50 | } 51 | 52 | 53 | @Override 54 | public Object execute() { 55 | body.forEach(ASTNode::execute); 56 | return null; 57 | } 58 | 59 | @Override 60 | public void addDependency(Graph graph) { 61 | graph.addVertex(this); 62 | 63 | body.forEach(node -> { 64 | node.addDependency(graph); 65 | graph.addEdge(this, node); 66 | }); 67 | } 68 | 69 | @Override 70 | public String toString() { 71 | return "BLOCK"; 72 | } 73 | 74 | @Override 75 | public CFG createCFG() { 76 | CFG cfg = new CFG(null); 77 | BasicBlock start = new BasicBlock("STATEMENT-START", false); 78 | BasicBlock end = new BasicBlock("STATEMENT-END", false); 79 | 80 | cfg.start = start; 81 | cfg.end = end; 82 | 83 | cfg.addVertex(start); 84 | cfg.addVertex(end); 85 | 86 | if (body.isEmpty()) { 87 | cfg.addEdge(start, end); 88 | return cfg; 89 | } 90 | 91 | 92 | BasicBlock prev = start; 93 | for (ASTNode node : body) { 94 | CFG stmtCFG = node.createCFG(); 95 | if (stmtCFG == null) continue; 96 | cfg.addVertex(stmtCFG.start); 97 | cfg.addVertex(stmtCFG.end); 98 | cfg.addEdge(prev, stmtCFG.start); 99 | 100 | System.out.println(stmtCFG); 101 | 102 | stmtCFG.vertexSet() 103 | .stream() 104 | .forEachOrdered(block -> 105 | stmtCFG 106 | .edgesOf(block) 107 | .stream() 108 | .forEachOrdered(edge -> { 109 | BasicBlock src = stmtCFG.getEdgeSource(edge); 110 | BasicBlock tgt = stmtCFG.getEdgeTarget(edge); 111 | cfg.addVertex(src); 112 | cfg.addVertex(tgt); 113 | cfg.addEdge(src, tgt); 114 | }) 115 | ); 116 | prev = stmtCFG.end; 117 | } 118 | 119 | cfg.addEdge(prev, end); 120 | 121 | return cfg; 122 | } 123 | 124 | @Override 125 | public String stringify() { 126 | return null; 127 | } 128 | 129 | 130 | } 131 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/WhileConditionalASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.BasicBlock; 34 | import dsllanguage.CFG; 35 | import dsllanguage.DSLLexer; 36 | import java.util.logging.Logger; 37 | 38 | /** 39 | * 40 | * @author Louis Jenkins 41 | */ 42 | public class WhileConditionalASTNode extends ConditionalASTNode { 43 | 44 | public WhileConditionalASTNode(ASTNode conditional, ASTNode primaryBlock) { 45 | super(conditional, primaryBlock, null); 46 | } 47 | 48 | @Override 49 | public Object execute() { 50 | while (true) { 51 | Object conditionalValue = conditional.execute(); 52 | boolean cond; 53 | 54 | if (conditionalValue == null) { 55 | cond = false; 56 | } else if (conditionalValue instanceof Boolean) { 57 | cond = (Boolean) conditionalValue; 58 | } else if (conditionalValue instanceof Integer) { 59 | cond = ((Integer) conditionalValue) != 0; 60 | } else { 61 | DSLLexer.yyerror("Conditional evaluated to be invalid: " + conditionalValue); 62 | cond = false; 63 | } 64 | 65 | if (!cond) { 66 | if (secondaryBlock != null) { 67 | secondaryBlock.execute(); 68 | } 69 | break; 70 | } 71 | 72 | primaryBlock.execute(); 73 | } 74 | 75 | return null; //To change body of generated methods, choose Tools | Templates. 76 | } 77 | 78 | 79 | 80 | 81 | @Override 82 | public String toString() { 83 | return "WHILE"; 84 | } 85 | 86 | @Override 87 | public CFG createCFG() { 88 | CFG cfg = new CFG(null); 89 | BasicBlock start = new BasicBlock("while (" + conditional.stringify() + ")", BasicBlock.REDUCIBLE_SINGLETON); 90 | BasicBlock loop = new BasicBlock("WHILE-STATEMENT-LOOP", false); 91 | BasicBlock end = new BasicBlock("WHILE-STATEMENT-END", false); 92 | cfg.start = start; 93 | cfg.end = end; 94 | 95 | cfg.addVertex(start); 96 | cfg.addVertex(end); 97 | cfg.addVertex(loop); 98 | 99 | CFG primaryCFG = primaryBlock.createCFG(); 100 | cfg.addVertex(primaryCFG.start); 101 | cfg.addVertex(primaryCFG.end); 102 | primaryCFG.vertexSet().forEach(System.out::println); 103 | primaryCFG.vertexSet().forEach(cfg::addVertex); 104 | cfg.addEdge(start, primaryCFG.start); 105 | cfg.addEdge(primaryCFG.end, loop); 106 | cfg.addEdge(loop, start); 107 | cfg.addEdge(start, end); 108 | 109 | primaryCFG.vertexSet() 110 | .stream() 111 | .forEach(block -> 112 | primaryCFG 113 | .edgesOf(block) 114 | .stream() 115 | .forEach(edge -> { 116 | BasicBlock src = primaryCFG.getEdgeSource(edge); 117 | BasicBlock tgt = primaryCFG.getEdgeTarget(edge); 118 | cfg.addVertex(src); 119 | cfg.addVertex(tgt); 120 | cfg.addEdge(src, tgt); 121 | }) 122 | ); 123 | 124 | 125 | Logger.getAnonymousLogger().info(cfg.toString()); 126 | return cfg; 127 | } 128 | 129 | @Override 130 | public String stringify() { 131 | return null; 132 | } 133 | 134 | 135 | 136 | } 137 | -------------------------------------------------------------------------------- /src/dsllanguage/DSL_Parser.output: -------------------------------------------------------------------------------- 1 | // Output created by jacc on Sat Apr 08 22:38:22 GMT 2017 2 | 3 | 0: shift/reduce conflict (shift 3 and red'n 3) on VAR 4 | 0: shift/reduce conflict (shift 4 and red'n 3) on error 5 | state 0 (entry on prog) 6 | $accept : _prog $end 7 | prog : _ (3) 8 | 9 | VAR shift 3 10 | error shift 4 11 | $end reduce 3 12 | . error 13 | 14 | prog goto 1 15 | stmt goto 2 16 | 17 | state 1 (entry on prog) 18 | $accept : prog_$end 19 | prog : prog_stmt (1) 20 | 21 | $end accept 22 | VAR shift 3 23 | error shift 4 24 | . error 25 | 26 | stmt goto 5 27 | 28 | state 2 (entry on stmt) 29 | prog : stmt_ (2) 30 | 31 | $end reduce 2 32 | error reduce 2 33 | VAR reduce 2 34 | . error 35 | 36 | state 3 (entry on VAR) 37 | stmt : VAR_NAME '=' expr ';' (4) 38 | 39 | NAME shift 6 40 | . error 41 | 42 | state 4 (entry on error) 43 | stmt : error_';' (5) 44 | 45 | ';' shift 7 46 | . error 47 | 48 | state 5 (entry on stmt) 49 | prog : prog stmt_ (1) 50 | 51 | $end reduce 1 52 | error reduce 1 53 | VAR reduce 1 54 | . error 55 | 56 | state 6 (entry on NAME) 57 | stmt : VAR NAME_'=' expr ';' (4) 58 | 59 | '=' shift 8 60 | . error 61 | 62 | state 7 (entry on ';') 63 | stmt : error ';'_ (5) 64 | 65 | $end reduce 5 66 | error reduce 5 67 | VAR reduce 5 68 | . error 69 | 70 | state 8 (entry on '=') 71 | stmt : VAR NAME '='_expr ';' (4) 72 | 73 | INTEGER shift 10 74 | STRING shift 11 75 | '(' shift 12 76 | . error 77 | 78 | expr goto 9 79 | 80 | state 9 (entry on expr) 81 | expr : expr_'+' expr (6) 82 | expr : expr_'-' expr (7) 83 | expr : expr_'*' expr (8) 84 | expr : expr_'/' expr (9) 85 | stmt : VAR NAME '=' expr_';' (4) 86 | 87 | '*' shift 13 88 | '+' shift 14 89 | '-' shift 15 90 | '/' shift 16 91 | ';' shift 17 92 | . error 93 | 94 | state 10 (entry on INTEGER) 95 | expr : INTEGER_ (11) 96 | 97 | ')' reduce 11 98 | ';' reduce 11 99 | '/' reduce 11 100 | '-' reduce 11 101 | '+' reduce 11 102 | '*' reduce 11 103 | . error 104 | 105 | state 11 (entry on STRING) 106 | expr : STRING_ (12) 107 | 108 | ')' reduce 12 109 | ';' reduce 12 110 | '/' reduce 12 111 | '-' reduce 12 112 | '+' reduce 12 113 | '*' reduce 12 114 | . error 115 | 116 | state 12 (entry on '(') 117 | expr : '('_expr ')' (10) 118 | 119 | INTEGER shift 10 120 | STRING shift 11 121 | '(' shift 12 122 | . error 123 | 124 | expr goto 18 125 | 126 | state 13 (entry on '*') 127 | expr : expr '*'_expr (8) 128 | 129 | INTEGER shift 10 130 | STRING shift 11 131 | '(' shift 12 132 | . error 133 | 134 | expr goto 19 135 | 136 | state 14 (entry on '+') 137 | expr : expr '+'_expr (6) 138 | 139 | INTEGER shift 10 140 | STRING shift 11 141 | '(' shift 12 142 | . error 143 | 144 | expr goto 20 145 | 146 | state 15 (entry on '-') 147 | expr : expr '-'_expr (7) 148 | 149 | INTEGER shift 10 150 | STRING shift 11 151 | '(' shift 12 152 | . error 153 | 154 | expr goto 21 155 | 156 | state 16 (entry on '/') 157 | expr : expr '/'_expr (9) 158 | 159 | INTEGER shift 10 160 | STRING shift 11 161 | '(' shift 12 162 | . error 163 | 164 | expr goto 22 165 | 166 | state 17 (entry on ';') 167 | stmt : VAR NAME '=' expr ';'_ (4) 168 | 169 | $end reduce 4 170 | error reduce 4 171 | VAR reduce 4 172 | . error 173 | 174 | state 18 (entry on expr) 175 | expr : expr_'+' expr (6) 176 | expr : expr_'-' expr (7) 177 | expr : expr_'*' expr (8) 178 | expr : expr_'/' expr (9) 179 | expr : '(' expr_')' (10) 180 | 181 | '*' shift 13 182 | '+' shift 14 183 | '-' shift 15 184 | '/' shift 16 185 | ')' shift 23 186 | . error 187 | 188 | 19: shift/reduce conflict (shift 13 and red'n 8) on '*' 189 | 19: shift/reduce conflict (shift 14 and red'n 8) on '+' 190 | 19: shift/reduce conflict (shift 15 and red'n 8) on '-' 191 | 19: shift/reduce conflict (shift 16 and red'n 8) on '/' 192 | state 19 (entry on expr) 193 | expr : expr_'+' expr (6) 194 | expr : expr_'-' expr (7) 195 | expr : expr_'*' expr (8) 196 | expr : expr '*' expr_ (8) 197 | expr : expr_'/' expr (9) 198 | 199 | '*' shift 13 200 | '+' shift 14 201 | '-' shift 15 202 | '/' shift 16 203 | ')' reduce 8 204 | ';' reduce 8 205 | . error 206 | 207 | 20: shift/reduce conflict (shift 13 and red'n 6) on '*' 208 | 20: shift/reduce conflict (shift 14 and red'n 6) on '+' 209 | 20: shift/reduce conflict (shift 15 and red'n 6) on '-' 210 | 20: shift/reduce conflict (shift 16 and red'n 6) on '/' 211 | state 20 (entry on expr) 212 | expr : expr_'+' expr (6) 213 | expr : expr '+' expr_ (6) 214 | expr : expr_'-' expr (7) 215 | expr : expr_'*' expr (8) 216 | expr : expr_'/' expr (9) 217 | 218 | '*' shift 13 219 | '+' shift 14 220 | '-' shift 15 221 | '/' shift 16 222 | ')' reduce 6 223 | ';' reduce 6 224 | . error 225 | 226 | 21: shift/reduce conflict (shift 13 and red'n 7) on '*' 227 | 21: shift/reduce conflict (shift 14 and red'n 7) on '+' 228 | 21: shift/reduce conflict (shift 15 and red'n 7) on '-' 229 | 21: shift/reduce conflict (shift 16 and red'n 7) on '/' 230 | state 21 (entry on expr) 231 | expr : expr_'+' expr (6) 232 | expr : expr_'-' expr (7) 233 | expr : expr '-' expr_ (7) 234 | expr : expr_'*' expr (8) 235 | expr : expr_'/' expr (9) 236 | 237 | '*' shift 13 238 | '+' shift 14 239 | '-' shift 15 240 | '/' shift 16 241 | ')' reduce 7 242 | ';' reduce 7 243 | . error 244 | 245 | 22: shift/reduce conflict (shift 13 and red'n 9) on '*' 246 | 22: shift/reduce conflict (shift 14 and red'n 9) on '+' 247 | 22: shift/reduce conflict (shift 15 and red'n 9) on '-' 248 | 22: shift/reduce conflict (shift 16 and red'n 9) on '/' 249 | state 22 (entry on expr) 250 | expr : expr_'+' expr (6) 251 | expr : expr_'-' expr (7) 252 | expr : expr_'*' expr (8) 253 | expr : expr_'/' expr (9) 254 | expr : expr '/' expr_ (9) 255 | 256 | '*' shift 13 257 | '+' shift 14 258 | '-' shift 15 259 | '/' shift 16 260 | ')' reduce 9 261 | ';' reduce 9 262 | . error 263 | 264 | state 23 (entry on ')') 265 | expr : '(' expr ')'_ (10) 266 | 267 | ')' reduce 10 268 | ';' reduce 10 269 | '/' reduce 10 270 | '-' reduce 10 271 | '+' reduce 10 272 | '*' reduce 10 273 | . error 274 | 275 | 14 terminals, 3 nonterminals; 276 | 12 grammar rules, 24 states; 277 | 18 shift/reduce and 0 reduce/reduce conflicts reported. 278 | -------------------------------------------------------------------------------- /src/dsllanguage/CFG.java: -------------------------------------------------------------------------------- 1 | /* 2 | * To change this license header, choose License Headers in Project Properties. 3 | * To change this template file, choose Tools | Templates 4 | * and open the template in the editor. 5 | */ 6 | package dsllanguage; 7 | 8 | import java.util.ArrayDeque; 9 | import java.util.ArrayList; 10 | import java.util.Deque; 11 | import java.util.List; 12 | import java.util.Set; 13 | import java.util.Stack; 14 | import java.util.TreeSet; 15 | import java.util.logging.Logger; 16 | import java.util.stream.Collectors; 17 | import org.jgrapht.Graph; 18 | import org.jgrapht.graph.DefaultEdge; 19 | import org.jgrapht.graph.ListenableDirectedGraph; 20 | 21 | /** 22 | * 23 | * @author lpj11535 24 | */ 25 | public class CFG { 26 | private Graph graph; 27 | public boolean reducible = false; 28 | public BasicBlock start, end; 29 | 30 | public DefaultEdge addEdge(BasicBlock v, BasicBlock v1) { 31 | Logger.getAnonymousLogger().info("Added Edge = (" + v + "," + v1 + ")"); 32 | return graph.addEdge(v, v1); 33 | } 34 | 35 | public boolean addVertex(BasicBlock v) { 36 | if (graph.containsVertex(v)) { 37 | Logger.getAnonymousLogger().info("Dropped Duplicate..."); 38 | return false; 39 | } 40 | Logger.getAnonymousLogger().info("Added Vertex = " + v); 41 | return graph.addVertex(v); 42 | } 43 | 44 | 45 | public Set edgesOf(BasicBlock v) { 46 | return graph.edgesOf(v); 47 | } 48 | 49 | public Set vertexSet() { 50 | return graph.vertexSet(); 51 | } 52 | 53 | public BasicBlock getEdgeSource(DefaultEdge e) { 54 | return graph.getEdgeSource(e); 55 | } 56 | 57 | public BasicBlock getEdgeTarget(DefaultEdge e) { 58 | return graph.getEdgeTarget(e); 59 | } 60 | 61 | 62 | 63 | 64 | public CFG(Graph graph) { 65 | this.graph = graph == null ? new ListenableDirectedGraph<>(DefaultEdge.class) : graph; 66 | } 67 | 68 | public BasicBlock getStart() { 69 | return start; 70 | } 71 | 72 | public void setStart(BasicBlock start) { 73 | this.start = start; 74 | } 75 | 76 | public BasicBlock getEnd() { 77 | return end; 78 | } 79 | 80 | public void setEnd(BasicBlock end) { 81 | this.end = end; 82 | } 83 | 84 | public Graph getGraph() { 85 | return graph; 86 | } 87 | 88 | @Override 89 | public String toString() { 90 | return "CFG{" + "graph=" + graph + ", start=" + start + ", end=" + end + '}'; 91 | } 92 | 93 | public Set getIngoing(BasicBlock bb) { 94 | return graph.edgesOf(bb) 95 | .stream() 96 | .filter(e -> graph.getEdgeTarget(e) == bb) 97 | .map(graph::getEdgeSource) 98 | .collect(Collectors.toSet()); 99 | } 100 | 101 | public Set getOutgoing(BasicBlock bb) { 102 | return graph.edgesOf(bb) 103 | .stream() 104 | .filter(e -> graph.getEdgeSource(e) == bb) 105 | .map(graph::getEdgeTarget) 106 | .collect(Collectors.toSet()); 107 | } 108 | 109 | public CFG reduce() { 110 | CFG cfg = new CFG(null); 111 | cfg.start = start; 112 | cfg.end = end; 113 | cfg.addVertex(start); 114 | cfg.addVertex(end); 115 | 116 | 117 | BasicBlock block = start; 118 | // BasicBlock current; 119 | BasicBlock prev = null; 120 | 121 | // System.out.println(toString()); 122 | Deque stack = new ArrayDeque<>(); 123 | stack.push(start); 124 | Set processed = new TreeSet<>(); 125 | processed.add(end); 126 | // BasicBlock block = new BasicBlock(start.toString()); 127 | // cfg.addVertex(block); 128 | // reduce(cfg, processed, null, start, block); 129 | while (!stack.isEmpty()) { 130 | final BasicBlock current = stack.pop(); 131 | // System.out.println("Popped " + current + " from Stack: " + stack); 132 | switch (current.reducibility) { 133 | case BasicBlock.NOT_REDUCIBLE: 134 | // Add basic block to the new graph... 135 | cfg.addVertex(block); 136 | if (prev != null) { 137 | cfg.addEdge(prev, block); 138 | } 139 | 140 | // Create a new basic block 141 | prev = block; 142 | block = new BasicBlock(""); 143 | prev.addChild(current); 144 | cfg.addVertex(block); 145 | cfg.addEdge(prev, block); 146 | break; 147 | case BasicBlock.REDUCIBLE: 148 | // Add to block... 149 | System.out.println("Reduced: " + current.toString()); 150 | block.addChild(current); 151 | break; 152 | case BasicBlock.REDUCIBLE_SINGLETON: 153 | // Add basic block to the new graph... 154 | cfg.addVertex(block); 155 | if (prev != null) { 156 | cfg.addEdge(prev, block); 157 | } 158 | 159 | // Create a new basic block 160 | prev = block; 161 | block = new BasicBlock(""); 162 | block.addChild(current); 163 | cfg.addVertex(block); 164 | cfg.addEdge(prev, block); 165 | 166 | prev = block; 167 | block = new BasicBlock(""); 168 | cfg.addVertex(block); 169 | cfg.addEdge(prev, block); 170 | break; 171 | } 172 | 173 | processed.add(current); 174 | 175 | 176 | graph.edgesOf(current) 177 | .stream() 178 | .map(graph::getEdgeTarget) 179 | .filter(b -> !processed.contains(b)) 180 | .peek(edge -> System.out.println("Edge for vertex: " + current + " is " + edge)) 181 | .forEachOrdered(stack::push); 182 | 183 | final BasicBlock currentBlock = block; 184 | 185 | // graph.edgesOf(current) 186 | // .stream() 187 | // .map(graph::getEdgeTarget) 188 | // .filter(b -> cfg.graph.ver) 189 | // .forEachOrdered(b -> { 190 | // if (cfg.graph.containsVertex(b) && currentBlock.children.stream().anyMatch(_b -> graph.containsVertex(_b))) { 191 | // cfg.addEdge(currentBlock, b); 192 | // } 193 | // 194 | // }); 195 | 196 | // System.out.println("New Stack: " + stack); 197 | 198 | 199 | 200 | } 201 | 202 | cfg.addVertex(block); 203 | cfg.addEdge(block, end); 204 | return cfg; 205 | } 206 | 207 | } 208 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/IfConditionalASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.BasicBlock; 34 | import dsllanguage.CFG; 35 | import dsllanguage.DSLLexer; 36 | import java.util.logging.Logger; 37 | 38 | /** 39 | * 40 | * @author Louis Jenkins 41 | */ 42 | public class IfConditionalASTNode extends ConditionalASTNode { 43 | 44 | public IfConditionalASTNode(ASTNode conditional, ASTNode primaryBlock, ASTNode secondaryBlock) { 45 | super(conditional, primaryBlock, secondaryBlock); 46 | } 47 | 48 | @Override 49 | public Object execute() { 50 | Object conditionalValue = conditional.execute(); 51 | boolean cond; 52 | 53 | if (conditionalValue == null) { 54 | cond = false; 55 | } else if (conditionalValue instanceof Boolean) { 56 | cond = (Boolean) conditionalValue; 57 | } else if (conditionalValue instanceof Integer) { 58 | cond = ((Integer) conditionalValue) != 0; 59 | } else { 60 | DSLLexer.yyerror("Conditional evaluated to be invalid: " + conditionalValue); 61 | cond = false; 62 | } 63 | 64 | if (!cond) { 65 | if (secondaryBlock != null) { 66 | secondaryBlock.execute(); 67 | } 68 | return null; 69 | } 70 | 71 | primaryBlock.execute(); 72 | 73 | return null; 74 | } 75 | 76 | 77 | @Override 78 | public String toString() { 79 | return "IF"; 80 | } 81 | 82 | @Override 83 | public CFG createCFG() { 84 | CFG cfg = new CFG(null); 85 | BasicBlock start = new BasicBlock("if (" + conditional.stringify() + ")", BasicBlock.NOT_REDUCIBLE); 86 | BasicBlock primaryEnter = new BasicBlock("IF-STATEMENT-IF-ENTER", false); 87 | BasicBlock primaryEnd = new BasicBlock("IF-STATEMENT-IF-END", false); 88 | BasicBlock secondaryEnter = new BasicBlock("IF-STATEMENT-ELSE-ENTER", false); 89 | BasicBlock secondaryEnd = new BasicBlock("IF-STATEMENT-ELSE-END", false); 90 | BasicBlock end = new BasicBlock("IF-STATEMENT-END", false); 91 | cfg.start = start; 92 | cfg.end = end; 93 | 94 | cfg.addVertex(start); 95 | cfg.addVertex(end); 96 | 97 | // The PrimaryBlock (evaluated if conditional is true...) 98 | CFG primaryCFG = primaryBlock.createCFG(); 99 | cfg.addVertex(primaryCFG.start); 100 | cfg.addVertex(primaryCFG.end); 101 | cfg.addVertex(primaryEnter); 102 | cfg.addVertex(primaryEnd); 103 | primaryCFG.vertexSet().forEach(System.out::println); 104 | primaryCFG.vertexSet().forEach(cfg::addVertex); 105 | // Start -> PrimaryBlock.Start 106 | cfg.addEdge(start, primaryEnter); 107 | cfg.addEdge(primaryEnter, primaryCFG.start); 108 | primaryCFG.vertexSet() 109 | .stream() 110 | .forEach(block -> 111 | primaryCFG 112 | .edgesOf(block) 113 | .stream() 114 | .forEach(edge -> { 115 | Logger.getAnonymousLogger().info("Added edge to graph: " + edge); 116 | BasicBlock src = primaryCFG.getEdgeSource(edge); 117 | BasicBlock tgt = primaryCFG.getEdgeTarget(edge); 118 | cfg.addVertex(src); 119 | cfg.addVertex(tgt); 120 | cfg.addEdge(src, tgt); 121 | }) 122 | ); 123 | // PrimaryBlock.End -> End 124 | cfg.addEdge(primaryCFG.end, primaryEnd); 125 | cfg.addEdge(primaryEnd, end); 126 | 127 | if (secondaryBlock != null) { 128 | // The SecondaryBlock (evaluated if conditional is false AND if SecondaryBlock exists) 129 | CFG secondaryCFG = secondaryBlock.createCFG(); 130 | cfg.addVertex(secondaryCFG.start); 131 | cfg.addVertex(secondaryCFG.end); 132 | secondaryCFG.vertexSet().forEach(cfg::addVertex); 133 | cfg.addVertex(secondaryEnter); 134 | cfg.addVertex(secondaryEnd); 135 | // Start -> SecondaryBlock.Start 136 | cfg.addEdge(start, secondaryEnter); 137 | cfg.addEdge(secondaryEnter, secondaryCFG.start); 138 | secondaryCFG.vertexSet() 139 | .stream() 140 | .forEach(block -> 141 | secondaryCFG 142 | .edgesOf(block) 143 | .stream() 144 | .forEach(edge -> { 145 | BasicBlock src = secondaryCFG.getEdgeSource(edge); 146 | BasicBlock tgt = secondaryCFG.getEdgeTarget(edge); 147 | cfg.addVertex(src); 148 | cfg.addVertex(tgt); 149 | cfg.addEdge(src, tgt); 150 | }) 151 | ); 152 | cfg.addEdge(secondaryCFG.end, secondaryEnd); 153 | cfg.addEdge(secondaryEnd, end); 154 | } else { 155 | // No else statement, but conditional evaluates to false... 156 | cfg.addEdge(start, end); 157 | } 158 | 159 | 160 | Logger.getAnonymousLogger().info(cfg.toString()); 161 | return cfg; 162 | } 163 | 164 | @Override 165 | public String stringify() { 166 | return null; 167 | } 168 | 169 | 170 | } 171 | -------------------------------------------------------------------------------- /src/dsllanguage/DSLLexer.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage; 32 | 33 | import dsllanguage.ASTNodes.NameASTNode; 34 | import dsllanguage.ASTNodes.ASTNode; 35 | import dsllanguage.ASTNodes.ConstantASTNode; 36 | import java.io.BufferedReader; 37 | import java.io.File; 38 | import java.io.FileInputStream; 39 | import java.io.FileReader; 40 | import java.io.IOException; 41 | import java.io.InputStreamReader; 42 | import java.util.ArrayDeque; 43 | import java.util.Deque; 44 | import java.util.Queue; 45 | import java.util.stream.Collectors; 46 | import java.util.stream.Stream; 47 | 48 | /** 49 | * 50 | * @author Louis Jenkins 51 | */ 52 | public class DSLLexer implements DSLTokens { 53 | 54 | Queue tokens; 55 | public DSLLexer(File file) throws IOException { 56 | // Read all into buffer... 57 | tokens = new BufferedReader(new FileReader(file)) 58 | .lines() 59 | .flatMap(line -> Stream.of(line.replace(";", " ;").replace("(", "( ").replace(")", " )").split("[ \r\t\n]"))) 60 | .filter(str -> !"".equals(str.trim())) 61 | .collect(Collectors.toCollection(() -> new ArrayDeque())); 62 | } 63 | 64 | public DSLLexer(String contents) { 65 | // Read all into buffer... 66 | tokens = Stream.of(contents.split("\n")) 67 | .flatMap(line -> Stream.of(line.replace(";", " ;").replace("(", "( ").replace(")", " )").split("[ \r\t\n]"))) 68 | .filter(str -> !"".equals(str.trim())) 69 | .collect(Collectors.toCollection(() -> new ArrayDeque())); 70 | } 71 | 72 | 73 | public static void yyerror(String msg) { 74 | throw new IllegalArgumentException(msg); 75 | } 76 | 77 | private int c; 78 | 79 | public void nextChar() { 80 | if (c >= 0) { 81 | try { 82 | c = System.in.read(); 83 | } catch (Exception e) { 84 | c = (-1); 85 | } 86 | } 87 | } 88 | 89 | int token; 90 | ASTNode yylval; 91 | 92 | int nextToken() { 93 | for (;;) { 94 | String tok = tokens.poll(); 95 | if (tok == null) { 96 | return token = ENDINPUT; 97 | } 98 | 99 | // Look for operator symbols 100 | if (tok.length() == 1) { 101 | char ch; 102 | switch (ch = tok.charAt(0)) { 103 | case '+': 104 | case '*': 105 | case '/': 106 | case '-': 107 | case '<': 108 | case '>': 109 | case ';': 110 | case '(': 111 | case ')': 112 | case '{': 113 | case '}': 114 | case '=': 115 | return token = ch; 116 | } 117 | } 118 | 119 | if (tok.equals("==")) { 120 | return token = EQ; 121 | } 122 | 123 | if (tok.equals("!=")) { 124 | return token = NEQ; 125 | } 126 | 127 | if (tok.equals("<=")) { 128 | return token = LE; 129 | } 130 | 131 | if (tok.equals(">=")) { 132 | return token = GE; 133 | } 134 | 135 | // String literal 136 | if (tok.startsWith("\"")) { 137 | // In case String was chunked, need to collect next lines up to ending '"' 138 | // Also drop the excess '"' 139 | String str = tok.substring(1); 140 | while (!str.endsWith("\"")) { 141 | String nextStr = tokens.poll(); 142 | if (nextStr == null) { 143 | yyerror("Unterminated String!"); 144 | } 145 | 146 | str += " " + nextStr; 147 | } 148 | yylval = new ConstantASTNode(str.substring(0, str.length() - 1)); 149 | return token = STRING; 150 | } 151 | 152 | // Integer literal 153 | if (tok.matches("0") || tok.matches("[1-9][0-9]*")) { 154 | yylval = new ConstantASTNode(Integer.parseInt(tok)); 155 | return token = INTEGER; 156 | } 157 | 158 | // 'var' declaration 159 | if (tok.equals("var")) { 160 | // Check next token for name... 161 | String t = tokens.element(); 162 | if (t == null) { 163 | yyerror("Need NAME after 'var'"); 164 | } 165 | return token = VAR; 166 | } 167 | 168 | if (tok.equals("print")) { 169 | String t = tokens.element(); 170 | if (t == null) { 171 | yyerror("Need EXPR after 'print'"); 172 | } 173 | return token = PRINT; 174 | } 175 | 176 | if (tok.equals("while")) { 177 | String t = tokens.element(); 178 | if (t == null) { 179 | yyerror("Need EXPR after 'while'"); 180 | } 181 | return token = WHILE; 182 | } 183 | 184 | if (tok.equals("if")) { 185 | String t = tokens.element(); 186 | if (t == null) { 187 | yyerror("Need EXPR after 'if'"); 188 | } 189 | return token = IF; 190 | } 191 | 192 | if (tok.equals("else")) { 193 | String t = tokens.element(); 194 | if (t == null) { 195 | yyerror("Need EXPR after 'else'"); 196 | } 197 | return token = ELSE; 198 | } 199 | 200 | // name declaration 201 | if (tok.matches("[A-z][A-z0-9_]*")) { 202 | yylval = new NameASTNode(tok); 203 | return token = NAME; 204 | } 205 | 206 | 207 | // At this point, does not match the above... 208 | yyerror("Bad Input: " + tok); 209 | } 210 | } 211 | 212 | int getToken() { 213 | return token; 214 | } 215 | 216 | ASTNode getSemantic() { 217 | return yylval; 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTGraph.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage; 32 | 33 | import dsllanguage.ASTNodes.ProgramASTNode; 34 | import dsllanguage.ASTNodes.ASTNode; 35 | import com.jgraph.layout.JGraphFacade; 36 | import com.jgraph.layout.hierarchical.JGraphHierarchicalLayout; 37 | import com.jgraph.layout.tree.JGraphTreeLayout; 38 | import com.mxgraph.layout.hierarchical.mxHierarchicalLayout; 39 | import com.mxgraph.layout.mxCircleLayout; 40 | import com.mxgraph.layout.mxCompactTreeLayout; 41 | import com.mxgraph.model.mxGraphModel; 42 | import com.mxgraph.swing.mxGraphComponent; 43 | import com.mxgraph.util.mxConstants; 44 | import com.mxgraph.util.mxUtils; 45 | import com.mxgraph.view.mxGraphView; 46 | import com.mxgraph.view.mxStylesheet; 47 | import java.awt.BorderLayout; 48 | import java.awt.Color; 49 | import java.awt.Component; 50 | import java.awt.Dimension; 51 | import java.awt.GridBagConstraints; 52 | import java.awt.GridBagLayout; 53 | import java.awt.Rectangle; 54 | import java.awt.ScrollPane; 55 | import java.awt.event.MouseWheelEvent; 56 | import java.util.ArrayList; 57 | import java.util.Collection; 58 | import java.util.HashMap; 59 | import java.util.Hashtable; 60 | import java.util.List; 61 | import java.util.Map; 62 | import javax.swing.JApplet; 63 | import javax.swing.JFrame; 64 | import javax.swing.ScrollPaneConstants; 65 | import javax.swing.SwingConstants; 66 | import javax.swing.SwingUtilities; 67 | import org.jgraph.JGraph; 68 | import org.jgraph.graph.DefaultGraphCell; 69 | import org.jgraph.graph.GraphConstants; 70 | import org.jgrapht.Graph; 71 | import org.jgrapht.ext.JGraphModelAdapter; 72 | import org.jgrapht.ext.JGraphXAdapter; 73 | import org.jgrapht.graph.DefaultEdge; 74 | import org.jgrapht.graph.ListenableDirectedGraph; 75 | 76 | /** 77 | * 78 | * @author Louis Jenkins 79 | */ 80 | public class ASTGraph extends JApplet { 81 | 82 | public static List roots = new ArrayList<>(); 83 | 84 | public static int x = 0, y = 0; 85 | public static final ASTGraph INSTANCE = new ASTGraph(); 86 | private static final Dimension DEFAULT_SIZE = new Dimension( 530, 320 ); 87 | private static final Color DEFAULT_BG_COLOR = Color.decode( "#FAFBFF" ); 88 | Graph graph; 89 | JGraph jgraph; 90 | JGraphModelAdapter adapter; 91 | 92 | public static void main(String[] args) 93 | { 94 | INSTANCE.init(); 95 | 96 | JFrame frame = new JFrame(); 97 | frame.getContentPane().add(INSTANCE); 98 | frame.setExtendedState(JFrame.MAXIMIZED_BOTH); 99 | frame.setTitle("JGraphT Adapter to JGraph Demo"); 100 | frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 101 | frame.pack(); 102 | frame.setVisible(true); 103 | } 104 | 105 | @Override 106 | public void init() { 107 | graph = new ListenableDirectedGraph<>(DefaultEdge.class); 108 | adapter = new JGraphModelAdapter(graph); 109 | jgraph = new JGraph(adapter); 110 | adjustDisplaySettings(jgraph); 111 | getContentPane( ).add(jgraph ); 112 | resize( DEFAULT_SIZE ); 113 | 114 | 115 | Main.main(null); 116 | ProgramASTNode progNode = new ProgramASTNode(roots.toArray(new ASTNode[0])); 117 | CFG cfg = progNode.createCFG(); 118 | // System.out.println(cfg.reduce()); 119 | // JGraph cfgGraph = new JGraph(new JGraphXAdapter(cfg.getGraph())); 120 | JGraphXAdapter jgxAdapter = new JGraphXAdapter(cfg.getGraph()); 121 | jgxAdapter.setAutoSizeCells(true); 122 | jgxAdapter.setCellsResizable(true); 123 | // jgxAdapter.alignCells(mxConstants.ALIGN_CENTER); 124 | mxStylesheet stylesheet = jgxAdapter.getStylesheet(); 125 | Hashtable style = new Hashtable(); 126 | style.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_RECTANGLE); 127 | style.put(mxConstants.STYLE_OPACITY, 50); 128 | style.put(mxConstants.STYLE_FONTCOLOR, "#774400"); 129 | stylesheet.putCellStyle("ROUNDED", style); 130 | jgxAdapter.setStylesheet(stylesheet); 131 | jgxAdapter.getStylesheet().getDefaultEdgeStyle().put(mxConstants.STYLE_NOLABEL, "1"); 132 | mxGraphComponent graphComponent = new mxGraphComponent(jgxAdapter); 133 | graphComponent.setWheelScrollingEnabled(false); 134 | mxGraphModel graphModel = (mxGraphModel)graphComponent.getGraph().getModel(); 135 | 136 | 137 | 138 | // Collection cells = graphModel.getCells().values(); 139 | 140 | // mxUtils.setCellStyles(graphComponent.getGraph().getModel(), 141 | // cells.toArray(), mxConstants.STYLE_ENDARROW, mxConstants.NONE); 142 | getContentPane().add(graphComponent); 143 | mxHierarchicalLayout layout = new mxHierarchicalLayout(jgxAdapter); 144 | layout.execute(jgxAdapter.getDefaultParent()); 145 | 146 | this.addMouseWheelListener(e -> { 147 | mxGraphView view = graphComponent.getGraph().getView(); 148 | int notches = e.getWheelRotation(); 149 | double scale = view.getScale(); 150 | double newScale = view.getScale() - ((double) notches / 61.8033988272); 151 | view.setScale(newScale); 152 | 153 | 154 | }); 155 | // adjustDisplaySettings(cfgGraph); 156 | // getContentPane().add(cfgGraph); 157 | // final JGraphTreeLayout hir = new JGraphTreeLayout(); 158 | // final JGraphFacade graphFacade = new JGraphFacade(cfgGraph, new Object[] {cfg.start}); 159 | // graphFacade.setOrdered(true); 160 | // hir.setPositionMultipleTrees(true); 161 | // hir.setTreeDistance(50); 162 | // hir.setLevelDistance(50); 163 | // hir.run(graphFacade); 164 | // final Map nestedMap = graphFacade.createNestedMap(true, true); 165 | // cfgGraph.getGraphLayoutCache().edit(nestedMap); 166 | 167 | // SwingUtilities.invokeLater(() -> { 168 | // mxGraphView view = graphComponent.getGraph().getView(); 169 | // int compLen = graphComponent.getWidth(); 170 | // int viewLen = (int)view.getGraphBounds().getWidth(); 171 | // System.out.println("Complen: " + compLen + ", viewlen: " + viewLen); 172 | // System.out.println("Scale: " + (double)compLen/viewLen * view.getScale()); 173 | // view.setScale(2); 174 | // }); 175 | } 176 | 177 | private void adjustDisplaySettings( JGraph jg ) { 178 | jg.setPreferredSize( DEFAULT_SIZE ); 179 | 180 | Color c = DEFAULT_BG_COLOR; 181 | String colorStr = null; 182 | 183 | try { 184 | colorStr = getParameter( "bgcolor" ); 185 | } 186 | catch( Exception e ) {} 187 | 188 | if( colorStr != null ) { 189 | c = Color.decode( colorStr ); 190 | } 191 | 192 | jg.setBackground( c ); 193 | } 194 | 195 | private void positionVertexAt( Object vertex, int x, int y ) { 196 | DefaultGraphCell cell = adapter.getVertexCell( vertex ); 197 | Map attr = cell.getAttributes( ); 198 | Rectangle b = GraphConstants.getBounds( attr ).getBounds(); 199 | 200 | GraphConstants.setBounds( attr, new Rectangle( x, y, b.width, b.height ) ); 201 | 202 | Map cellAttr = new HashMap( ); 203 | cellAttr.put( cell, attr ); 204 | adapter.edit( cellAttr, null, null, null ); 205 | } 206 | 207 | public static void graph(ASTNode node) { 208 | node.addDependency(INSTANCE.graph); 209 | roots.add(node); 210 | 211 | final JGraphTreeLayout hir = new JGraphTreeLayout(); 212 | final JGraphFacade graphFacade = new JGraphFacade(INSTANCE.jgraph, roots.toArray()); 213 | graphFacade.setOrdered(true); 214 | hir.setPositionMultipleTrees(true); 215 | hir.setTreeDistance(25); 216 | hir.run(graphFacade); 217 | final Map nestedMap = graphFacade.createNestedMap(true, true); 218 | INSTANCE.jgraph.getGraphLayoutCache().edit(nestedMap); 219 | } 220 | } 221 | -------------------------------------------------------------------------------- /src/dsllanguage/DSLPanel.form: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | -------------------------------------------------------------------------------- /src/dsllanguage/ASTNodes/ProgramASTNode.java: -------------------------------------------------------------------------------- 1 | /* BSD 3-Clause License 2 | * 3 | * Copyright (c) 2017, Louis Jenkins 4 | * All rights reserved. 5 | * 6 | * Redistribution and use in source and binary forms, with or without 7 | * modification, are permitted provided that the following conditions are met: 8 | * 9 | * - Redistributions of source code must retain the above copyright 10 | * notice, this list of conditions and the following disclaimer. 11 | * 12 | * - Redistributions in binary form must reproduce the above copyright 13 | * notice, this list of conditions and the following disclaimer in the 14 | * documentation and/or other materials provided with the distribution. 15 | * 16 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 17 | * contributors may be used to endorse or promote products derived 18 | * from this software without specific prior written permission. 19 | * 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 22 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 23 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 24 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 | */ 31 | package dsllanguage.ASTNodes; 32 | 33 | import dsllanguage.BasicBlock; 34 | import dsllanguage.CFG; 35 | import java.util.ArrayList; 36 | import java.util.Collections; 37 | import java.util.List; 38 | import java.util.Set; 39 | import java.util.TreeSet; 40 | import java.util.logging.Logger; 41 | import java.util.stream.Stream; 42 | import org.jgrapht.DirectedGraph; 43 | import org.jgrapht.Graph; 44 | import org.jgrapht.graph.DefaultEdge; 45 | import org.jgrapht.graph.ListenableDirectedGraph; 46 | 47 | /** 48 | * 49 | * @author Louis Jenkins 50 | */ 51 | public class ProgramASTNode implements ASTNode { 52 | protected List body = new ArrayList<>(); 53 | 54 | public ProgramASTNode(ASTNode ...nodes) { 55 | Collections.addAll(body, nodes); 56 | } 57 | 58 | public Graph getAST() { 59 | Graph ast = new ListenableDirectedGraph<>(DefaultEdge.class); 60 | ast.addVertex(this); 61 | body.forEach(node -> { 62 | node.addDependency(ast); 63 | ast.addEdge(this, node); 64 | }); 65 | 66 | return ast; 67 | } 68 | 69 | @Override 70 | public Object execute() { 71 | body.forEach(ASTNode::execute); 72 | return null; 73 | } 74 | 75 | @Override 76 | public void addDependency(Graph graph) { 77 | graph.addVertex(this); 78 | 79 | body.forEach(node -> { 80 | node.addDependency(graph); 81 | graph.addEdge(this, node); 82 | }); 83 | } 84 | 85 | @Override 86 | public String toString() { 87 | return "PROGRAM"; 88 | } 89 | 90 | @Override 91 | public CFG createCFG() { 92 | CFG cfg = new CFG(null); 93 | BasicBlock start = new BasicBlock("PROGRAM-START", BasicBlock.NOT_REDUCIBLE); 94 | BasicBlock end = new BasicBlock("PROGRAM-END", BasicBlock.REDUCIBLE_SINGLETON); 95 | 96 | cfg.start = start; 97 | cfg.end = end; 98 | 99 | cfg.addVertex(start); 100 | cfg.addVertex(end); 101 | 102 | CFG firstCFG = body.get(0).createCFG(); 103 | cfg.addVertex(firstCFG.start); 104 | cfg.addVertex(firstCFG.end); 105 | cfg.addEdge(start, firstCFG.start); 106 | 107 | BasicBlock prev = firstCFG.end; 108 | 109 | // If there is only one node, then we would fall through the for loop, so 110 | // we just copy over all nodes in the CFG here. TODO: Don't Copy Paste 111 | if (body.size() == 1) { 112 | firstCFG.vertexSet() 113 | .stream() 114 | .forEachOrdered(block -> 115 | firstCFG 116 | .edgesOf(block) 117 | .stream() 118 | .forEachOrdered(edge -> { 119 | BasicBlock src = firstCFG.getEdgeSource(edge); 120 | BasicBlock tgt = firstCFG.getEdgeTarget(edge); 121 | 122 | cfg.addVertex(src); 123 | cfg.addVertex(tgt); 124 | cfg.addEdge(src, tgt); 125 | }) 126 | ); 127 | } 128 | for (ASTNode node : body.subList(1, body.size())) { 129 | if (node instanceof IfConditionalASTNode) { 130 | Logger.getAnonymousLogger().info(node.getClass().getName()); 131 | } 132 | CFG stmtCFG = node.createCFG(); 133 | if (stmtCFG == null) { 134 | continue; 135 | } 136 | cfg.addVertex(stmtCFG.start); 137 | cfg.addVertex(stmtCFG.end); 138 | cfg.addEdge(prev, stmtCFG.start); 139 | 140 | Logger.getAnonymousLogger().info(stmtCFG.toString()); 141 | 142 | stmtCFG.vertexSet() 143 | .stream() 144 | .forEachOrdered(block -> 145 | stmtCFG 146 | .edgesOf(block) 147 | .stream() 148 | .forEachOrdered(edge -> { 149 | BasicBlock src = stmtCFG.getEdgeSource(edge); 150 | BasicBlock tgt = stmtCFG.getEdgeTarget(edge); 151 | 152 | if (node instanceof IfConditionalASTNode) { 153 | Logger.getAnonymousLogger().info("Bridging Gap for: " + src + " and " + tgt); 154 | } 155 | cfg.addVertex(src); 156 | cfg.addVertex(tgt); 157 | cfg.addEdge(src, tgt); 158 | }) 159 | ); 160 | prev = stmtCFG.end; 161 | } 162 | 163 | cfg.addEdge(prev, end); 164 | 165 | removeTrivialNodes(cfg); 166 | 167 | Logger.getAnonymousLogger().info("Reducing..."); 168 | Set processed = new TreeSet<>(); 169 | reduceCFG(cfg, processed); 170 | // processed.clear(); 171 | // reduceCFG(cfg, processed); 172 | 173 | 174 | 175 | return cfg; 176 | } 177 | 178 | private static void removeTrivialNodes(CFG cfg) { 179 | Logger.getAnonymousLogger().info(cfg.toString()); 180 | Logger.getAnonymousLogger().info("Removing trivial nodes..."); 181 | // Remove any nodes without an intended description 182 | for (BasicBlock bb : cfg.vertexSet()) { 183 | Logger.getAnonymousLogger().info("BasicBlock: " + bb.description + ", Has Description: " + bb.hasDescription); 184 | if (!bb.hasDescription) { 185 | Logger.getAnonymousLogger().info("Removing: " + bb); 186 | if (cfg.getIngoing(bb).isEmpty()) { 187 | Logger.getAnonymousLogger().info(cfg.getGraph().edgeSet().toString()); 188 | } 189 | // Bridge gap... We know for a fact we only have ONE target, 190 | // because only trivial nodes are to be removed this way. 191 | if (cfg.getOutgoing(bb).isEmpty()) { 192 | continue; 193 | } 194 | BasicBlock tgt = cfg.getOutgoing(bb).iterator().next(); 195 | cfg.getIngoing(bb).stream() 196 | .forEachOrdered(src -> { 197 | cfg.addEdge(src, tgt); 198 | }); 199 | cfg.getGraph().removeVertex(bb); 200 | 201 | removeTrivialNodes(cfg); 202 | return; 203 | } 204 | } 205 | } 206 | 207 | 208 | private static void reduceCFG(CFG cfg, Set processed) { 209 | // BasicBlock root = cfg.getOutgoing(cfg.start).iterator().next(); 210 | for (BasicBlock bb : cfg.vertexSet()) { 211 | if (processed.contains(bb)) { 212 | continue; 213 | } 214 | // (b, bb) 215 | for (BasicBlock b : cfg.getIngoing(bb)) { 216 | 217 | if ((bb.reducibility == BasicBlock.REDUCIBLE || bb.reducibility == BasicBlock.NOT_REDUCIBLE) && b.reducibility == BasicBlock.REDUCIBLE) { 218 | BasicBlock block = new BasicBlock("", bb.reducibility); 219 | block.addChild(b); 220 | block.addChild(bb); 221 | 222 | Logger.getAnonymousLogger().info("Reducing to: \n" + block.description); 223 | cfg.addVertex(block); 224 | 225 | // Add all edges going in 226 | cfg.getIngoing(b) 227 | .stream() 228 | // .filter(incomingBlock -> !bb.equals(incomingBlock)) 229 | .forEach(incomingBlock -> cfg.addEdge(incomingBlock, block)); 230 | 231 | // Add all edges going out 232 | cfg.getOutgoing(bb) 233 | .stream() 234 | // .filter(outgoingBlock -> !b.equals(outgoingBlock)) 235 | .forEach(outgoingBlock -> cfg.addEdge(block, outgoingBlock)); 236 | 237 | cfg.getGraph().removeVertex(b); 238 | cfg.getGraph().removeVertex(bb); 239 | // processed.add(bb); 240 | // processed.add(b); 241 | processed.add(block); 242 | reduceCFG(cfg, processed); 243 | return; 244 | 245 | } 246 | } 247 | 248 | // (bb, b) 249 | for (BasicBlock b : cfg.getOutgoing(bb)) { 250 | 251 | if (bb.reducibility == BasicBlock.REDUCIBLE && b.reducibility == BasicBlock.REDUCIBLE) { 252 | BasicBlock block = new BasicBlock(""); 253 | block.addChild(bb); 254 | block.addChild(b); 255 | 256 | Logger.getAnonymousLogger().info("Reducing to: \n" + block.description); 257 | cfg.addVertex(block); 258 | 259 | // Add all edges going in 260 | cfg.getIngoing(bb) 261 | .stream() 262 | // .filter(incomingBlock -> !bb.equals(incomingBlock)) 263 | .forEach(incomingBlock -> cfg.addEdge(incomingBlock, block)); 264 | 265 | // Add all edges going out 266 | cfg.getOutgoing(b) 267 | .stream() 268 | // .filter(outgoingBlock -> !b.equals(outgoingBlock)) 269 | .forEach(outgoingBlock -> cfg.addEdge(block, outgoingBlock)); 270 | 271 | cfg.getGraph().removeVertex(b); 272 | cfg.getGraph().removeVertex(bb); 273 | // processed.add(bb); 274 | // processed.add(b); 275 | processed.add(block); 276 | reduceCFG(cfg, processed); 277 | return; 278 | 279 | } 280 | } 281 | } 282 | } 283 | 284 | @Override 285 | public String stringify() { 286 | return null; 287 | } 288 | 289 | 290 | } 291 | -------------------------------------------------------------------------------- /src/dsllanguage/DSLPanel.java: -------------------------------------------------------------------------------- 1 | package dsllanguage; 2 | 3 | 4 | import dsllanguage.ASTNodes.ProgramASTNode; 5 | import dsllanguage.ASTNodes.ASTNode; 6 | import com.mxgraph.layout.hierarchical.mxHierarchicalLayout; 7 | import com.mxgraph.layout.mxCompactTreeLayout; 8 | import com.mxgraph.layout.mxGraphLayout; 9 | import com.mxgraph.model.mxGraphModel; 10 | import com.mxgraph.swing.mxGraphComponent; 11 | import com.mxgraph.util.mxConstants; 12 | import com.mxgraph.view.mxGraphView; 13 | import com.mxgraph.view.mxStylesheet; 14 | import dsllanguage.DSL; 15 | import dsllanguage.DSLLexer; 16 | import java.awt.Component; 17 | import java.nio.file.Paths; 18 | import java.util.ArrayList; 19 | import java.util.Hashtable; 20 | import java.util.List; 21 | import java.util.logging.Logger; 22 | import javax.swing.JFrame; 23 | import javax.swing.JOptionPane; 24 | import javax.swing.text.AttributeSet; 25 | import javax.swing.text.Document; 26 | import javax.swing.text.PlainDocument; 27 | import javax.swing.text.SimpleAttributeSet; 28 | import javax.swing.text.StyleConstants; 29 | import javax.swing.text.StyleContext; 30 | import org.jgraph.JGraph; 31 | import org.jgrapht.Graph; 32 | import org.jgrapht.ext.JGraphModelAdapter; 33 | import org.jgrapht.ext.JGraphXAdapter; 34 | import org.jgrapht.graph.DefaultEdge; 35 | import org.jgrapht.graph.ListenableDirectedGraph; 36 | 37 | /* BSD 3-Clause License 38 | * 39 | * Copyright (c) 2017, Louis Jenkins 40 | * All rights reserved. 41 | * 42 | * Redistribution and use in source and binary forms, with or without 43 | * modification, are permitted provided that the following conditions are met: 44 | * 45 | * - Redistributions of source code must retain the above copyright 46 | * notice, this list of conditions and the following disclaimer. 47 | * 48 | * - Redistributions in binary form must reproduce the above copyright 49 | * notice, this list of conditions and the following disclaimer in the 50 | * documentation and/or other materials provided with the distribution. 51 | * 52 | * - Neither the name of Louis Jenkins, Bloomsburg University nor the names of its 53 | * contributors may be used to endorse or promote products derived 54 | * from this software without specific prior written permission. 55 | * 56 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 57 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 58 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 59 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 60 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 61 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 62 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 63 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 64 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 65 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 66 | */ 67 | 68 | /** 69 | * 70 | * @author Louis Jenkins 71 | */ 72 | public class DSLPanel extends javax.swing.JFrame { 73 | 74 | private static List roots = new ArrayList<>(); 75 | private static Graph graph; 76 | 77 | /** 78 | * Creates new form DSLPanel 79 | */ 80 | public DSLPanel() { 81 | initComponents(); 82 | Document doc = textInput.getDocument(); 83 | doc.putProperty(PlainDocument.tabSizeAttribute, 2); 84 | GlobalOutputStream.PRINTER = msg -> { 85 | textOutput.append(msg + "\n"); 86 | }; 87 | runButton.addActionListener(e -> { 88 | tearDown(); 89 | DSLLexer lexer = new DSLLexer(textInput.getText()); 90 | lexer.nextToken(); 91 | DSL dsl = new DSL(lexer); 92 | try { 93 | dsl.parse(); 94 | } catch (Exception ex) { 95 | JOptionPane.showMessageDialog( 96 | this, 97 | ex.getMessage(), 98 | "Error", 99 | JOptionPane.ERROR_MESSAGE 100 | ); 101 | return; 102 | } 103 | 104 | createAST(); 105 | createCFG(); 106 | 107 | 108 | SymbolTable.clear(); 109 | }); 110 | graph = new ListenableDirectedGraph<>(DefaultEdge.class); 111 | } 112 | 113 | public static void addNode(ASTNode node) { 114 | node.addDependency(graph); 115 | roots.add(node); 116 | } 117 | 118 | public void tearDown() { 119 | roots.clear(); 120 | SymbolTable.clear(); 121 | graph = new ListenableDirectedGraph<>(DefaultEdge.class); 122 | destroyCFG(); 123 | destroyAST(); 124 | } 125 | 126 | public void destroyCFG() { 127 | cfgPane.removeAll(); 128 | } 129 | 130 | public void createCFG() { 131 | ProgramASTNode progNode = new ProgramASTNode(roots.toArray(new ASTNode[0])); 132 | CFG cfg = progNode.createCFG(); 133 | JGraphXAdapter jgxAdapter = new JGraphXAdapter(cfg.getGraph()); 134 | jgxAdapter.setAutoSizeCells(true); 135 | jgxAdapter.setCellsResizable(true); 136 | // jgxAdapter.alignCells(mxConstants.ALIGN_CENTER); 137 | mxStylesheet stylesheet = jgxAdapter.getStylesheet(); 138 | Hashtable style = new Hashtable(); 139 | style.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_RECTANGLE); 140 | style.put(mxConstants.STYLE_OPACITY, 50); 141 | style.put(mxConstants.STYLE_FONTCOLOR, "#774400"); 142 | stylesheet.putCellStyle("ROUNDED", style); 143 | jgxAdapter.setStylesheet(stylesheet); 144 | jgxAdapter.getStylesheet().getDefaultEdgeStyle().put(mxConstants.STYLE_NOLABEL, "1"); 145 | mxGraphComponent graphComponent = new mxGraphComponent(jgxAdapter); 146 | graphComponent.setWheelScrollingEnabled(false); 147 | mxGraphModel graphModel = (mxGraphModel)graphComponent.getGraph().getModel(); 148 | 149 | cfgPane.add(graphComponent); 150 | mxHierarchicalLayout layout = new mxHierarchicalLayout(jgxAdapter); 151 | layout.execute(jgxAdapter.getDefaultParent()); 152 | 153 | this.addMouseWheelListener(e -> { 154 | mxGraphView view = graphComponent.getGraph().getView(); 155 | int notches = e.getWheelRotation(); 156 | double scale = view.getScale(); 157 | double newScale = view.getScale() - ((double) notches / 61.8033988272); 158 | view.setScale(newScale); 159 | 160 | 161 | }); 162 | 163 | cfgPane.revalidate(); 164 | cfgPane.repaint(); 165 | } 166 | 167 | public void destroyAST() { 168 | astPane.removeAll(); 169 | } 170 | 171 | public void createAST() { 172 | Logger.getAnonymousLogger().info(" Graph: " + graph); 173 | ProgramASTNode progNode = new ProgramASTNode(roots.toArray(new ASTNode[0])); 174 | JGraphXAdapter jgxAdapter = new JGraphXAdapter(progNode.getAST()); 175 | jgxAdapter.setAutoSizeCells(true); 176 | jgxAdapter.setCellsResizable(true); 177 | // jgxAdapter.alignCells(mxConstants.ALIGN_CENTER); 178 | mxStylesheet stylesheet = jgxAdapter.getStylesheet(); 179 | Hashtable style = new Hashtable(); 180 | style.put(mxConstants.STYLE_SHAPE, mxConstants.SHAPE_RECTANGLE); 181 | style.put(mxConstants.STYLE_OPACITY, 50); 182 | style.put(mxConstants.STYLE_FONTCOLOR, "#774400"); 183 | stylesheet.putCellStyle("ROUNDED", style); 184 | jgxAdapter.setStylesheet(stylesheet); 185 | jgxAdapter.getStylesheet().getDefaultEdgeStyle().put(mxConstants.STYLE_NOLABEL, "1"); 186 | mxGraphComponent graphComponent = new mxGraphComponent(jgxAdapter); 187 | graphComponent.setWheelScrollingEnabled(false); 188 | mxGraphModel graphModel = (mxGraphModel)graphComponent.getGraph().getModel(); 189 | 190 | astPane.add(graphComponent); 191 | mxGraphLayout layout = new mxCompactTreeLayout(jgxAdapter, false); 192 | layout.execute(jgxAdapter.getDefaultParent()); 193 | 194 | this.addMouseWheelListener(e -> { 195 | mxGraphView view = graphComponent.getGraph().getView(); 196 | int notches = e.getWheelRotation(); 197 | double scale = view.getScale(); 198 | double newScale = view.getScale() - ((double) notches / 61.8033988272); 199 | view.setScale(newScale); 200 | System.out.println(scale + " -> " + newScale); 201 | 202 | 203 | }); 204 | 205 | astPane.revalidate(); 206 | astPane.repaint(); 207 | } 208 | 209 | /** 210 | * This method is called from within the constructor to initialize the form. 211 | * WARNING: Do NOT modify this code. The content of this method is always 212 | * regenerated by the Form Editor. 213 | */ 214 | @SuppressWarnings("unchecked") 215 | // //GEN-BEGIN:initComponents 216 | private void initComponents() { 217 | 218 | jPanel1 = new javax.swing.JPanel(); 219 | jToolBar1 = new javax.swing.JToolBar(); 220 | runButton = new javax.swing.JButton(); 221 | jPanel2 = new javax.swing.JPanel(); 222 | jScrollPane1 = new javax.swing.JScrollPane(); 223 | textInput = new javax.swing.JEditorPane(); 224 | tabbedPane = new javax.swing.JTabbedPane(); 225 | outputPane = new javax.swing.JPanel(); 226 | jScrollPane2 = new javax.swing.JScrollPane(); 227 | textOutput = new javax.swing.JTextArea(); 228 | astPane = new javax.swing.JPanel(); 229 | cfgPane = new javax.swing.JPanel(); 230 | 231 | javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); 232 | jPanel1.setLayout(jPanel1Layout); 233 | jPanel1Layout.setHorizontalGroup( 234 | jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 235 | .addGap(0, 100, Short.MAX_VALUE) 236 | ); 237 | jPanel1Layout.setVerticalGroup( 238 | jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 239 | .addGap(0, 100, Short.MAX_VALUE) 240 | ); 241 | 242 | setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); 243 | 244 | jToolBar1.setRollover(true); 245 | 246 | runButton.setText("Run"); 247 | runButton.setFocusable(false); 248 | runButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER); 249 | runButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM); 250 | jToolBar1.add(runButton); 251 | 252 | jPanel2.setBackground(new java.awt.Color(255, 255, 255)); 253 | 254 | textInput.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N 255 | jScrollPane1.setViewportView(textInput); 256 | 257 | javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); 258 | jPanel2.setLayout(jPanel2Layout); 259 | jPanel2Layout.setHorizontalGroup( 260 | jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 261 | .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 328, Short.MAX_VALUE) 262 | ); 263 | jPanel2Layout.setVerticalGroup( 264 | jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 265 | .addGroup(jPanel2Layout.createSequentialGroup() 266 | .addContainerGap() 267 | .addComponent(jScrollPane1)) 268 | ); 269 | 270 | tabbedPane.setBackground(new java.awt.Color(255, 255, 255)); 271 | 272 | textOutput.setEditable(false); 273 | textOutput.setBackground(new java.awt.Color(204, 204, 204)); 274 | textOutput.setColumns(20); 275 | textOutput.setRows(5); 276 | jScrollPane2.setViewportView(textOutput); 277 | 278 | javax.swing.GroupLayout outputPaneLayout = new javax.swing.GroupLayout(outputPane); 279 | outputPane.setLayout(outputPaneLayout); 280 | outputPaneLayout.setHorizontalGroup( 281 | outputPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 282 | .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE) 283 | ); 284 | outputPaneLayout.setVerticalGroup( 285 | outputPaneLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 286 | .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 465, Short.MAX_VALUE) 287 | ); 288 | 289 | tabbedPane.addTab("Output", outputPane); 290 | 291 | astPane.setLayout(new java.awt.BorderLayout()); 292 | tabbedPane.addTab("AST", astPane); 293 | 294 | cfgPane.setLayout(new java.awt.BorderLayout()); 295 | tabbedPane.addTab("CFG", cfgPane); 296 | 297 | javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); 298 | getContentPane().setLayout(layout); 299 | layout.setHorizontalGroup( 300 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 301 | .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 302 | .addGroup(layout.createSequentialGroup() 303 | .addContainerGap() 304 | .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 305 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 306 | .addComponent(tabbedPane) 307 | .addContainerGap()) 308 | ); 309 | layout.setVerticalGroup( 310 | layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 311 | .addGroup(layout.createSequentialGroup() 312 | .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE) 313 | .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) 314 | .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) 315 | .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 316 | .addComponent(tabbedPane)) 317 | .addContainerGap()) 318 | ); 319 | 320 | pack(); 321 | }// //GEN-END:initComponents 322 | 323 | /** 324 | * @param args the command line arguments 325 | */ 326 | public static void main(String args[]) { 327 | /* Set the Nimbus look and feel */ 328 | // 329 | /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 330 | * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 331 | */ 332 | try { 333 | for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { 334 | if ("Nimbus".equals(info.getName())) { 335 | javax.swing.UIManager.setLookAndFeel(info.getClassName()); 336 | break; 337 | } 338 | } 339 | } catch (ClassNotFoundException ex) { 340 | java.util.logging.Logger.getLogger(DSLPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 341 | } catch (InstantiationException ex) { 342 | java.util.logging.Logger.getLogger(DSLPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 343 | } catch (IllegalAccessException ex) { 344 | java.util.logging.Logger.getLogger(DSLPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 345 | } catch (javax.swing.UnsupportedLookAndFeelException ex) { 346 | java.util.logging.Logger.getLogger(DSLPanel.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); 347 | } 348 | // 349 | 350 | /* Create and display the form */ 351 | java.awt.EventQueue.invokeLater(new Runnable() { 352 | public void run() { 353 | new DSLPanel().setVisible(true); 354 | } 355 | }); 356 | } 357 | 358 | // Variables declaration - do not modify//GEN-BEGIN:variables 359 | private javax.swing.JPanel astPane; 360 | private javax.swing.JPanel cfgPane; 361 | private javax.swing.JPanel jPanel1; 362 | private javax.swing.JPanel jPanel2; 363 | private javax.swing.JScrollPane jScrollPane1; 364 | private javax.swing.JScrollPane jScrollPane2; 365 | private javax.swing.JToolBar jToolBar1; 366 | private javax.swing.JPanel outputPane; 367 | private javax.swing.JButton runButton; 368 | private javax.swing.JTabbedPane tabbedPane; 369 | private javax.swing.JEditorPane textInput; 370 | private javax.swing.JTextArea textOutput; 371 | // End of variables declaration//GEN-END:variables 372 | } 373 | --------------------------------------------------------------------------------