📄 validator.java
字号:
// $ANTLR 3.0b7 Validator.g 2007-05-03 14:07:36 import java.util.HashMap; import java.util.Vector; import java.util.Map; import java.io.*;import org.antlr.runtime.*;import org.antlr.runtime.tree.*;import java.util.Stack;import java.util.List;import java.util.ArrayList;public class Validator 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 Validator(TreeNodeStream input) { super(input); } public String[] getTokenNames() { return tokenNames; } public String getGrammarFileName() { return "Validator.g"; } // $ANTLR start program // Validator.g:19:1: program : ^( PROGRAM ^( VARIABLES ( varDecl )* ) ^( FUNCTIONS ( funDecl )* ) call ) EOF ; public final void program() throws RecognitionException { Main.symbols.reset_globals(); try { // Validator.g:24:3: ( ^( PROGRAM ^( VARIABLES ( varDecl )* ) ^( FUNCTIONS ( funDecl )* ) call ) EOF ) // Validator.g:24:3: ^( PROGRAM ^( VARIABLES ( varDecl )* ) ^( FUNCTIONS ( funDecl )* ) call ) EOF { match(input,PROGRAM,FOLLOW_PROGRAM_in_program51); match(input, Token.DOWN, null); match(input,VARIABLES,FOLLOW_VARIABLES_in_program54); if ( input.LA(1)==Token.DOWN ) { match(input, Token.DOWN, null); // Validator.g:24:25: ( 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 : // Validator.g:24:25: varDecl { pushFollow(FOLLOW_varDecl_in_program56); varDecl(); _fsp--; } break; default : break loop1; } } while (true); match(input, Token.UP, null); } match(input,FUNCTIONS,FOLLOW_FUNCTIONS_in_program61); if ( input.LA(1)==Token.DOWN ) { match(input, Token.DOWN, null); // Validator.g:24:47: ( funDecl )* loop2: do { int alt2=2; int LA2_0 = input.LA(1); if ( (LA2_0==FUNCTION) ) { alt2=1; } switch (alt2) { case 1 : // Validator.g:24:47: funDecl { pushFollow(FOLLOW_funDecl_in_program63); funDecl(); _fsp--; } break; default : break loop2; } } while (true); match(input, Token.UP, null); } pushFollow(FOLLOW_call_in_program67); call(); _fsp--; match(input, Token.UP, null); match(input,EOF,FOLLOW_EOF_in_program70); } } catch (RecognitionException re) { reportError(re); recover(input,re); } finally { } return ; } // $ANTLR end program // $ANTLR start funDecl // Validator.g:27:1: funDecl : ^( FUNCTION type= TYPE name= ID ^( PARAMATERS ( . )* ) block ) ; public final void funDecl() throws RecognitionException { CommonTree type=null; CommonTree name=null; try { // Validator.g:28:4: ( ^( FUNCTION type= TYPE name= ID ^( PARAMATERS ( . )* ) block ) ) // Validator.g:28:4: ^( FUNCTION type= TYPE name= ID ^( PARAMATERS ( . )* ) block ) { match(input,FUNCTION,FOLLOW_FUNCTION_in_funDecl82); match(input, Token.DOWN, null); type=(CommonTree)input.LT(1); match(input,TYPE,FOLLOW_TYPE_in_funDecl86); name=(CommonTree)input.LT(1); match(input,ID,FOLLOW_ID_in_funDecl90); match(input,PARAMATERS,FOLLOW_PARAMATERS_in_funDecl93); if ( input.LA(1)==Token.DOWN ) { match(input, Token.DOWN, null); // Validator.g:28:46: ( . )* loop3: do { int alt3=2; int LA3_0 = input.LA(1); if ( (LA3_0==UP) ) { alt3=2; } else if ( ((LA3_0>=POS && LA3_0<=81)) ) { alt3=1; } switch (alt3) { case 1 : // Validator.g:28:46: . { matchAny(input); } break; default : break loop3; } } while (true); match(input, Token.UP, null); } Main.symbols.enter_frame(name.getText()); pushFollow(FOLLOW_block_in_funDecl101); block(); _fsp--; Main.symbols.exit_frame(); match(input, Token.UP, null); } } catch (RecognitionException re) { reportError(re); recover(input,re); } finally { } return ; } // $ANTLR end funDecl // $ANTLR start varDecl // Validator.g:31:1: varDecl : ( ^( VARDEF TYPE name= ID ) | ^( ARRDEF TYPE name= ID size= INT ) ); public final void varDecl() throws RecognitionException { CommonTree name=null; CommonTree size=null; CommonTree TYPE1=null; CommonTree TYPE2=null; try { // Validator.g:31:11: ( ^( VARDEF TYPE name= ID ) | ^( ARRDEF TYPE name= ID size= INT ) ) int alt4=2; int LA4_0 = input.LA(1); if ( (LA4_0==VARDEF) ) { alt4=1; } else if ( (LA4_0==ARRDEF) ) { alt4=2; } else { NoViableAltException nvae = new NoViableAltException("31:1: varDecl : ( ^( VARDEF TYPE name= ID ) | ^( ARRDEF TYPE name= ID size= INT ) );", 4, 0, input); throw nvae; } switch (alt4) { case 1 : // Validator.g:31:11: ^( VARDEF TYPE name= ID ) { match(input,VARDEF,FOLLOW_VARDEF_in_varDecl116); match(input, Token.DOWN, null); TYPE1=(CommonTree)input.LT(1); match(input,TYPE,FOLLOW_TYPE_in_varDecl118); name=(CommonTree)input.LT(1); match(input,ID,FOLLOW_ID_in_varDecl122); match(input, Token.UP, null); Main.symbols.declare_variable(name.getText(),TYPE1.getText()); } break; case 2 : // Validator.g:32:4: ^( ARRDEF TYPE name= ID size= INT ) { match(input,ARRDEF,FOLLOW_ARRDEF_in_varDecl133); match(input, Token.DOWN, null); TYPE2=(CommonTree)input.LT(1); match(input,TYPE,FOLLOW_TYPE_in_varDecl135); name=(CommonTree)input.LT(1); match(input,ID,FOLLOW_ID_in_varDecl139); size=(CommonTree)input.LT(1); match(input,INT,FOLLOW_INT_in_varDecl143); match(input, Token.UP, null); Main.symbols.declare_array(name.getText(),TYPE2.getText(),Integer.parseInt(size.getText())); } break; } } catch (RecognitionException re) { reportError(re); recover(input,re); } finally { } return ; } // $ANTLR end varDecl // $ANTLR start block // Validator.g:35:1: block : ^( BLOCK ^( VARIABLES ( varDecl )* ) ^( STMTS ( stmt )* ) ) ; public final void block() throws RecognitionException { Main.symbols.enter_block(); try { // Validator.g:42:4: ( ^( BLOCK ^( VARIABLES ( varDecl )* ) ^( STMTS ( stmt )* ) ) ) // Validator.g:42:4: ^( BLOCK ^( VARIABLES ( varDecl )* ) ^( STMTS ( stmt )* ) ) { match(input,BLOCK,FOLLOW_BLOCK_in_block167); match(input, Token.DOWN, null); match(input,VARIABLES,FOLLOW_VARIABLES_in_block170); if ( input.LA(1)==Token.DOWN ) { match(input, Token.DOWN, null); // Validator.g:42:24: ( varDecl )* loop5: do { int alt5=2; int LA5_0 = input.LA(1); if ( ((LA5_0>=VARDEF && LA5_0<=ARRDEF)) ) { alt5=1; } switch (alt5) { case 1 : // Validator.g:42:24: varDecl { pushFollow(FOLLOW_varDecl_in_block172); varDecl(); _fsp--; } break; default : break loop5; } } while (true); match(input, Token.UP, null); } match(input,STMTS,FOLLOW_STMTS_in_block177); if ( input.LA(1)==Token.DOWN ) { match(input, Token.DOWN, null);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -