In ANTLR 3, how do I generate a lexer (and parser) at runtime instead of ahead of time?

前端 未结 3 2064
小鲜肉
小鲜肉 2021-02-06 13:56

I want to generate an antlr lexer at runtime -- that is, generate the grammar and from the grammar generate the lexer class, and its supporting bits at runtime. I am happy to fe

3条回答
  •  既然无缘
    2021-02-06 14:33

    You'll have to use org.antlr.Tool() class to get it working.

    You can check ANTLRWorks source code on github to have an idea how to use it, specifically the generate() method here:

    ErrorListener el = ErrorListener.getThreadInstance();
    ErrorManager.setErrorListener(el);
    
    String[] params;
    if(debug)
        params = new String[] { "-debug", "-o", getOutputPath(), "-lib", window.getFileFolder(), window.getFilePath() };
    else
        params = new String[] { "-o", getOutputPath(), "-lib", window.getFileFolder(), window.getFilePath() };
    
    new File(getOutputPath()).mkdirs();
    
    Tool antlr = new Tool(Utils.concat(params, AWPrefs.getANTLR3Options()));
    antlr.process();
    
    boolean success = !el.hasErrors();
    if(success) {
        dateOfModificationOnDisk = window.getDocument().getDateOfModificationOnDisk();
    }
    lastError = el.getFirstErrorMessage();
    el.clear();
    ErrorManager.removeErrorListener();
    return success;
    

提交回复
热议问题