Search in sources :

Example 1 with ParseException

use of org.apache.sysml.parser.ParseException in project incubator-systemml by apache.

the class PyDMLParserWrapper method doParse.

/**
	 * This function is supposed to be called directly only from PydmlSyntacticValidator when it encounters 'import'
	 * @param fileName script file name
	 * @param dmlScript script file contents
	 * @param sourceNamespace namespace from source statement
	 * @param argVals script arguments
	 * @return dml program, or null if at least one error
	 * @throws ParseException if ParseException occurs
	 */
public DMLProgram doParse(String fileName, String dmlScript, String sourceNamespace, Map<String, String> argVals) throws ParseException {
    DMLProgram dmlPgm = null;
    ANTLRInputStream in;
    try {
        if (dmlScript == null) {
            dmlScript = readDMLScript(fileName, LOG);
        }
        InputStream stream = new ByteArrayInputStream(dmlScript.getBytes());
        in = new org.antlr.v4.runtime.ANTLRInputStream(stream);
    } catch (FileNotFoundException e) {
        throw new ParseException("Cannot find file/resource: " + fileName, e);
    } catch (IOException e) {
        throw new ParseException("Cannot open file: " + fileName, e);
    } catch (LanguageException e) {
        throw new ParseException(e.getMessage(), e);
    }
    ProgramrootContext ast = null;
    CustomErrorListener errorListener = new CustomErrorListener();
    try {
        PydmlLexer lexer = new PydmlLexer(in);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        PydmlParser antlr4Parser = new PydmlParser(tokens);
        // For now no optimization, since it is not able to parse integer value. 
        boolean tryOptimizedParsing = false;
        if (tryOptimizedParsing) {
            // Try faster and simpler SLL
            antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.SLL);
            antlr4Parser.removeErrorListeners();
            antlr4Parser.setErrorHandler(new BailErrorStrategy());
            try {
                ast = antlr4Parser.programroot();
            // If successful, no need to try out full LL(*) ... SLL was enough
            } catch (ParseCancellationException ex) {
                // Error occurred, so now try full LL(*) for better error messages
                tokens.reset();
                antlr4Parser.reset();
                if (fileName != null) {
                    errorListener.setCurrentFileName(fileName);
                } else {
                    errorListener.setCurrentFileName("MAIN_SCRIPT");
                }
                // Set our custom error listener
                antlr4Parser.addErrorListener(errorListener);
                antlr4Parser.setErrorHandler(new DefaultErrorStrategy());
                antlr4Parser.getInterpreter().setPredictionMode(PredictionMode.LL);
                ast = antlr4Parser.programroot();
            }
        } else {
            // Set our custom error listener
            antlr4Parser.removeErrorListeners();
            antlr4Parser.addErrorListener(errorListener);
            errorListener.setCurrentFileName(fileName);
            // Now do the parsing
            ast = antlr4Parser.programroot();
        }
    } catch (Exception e) {
        throw new ParseException("ERROR: Cannot parse the program:" + fileName, e);
    }
    // Now convert the parse tree into DMLProgram
    // Do syntactic validation while converting 
    ParseTree tree = ast;
    // And also do syntactic validation
    ParseTreeWalker walker = new ParseTreeWalker();
    // Get list of function definitions which take precedence over built-in functions if same name
    PydmlPreprocessor prep = new PydmlPreprocessor(errorListener);
    walker.walk(prep, tree);
    // Syntactic validation
    PydmlSyntacticValidator validator = new PydmlSyntacticValidator(errorListener, argVals, sourceNamespace, prep.getFunctionDefs());
    walker.walk(validator, tree);
    errorListener.unsetCurrentFileName();
    this.parseIssues = errorListener.getParseIssues();
    this.atLeastOneWarning = errorListener.isAtLeastOneWarning();
    this.atLeastOneError = errorListener.isAtLeastOneError();
    if (atLeastOneError) {
        throw new ParseException(parseIssues, dmlScript);
    }
    if (atLeastOneWarning) {
        LOG.warn(CustomErrorListener.generateParseIssuesMessage(dmlScript, parseIssues));
    }
    dmlPgm = createDMLProgram(ast, sourceNamespace);
    return dmlPgm;
}
Also used : FileNotFoundException(java.io.FileNotFoundException) LanguageException(org.apache.sysml.parser.LanguageException) DefaultErrorStrategy(org.antlr.v4.runtime.DefaultErrorStrategy) DMLProgram(org.apache.sysml.parser.DMLProgram) ParseTreeWalker(org.antlr.v4.runtime.tree.ParseTreeWalker) CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) CustomErrorListener(org.apache.sysml.parser.common.CustomErrorListener) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) InputStream(java.io.InputStream) BailErrorStrategy(org.antlr.v4.runtime.BailErrorStrategy) ProgramrootContext(org.apache.sysml.parser.pydml.PydmlParser.ProgramrootContext) IOException(java.io.IOException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ParseException(org.apache.sysml.parser.ParseException) ByteArrayInputStream(java.io.ByteArrayInputStream) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ParseException(org.apache.sysml.parser.ParseException) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 2 with ParseException

use of org.apache.sysml.parser.ParseException in project incubator-systemml by apache.

the class DMLScript method executeScript.

/**
	 * Single entry point for all public invocation alternatives (e.g.,
	 * main, executeScript, JaqlUdf etc)
	 * 
	 * @param conf Hadoop configuration
	 * @param args arguments
	 * @return true if success, false otherwise
	 * @throws DMLException if DMLException occurs
	 * @throws ParseException if ParseException occurs
	 */
public static boolean executeScript(Configuration conf, String[] args) throws DMLException {
    //parse arguments and set execution properties
    //keep old rtplatform
    RUNTIME_PLATFORM oldrtplatform = rtplatform;
    //keep old explain
    ExplainType oldexplain = EXPLAIN;
    Options options = createCLIOptions();
    try {
        DMLOptions dmlOptions = parseCLArguments(args, options);
        // String[] scriptArgs = null; //optional script arguments
        // boolean namedScriptArgs = false;
        STATISTICS = dmlOptions.stats;
        STATISTICS_COUNT = dmlOptions.statsCount;
        USE_ACCELERATOR = dmlOptions.gpu;
        FORCE_ACCELERATOR = dmlOptions.forceGPU;
        EXPLAIN = dmlOptions.explainType;
        ENABLE_DEBUG_MODE = dmlOptions.debug;
        SCRIPT_TYPE = dmlOptions.scriptType;
        rtplatform = dmlOptions.execMode;
        String fnameOptConfig = dmlOptions.configFile;
        boolean isFile = dmlOptions.filePath != null;
        String fileOrScript = isFile ? dmlOptions.filePath : dmlOptions.script;
        boolean help = dmlOptions.help;
        if (help) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("systemml", options);
            return true;
        }
        if (dmlOptions.clean) {
            cleanSystemMLWorkspace();
            return true;
        }
        //set log level
        if (!ENABLE_DEBUG_MODE)
            setLoggingProperties(conf);
        //Step 2: prepare script invocation
        if (isFile && StringUtils.endsWithIgnoreCase(fileOrScript, ".pydml")) {
            SCRIPT_TYPE = ScriptType.PYDML;
        }
        String dmlScriptStr = readDMLScript(isFile, fileOrScript);
        Map<String, String> argVals = dmlOptions.argVals;
        DML_FILE_PATH_ANTLR_PARSER = dmlOptions.filePath;
        //Step 3: invoke dml script
        printInvocationInfo(fileOrScript, fnameOptConfig, argVals);
        if (ENABLE_DEBUG_MODE) {
            // inner try loop is just to isolate the debug exception, which will allow to manage the bugs from debugger v/s runtime
            launchDebugger(dmlScriptStr, fnameOptConfig, argVals, SCRIPT_TYPE);
        } else {
            execute(dmlScriptStr, fnameOptConfig, argVals, args, SCRIPT_TYPE);
        }
    } catch (AlreadySelectedException e) {
        System.err.println("Mutually exclusive options were selected. " + e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("systemml", options);
        return false;
    } catch (org.apache.commons.cli.ParseException e) {
        System.err.println(e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("systemml", options);
    } catch (ParseException pe) {
        throw pe;
    } catch (DMLScriptException e) {
        //rethrow DMLScriptException to propagate stop call
        throw e;
    } catch (Exception ex) {
        LOG.error("Failed to execute DML script.", ex);
        throw new DMLException(ex);
    } finally {
        //reset runtime platform and visualize flag
        rtplatform = oldrtplatform;
        EXPLAIN = oldexplain;
    }
    return true;
}
Also used : Options(org.apache.commons.cli.Options) AlreadySelectedException(org.apache.commons.cli.AlreadySelectedException) HopsException(org.apache.sysml.hops.HopsException) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) LopsException(org.apache.sysml.lops.LopsException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) AlreadySelectedException(org.apache.commons.cli.AlreadySelectedException) ParseException(org.apache.sysml.parser.ParseException) DMLDebuggerException(org.apache.sysml.debug.DMLDebuggerException) HelpFormatter(org.apache.commons.cli.HelpFormatter) ExplainType(org.apache.sysml.utils.Explain.ExplainType) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) ParseException(org.apache.sysml.parser.ParseException)

Example 3 with ParseException

use of org.apache.sysml.parser.ParseException in project incubator-systemml by apache.

the class Connection method prepareScript.

/**
	 * Prepares (precompiles) a script, sets input parameter values, and registers input and output variables.
	 * 
	 * @param script string representing the DML or PyDML script
	 * @param args map of input parameters ($) and their values
	 * @param inputs string array of input variables to register
	 * @param outputs string array of output variables to register
	 * @param parsePyDML {@code true} if PyDML, {@code false} if DML
	 * @return PreparedScript object representing the precompiled script
	 * @throws DMLException if DMLException occurs
	 */
public PreparedScript prepareScript(String script, Map<String, String> args, String[] inputs, String[] outputs, boolean parsePyDML) throws DMLException {
    DMLScript.SCRIPT_TYPE = parsePyDML ? ScriptType.PYDML : ScriptType.DML;
    //prepare arguments
    //simplified compilation chain
    Program rtprog = null;
    try {
        //parsing
        ParserWrapper parser = ParserFactory.createParser(parsePyDML ? ScriptType.PYDML : ScriptType.DML);
        DMLProgram prog = parser.parse(null, script, args);
        //language validate
        DMLTranslator dmlt = new DMLTranslator(prog);
        dmlt.liveVariableAnalysis(prog);
        dmlt.validateParseTree(prog);
        //hop construct/rewrite
        dmlt.constructHops(prog);
        dmlt.rewriteHopsDAG(prog);
        //rewrite persistent reads/writes
        RewriteRemovePersistentReadWrite rewrite = new RewriteRemovePersistentReadWrite(inputs, outputs);
        ProgramRewriter rewriter2 = new ProgramRewriter(rewrite);
        rewriter2.rewriteProgramHopDAGs(prog);
        //lop construct and runtime prog generation
        dmlt.constructLops(prog);
        rtprog = prog.getRuntimeProgram(_dmlconf);
        //final cleanup runtime prog
        JMLCUtils.cleanupRuntimeProgram(rtprog, outputs);
    //System.out.println(Explain.explain(rtprog));
    } catch (ParseException pe) {
        // don't chain ParseException (for cleaner error output)
        throw pe;
    } catch (Exception ex) {
        throw new DMLException(ex);
    }
    //return newly create precompiled script 
    return new PreparedScript(rtprog, inputs, outputs);
}
Also used : ProgramRewriter(org.apache.sysml.hops.rewrite.ProgramRewriter) DMLProgram(org.apache.sysml.parser.DMLProgram) Program(org.apache.sysml.runtime.controlprogram.Program) DMLException(org.apache.sysml.api.DMLException) DMLProgram(org.apache.sysml.parser.DMLProgram) ParserWrapper(org.apache.sysml.parser.ParserWrapper) ParseException(org.apache.sysml.parser.ParseException) DMLTranslator(org.apache.sysml.parser.DMLTranslator) RewriteRemovePersistentReadWrite(org.apache.sysml.hops.rewrite.RewriteRemovePersistentReadWrite) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) DMLException(org.apache.sysml.api.DMLException) IOException(java.io.IOException) ParseException(org.apache.sysml.parser.ParseException)

Example 4 with ParseException

use of org.apache.sysml.parser.ParseException in project incubator-systemml by apache.

the class DMLScript method cleanupHadoopExecution.

public static void cleanupHadoopExecution(DMLConfig config) throws IOException, ParseException {
    //create dml-script-specific suffix
    StringBuilder sb = new StringBuilder();
    sb.append(Lop.FILE_SEPARATOR);
    sb.append(Lop.PROCESS_PREFIX);
    sb.append(DMLScript.getUUID());
    String dirSuffix = sb.toString();
    //1) cleanup scratch space (everything for current uuid) 
    //(required otherwise export to hdfs would skip assumed unnecessary writes if same name)
    MapReduceTool.deleteFileIfExistOnHDFS(config.getTextValue(DMLConfig.SCRATCH_SPACE) + dirSuffix);
    //2) cleanup hadoop working dirs (only required for LocalJobRunner (local job tracker), because
    //this implementation does not create job specific sub directories)
    JobConf job = new JobConf(ConfigurationManager.getCachedJobConf());
    if (InfrastructureAnalyzer.isLocalMode(job)) {
        try {
            LocalFileUtils.deleteFileIfExists(//staging dir (for local mode only) 
            DMLConfig.LOCAL_MR_MODE_STAGING_DIR + dirSuffix);
            LocalFileUtils.deleteFileIfExists(//local dir
            MRJobConfiguration.getLocalWorkingDirPrefix(job) + dirSuffix);
            MapReduceTool.deleteFileIfExistOnHDFS(//system dir
            MRJobConfiguration.getSystemWorkingDirPrefix(job) + dirSuffix);
            MapReduceTool.deleteFileIfExistOnHDFS(//staging dir
            MRJobConfiguration.getStagingWorkingDirPrefix(job) + dirSuffix);
        } catch (Exception ex) {
            //we give only a warning because those directories are written by the mapred deamon 
            //and hence, execution can still succeed
            LOG.warn("Unable to cleanup hadoop working dirs: " + ex.getMessage());
        }
    }
    //3) cleanup systemml-internal working dirs
    //might be local/hdfs
    CacheableData.cleanupCacheDir();
    LocalFileUtils.cleanupWorkingDirectory();
}
Also used : JobConf(org.apache.hadoop.mapred.JobConf) HopsException(org.apache.sysml.hops.HopsException) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) DMLRuntimeException(org.apache.sysml.runtime.DMLRuntimeException) LopsException(org.apache.sysml.lops.LopsException) LanguageException(org.apache.sysml.parser.LanguageException) IOException(java.io.IOException) AlreadySelectedException(org.apache.commons.cli.AlreadySelectedException) ParseException(org.apache.sysml.parser.ParseException) DMLDebuggerException(org.apache.sysml.debug.DMLDebuggerException)

