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

📄 treetonfaconverter.java

📁 antlr最新版本V3源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
// $ANTLR 2.7.7 (2006-01-29): "buildnfa.g" -> "TreeToNFAConverter.java"$/* [The "BSD licence"] Copyright (c) 2005-2006 Terence Parr All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright    notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright    notice, this list of conditions and the following disclaimer in the    documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products    derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/package org.antlr.tool;import java.util.*;import org.antlr.analysis.*;import org.antlr.misc.*;import antlr.TreeParser;import antlr.Token;import antlr.collections.AST;import antlr.RecognitionException;import antlr.ANTLRException;import antlr.NoViableAltException;import antlr.MismatchedTokenException;import antlr.SemanticException;import antlr.collections.impl.BitSet;import antlr.ASTPair;import antlr.collections.impl.ASTArray;/** Build an NFA from a tree representing an ANTLR grammar. */public class TreeToNFAConverter extends antlr.TreeParser       implements TreeToNFAConverterTokenTypes {/** Factory used to create nodes and submachines */protected NFAFactory factory = null;/** Which NFA object are we filling in? */protected NFA nfa = null;/** Which grammar are we converting an NFA for? */protected Grammar grammar = null;protected String currentRuleName = null;protected int outerAltNum = 0;protected int blockLevel = 0;public TreeToNFAConverter(Grammar g, NFA nfa, NFAFactory factory) {	this();	this.grammar = g;	this.nfa = nfa;	this.factory = factory;}protected void init() {    // define all the rule begin/end NFAStates to solve forward reference issues    Collection rules = grammar.getRules();    for (Iterator itr = rules.iterator(); itr.hasNext();) {		Rule r = (Rule) itr.next();        String ruleName = r.name;        NFAState ruleBeginState = factory.newState();        ruleBeginState.setDescription("rule "+ruleName+" start");		ruleBeginState.setEnclosingRuleName(ruleName);        grammar.setRuleStartState(ruleName, ruleBeginState);        NFAState ruleEndState = factory.newState();        ruleEndState.setDescription("rule "+ruleName+" end");        ruleEndState.setAcceptState(true);		ruleEndState.setEnclosingRuleName(ruleName);        grammar.setRuleStopState(ruleName, ruleEndState);    }}protected void addFollowTransition(String ruleName, NFAState following) {     //System.out.println("adding follow link to rule "+ruleName);     // find last link in FOLLOW chain emanating from rule     NFAState end = grammar.getRuleStopState(ruleName);     while ( end.transition(1)!=null ) {         end = (NFAState)end.transition(1).target;     }     if ( end.transition(0)!=null ) {         // already points to a following node         // gotta add another node to keep edges to a max of 2         NFAState n = factory.newState();         Transition e = new Transition(Label.EPSILON, n);         end.addTransition(e);         end = n;     }     Transition followEdge = new Transition(Label.EPSILON, following);     end.addTransition(followEdge);}protected void finish() {    List rules = new LinkedList();    rules.addAll(grammar.getRules());    int numEntryPoints = factory.build_EOFStates(rules);    if ( numEntryPoints==0 ) {        ErrorManager.grammarWarning(ErrorManager.MSG_NO_GRAMMAR_START_RULE,                                   grammar,                                   null,                                   grammar.name);    }}    public void reportError(RecognitionException ex) {		Token token = null;		if ( ex instanceof MismatchedTokenException ) {			token = ((MismatchedTokenException)ex).token;		}		else if ( ex instanceof NoViableAltException ) {			token = ((NoViableAltException)ex).token;		}        ErrorManager.syntaxError(            ErrorManager.MSG_SYNTAX_ERROR,            grammar,            token,            "buildnfa: "+ex.toString(),            ex);    }public TreeToNFAConverter() {	tokenNames = _tokenNames;}	public final void grammar(AST _t) throws RecognitionException {				GrammarAST grammar_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;				try {      // for error handling			init();			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case LEXER_GRAMMAR:			{				AST __t3 = _t;				GrammarAST tmp1_AST_in = (GrammarAST)_t;				match(_t,LEXER_GRAMMAR);				_t = _t.getFirstChild();				grammarSpec(_t);				_t = _retTree;				_t = __t3;				_t = _t.getNextSibling();				break;			}			case PARSER_GRAMMAR:			{				AST __t4 = _t;				GrammarAST tmp2_AST_in = (GrammarAST)_t;				match(_t,PARSER_GRAMMAR);				_t = _t.getFirstChild();				grammarSpec(_t);				_t = _retTree;				_t = __t4;				_t = _t.getNextSibling();				break;			}			case TREE_GRAMMAR:			{				AST __t5 = _t;				GrammarAST tmp3_AST_in = (GrammarAST)_t;				match(_t,TREE_GRAMMAR);				_t = _t.getFirstChild();				grammarSpec(_t);				_t = _retTree;				_t = __t5;				_t = _t.getNextSibling();				break;			}			case COMBINED_GRAMMAR:			{				AST __t6 = _t;				GrammarAST tmp4_AST_in = (GrammarAST)_t;				match(_t,COMBINED_GRAMMAR);				_t = _t.getFirstChild();				grammarSpec(_t);				_t = _retTree;				_t = __t6;				_t = _t.getNextSibling();				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			finish();		}		catch (RecognitionException ex) {			reportError(ex);			if (_t!=null) {_t = _t.getNextSibling();}		}		_retTree = _t;	}		public final void grammarSpec(AST _t) throws RecognitionException {				GrammarAST grammarSpec_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;		GrammarAST cmt = null;				try {      // for error handling			GrammarAST tmp5_AST_in = (GrammarAST)_t;			match(_t,ID);			_t = _t.getNextSibling();			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case DOC_COMMENT:			{				cmt = (GrammarAST)_t;				match(_t,DOC_COMMENT);				_t = _t.getNextSibling();				break;			}			case OPTIONS:			case TOKENS:			case RULE:			case SCOPE:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case OPTIONS:			{				AST __t12 = _t;				GrammarAST tmp6_AST_in = (GrammarAST)_t;				match(_t,OPTIONS);				_t = _t.getFirstChild();				GrammarAST tmp7_AST_in = (GrammarAST)_t;				if ( _t==null ) throw new MismatchedTokenException();				_t = _t.getNextSibling();				_t = __t12;				_t = _t.getNextSibling();				break;			}			case TOKENS:			case RULE:			case SCOPE:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case TOKENS:			{				AST __t14 = _t;				GrammarAST tmp8_AST_in = (GrammarAST)_t;				match(_t,TOKENS);				_t = _t.getFirstChild();				GrammarAST tmp9_AST_in = (GrammarAST)_t;				if ( _t==null ) throw new MismatchedTokenException();				_t = _t.getNextSibling();				_t = __t14;				_t = _t.getNextSibling();				break;			}			case RULE:			case SCOPE:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			{			_loop16:			do {				if (_t==null) _t=ASTNULL;				if ((_t.getType()==SCOPE)) {					attrScope(_t);					_t = _retTree;				}				else {					break _loop16;				}							} while (true);			}			{			_loop18:			do {				if (_t==null) _t=ASTNULL;				if ((_t.getType()==AMPERSAND)) {					GrammarAST tmp10_AST_in = (GrammarAST)_t;					match(_t,AMPERSAND);					_t = _t.getNextSibling();				}				else {					break _loop18;				}							} while (true);			}			rules(_t);			_t = _retTree;		}		catch (RecognitionException ex) {			reportError(ex);			if (_t!=null) {_t = _t.getNextSibling();}		}		_retTree = _t;	}		public final void attrScope(AST _t) throws RecognitionException {				GrammarAST attrScope_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;				try {      // for error handling			AST __t8 = _t;			GrammarAST tmp11_AST_in = (GrammarAST)_t;			match(_t,SCOPE);			_t = _t.getFirstChild();			GrammarAST tmp12_AST_in = (GrammarAST)_t;			match(_t,ID);			_t = _t.getNextSibling();			GrammarAST tmp13_AST_in = (GrammarAST)_t;			match(_t,ACTION);			_t = _t.getNextSibling();			_t = __t8;			_t = _t.getNextSibling();		}		catch (RecognitionException ex) {			reportError(ex);			if (_t!=null) {_t = _t.getNextSibling();}		}		_retTree = _t;	}		public final void rules(AST _t) throws RecognitionException {				GrammarAST rules_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;				try {      // for error handling			{			int _cnt21=0;			_loop21:			do {				if (_t==null) _t=ASTNULL;				if ((_t.getType()==RULE)) {					rule(_t);					_t = _retTree;				}				else {					if ( _cnt21>=1 ) { break _loop21; } else {throw new NoViableAltException(_t);}				}								_cnt21++;			} while (true);			}		}		catch (RecognitionException ex) {			reportError(ex);			if (_t!=null) {_t = _t.getNextSibling();}		}		_retTree = _t;	}		public final void rule(AST _t) throws RecognitionException {				GrammarAST rule_AST_in = (_t == ASTNULL) ? null : (GrammarAST)_t;		GrammarAST id = null;				StateCluster g=null;		StateCluster b = null;		String r=null;						try {      // for error handling			AST __t23 = _t;			GrammarAST tmp14_AST_in = (GrammarAST)_t;			match(_t,RULE);			_t = _t.getFirstChild();			id = (GrammarAST)_t;			match(_t,ID);			_t = _t.getNextSibling();			r=id.getText();			currentRuleName = r; factory.currentRuleName = r;			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case FRAGMENT:			case LITERAL_protected:			case LITERAL_public:			case LITERAL_private:			{				modifier(_t);				_t = _retTree;				break;			}			case ARG:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			{			GrammarAST tmp15_AST_in = (GrammarAST)_t;			match(_t,ARG);			_t = _t.getNextSibling();			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case ARG_ACTION:			{				GrammarAST tmp16_AST_in = (GrammarAST)_t;				match(_t,ARG_ACTION);				_t = _t.getNextSibling();				break;			}			case RET:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			}			{			GrammarAST tmp17_AST_in = (GrammarAST)_t;			match(_t,RET);			_t = _t.getNextSibling();			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case ARG_ACTION:			{				GrammarAST tmp18_AST_in = (GrammarAST)_t;				match(_t,ARG_ACTION);				_t = _t.getNextSibling();				break;			}			case OPTIONS:			case BLOCK:			case SCOPE:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			}			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case OPTIONS:			{				GrammarAST tmp19_AST_in = (GrammarAST)_t;				match(_t,OPTIONS);				_t = _t.getNextSibling();				break;			}			case BLOCK:			case SCOPE:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case SCOPE:			{				ruleScopeSpec(_t);				_t = _retTree;				break;			}			case BLOCK:			case AMPERSAND:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			{			_loop32:			do {				if (_t==null) _t=ASTNULL;				if ((_t.getType()==AMPERSAND)) {					GrammarAST tmp20_AST_in = (GrammarAST)_t;					match(_t,AMPERSAND);					_t = _t.getNextSibling();				}				else {					break _loop32;				}							} while (true);			}			GrammarAST blk = (GrammarAST)_t;			b=block(_t);			_t = _retTree;			{			if (_t==null) _t=ASTNULL;			switch ( _t.getType()) {			case LITERAL_catch:			case LITERAL_finally:			{				exceptionGroup(_t);				_t = _retTree;				break;			}			case EOR:			{				break;			}			default:			{				throw new NoViableAltException(_t);			}			}			}			GrammarAST tmp21_AST_in = (GrammarAST)_t;

⌨️ 快捷键说明

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