/// <summary> /// Closes this item to a set of items /// </summary> /// <param name="closure">The list to close</param> /// <param name="map">The current helper map</param> public override void CloseTo(List <Item> closure, Dictionary <Rule, Dictionary <int, List <Item> > > map) { // the item was of the form [Var -> alpha .] (reduction) // nothing to do if (Action == LRActionCode.Reduce) { return; } // Get the next symbol in the item Symbol next = GetNextSymbol(); // Here the item is of the form [Var -> alpha . next beta] // If the next symbol is not a variable : do nothing // If the next symbol is a variable : Variable nextVar = next as Variable; if (nextVar == null) { return; } // Firsts is a copy of the Firsts set for beta (next choice) // Firsts will contains symbols that may follow Next // Firsts will therefore be the lookahead for child items TerminalSet firsts = new TerminalSet(GetNextChoice().Firsts); // If beta is nullifiable (contains ε) : if (firsts.Contains(Epsilon.Instance)) { // Remove ε firsts.Remove(Epsilon.Instance); // Add the item's lookaheads firsts.AddRange(lookaheads); } // For each rule that has Next as a head variable : foreach (Rule rule in nextVar.Rules) { if (!map.ContainsKey(rule)) { map.Add(rule, new Dictionary <int, List <Item> >()); } Dictionary <int, List <Item> > sub = map[rule]; if (sub.ContainsKey(0)) { List <Item> previouses = sub[0]; ItemLALR1 previous = previouses[0] as ItemLALR1; previous.Lookaheads.AddRange(firsts); } else { List <Item> items = new List <Item>(); sub.Add(0, items); ItemLALR1 New = new ItemLALR1(rule, 0, firsts); closure.Add(New); items.Add(New); } } }
private void RemoveTerminals(TerminalSet terms, params Terminal[] termsToRemove) { foreach (var termToRemove in termsToRemove) { if (terms.Contains(termToRemove)) { terms.Remove(termToRemove); } } }
public override bool Match(ConflictResolutionArgs args) { if (args.Context.CurrentParserInput.Term.Name == "<") { args.Scanner.BeginPreview(); int ltCount = 0; string previewSym; bool isKeyword = false; while (true) { // Find first token ahead (using preview mode) that is either end of generic parameter (">") or something else Token preview; do { preview = args.Scanner.GetToken(); }while ((preview.Terminal.FlagIsSet(TermFlags.IsNonGrammar) || skipTokens.Contains(preview.Terminal)) && preview.Terminal != Grammar.Eof); isKeyword = preview.EditorInfo.Color == TokenColor.Keyword; // See what did we find previewSym = preview.Terminal.Name; if (previewSym == "<") { ltCount++; } else if (previewSym == ">" && ltCount > 0) { ltCount--; } else { break; } } args.Scanner.EndPreview(true); // if we see ">", then it is type argument, not operator // if previewSym == ">" then shift else reduce if (previewSym == ">" || isKeyword) { args.Result = ParserActionType.Shift; return(true); } else { args.Result = ParserActionType.Reduce; return(true); } } return(false); }
public Token Preview(TerminalSet skipTerms) { Token tkn; while (true) { tkn = ReadToken(); if (tkn.Terminal == _grammar.Eof || !skipTerms.Contains(tkn.Terminal)) { break; } } return(tkn); }
private void RemoveTerminals(TerminalSet terms, params Terminal[] termsToRemove) { foreach(var termToRemove in termsToRemove) if (terms.Contains(termToRemove)) terms.Remove(termToRemove); }
private void PrologTerm(TerminalSet _TS, out Term t) { Term lastInfix = null; string s; ArrayList termInfixArr = new ArrayList(); termInfixArr.Add(null); do { GetSymbol(new TerminalSet(LeftParen, Identifier, IntLiteral, RealLiteral, StringLiteral, Operator, Atom, Anonymous, CutSym, LSqBracket, LCuBracket, PrologString), false, true); if (symbol.Terminal == Operator) { symbol.SetProcessed(); t = new Term((OperatorDescr)symbol.OValue); } else if (symbol.IsMemberOf(StringLiteral, Atom)) { GetSymbol(new TerminalSet(StringLiteral, Atom), false, true); if (symbol.Terminal == Atom) { symbol.SetProcessed(); s = symbol.ToString(); } else { symbol.SetProcessed(); s = Utils.UnquoteIfUnnecessary(symbol.ToString()); } Term[] terms = null; bool b = is1000OperatorSetting; GetSymbol(_TS.Union(LeftParen, Identifier, IntLiteral, RealLiteral, StringLiteral, Operator, Atom, Anonymous, CutSym, LSqBracket, LCuBracket, PrologString), false, true); if (symbol.Terminal == LeftParen) { symbol.SetProcessed(); Set1000Operators(true); terminalTable[DOT] = Atom; terminalTable[OP] = Atom; PrologTerm(new TerminalSet(RightParen), out t); terminalTable[DOT] = Dot; terminalTable[OP] = OpSym; terms = t.ArgumentsToTermArray(); Set1000Operators(b); GetSymbol(new TerminalSet(RightParen), true, true); } if (terms == null) t = new Term(s); else t = new Term(s, terms); } else if (symbol.Terminal == Identifier) { symbol.SetProcessed(); s = symbol.ToString(); t = Globals.GetVariable(s); if (t == null) { t = new NamedVar(s); Globals.SetVariable(t, s); } } else if (symbol.Terminal == Anonymous) { symbol.SetProcessed(); t = new Term(); } else if (symbol.IsMemberOf(IntLiteral, RealLiteral)) { GetSymbol(new TerminalSet(IntLiteral, RealLiteral), false, true); if (symbol.Terminal == IntLiteral) { symbol.SetProcessed(); } else { symbol.SetProcessed(); } t = new Term(symbol.ToString(), FType.number); } else if (symbol.Terminal == LSqBracket) { List(_TS.Union(LeftParen, Identifier, IntLiteral, RealLiteral, StringLiteral, Operator, Atom, Anonymous, CutSym, LSqBracket, LCuBracket, PrologString), out t); } else if (symbol.Terminal == LCuBracket) { DCGBracketList(_TS.Union(LeftParen, Identifier, IntLiteral, RealLiteral, StringLiteral, Operator, Atom, Anonymous, CutSym, LSqBracket, LCuBracket, PrologString), out t); } else if (symbol.Terminal == PrologString) { symbol.SetProcessed(); t = new Term(symbol.ToUnquoted(), FType.text); } else if (symbol.Terminal == CutSym) { symbol.SetProcessed(); t = new Cut(0); } else { symbol.SetProcessed(); PrologTerm(new TerminalSet(RightParen), out t); GetSymbol(new TerminalSet(RightParen), true, true); t.IsPacked = true; } termInfixArr.Add(t); Term.AnalyzeInput(ref termInfixArr, ref lastInfix); GetSymbol(_TS.Union(LeftParen, Identifier, IntLiteral, RealLiteral, StringLiteral, Operator, Atom, Anonymous, CutSym, LSqBracket, LCuBracket, PrologString), false, true); } while (!(_TS.Contains(symbol.Terminal))); termInfixArr.Add(null); Term.AnalyzeInput(ref termInfixArr, ref lastInfix); t = Term.InfixToPrefix(termInfixArr); }
private void Program(TerminalSet _TS) { do { ClauseNode(_TS.Union(LeftParen, Identifier, IntLiteral, RealLiteral, StringLiteral, Operator, Atom, Anonymous, CutSym, ImpliesSym, PromptSym, LSqBracket, LCuBracket, PrologString)); GetSymbol(_TS.Union(LeftParen, Identifier, IntLiteral, RealLiteral, StringLiteral, Operator, Atom, Anonymous, CutSym, ImpliesSym, PromptSym, LSqBracket, LCuBracket, PrologString), false, true); } while (!(_TS.Contains(symbol.Terminal))); }
private bool GetSymbol(TerminalSet followers, bool done, bool genXCPN) { string s; if (symbol.IsProcessed) NextSymbol(); symbol.SetProcessed(done); if (parseAnyText || followers.IsEmpty()) return true; if (syntaxErrorStat) return false; if (symbol.Terminal == ANYSYM || followers.Contains(symbol.Terminal)) return true; else { if (terminalAsId && symbol.Terminal != Identifier) { int t = symbol.Terminal; symbol.Terminal = Identifier; if (symbol.HasIdFormat && followers.Contains(symbol.Terminal)) { return true; } symbol.Terminal = t; // restore error value } switch (symbol.Terminal) { case EndOfLine: if (seeEndOfLine) s = "<EndOfLine>"; else goto default; s = "<EndOfLine>"; break; case EndOfInput: s = "<EndOfInput>"; break; default: s = String.Format("\"{0}\"", symbol.ToString()); break; } } s = String.Format("*** Unexpected symbol: {0}{1}*** Expected one of: {2}", s, Environment.NewLine, terminalTable.TerminalImageSet(followers)); if (genXCPN) SyntaxError = s; else errorMessage = s; return true; }
private void ClauseSequence(TerminalSet _TS) { Monitor.Enter(PrologEngine.TermMonitor); do { ClauseNode(_TS.Union(LeftParen, Identifier, IntLiteral, RealLiteral, StringLiteral, Operator, Atom, Anonymous, CutSym, ImpliesSym, PromptSym, LSqBracket, LCuBracket, PrologString)); Monitor.Pulse(PrologEngine.TermMonitor); Monitor.Wait(PrologEngine.TermMonitor); GetSymbol(_TS.Union(LeftParen, Identifier, IntLiteral, RealLiteral, StringLiteral, Operator, Atom, Anonymous, CutSym, ImpliesSym, PromptSym, LSqBracket, LCuBracket, PrologString), false, true); } while (!(_TS.Contains(symbol.Terminal))); readTerm = new Term(EOF); Monitor.Pulse(PrologEngine.TermMonitor); Monitor.Exit(PrologEngine.TermMonitor); }
private void ANYTEXT(TerminalSet followers) { StreamPointer anyStart; StreamPointer follower; if (symbol.IsProcessed) anyTextStart = streamInPtr.Position; else anyTextStart = symbol.Start; if (followers.Contains(symbol.Terminal) && !symbol.IsProcessed) { anyTextFinal = anyTextStart; // empty, nullstring anyTextFPlus = anyTextStart; return; } parseAnyText = true; anyStart = streamInPtr; do { follower = streamInPtr; NextSymbol(); symbol.FinalPlus = symbol.Start; // up to next symbol anyTextFPlus = symbol.Start; } while (symbol.Terminal != EndOfInput && !followers.Contains(symbol.Terminal)); InitCh(follower); symbol.Start = anyTextStart; symbol.LineNo = anyStart.LineNo; symbol.LineStart = anyStart.LineStart; symbol.Final = streamInPtr.Position; symbol.Terminal = Undefined; symbol.SetProcessed(true); anyTextFinal = streamInPtr.Position; parseAnyText = false; }
public static bool IsTerminal(string cmd) { return(TerminalSet.Contains(cmd)); }
protected override bool GetSymbol(TerminalSet followers, bool done, bool genXCPN) { string s; if (symbol.IsProcessed) { NextSymbol(); } symbol.SetProcessed(done); if (parseAnyText || followers.IsEmpty()) { return(true); } if (syntaxErrorStat) { return(false); } if (symbol.TerminalId == ANYSYM || followers.Contains(symbol.TerminalId)) { return(true); } switch (symbol.TerminalId) { case EndOfLine: if (seeEndOfLine) { s = "<EndOfLine>"; } else { goto default; } s = "<EndOfLine>"; break; case EndOfInput: s = "<EndOfInput>"; break; default: s = String.Format("\"{0}\"", symbol.ToString()); break; } s = String.Format("*** Unexpected symbol: {0}{1}*** Expected one of: {2}", s, Environment.NewLine, terminalTable.TerminalImageSet(followers)); if (genXCPN) { SyntaxError = s; } else { errorMessage = s; } return(true); }
public override void OnResolvingConflict(ConflictResolutionArgs args) { switch (args.Context.CurrentParserInput.Term.Name) { case "[": { args.Scanner.BeginPreview(); var preview = args.Scanner.GetToken(); string previewSym = preview.Terminal.Name; args.Result = previewSym == "]" ? ParserActionType.Reduce : ParserActionType.Shift; args.Scanner.EndPreview(true); return; } case "dot": { args.Scanner.BeginPreview(); var preview = args.Scanner.GetToken(); string previewSym = preview.Text; if (previewSym == "<") { // skip over any type arguments int depth = 0; do { if (previewSym == "<") { ++depth; } else if (previewSym == ">") { --depth; } preview = args.Scanner.GetToken(); previewSym = preview.Text; } while (depth > 0 && preview.Terminal != Eof); } switch (previewSym) { case "new": case "super": case "this": args.Result = ParserActionType.Reduce; break; default: args.Result = ParserActionType.Shift; break; } args.Scanner.EndPreview(true); return; } case "lt": { args.Scanner.BeginPreview(); int ltCount = 0; string previewSym; while (true) { //Find first token ahead (using preview mode) that is either end of generic parameter (">") or something else Token preview; do { preview = args.Scanner.GetToken(); } while (mSkipTokensInPreview.Contains(preview.Terminal) && preview.Terminal != Eof); //See what did we find previewSym = preview.Terminal.Name; if ((previewSym == "<") || (previewSym == "lt")) { ltCount++; } else if (((previewSym == ">") || (previewSym == "gt")) && ltCount > 0) { ltCount--; continue; } else { break; } } //if we see ">", then it is type argument, not operator if ((previewSym == ">") || (previewSym == "gt")) { args.Result = ParserActionType.Shift; } else { args.Result = ParserActionType.Reduce; } args.Scanner.EndPreview(true); //keep previewed tokens; important to keep ">>" matched to two ">" symbols, not one combined symbol (see method below) return; } } }
/// <summary> /// Closes this item to a set of items /// </summary> /// <param name="closure">The list to close</param> /// <param name="map">The current helper map</param> public override void CloseTo(List <Item> closure, Dictionary <Rule, Dictionary <int, List <Item> > > map) { // the item was of the form [Var -> alpha .] (reduction) // nothing to do if (Action == LRActionCode.Reduce) { return; } // Get the next symbol in the item Symbol next = GetNextSymbol(); // Here the item is of the form [Var -> alpha . Next beta] // If the next symbol is not a variable : do nothing // If the next symbol is a variable : Variable nextVar = next as Variable; if (nextVar == null) { return; } // Firsts is a copy of the Firsts set for beta (next choice) // Firsts will contains symbols that may follow Next // Firsts will therefore be the lookahead for child items TerminalSet firsts = new TerminalSet(GetNextChoice().Firsts); // If beta is nullifiable (contains ε) : if (firsts.Contains(Epsilon.Instance)) { // Remove ε firsts.Remove(Epsilon.Instance); // Add the item's lookahead as possible symbol for firsts firsts.Add(lookahead); } // For each rule that has Next as a head variable : foreach (Rule rule in nextVar.Rules) { if (!map.ContainsKey(rule)) { map.Add(rule, new Dictionary <int, List <Item> >()); } Dictionary <int, List <Item> > sub = map[rule]; if (!sub.ContainsKey(0)) { sub.Add(0, new List <Item>()); } List <Item> previouses = sub[0]; // For each symbol in Firsts : create the child with this symbol as lookahead foreach (Terminal first in firsts) { // Child item creation and unique insertion int sid = first.ID; bool found = false; foreach (Item previous in previouses) { if (previous.Lookaheads[0].ID == sid) { found = true; break; } } if (!found) { ItemLR1 New = new ItemLR1(rule, 0, first); closure.Add(New); previouses.Add(New); } } } }
public Token Preview(TerminalSet skipTerms) { Token tkn; while (true) { tkn = ReadToken(); if (tkn.Terminal == _grammar.Eof || !skipTerms.Contains(tkn.Terminal)) break; } return tkn; }