Example 5 with ParseException

use of org.apache.sysml.parser.ParseException in project incubator-systemml by apache.

the class DMLScript method main.

/**
	 *
	 * @param args command-line arguments
	 * @throws IOException if an IOException occurs
	 * @throws DMLException if a DMLException occurs
	 */
public static void main(String[] args) throws IOException, DMLException {
    Configuration conf = new Configuration(ConfigurationManager.getCachedJobConf());
    String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
    try {
        DMLScript.executeScript(conf, otherArgs);
    } catch (ParseException pe) {
        System.err.println(pe.getMessage());
    } catch (DMLScriptException e) {
        // In case of DMLScriptException, simply print the error message.
        System.err.println(e.getMessage());
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) MRJobConfiguration(org.apache.sysml.runtime.matrix.mapred.MRJobConfiguration) DMLScriptException(org.apache.sysml.runtime.DMLScriptException) ParseException(org.apache.sysml.parser.ParseException) GenericOptionsParser(org.apache.hadoop.util.GenericOptionsParser)

Aggregations

ParseException (org.apache.sysml.parser.ParseException)10 IOException (java.io.IOException)5 DMLProgram (org.apache.sysml.parser.DMLProgram)5 LanguageException (org.apache.sysml.parser.LanguageException)4 FileNotFoundException (java.io.FileNotFoundException)3 DMLRuntimeException (org.apache.sysml.runtime.DMLRuntimeException)3 DMLScriptException (org.apache.sysml.runtime.DMLScriptException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)2 BailErrorStrategy (org.antlr.v4.runtime.BailErrorStrategy)2 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)2 DefaultErrorStrategy (org.antlr.v4.runtime.DefaultErrorStrategy)2 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)2 ParseTree (org.antlr.v4.runtime.tree.ParseTree)2 ParseTreeWalker (org.antlr.v4.runtime.tree.ParseTreeWalker)2 AlreadySelectedException (org.apache.commons.cli.AlreadySelectedException)2 DMLDebuggerException (org.apache.sysml.debug.DMLDebuggerException)2 HopsException (org.apache.sysml.hops.HopsException)2