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

📄 exprparser.cs

📁 antlr小例子 。
💻 CS
📖 第 1 页 / 共 2 页
字号:
// $ANTLR 3.0b5 Expr.g 2006-12-27 16:34:35

using System;
using Antlr.Runtime;
using IList 		= System.Collections.IList;
using ArrayList 	= System.Collections.ArrayList;
using Stack 		= Antlr.Runtime.Collections.StackList;




using Antlr.Runtime.Tree;

public class ExprParser : Parser 
{
    public static readonly string[] tokenNames = new string[] 
	{
        "<invalid>", 
		"<EOR>", 
		"<DOWN>", 
		"<UP>", 
		"NEWLINE", 
		"ID", 
		"INT", 
		"WS", 
		"'='", 
		"'+'", 
		"'-'", 
		"'*'", 
		"'('", 
		"')'"
    };

    public const int INT = 6;
    public const int EOF = -1;
    public const int WS = 7;
    public const int NEWLINE = 4;
    public const int ID = 5;
    
    
        public ExprParser(ITokenStream input) 
    		: base(input)
    	{
    		InitializeCyclicDFAs();
        }
        
    protected ITreeAdaptor adaptor = new CommonTreeAdaptor();
    
    public ITreeAdaptor TreeAdaptor
    {
        get { return this.adaptor; }
        set { this.adaptor = value; }
    }

    override public string[] TokenNames
	{
		get { return tokenNames; }
	}

    override public string GrammarFileName
	{
		get { return "Expr.g"; }
	}


    public class prog_return : ParserRuleReturnScope 
    {
        internal CommonTree tree;
        override public object Tree
        {
        	get { return tree; }
        }
    };
    
    // $ANTLR start prog
    // Expr.g:9: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 
    	{
            // Expr.g:9:9: ( ( stat )+ )
            // Expr.g:9:9: ( stat )+
            {
            	root_0 = (CommonTree)adaptor.GetNilNode();
            
            	// Expr.g:9:9: ( stat )+
            	int cnt1 = 0;
            	do 
            	{
            	    int alt1 = 2;
            	    int LA1_0 = input.LA(1);
            	    if ( ((LA1_0 >= NEWLINE && LA1_0 <= INT) || LA1_0 == 12) )
            	    {
            	        alt1 = 1;
            	    }
            	    
            	
            	    switch (alt1) 
            		{
            			case 1 :
            			    // Expr.g:9:11: stat
            			    {
            			    	CommonTree root_1 = (CommonTree)adaptor.GetNilNode();
            			    
            			    	PushFollow(FOLLOW_stat_in_prog43);
            			    	stat1 = stat();
            			    	followingStackPointer_--;
            			    	
            			    	adaptor.AddChild(root_1, stat1.Tree);
            			    	
            			    						if (((CommonTree)stat1.tree) != null){
            			    							Console.WriteLine(((CommonTree)stat1.tree).ToStringTree());
            			    						} else {
            			    							Console.WriteLine();
            			    						}
            			    					
            			    
            			    	adaptor.AddChild(root_0, root_1);
            			    
            			    }
            			    break;
            	
            			default:
            			    if ( cnt1 >= 1 ) goto loop1;
            		            EarlyExitException eee =
            		                new EarlyExitException(1, input);
            		            throw eee;
            	    }
            	    cnt1++;
            	} while (true);
            	
            	loop1:
            		;	// Stops C# compiler whinging that label 'loop1' has no statements

            
            }
    
        }
        catch (RecognitionException re) 
    	{
            ReportError(re);
            Recover(input,re);
        }
        finally 
    	{
            retval.stop = input.LT(-1);
            
            	retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0);
            	adaptor.SetTokenBoundaries(retval.Tree, retval.start, retval.stop);
    
        }
        return retval;
    }
    // $ANTLR end prog

    public class stat_return : ParserRuleReturnScope 
    {
        internal CommonTree tree;
        override public object Tree
        {
        	get { return tree; }
        }
    };
    
    // $ANTLR start stat
    // Expr.g:19:1: stat : ( expr NEWLINE -> expr | ID '=' expr NEWLINE -> ^( '=' ID expr ) | 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;
        
        IList list_expr = new ArrayList();
        IList list_8 = new ArrayList();
        IList list_ID = new ArrayList();
        IList list_NEWLINE = new ArrayList();
        CommonTree NEWLINE3_tree=null;
        CommonTree ID4_tree=null;
        CommonTree char_literal5_tree=null;
        CommonTree NEWLINE7_tree=null;
        CommonTree NEWLINE8_tree=null;
    
        try 
    	{
            // Expr.g:19:9: ( expr NEWLINE -> expr | ID '=' expr NEWLINE -> ^( '=' ID expr ) | NEWLINE ->)
            int alt2 = 3;
            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 -> expr | ID '=' expr NEWLINE -> ^( '=' ID expr ) | NEWLINE ->);", 2, 2, input);
                
                    throw nvae_d2s2;
                }
                break;
            case NEWLINE:
                alt2 = 3;
                break;
            	default:
            	    NoViableAltException nvae_d2s0 =
            	        new NoViableAltException("19:1: stat : ( expr NEWLINE -> expr | ID '=' expr NEWLINE -> ^( '=' ID expr ) | NEWLINE ->);", 2, 0, input);
            
            	    throw nvae_d2s0;
            }
            
            switch (alt2) 
            {
                case 1 :
                    // Expr.g:19:9: expr NEWLINE
                    {
                    	PushFollow(FOLLOW_expr_in_stat66);
                    	expr2 = expr();
                    	followingStackPointer_--;
                    	
                    	list_expr.Add(expr2.Tree);
                    	NEWLINE3 = (IToken)input.LT(1);
                    	Match(input,NEWLINE,FOLLOW_NEWLINE_in_stat68); 
                    	list_NEWLINE.Add(NEWLINE3);

                    	
                    	// AST REWRITE
                    	int i_0 = 0;
                    	retval.tree = root_0;
                    	root_0 = (CommonTree)adaptor.GetNilNode();
                    	// 19:29: -> expr
                    	{
                    	    adaptor.AddChild(root_0, list_expr[i_0]);
                    	
                    	}
                    	

                    
                    }
                    break;
                case 2 :
                    // Expr.g:20:9: ID '=' expr NEWLINE
                    {
                    	ID4 = (IToken)input.LT(1);
                    	Match(input,ID,FOLLOW_ID_in_stat89); 
                    	list_ID.Add(ID4);

                    	char_literal5 = (IToken)input.LT(1);
                    	Match(input,8,FOLLOW_8_in_stat91); 
                    	list_8.Add(char_literal5);

                    	PushFollow(FOLLOW_expr_in_stat93);
                    	expr6 = expr();
                    	followingStackPointer_--;
                    	
                    	list_expr.Add(expr6.Tree);
                    	NEWLINE7 = (IToken)input.LT(1);
                    	Match(input,NEWLINE,FOLLOW_NEWLINE_in_stat95); 
                    	list_NEWLINE.Add(NEWLINE7);

                    	
                    	// AST REWRITE
                    	int i_0 = 0;
                    	retval.tree = root_0;
                    	root_0 = (CommonTree)adaptor.GetNilNode();
                    	// 20:29: -> ^( '=' ID expr )
                    	{
                    	    // Expr.g:20:32: ^( '=' ID expr )
                    	    {
                    	    CommonTree root_1 = (CommonTree)adaptor.GetNilNode();
                    	    root_1 = (CommonTree)adaptor.BecomeRoot((IToken)list_8[i_0], root_1);
                    	    
                    	    adaptor.AddChild(root_1, (IToken)list_ID[i_0]);
                    	    adaptor.AddChild(root_1, list_expr[i_0]);
                    	    
                    	    adaptor.AddChild(root_0, root_1);
                    	    }
                    	
                    	}
                    	

                    
                    }
                    break;
                case 3 :
                    // Expr.g:21:9: NEWLINE
                    {
                    	NEWLINE8 = (IToken)input.LT(1);
                    	Match(input,NEWLINE,FOLLOW_NEWLINE_in_stat115); 
                    	list_NEWLINE.Add(NEWLINE8);

                    	
                    	// AST REWRITE
                    	int i_0 = 0;
                    	retval.tree = root_0;
                    	root_0 = (CommonTree)adaptor.GetNilNode();
                    	// 21:20: ->
                    	{
                    	    root_0 = null;
                    	}
                    	

                    
                    }
                    break;
            
            }
        }
        catch (RecognitionException re) 
    	{
            ReportError(re);
            Recover(input,re);
        }
        finally 
    	{
            retval.stop = input.LT(-1);
            
            	retval.tree = (CommonTree)adaptor.RulePostProcessing(root_0);
            	adaptor.SetTokenBoundaries(retval.Tree, retval.start, retval.stop);
    
        }
        return retval;
    }
    // $ANTLR end stat

    public class expr_return : ParserRuleReturnScope 
    {
        internal CommonTree tree;
        override public object Tree
        {
        	get { return tree; }
        }
    };
    
    // $ANTLR start expr
    // Expr.g:24:1: expr : multExpr ( ( '+'^^ | '-'^^ ) 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_literal10 = null;
        IToken char_literal11 = null;
        multExpr_return multExpr9 = null;

        multExpr_return multExpr12 = null;

⌨️ 快捷键说明

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