/// <summary> /// Creates a new instance of <c>Rule</c> class. /// </summary> /// <param name="index">Index of the rule in the grammar rule table.</param> /// <param name="nonTerminal">Nonterminal of the rule.</param> /// <param name="symbols">Terminal and nonterminal symbols of the rule.</param> public Rule(Int32 index, Symbol nonTerminal, Symbol[] symbols) { m_Index = index; m_NonTerminal = nonTerminal; m_Symbols = symbols; m_HasOneNonTerminal = (symbols.Length == 1) && (symbols[0].SymbolType == SymbolType.NonTerminal); }
/// <summary> /// Creats a new instance of the <c>LRStateAction</c> class. /// </summary> /// <param name="index">Index of the LR state action.</param> /// <param name="symbol">Symbol associated with the action.</param> /// <param name="action">Action type.</param> /// <param name="value">Action value.</param> public LRStateAction(int index, Symbol symbol, LRAction action, int value) { m_index = index; m_symbol = symbol; m_action = action; m_value = value; }
/// <summary> /// Creats a new instance of the <c>LRStateAction</c> class. /// </summary> /// <param name="index">Index of the LR state action.</param> /// <param name="symbol">Symbol associated with the action.</param> /// <param name="action">Action type.</param> /// <param name="value">Action value.</param> public LRStateAction(Int32 index, Symbol symbol, LRAction action, Int32 value) { m_Index = index; m_Symbol = symbol; m_Action = action; m_Value = value; }
public override string symbol_to_string(Symbol symbol) { switch (symbol.Index) { case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKMINUS: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKOR: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKXOR: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKAND: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKDIV: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKMOD: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKSHL: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKSHR: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKSLASH: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKSTAR: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKGREATER: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKGREATEREQUAL: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKIN: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKLOWER: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKLOWEREQUAL: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKNOTEQUAL: return StringResources.Get("OPERATOR"); case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKSQUARECLOSE: return "]"; case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKSQUAREOPEN: return "["; } string res = StringResources.Get(symbol.Name.ToUpper()); if (res.IndexOf("TK") == 0) return res.Remove(0, 2).ToLower(); return res; }
public override int symbol_priority(Symbol symbol) { switch (symbol.Index) { case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_END: return 8; case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_BEGIN: case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_INTEGER: case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_ROUNDCLOSE: return 9; case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_IDENTIFIER: return 10; case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_ASSIGN: return 25; case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_EQUAL: return 25; case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_EXPRESSION: return 100; case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_STATEMENT: return 110; case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_SEMICOLON: return 1000; } if (symbol.SymbolType == SymbolType.Terminal) return 1; return 0; }
public override int symbol_priority(Symbol symbol) { switch (symbol.Index) { case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKEND: return 8; case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKBEGIN: case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKNUMBER: case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKROUNDCLOSE: return 9; case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKIDENT: return 10; case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKASSIGN: return 25; case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKEQUAL: return 25; case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKSEMICOLON: return 30; case (int)GPBParser_PL0.SymbolConstants.SYMBOL_EXPRESSION: return 100; case (int)GPBParser_PL0.SymbolConstants.SYMBOL_STATEMENT: return 110; } if (symbol.SymbolType == SymbolType.Terminal) return 1; return 0; }
/// <summary> /// Creates a new instance of <c>Rule</c> class. /// </summary> /// <param name="index">Index of the rule in the grammar rule table.</param> /// <param name="nonTerminal">Nonterminal of the rule.</param> /// <param name="symbols">Terminal and nonterminal symbols of the rule.</param> public Rule(int index, Symbol nonTerminal, Symbol[] symbols) { m_index = index; m_nonTerminal = nonTerminal; m_symbols = symbols; m_hasOneNonTerminal = (symbols.Length == 1) && (symbols[0].SymbolType == SymbolType.NonTerminal); }
/// <summary> /// Creates a new instance of <c>Rule</c> class. /// </summary> /// <param name="index">Index of the rule in the grammar rule table.</param> /// <param name="nonTerminal">Nonterminal of the rule.</param> /// <param name="symbols">Terminal and nonterminal symbols of the rule.</param> public Rule(int index, Symbol nonTerminal, Symbol[] symbols) { m_index = index; m_nonTerminal = nonTerminal; m_symbols = symbols; m_hasOneNonTerminal = (symbols.Length == 1) && (symbols[0].SymbolType == SymbolType.NonTerminal); m_hasEmpty=symbols.Length == 0;// && m_nonTerminal.Name == "empty"; }
public override string symbol_to_string(Symbol symbol) { switch (symbol.Index) { case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKMINUS: case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKSLASH: case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKSTAR: case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKGREATER: case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKGREATEREQUAL: case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKLOWER: case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKLOWEREQUAL: case (int)GPBParser_PL0.SymbolConstants.SYMBOL_TKNOTEQUAL: return StringResources.Get("OPERATOR"); } string res = StringResources.Get(symbol.Name.ToUpper()); if (res.IndexOf("TK") == 0) return res.Remove(0, 2).ToLower(); return res; }
public override string symbol_to_string(Symbol symbol) { switch (symbol.Index) { case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_MINUS: case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_DIV: case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_MULT: case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_GREATER: case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_GREATEREQUAL: case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_LOWER: case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_LOWEREQUAL: case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_NOTEQUAL: case (int)GPBParser_KuMir.SymbolConstants.SYMBOL_TK_POWER: return StringResources.Get("OPERATOR"); } string res = StringResources.Get(symbol.Name); /*if (res.IndexOf("TK_") == 0) return res.Remove(0, 3).ToLower(); return res;*/ return res; }
private TokenParseResult ParseToken() { LRStateAction stateAction = m_lrState.m_transitionVector[m_token.m_symbol.m_index]; if (stateAction != null) { //Work - shift or reduce if (m_reductionCount > 0) { int newIndex = m_lrStackIndex - m_reductionCount; m_lrStack[newIndex] = m_lrStack[m_lrStackIndex]; m_lrStackIndex = newIndex; } m_reductionCount = Undefined; switch (stateAction.Action) { case LRAction.Accept: m_reductionCount = 0; return TokenParseResult.Accept; case LRAction.Shift: m_lrState = m_grammar.m_lrStateTable[stateAction.m_value]; LRStackItem nextToken = new LRStackItem(); nextToken.m_token = m_token; nextToken.m_state = m_lrState; if (m_lrStack.Length == ++m_lrStackIndex) { LRStackItem[] larger_m_lrStack = new LRStackItem[m_lrStack.Length + MinimumLRStackSize]; Array.Copy(m_lrStack, larger_m_lrStack, m_lrStack.Length); m_lrStack = larger_m_lrStack; } m_lrStack[m_lrStackIndex] = nextToken; return TokenParseResult.Shift; case LRAction.Reduce: //Produce a reduction - remove as many tokens as members in the rule & push a nonterminal token int ruleIndex = stateAction.m_value; Rule currentRule = m_grammar.m_ruleTable[ruleIndex]; //======== Create Reduction LRStackItem head; TokenParseResult parseResult; LRState nextState; if (m_trimReductions && currentRule.m_hasOneNonTerminal) { //The current rule only consists of a single nonterminal and can be trimmed from the //parse tree. Usually we create a new Reduction, assign it to the Data property //of Head and push it on the stack. However, in this case, the Data property of the //Head will be assigned the Data property of the reduced token (i.e. the only one //on the stack). //In this case, to save code, the value popped of the stack is changed into the head. head = m_lrStack[m_lrStackIndex]; head.m_token.m_symbol = currentRule.m_nonTerminal; head.m_token.m_text = null; parseResult = TokenParseResult.ReduceEliminated; //========== Goto nextState = m_lrStack[m_lrStackIndex - 1].m_state; } else { //Build a Reduction head = new LRStackItem(); head.m_rule = currentRule; head.m_token.m_symbol = currentRule.m_nonTerminal; head.m_token.m_text = null; m_reductionCount = currentRule.m_symbols.Length; parseResult = TokenParseResult.ReduceNormal; //========== Goto nextState = m_lrStack[m_lrStackIndex - m_reductionCount].m_state; } //========= If nextAction is null here, then we have an Internal Table Error!!!! LRStateAction nextAction = nextState.m_transitionVector[currentRule.m_nonTerminal.m_index]; if (nextAction != null) { m_lrState = m_grammar.m_lrStateTable[nextAction.m_value]; head.m_state = m_lrState; if (parseResult == TokenParseResult.ReduceNormal) { if (m_lrStack.Length == ++m_lrStackIndex) { LRStackItem[] larger_m_lrStack = new LRStackItem[m_lrStack.Length + MinimumLRStackSize]; Array.Copy(m_lrStack, larger_m_lrStack, m_lrStack.Length); m_lrStack = larger_m_lrStack; } m_lrStack[m_lrStackIndex] = head; } else { m_lrStack[m_lrStackIndex] = head; } return parseResult; } else { return TokenParseResult.InternalError; } } } //=== Syntax Error! Fill Expected Tokens m_expectedTokens = new Symbol[m_lrState.ActionCount]; int length = 0; for (int i = 0; i < m_lrState.ActionCount; i++) { switch (m_lrState.GetAction(i).Symbol.SymbolType) { case SymbolType.Terminal: case SymbolType.End: m_expectedTokens[length++] = m_lrState.GetAction(i).Symbol; break; } } if (length < m_expectedTokens.Length) { Symbol[] newArray = new Symbol[length]; Array.Copy(m_expectedTokens, newArray, length); m_expectedTokens = newArray; } return TokenParseResult.SyntaxError; }
public virtual string symbol_to_string(Symbol symbol) { return null; }
public string[] SymbolsToStrings(Symbol[] Symbols) { string[] res = new string[Symbols.Length>0?1:0]; for (int i = 0; i < res.Length; i++) res[i] = symbol_to_string(Symbols[i]); return res; }
/// <summary> /// Reads table record counts and initializes tables. /// </summary> private void ReadTableCounts(BinaryReader reader) { // Initialize tables m_SymbolTable = new Symbol[ReadInt16Entry(reader)]; m_CharSetTable = new String[ReadInt16Entry(reader)]; m_RuleTable = new Rule[ReadInt16Entry(reader)]; m_DfaStateTable = new DfaState[ReadInt16Entry(reader)]; m_LRStateTable = new LRState[ReadInt16Entry(reader)]; }
/// <summary> /// Read symbol information. /// </summary> private void ReadSymbols() { int index = ReadInt16Entry(); string name = ReadStringEntry(); SymbolType symbolType = (SymbolType) ReadInt16Entry(); Symbol symbol = new Symbol(index, name, symbolType); switch (symbolType) { case SymbolType.Error: m_errorSymbol = symbol; break; case SymbolType.End: m_endSymbol = symbol; break; } m_symbolTable[index] = symbol; }
/// <summary> /// Read rule information. /// </summary> private void ReadRules() { int index = ReadInt16Entry(); Symbol nonTerminal = m_symbolTable[ReadInt16Entry()]; ReadEmptyEntry(); Symbol[] symbols = new Symbol[m_entryCount]; for (int i = 0; i < symbols.Length; i++) { symbols[i] = m_symbolTable[ReadInt16Entry()]; } Rule rule = new Rule(index, nonTerminal, symbols); m_ruleTable[index] = rule; }
public Terminal(Symbol symbol, string text) { Symbol = symbol; Text = text; }
/// <summary> /// Read symbol information. /// </summary> private void ReadSymbols(BinaryReader reader) { Int32 index = ReadInt16Entry(reader); String name = ReadStringEntry(reader); var symbolType = (SymbolType)ReadInt16Entry(reader); var symbol = new Symbol(index, name, symbolType); switch (symbolType) { case SymbolType.Error: m_ErrorSymbol = symbol; break; case SymbolType.End: m_EndSymbol = symbol; break; } m_SymbolTable[index] = symbol; }
/// <summary> /// Read rule information. /// </summary> private void ReadRules(BinaryReader reader) { Int32 index = ReadInt16Entry(reader); Symbol nonTerminal = m_SymbolTable[ReadInt16Entry(reader)]; ReadEmptyEntry(reader); var symbols = new Symbol[m_EntryCount]; for (Int32 i = 0; i < symbols.Length; i++) { symbols[i] = m_SymbolTable[ReadInt16Entry(reader)]; } var rule = new Rule(index, nonTerminal, symbols); m_RuleTable[index] = rule; }
/*public virtual string terminal_token_to_string(TerminalToken token) { string name=symbol_to_string(token.Symbol); if (name!=null) return name; if (token.UserObject!=null) { if (token.UserObject is token_info) return ((token_info)token.UserObject).text; return token.UserObject.ToString(); } return token.Text; } */ public virtual List<Symbol> GetPrioritySymbols(Symbol[] Symbols) { List<Symbol> res = new List<Symbol>(); int maxp = -1, p; foreach (Symbol symbol in Symbols) { p = symbol_priority(symbol); if (p > maxp) maxp = p; } foreach (Symbol symbol in Symbols) if (symbol_priority(symbol) == maxp) res.Add(symbol); return res; }
/// <summary> /// Creates a new instance of the <c>DfaState</c> class. /// </summary> /// <param name="index">Index in the DFA state table.</param> /// <param name="acceptSymbol">Symbol to accept.</param> /// <param name="transitionVector">Transition vector.</param> public DfaState(int index, Symbol acceptSymbol, ObjectMap transitionVector) { m_index = index; m_acceptSymbol = acceptSymbol; m_transitionVector = transitionVector; }
public virtual string symbol_collection_to_string(Symbol[] Symbols) { return PascalABCCompiler.FormatTools.ObjectsToString(SymbolsToStrings(GetPrioritySymbols(Symbols).ToArray()), ","); }
/// <summary> /// Creates a new instance of the <c>DfaState</c> class. /// </summary> /// <param name="index">Index in the DFA state table.</param> /// <param name="acceptSymbol">Symbol to accept.</param> /// <param name="transitionVector">Transition vector.</param> public DfaState(Int32 index, Symbol acceptSymbol, ObjectMap transitionVector) { m_Index = index; m_AcceptSymbol = acceptSymbol; m_TransitionVector = transitionVector; }
public virtual int symbol_priority(Symbol symbol) { return 0; }
public override int symbol_priority(Symbol symbol) { switch (symbol.Index) { case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKEND: return 8; case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKBEGIN: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKINTEGER: case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKROUNDCLOSE: return 9; case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKIDENTIFIER: return 10; case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKASSIGN: return 25; case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKCOLON: return 20; case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKEQUAL: return 25; case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKSEMICOLON: return 30; // case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKDO: // return 50; //case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_TKPOINT: // return 35; // return 5; case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_EXPR: return 100; case (int)GPBParser_PascalABC.SymbolConstants.SYMBOL_STMT: return 110; } if (symbol.SymbolType == SymbolType.Terminal) return 1; return 0; }
/// <summary> /// Pushes a token to the input token stack. /// </summary> /// <param name="symbol">Token symbol.</param> /// <param name="text">Token text.</param> /// <param name="syntaxNode">Syntax node associated with the token.</param> public void PushInputToken(Symbol symbol, string text, object syntaxNode) { if (m_token.m_symbol != null) { if (m_inputTokenCount == m_inputTokens.Length) { Token[] newTokenArray = new Token[m_inputTokenCount*2]; Array.Copy(m_inputTokens, newTokenArray, m_inputTokenCount); m_inputTokens = newTokenArray; } m_inputTokens[m_inputTokenCount++] = m_token; } m_token = new Token(); m_token.m_symbol = symbol; m_token.m_text = text; m_token.m_length = (text != null) ? text.Length : 0; m_token.m_syntaxNode = syntaxNode; }
/// <summary> /// Pushes a token to the input token stack. /// </summary> /// <param name="symbol">Token symbol.</param> /// <param name="text">Token text.</param> /// <param name="syntaxNode">Syntax node associated with the token.</param> public void PushInputToken(Symbol symbol, String text, Object syntaxNode) { if (m_Token.Symbol != null) { if (m_InputTokenCount == m_InputTokens.Length) { var newTokenArray = new Token[m_InputTokenCount * 2]; Array.Copy(m_InputTokens, newTokenArray, m_InputTokenCount); m_InputTokens = newTokenArray; } m_InputTokens[m_InputTokenCount++] = m_Token; } m_Token = new Token { Symbol = symbol, Text = text, Length = (text != null) ? text.Length : 0, SyntaxNode = syntaxNode }; }