Exemple #1
0
 public void Push(AstNode node, ParserState state)
 {
   _data.Add(new ParserStackElement(node, state));
 }
    private bool AddClosureItems(ParserState state)
    {
      bool result = false;
      for (int i = 0; i < state.Items.Count; i++)
      {
        LRItem item = state.Items[i];
        NonTerminal nextNT = item.Core.NextElement as NonTerminal;
        if (nextNT == null) continue;

        foreach (Production prod in nextNT.Productions)
        {
          LR0Item core = prod.LR0Items[0]; 
          LRItem newItem = TryFindItem(state, core);
          if (newItem == null)
          {
            newItem = new LRItem(state, core);
            state.Items.Add(newItem);
            result = true;
          }

          newItem.NewLookaheads.AddRange(item.Core.TailFirsts);
          if (item.Core.TailIsNullable && !item.PropagateTargets.Contains(newItem))
            item.PropagateTargets.Add(newItem);
        }
      }

      return result;
    }
Exemple #3
0
 public ParserStackElement(AstNode node, ParserState state)
 {
   Node = node;
   State = state;
 }
 private Dictionary<string, LR0ItemList> GetStateShifts(ParserState state)
 {
   Dictionary<string, LR0ItemList> shifts = new Dictionary<string, LR0ItemList>();
   LR0ItemList list;
   foreach (LRItem item in state.Items)
   {
     GrammarTerm term = item.Core.NextElement;
     if (term == null) continue;
     LR0Item shiftedItem = item.Core.Production.LR0Items[item.Core.Position + 1];
     if (!shifts.TryGetValue(term.Key, out list))
       shifts[term.Key] = list = new LR0ItemList();
     list.Add(shiftedItem);
   }
   return shifts;
 }
 private ParserState FindOrCreateState(LR0ItemList lr0Items)
 {
   string key = CalcItemListKey(lr0Items);
   ParserState result;
   if (_stateHash.TryGetValue(key, out result))
     return result;
   result = new ParserState("S" + _data.States.Count, lr0Items);
   _data.States.Add(result);
   _stateHash[key] = result;
   return result;
 }
 private LRItem FindItem(ParserState state, Production production, int position)
 {
   foreach (LRItem item in state.Items)
     if (item.Core.Production == production && item.Core.Position == position)
       return item;
   string msg = string.Format("Failed to find an LRItem in state {0} by production [{1}] and position {2}. ",
     state, production.ToString(), position.ToString());
   throw new CompilerException(msg);
 }
 private LRItem TryFindItem(ParserState state, LR0Item core)
 {
   foreach (LRItem item in state.Items)
     if (item.Core == core)
       return item;
   return null;
 }
Exemple #8
0
 public SyntaxError(SourceLocation location, string message, ParserState state)
 {
   Location = location;
   Message = message;
   State = state;
 }
 public void AddError(SourceLocation location, string message, ParserState state)
 {
   Errors.Add(new SyntaxError(location, message, state));
 }
Exemple #10
0
 private void ExecuteShiftAction(ActionRecord action)
 {
     _stack.Push(_currentToken, _currentState);
     _currentState = action.NewState;
     NextToken();
 }