📄 exprparser.cs
字号:
// $ANTLR 3.0.1 G:\\expr.g 2008-05-09 16:59:12//import java.util.HashMap;using System;using Antlr.Runtime;using IList = System.Collections.IList;using ArrayList = System.Collections.ArrayList;using Stack = Antlr.Runtime.Collections.StackList;using Antlr.Runtime.Debug;using Antlr.Runtime.Tree;public class exprParser : DebugParser { public static readonly string[] tokenNames = new string[] { "<invalid>", "<EOR>", "<DOWN>", "<UP>", "NEWLINE", "ID", "INT", "WS", "'='", "'+'", "'-'", "'*'", "'('", "')'" }; public const int WS = 7; public const int NEWLINE = 4; public const int INT = 6; public const int ID = 5; public const int EOF = -1; public static readonly string[] ruleNames = new string[] { "invalidRule", "prog", "stat", "expr", "multExpr", "atom" }; public int ruleLevel = 0; public exprParser(ITokenStream input) : base(input) { } public exprParser(ITokenStream input, IDebugEventListener dbg) : base(input, dbg) { } protected bool EvalPredicate(bool result, string predicate) { dbg.SemanticPredicate(result, predicate); return result; } protected ITreeAdaptor adaptor = new DebugTreeAdaptor(dbg, new CommonTreeAdaptor()); public ITreeAdaptor TreeAdaptor { get { return this.adaptor; } set { this.adaptor = new DebugTreeAdaptor(dbg, value); } } override public string[] TokenNames { get { return tokenNames; } } override public string GrammarFileName { get { return "G:\\expr.g"; } } /** Map variable name to Integer object holding value */ //HashMap memory = new HashMap(); public class prog_return : ParserRuleReturnScope { internal CommonTree tree; override public object Tree { get { return tree; } } }; // $ANTLR start prog // G:\\expr.g:17:1: prog : ( stat )+ ; public prog_return prog() // throws RecognitionException [1] { prog_return retval = new prog_return(); retval.start = input.LT(1); CommonTree root_0 = null; stat_return stat1 = null; try { dbg.EnterRule("prog"); if ( ruleLevel==0 ) {dbg.Commence();} ruleLevel++; dbg.Location(17, 1); try { // G:\\expr.g:17:5: ( ( stat )+ ) dbg.EnterAlt(1); // G:\\expr.g:17:9: ( stat )+ { root_0 = (CommonTree)adaptor.GetNilNode(); dbg.Location(17,9); // G:\\expr.g:17:9: ( stat )+ int cnt1 = 0; try { dbg.EnterSubRule(1); do { int alt1 = 2; try { dbg.EnterDecision(1); int LA1_0 = input.LA(1); if ( ((LA1_0 >= NEWLINE && LA1_0 <= INT) || LA1_0 == 12) ) { alt1 = 1; } } finally { dbg.ExitDecision(1); } switch (alt1) { case 1 : dbg.EnterAlt(1); // G:\\expr.g:17:9: stat { dbg.Location(17,9); PushFollow(FOLLOW_stat_in_prog54); stat1 = stat(); followingStackPointer_--; adaptor.AddChild(root_0, stat1.Tree); } break; default: if ( cnt1 >= 1 ) goto loop1; EarlyExitException eee = new EarlyExitException(1, input); dbg.RecognitionException(eee); throw eee; } cnt1++; } while (true); loop1: ; // Stops C# compiler whinging that label 'loop1' has no statements } finally { dbg.ExitSubRule(1); } } retval.stop = input.LT(-1); retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); adaptor.SetTokenBoundaries(retval.Tree, retval.start, retval.stop); } catch (RecognitionException re) { ReportError(re); Recover(input,re); } finally { } dbg.Location(17, 15); } finally { dbg.ExitRule("prog"); ruleLevel--; if ( ruleLevel==0 ) {dbg.Terminate();} } return retval; } // $ANTLR end prog public class stat_return : ParserRuleReturnScope { internal CommonTree tree; override public object Tree { get { return tree; } } }; // $ANTLR start stat // G:\\expr.g:19:1: stat : ( expr NEWLINE | ID '=' expr NEWLINE | NEWLINE ); public stat_return stat() // throws RecognitionException [1] { stat_return retval = new stat_return(); retval.start = input.LT(1); CommonTree root_0 = null; IToken NEWLINE3 = null; IToken ID4 = null; IToken char_literal5 = null; IToken NEWLINE7 = null; IToken NEWLINE8 = null; expr_return expr2 = null; expr_return expr6 = null; CommonTree NEWLINE3_tree=null; CommonTree ID4_tree=null; CommonTree char_literal5_tree=null; CommonTree NEWLINE7_tree=null; CommonTree NEWLINE8_tree=null; try { dbg.EnterRule("stat"); if ( ruleLevel==0 ) {dbg.Commence();} ruleLevel++; dbg.Location(19, 1); try { // G:\\expr.g:19:5: ( expr NEWLINE | ID '=' expr NEWLINE | NEWLINE ) int alt2 = 3; try { dbg.EnterDecision(2); switch ( input.LA(1) ) { case INT: case 12: { alt2 = 1; } break; case ID: { int LA2_2 = input.LA(2); if ( (LA2_2 == 8) ) { alt2 = 2; } else if ( (LA2_2 == NEWLINE || (LA2_2 >= 9 && LA2_2 <= 11)) ) { alt2 = 1; } else { NoViableAltException nvae_d2s2 = new NoViableAltException("19:1: stat : ( expr NEWLINE | ID '=' expr NEWLINE | NEWLINE );", 2, 2, input); dbg.RecognitionException(nvae_d2s2); throw nvae_d2s2; } } break; case NEWLINE: { alt2 = 3; } break; default: NoViableAltException nvae_d2s0 = new NoViableAltException("19:1: stat : ( expr NEWLINE | ID '=' expr NEWLINE | NEWLINE );", 2, 0, input); dbg.RecognitionException(nvae_d2s0); throw nvae_d2s0; } } finally { dbg.ExitDecision(2); } switch (alt2) { case 1 : dbg.EnterAlt(1); // G:\\expr.g:19:9: expr NEWLINE { root_0 = (CommonTree)adaptor.GetNilNode(); dbg.Location(19,9); PushFollow(FOLLOW_expr_in_stat81); expr2 = expr(); followingStackPointer_--; adaptor.AddChild(root_0, expr2.Tree); dbg.Location(19,14); NEWLINE3 = (IToken)input.LT(1); Match(input,NEWLINE,FOLLOW_NEWLINE_in_stat83); NEWLINE3_tree = (CommonTree)adaptor.Create(NEWLINE3); adaptor.AddChild(root_0, NEWLINE3_tree); dbg.Location(19,22); (expr2.value); } break; case 2 : dbg.EnterAlt(2); // G:\\expr.g:20:9: ID '=' expr NEWLINE { root_0 = (CommonTree)adaptor.GetNilNode(); dbg.Location(20,9); ID4 = (IToken)input.LT(1); Match(input,ID,FOLLOW_ID_in_stat95); ID4_tree = (CommonTree)adaptor.Create(ID4); adaptor.AddChild(root_0, ID4_tree); dbg.Location(20,12); char_literal5 = (IToken)input.LT(1); Match(input,8,FOLLOW_8_in_stat97); char_literal5_tree = (CommonTree)adaptor.Create(char_literal5); adaptor.AddChild(root_0, char_literal5_tree); dbg.Location(20,16); PushFollow(FOLLOW_expr_in_stat99); expr6 = expr(); followingStackPointer_--; adaptor.AddChild(root_0, expr6.Tree); dbg.Location(20,21); NEWLINE7 = (IToken)input.LT(1); Match(input,NEWLINE,FOLLOW_NEWLINE_in_stat101); NEWLINE7_tree = (CommonTree)adaptor.Create(NEWLINE7); adaptor.AddChild(root_0, NEWLINE7_tree); dbg.Location(21,9); memory.put(ID4.Text, new Integer(expr6.value)); } break; case 3 : dbg.EnterAlt(3); // G:\\expr.g:22:9: NEWLINE { root_0 = (CommonTree)adaptor.GetNilNode(); dbg.Location(22,9); NEWLINE8 = (IToken)input.LT(1); Match(input,NEWLINE,FOLLOW_NEWLINE_in_stat121); NEWLINE8_tree = (CommonTree)adaptor.Create(NEWLINE8); adaptor.AddChild(root_0, NEWLINE8_tree); } break; } retval.stop = input.LT(-1); retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0); adaptor.SetTokenBoundaries(retval.Tree, retval.start, retval.stop); } catch (RecognitionException re) { ReportError(re); Recover(input,re); } finally { } dbg.Location(23, 5); } finally { dbg.ExitRule("stat"); ruleLevel--; if ( ruleLevel==0 ) {dbg.Terminate();} } return retval; } // $ANTLR end stat public class expr_return : ParserRuleReturnScope { public int value; internal CommonTree tree; override public object Tree { get { return tree; } } }; // $ANTLR start expr // G:\\expr.g:25:1: expr returns [int value] : e= multExpr ( '+' e= multExpr | '-' e= multExpr )* ; public expr_return expr() // throws RecognitionException [1] { expr_return retval = new expr_return(); retval.start = input.LT(1); CommonTree root_0 = null; IToken char_literal9 = null; IToken char_literal10 = null; multExpr_return e = null; CommonTree char_literal9_tree=null; CommonTree char_literal10_tree=null; try { dbg.EnterRule("expr"); if ( ruleLevel==0 ) {dbg.Commence();} ruleLevel++; dbg.Location(25, 1); try { // G:\\expr.g:26:5: (e= multExpr ( '+' e= multExpr | '-' e= multExpr )* ) dbg.EnterAlt(1); // G:\\expr.g:26:9: e= multExpr ( '+' e= multExpr | '-' e= multExpr )* { root_0 = (CommonTree)adaptor.GetNilNode(); dbg.Location(26,10); PushFollow(FOLLOW_multExpr_in_expr146); e = multExpr();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -