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

📄 proparser.java

📁 使用antlr实现c和pascal的转换
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
// $ANTLR 3.0 PRO.g 2007-07-14 13:28:12

import java.util.HashMap;

import org.antlr.runtime.*;import java.util.Stack;import java.util.List;import java.util.ArrayList;import java.util.Map;import java.util.HashMap;public class PROParser extends Parser {    public static final String[] tokenNames = new String[] {        "<invalid>", "<EOR>", "<DOWN>", "<UP>", "PROGRAM", "SEPARATOR", "ID", "VAR", "ARRAY", "NUM", "INTEGER", "REAL", "FUNCTION", "PROCEDURE", "BEGIN", "END", "ASSIGNOP", "IF", "THEN", "ELSE", "WHILE", "DO", "NOT", "EQUAL", "NEQ", "NB", "SMALL", "NS", "GRAT", "ADDOPA", "ADDOPB", "OR", "MULOPA", "MULOPB", "DIV", "MOD", "AND", "OF", "NEWLINE", "DIGITS", "OPTIONAL_FRACTION", "OPTIONAL_EXPONENT", "LETTER", "DIGIT", "WS", "'('", "')'", "'.'", "':'", "';'", "','", "'['", "'..'", "']'", "'OF'"    };    public static final int FUNCTION=12;    public static final int WHILE=20;    public static final int MOD=35;    public static final int LETTER=42;    public static final int DIGITS=39;    public static final int DO=21;    public static final int SEPARATOR=5;    public static final int NOT=22;    public static final int ASSIGNOP=16;    public static final int ID=6;    public static final int AND=36;    public static final int EOF=-1;    public static final int ADDOPA=29;    public static final int IF=17;    public static final int THEN=18;    public static final int SMALL=26;    public static final int ADDOPB=30;    public static final int OPTIONAL_FRACTION=40;    public static final int BEGIN=14;    public static final int EQUAL=23;    public static final int MULOPA=32;    public static final int MULOPB=33;    public static final int VAR=7;    public static final int DIGIT=43;    public static final int ARRAY=8;    public static final int NB=25;    public static final int INTEGER=10;    public static final int GRAT=28;    public static final int NS=27;    public static final int ELSE=19;    public static final int OF=37;    public static final int PROCEDURE=13;    public static final int NUM=9;    public static final int REAL=11;    public static final int WS=44;    public static final int NEQ=24;    public static final int NEWLINE=38;    public static final int OR=31;    public static final int PROGRAM=4;    public static final int DIV=34;    public static final int END=15;    public static final int OPTIONAL_EXPONENT=41;        public PROParser(TokenStream input) {            super(input);            ruleMemo = new HashMap[126+1];         }            public String[] getTokenNames() { return tokenNames; }    public String getGrammarFileName() { return "PRO.g"; }    
    /** Map variable name to Integer object holding value */
    HashMap memory = new HashMap();
    static int tag = 0;
    
    private static String newtemp()
    {
    	return new String("t"+Integer.toString(tag++));
    }
    private static void emit(String s)
    {
    	System.out.print(s);
    }
    
    
        public static void main(String[] args) throws Exception {
            PROLexer lex = new PROLexer(new ANTLRFileStream(args[0]));
           	CommonTokenStream tokens = new CommonTokenStream(lex);
    
            PROParser parser = new PROParser(tokens);
    
            try {
                parser.prog();
            } catch (RecognitionException e)  {
                e.printStackTrace();
            }
        }
    
    
    // $ANTLR start prog    // PRO.g:45:1: prog : program ;    public final void prog() throws RecognitionException {        try {            // PRO.g:45:10: ( program )            // PRO.g:45:10: program            {            pushFollow(FOLLOW_program_in_prog54);            program();            _fsp--;            if (failed) return ;            }        }        catch (RecognitionException re) {            reportError(re);            recover(input,re);        }        finally {        }        return ;    }    // $ANTLR end prog    // $ANTLR start program    // PRO.g:47:1: program : PROGRAM ( SEPARATOR )* ID '(' identifier_list ')' ( SEPARATOR )* declarations ( SEPARATOR )* subprogram_declarations ( SEPARATOR )* compound_statement ( SEPARATOR )* '.' ;    public final void program() throws RecognitionException {        try {            // PRO.g:47:10: ( PROGRAM ( SEPARATOR )* ID '(' identifier_list ')' ( SEPARATOR )* declarations ( SEPARATOR )* subprogram_declarations ( SEPARATOR )* compound_statement ( SEPARATOR )* '.' )            // PRO.g:47:10: PROGRAM ( SEPARATOR )* ID '(' identifier_list ')' ( SEPARATOR )* declarations ( SEPARATOR )* subprogram_declarations ( SEPARATOR )* compound_statement ( SEPARATOR )* '.'            {            match(input,PROGRAM,FOLLOW_PROGRAM_in_program61); if (failed) return ;            // PRO.g:47:18: ( SEPARATOR )*            loop1:            do {                int alt1=2;                int LA1_0 = input.LA(1);                if ( (LA1_0==SEPARATOR) ) {                    alt1=1;                }                switch (alt1) {            	case 1 :            	    // PRO.g:0:0: SEPARATOR            	    {            	    match(input,SEPARATOR,FOLLOW_SEPARATOR_in_program63); if (failed) return ;            	    }            	    break;            	default :            	    break loop1;                }            } while (true);            match(input,ID,FOLLOW_ID_in_program66); if (failed) return ;            match(input,45,FOLLOW_45_in_program68); if (failed) return ;            pushFollow(FOLLOW_identifier_list_in_program70);            identifier_list();            _fsp--;            if (failed) return ;            match(input,46,FOLLOW_46_in_program72); if (failed) return ;            // PRO.g:47:56: ( SEPARATOR )*            loop2:            do {                int alt2=2;                int LA2_0 = input.LA(1);                if ( (LA2_0==SEPARATOR) ) {                    int LA2_2 = input.LA(2);                    if ( (synpred2()) ) {                        alt2=1;                    }                }                switch (alt2) {            	case 1 :            	    // PRO.g:0:0: SEPARATOR            	    {            	    match(input,SEPARATOR,FOLLOW_SEPARATOR_in_program74); if (failed) return ;            	    }            	    break;            	default :            	    break loop2;                }            } while (true);            pushFollow(FOLLOW_declarations_in_program86);            declarations();            _fsp--;            if (failed) return ;            // PRO.g:48:23: ( SEPARATOR )*            loop3:            do {                int alt3=2;                int LA3_0 = input.LA(1);                if ( (LA3_0==SEPARATOR) ) {                    int LA3_1 = input.LA(2);                    if ( (synpred3()) ) {                        alt3=1;                    }                }                switch (alt3) {            	case 1 :            	    // PRO.g:0:0: SEPARATOR            	    {            	    match(input,SEPARATOR,FOLLOW_SEPARATOR_in_program88); if (failed) return ;            	    }            	    break;            	default :            	    break loop3;                }            } while (true);            pushFollow(FOLLOW_subprogram_declarations_in_program100);            subprogram_declarations();            _fsp--;            if (failed) return ;            // PRO.g:49:34: ( SEPARATOR )*            loop4:            do {                int alt4=2;                int LA4_0 = input.LA(1);                if ( (LA4_0==SEPARATOR) ) {                    alt4=1;                }                switch (alt4) {            	case 1 :            	    // PRO.g:0:0: SEPARATOR            	    {            	    match(input,SEPARATOR,FOLLOW_SEPARATOR_in_program102); if (failed) return ;            	    }            	    break;            	default :            	    break loop4;                }            } while (true);            if ( backtracking==0 ) {              emit("void main(){\n");            }            pushFollow(FOLLOW_compound_statement_in_program116);            compound_statement();            _fsp--;            if (failed) return ;            // PRO.g:50:29: ( SEPARATOR )*            loop5:            do {                int alt5=2;                int LA5_0 = input.LA(1);                if ( (LA5_0==SEPARATOR) ) {                    alt5=1;                }                switch (alt5) {            	case 1 :            	    // PRO.g:0:0: SEPARATOR            	    {            	    match(input,SEPARATOR,FOLLOW_SEPARATOR_in_program118); if (failed) return ;            	    }            	    break;            	default :            	    break loop5;                }            } while (true);            if ( backtracking==0 ) {              emit("}");            }            match(input,47,FOLLOW_47_in_program133); if (failed) return ;            }        }        catch (RecognitionException re) {            reportError(re);            recover(input,re);        }        finally {        }        return ;    }    // $ANTLR end program    // $ANTLR start declarations    // PRO.g:54:1: declarations returns [String place] : () ( VAR ( SEPARATOR )* identifier_list ( SEPARATOR )* ':' ( SEPARATOR )* type ( SEPARATOR )* ';' )* ;    public final String declarations() throws RecognitionException {        String place = null;        String type1 = null;        String identifier_list2 = null;        try {            // PRO.g:55:3: ( () ( VAR ( SEPARATOR )* identifier_list ( SEPARATOR )* ':' ( SEPARATOR )* type ( SEPARATOR )* ';' )* )            // PRO.g:55:3: () ( VAR ( SEPARATOR )* identifier_list ( SEPARATOR )* ':' ( SEPARATOR )* type ( SEPARATOR )* ';' )*            {            // PRO.g:55:3: ()            // PRO.g:55:4:             {            }            if ( backtracking==0 ) {              place ="";            }            // PRO.g:55:18: ( VAR ( SEPARATOR )* identifier_list ( SEPARATOR )* ':' ( SEPARATOR )* type ( SEPARATOR )* ';' )*            loop10:            do {                int alt10=2;                int LA10_0 = input.LA(1);                if ( (LA10_0==VAR) ) {                    alt10=1;                }                switch (alt10) {            	case 1 :            	    // PRO.g:55:19: VAR ( SEPARATOR )* identifier_list ( SEPARATOR )* ':' ( SEPARATOR )* type ( SEPARATOR )* ';'            	    {            	    match(input,VAR,FOLLOW_VAR_in_declarations152); if (failed) return place;            	    // PRO.g:55:23: ( SEPARATOR )*            	    loop6:            	    do {            	        int alt6=2;            	        int LA6_0 = input.LA(1);            	        if ( (LA6_0==SEPARATOR) ) {            	            alt6=1;            	        }            	        switch (alt6) {            	    	case 1 :            	    	    // PRO.g:0:0: SEPARATOR            	    	    {            	    	    match(input,SEPARATOR,FOLLOW_SEPARATOR_in_declarations154); if (failed) return place;            	    	    }            	    	    break;            	    	default :            	    	    break loop6;            	        }            	    } while (true);            	    pushFollow(FOLLOW_identifier_list_in_declarations157);            	    identifier_list2=identifier_list();            	    _fsp--;            	    if (failed) return place;            	    // PRO.g:55:50: ( SEPARATOR )*            	    loop7:            	    do {            	        int alt7=2;            	        int LA7_0 = input.LA(1);            	        if ( (LA7_0==SEPARATOR) ) {            	            alt7=1;            	        }            	        switch (alt7) {            	    	case 1 :            	    	    // PRO.g:0:0: SEPARATOR            	    	    {            	    	    match(input,SEPARATOR,FOLLOW_SEPARATOR_in_declarations159); if (failed) return place;            	    	    }            	    	    break;            	    	default :            	    	    break loop7;            	        }            	    } while (true);            	    match(input,48,FOLLOW_48_in_declarations162); if (failed) return place;            	    // PRO.g:55:65: ( SEPARATOR )*            	    loop8:            	    do {            	        int alt8=2;            	        int LA8_0 = input.LA(1);            	        if ( (LA8_0==SEPARATOR) ) {            	            alt8=1;            	        }            	        switch (alt8) {            	    	case 1 :            	    	    // PRO.g:0:0: SEPARATOR

⌨️ 快捷键说明

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