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

📄 compiler.java

📁 c--词法分析
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
// $ANTLR 3.0b7 Compiler.g 2007-05-03 14:07:35	import java.util.HashMap;	import java.util.Vector;	import java.util.Map;    	import java.util.List;	import java.io.*;	import org.antlr.stringtemplate.*;	import org.antlr.stringtemplate.language.*;import org.antlr.runtime.*;import org.antlr.runtime.tree.*;import java.util.Stack;import java.util.List;import java.util.ArrayList;import org.antlr.stringtemplate.*;import org.antlr.stringtemplate.language.*;import java.util.HashMap;public class Compiler extends TreeParser {    public static final String[] tokenNames = new String[] {        "<invalid>", "<EOR>", "<DOWN>", "<UP>", "POS", "NEG", "POSTFIX", "PREFIX", "NOP", "PROGRAM", "FUNCTION", "VARDEF", "ARRDEF", "VARPAR", "ARRPAR", "BLOCK", "ASSIGN", "EXPR", "EXPRLIST", "CALL", "INDEX", "NOT", "NEGATE", "FUNCTIONS", "VARIABLES", "PARAMATERS", "STMTS", "NUM", "VAR", "TYPE", "ID", "INT", "RETURN", "READ", "WRITE", "WRITELN", "BREAK", "WHILE", "IF", "SCHAR", "SQUOTE", "CHAR", "LCHAR", "DIGIT", "LC", "UC", "NEWLINE", "WS", "COMMENT", "LINECOMMENT", "';'", "'['", "']'", "'('", "')'", "','", "'{'", "'}'", "'do'", "'for'", "'else'", "'unless'", "'='", "'or'", "'||'", "'and'", "'&&'", "'=='", "'!='", "'<>'", "'>'", "'<'", "'<='", "'>='", "'+'", "'-'", "'*'", "'/'", "'!'", "'not'", "'++'", "'--'"    };    public static final int INDEX=20;    public static final int TYPE=29;    public static final int EXPR=17;    public static final int VARPAR=13;    public static final int PARAMATERS=25;    public static final int ARRPAR=14;    public static final int NEGATE=22;    public static final int VARIABLES=24;    public static final int SQUOTE=40;    public static final int VARDEF=11;    public static final int STMTS=26;    public static final int PROGRAM=9;    public static final int NEWLINE=46;    public static final int FUNCTIONS=23;    public static final int BLOCK=15;    public static final int PREFIX=7;    public static final int FUNCTION=10;    public static final int INT=31;    public static final int SCHAR=39;    public static final int ASSIGN=16;    public static final int POS=4;    public static final int POSTFIX=6;    public static final int DIGIT=43;    public static final int EXPRLIST=18;    public static final int LCHAR=42;    public static final int ID=30;    public static final int WHILE=37;    public static final int WS=47;    public static final int CHAR=41;    public static final int WRITE=34;    public static final int LC=44;    public static final int COMMENT=48;    public static final int LINECOMMENT=49;    public static final int VAR=28;    public static final int RETURN=32;    public static final int IF=38;    public static final int EOF=-1;    public static final int NOP=8;    public static final int BREAK=36;    public static final int NUM=27;    public static final int CALL=19;    public static final int UC=45;    public static final int NEG=5;    public static final int NOT=21;    public static final int WRITELN=35;    public static final int READ=33;    public static final int ARRDEF=12;        public Compiler(TreeNodeStream input) {            super(input);        }            protected StringTemplateGroup templateLib =      new StringTemplateGroup("CompilerTemplates", AngleBracketTemplateLexer.class);    public void setTemplateLib(StringTemplateGroup templateLib) {      this.templateLib = templateLib;    }    public StringTemplateGroup getTemplateLib() {      return templateLib;    }    /** allows convenient multi-value initialization:     *  "new STAttrMap().put(...).put(...)"     */    public static class STAttrMap extends HashMap {      public STAttrMap put(String attrName, Object value) {        super.put(attrName, value);        return this;      }      public STAttrMap put(String attrName, int value) {        super.put(attrName, new Integer(value));        return this;      }    }    public String[] getTokenNames() { return tokenNames; }    public String getGrammarFileName() { return "Compiler.g"; }    	CommonTreeNodeStream stream = (CommonTreeNodeStream)input;    	Boolean global=true;    	    	int next_power_of_two(int v){    		//FIXME Currently arrays are set to 2^16, and this function isn't used    		double t=(double)v;    		double nv=Math.log(t)/Math.log(2);    		int res=(int)Math.ceil(nv);    		return (int)Math.pow(2,res);    	}    	StringTemplateGroup mtemplates;    public static class program_return extends TreeRuleReturnScope {        public StringTemplate st;        public Object getTemplate() { return st; }        public String toString() { return st==null?null:st.toString(); }    };    // $ANTLR start program    // Compiler.g:35:1: program : ^( PROGRAM ^( VARIABLES (vars+= varDecl )* ) ^( FUNCTIONS (funs+= funDecl )* ) . ) EOF -> wrapper(globals=$varsfunctions=$funs);    public final program_return program() throws RecognitionException {        program_return retval = new program_return();        retval.start = input.LT(1);        List list_vars=null;        List list_funs=null;        RuleReturnScope vars = null;        RuleReturnScope funs = null;        List list_funDecl=new ArrayList();        List list_varDecl=new ArrayList();        	Main.symbols.reset_globals();        try {            // Compiler.g:40:3: ( ^( PROGRAM ^( VARIABLES (vars+= varDecl )* ) ^( FUNCTIONS (funs+= funDecl )* ) . ) EOF -> wrapper(globals=$varsfunctions=$funs))            // Compiler.g:40:3: ^( PROGRAM ^( VARIABLES (vars+= varDecl )* ) ^( FUNCTIONS (funs+= funDecl )* ) . ) EOF            {            match(input,PROGRAM,FOLLOW_PROGRAM_in_program59);             match(input, Token.DOWN, null);             match(input,VARIABLES,FOLLOW_VARIABLES_in_program62);             if ( input.LA(1)==Token.DOWN ) {                match(input, Token.DOWN, null);                 // Compiler.g:40:25: (vars+= varDecl )*                loop1:                do {                    int alt1=2;                    int LA1_0 = input.LA(1);                    if ( ((LA1_0>=VARDEF && LA1_0<=ARRDEF)) ) {                        alt1=1;                    }                    switch (alt1) {                	case 1 :                	    // Compiler.g:40:26: vars+= varDecl                	    {                	    pushFollow(FOLLOW_varDecl_in_program67);                	    vars=varDecl();                	    _fsp--;                	    if (list_vars==null) list_vars=new ArrayList();                	    list_vars.add(vars.getTemplate());                	    }                	    break;                	default :                	    break loop1;                    }                } while (true);                match(input, Token.UP, null);             }            match(input,FUNCTIONS,FOLLOW_FUNCTIONS_in_program73);             if ( input.LA(1)==Token.DOWN ) {                match(input, Token.DOWN, null);                 // Compiler.g:40:55: (funs+= funDecl )*                loop2:                do {                    int alt2=2;                    int LA2_0 = input.LA(1);                    if ( (LA2_0==FUNCTION) ) {                        alt2=1;                    }                    switch (alt2) {                	case 1 :                	    // Compiler.g:40:56: funs+= funDecl                	    {                	    pushFollow(FOLLOW_funDecl_in_program78);                	    funs=funDecl();                	    _fsp--;                	    if (list_funs==null) list_funs=new ArrayList();                	    list_funs.add(funs.getTemplate());                	    }                	    break;                	default :                	    break loop2;                    }                } while (true);                match(input, Token.UP, null);             }            matchAny(input);             match(input, Token.UP, null);             match(input,EOF,FOLLOW_EOF_in_program86);             // TEMPLATE REWRITE            // 40:80: -> wrapper(globals=$varsfunctions=$funs)            {                retval.st = templateLib.getInstanceOf("wrapper",              new STAttrMap().put("globals", list_vars).put("functions", list_funs));            }            }        }        catch (RecognitionException re) {            reportError(re);            recover(input,re);        }        finally {        }        return retval;    }    // $ANTLR end program    public static class funDecl_return extends TreeRuleReturnScope {        public StringTemplate st;        public Object getTemplate() { return st; }        public String toString() { return st==null?null:st.toString(); }    };    // $ANTLR start funDecl    // Compiler.g:43:1: funDecl : ^( FUNCTION TYPE name= ID ^( PARAMATERS (params+= paramDecl )* ) b+= block ) -> function(name=$nameparams=$paramscontent=$b);    public final funDecl_return funDecl() throws RecognitionException {        funDecl_return retval = new funDecl_return();        retval.start = input.LT(1);        CommonTree name=null;        List list_params=null;        List list_b=null;        RuleReturnScope params = null;        RuleReturnScope b = null;        List list_paramDecl=new ArrayList();        List list_block=new ArrayList();        	global=false;        	Main.symbols.enter_frame();        try {            // Compiler.g:52:3: ( ^( FUNCTION TYPE name= ID ^( PARAMATERS (params+= paramDecl )* ) b+= block ) -> function(name=$nameparams=$paramscontent=$b))            // Compiler.g:52:3: ^( FUNCTION TYPE name= ID ^( PARAMATERS (params+= paramDecl )* ) b+= block )            {            match(input,FUNCTION,FOLLOW_FUNCTION_in_funDecl119);             match(input, Token.DOWN, null);             match(input,TYPE,FOLLOW_TYPE_in_funDecl121);             name=(CommonTree)input.LT(1);            match(input,ID,FOLLOW_ID_in_funDecl125);             match(input,PARAMATERS,FOLLOW_PARAMATERS_in_funDecl128);             if ( input.LA(1)==Token.DOWN ) {                match(input, Token.DOWN, null);                 // Compiler.g:52:40: (params+= paramDecl )*                loop3:                do {                    int alt3=2;                    int LA3_0 = input.LA(1);                    if ( ((LA3_0>=VARPAR && LA3_0<=ARRPAR)) ) {                        alt3=1;                    }

⌨️ 快捷键说明

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