⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scantest.java

📁 一些基本的编译文法的描述 基于java的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            this.analyseSList(stn,false);            stn = stn.getSibling();            lexicalResult = this.scan();            if(lexicalResult==null) {                this.syntaxError();                return ;            }            if(lexicalResult.getType().equals("end")) {                stn.setSibling(new SyntaxTreeNode(lexicalResult.getType(),null,null));            }            else {                this.syntaxError();                return ;            }        }        else {            this.syntaxError();            return ;        }    }    private void analyseSList(SyntaxTreeNode stn,boolean isChild) {        if(isChild) {            stn.setChild(new SyntaxTreeNode("SList",null,null));            stn = stn.getChild();        }        else {            stn.setSibling(new SyntaxTreeNode("SList",null,null));            stn = stn.getSibling();        }        this.analyseS(stn,true);        stn = stn.getChild();        this.analyseSList$(stn,false);    }    private void analyseSList$(SyntaxTreeNode stn,boolean isChild) {        if(isChild) {            stn.setChild(new SyntaxTreeNode("SList$",null,null));            stn = stn.getChild();        }        else {            stn.setSibling(new SyntaxTreeNode("SList$",null,null));            stn = stn.getSibling();        }        LexicalResult lexicalResult = this.scan();        if(lexicalResult==null) {            stn.setChild(new SyntaxTreeNode("ε",null,null));        }        else if(lexicalResult.getType().equals(";")) {            stn.setChild(new SyntaxTreeNode(";",null,null));            stn = stn.getChild();            this.analyseS(stn,false);            stn = stn.getSibling();            this.analyseSList$(stn,false);        }        else {            this.backScan();        }    }    private void analyseC(SyntaxTreeNode stn,boolean isChild) {        if(isChild) {            stn.setChild(new SyntaxTreeNode("C",null,null));            stn = stn.getChild();        }        else {            stn.setSibling(new SyntaxTreeNode("C",null,null));            stn = stn.getSibling();        }        this.analyseE(stn,true);        stn = stn.getChild();        LexicalResult lexicalResult = this.scan();        if(lexicalResult==null) {            this.syntaxError();            return ;        }        if(lexicalResult.getType().equals(">")) {            stn.setSibling(new SyntaxTreeNode(">",null,null));            stn = stn.getSibling();        }        else if(lexicalResult.getType().equals("<")) {            stn.setSibling(new SyntaxTreeNode("<",null,null));            stn = stn.getSibling();        }        else if(lexicalResult.getType().equals("=")) {            stn.setSibling(new SyntaxTreeNode("=",null,null));            stn = stn.getSibling();        }        else {            this.syntaxError();            return ;        }        this.analyseE(stn,false);    }    private void analyseE(SyntaxTreeNode stn,boolean isChild) {        if(isChild) {            stn.setChild(new SyntaxTreeNode("E",null,null));            stn = stn.getChild();        }        else {            stn.setSibling(new SyntaxTreeNode("E",null,null));            stn = stn.getSibling();        }        this.analyseT(stn,true);        stn = stn.getChild();        this.analyseE$(stn,false);    }    private void analyseE$(SyntaxTreeNode stn,boolean isChild) {        if(isChild) {            stn.setChild(new SyntaxTreeNode("E$",null,null));            stn = stn.getChild();        }        else {            stn.setSibling(new SyntaxTreeNode("E$",null,null));            stn = stn.getSibling();        }        LexicalResult lexicalResult = this.scan();        if(lexicalResult==null) {            stn.setChild(new SyntaxTreeNode("ε",null,null));        }        else if(lexicalResult.getType().equals("+")) {            stn.setChild(new SyntaxTreeNode("+",null,null));            stn = stn.getChild();            this.analyseT(stn,false);            stn = stn.getSibling();            this.analyseE$(stn,false);        }        else if(lexicalResult.getType().equals("-")) {            stn.setChild(new SyntaxTreeNode("-",null,null));            stn = stn.getChild();            this.analyseT(stn,false);            stn = stn.getSibling();            this.analyseE$(stn,false);        }        else {            this.backScan();        }    }    private void analyseT(SyntaxTreeNode stn,boolean isChild) {        if(isChild) {            stn.setChild(new SyntaxTreeNode("T",null,null));            stn = stn.getChild();        }        else {            stn.setSibling(new SyntaxTreeNode("T",null,null));            stn = stn.getSibling();        }        this.analyseF(stn,true);        stn = stn.getChild();        this.analyseT$(stn,false);    }    private void analyseT$(SyntaxTreeNode stn,boolean isChild) {        if(isChild) {            stn.setChild(new SyntaxTreeNode("T$",null,null));            stn = stn.getChild();        }        else {            stn.setSibling(new SyntaxTreeNode("T$",null,null));            stn = stn.getSibling();        }        LexicalResult lexicalResult = this.scan();        if(lexicalResult==null) {            stn.setChild(new SyntaxTreeNode("ε",null,null));        }        else if(lexicalResult.getType().equals("*")) {            stn.setChild(new SyntaxTreeNode("*",null,null));            stn = stn.getChild();            this.analyseF(stn,false);            stn = stn.getSibling();            this.analyseT$(stn,false);        }        else if(lexicalResult.getType().equals("/")) {            stn.setChild(new SyntaxTreeNode("/",null,null));            stn = stn.getChild();            this.analyseF(stn,false);            stn = stn.getSibling();            this.analyseT$(stn,false);        }        else {            this.backScan();        }    }    private void analyseF(SyntaxTreeNode stn,boolean isChild) {        if(isChild) {            stn.setChild(new SyntaxTreeNode("F",null,null));            stn = stn.getChild();        }        else {            stn.setSibling(new SyntaxTreeNode("F",null,null));            stn = stn.getSibling();        }        LexicalResult lexicalResult = this.scan();        if(lexicalResult==null) {            this.syntaxError();            return ;        }        if(lexicalResult.getType().equals("(")) {            stn.setChild(new SyntaxTreeNode("(",null,null));            stn = stn.getChild();            this.analyseE(stn,false);            stn = stn.getSibling();            lexicalResult = this.scan();            if(lexicalResult ==null) {                this.syntaxError();                return ;            }            if(lexicalResult.getType().equals(")")) {                stn.setSibling(new SyntaxTreeNode(")",null,null));            }            else {                this.syntaxError();                return ;            }        }        else if(lexicalResult.getType().equals("IDN")) {            stn.setChild(new SyntaxTreeNode(lexicalResult.getType()+" : "+lexicalResult.getValue(),null,null));        }        else if(lexicalResult.getType().equals("INT8")) {            stn.setChild(new SyntaxTreeNode(lexicalResult.getType()+" : "+lexicalResult.getValue(),null,null));        }        else if(lexicalResult.getType().equals("INT10")) {            stn.setChild(new SyntaxTreeNode(lexicalResult.getType()+" : "+lexicalResult.getValue(),null,null));        }        else if(lexicalResult.getType().equals("INT16")) {            stn.setChild(new SyntaxTreeNode(lexicalResult.getType()+" : "+lexicalResult.getValue(),null,null));        }        else {            this.syntaxError();            return ;        }    }    private void optimizeSyntaxTree(SyntaxTreeNode stn) {        if(stn==null)            return ;        this.optimizeSyntaxTree(stn.getChild());        this.optimizeSyntaxTree(stn.getSibling());        if(stn.getChild()!=null&&(stn.getChild().getValue().equals("SList")||stn.getChild().getValue().equals("SList$")||stn.getChild().getValue().equals("E$")||stn.getChild().getValue().equals("T$")||stn.getChild().getValue().equals(";")||stn.getChild().getValue().equals("ε"))) {            SyntaxTreeNode temp = stn.getChild().getChild();            if(temp==null) {                stn.setChild(stn.getChild().getSibling());            }            else {                temp.getLastSibling().setSibling(stn.getChild().getSibling());                stn.setChild(temp);            }        }        if(stn.getSibling()!=null&&(stn.getSibling().getValue().equals("SList")||stn.getSibling().getValue().equals("SList$")||stn.getSibling().getValue().equals("E$")||stn.getSibling().getValue().equals("T$")||stn.getSibling().getValue().equals(";")||stn.getSibling().getValue().equals("ε"))) {            SyntaxTreeNode temp = stn.getSibling().getChild();            if(temp==null) {                stn.setSibling(stn.getSibling().getSibling());            }            else {                temp.getLastSibling().setSibling(stn.getSibling().getSibling());                stn.setSibling(temp);            }        }    }    private void printSyntaxTree(SyntaxTreeNode stn,int count) {        if(stn!=null) {            for(int i=0;i<count;i++) {                System.out.print("    ");            }            System.out.println(stn.getValue());            this.printSyntaxTree(stn.getChild(),count+1);            this.printSyntaxTree(stn.getSibling(),count);        }    }    private void printSyntaxTree(SyntaxTreeNode stn) {        int count = 0;        if(stn!=null) {            for(int i=0;i<count;i++) {                System.out.print("    ");            }            System.out.println(stn.getValue());            this.printSyntaxTree(stn.getChild(),count+1);            this.printSyntaxTree(stn.getSibling(),count);        }    }    private void lexicalError() {        lexicalError = true;    }    private void syntaxError() {        syntaxError = true;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -