public void Action(Expansion e) { if (e is NonTerminal) { NonTerminal nt = (NonTerminal)e; NormalProduction prod; if (!CSharpCCGlobals.production_table.TryGetValue(nt.Name, out prod)) { CSharpCCErrors.SemanticError(e, "Non-terminal " + nt.Name + " has not been defined."); } else { nt.Production = prod; nt.Production.Parents.Add(nt); } } }
private static bool implicitLA(Expansion exp) { if (!(exp is Sequence)) { return(true); } Sequence seq = (Sequence)exp; Object obj = seq.Units[0]; if (!(obj is Lookahead)) { return(true); } Lookahead la = (Lookahead)obj; return(!la.IsExplicit); }
private static void ReInitAll() { Expansion.reInit(); CSharpCCErrors.ReInit(); CSharpCCGlobals.ReInit(); Options.init(); CSharpCCParserInternals.reInit(); RStringLiteral.reInit(); // CSharpFiles.reInit(); LexGen.reInit(); NfaState.reInit(); MatchInfo.reInit(); LookaheadWalk.reInit(); Semanticize.reInit(); ParseGen.reInit(); OtherFilesGen.reInit(); ParseEngine.reInit(); }
public virtual StringBuilder Dump(int indent, IList alreadyDumped) { StringBuilder sb = DumpPrefix(indent) .Append(GetHashCode()) .Append(' ') .Append(GetType().Name) .Append(' ') .Append(Lhs); if (!alreadyDumped.Contains(this)) { alreadyDumped.Add(this); if (Expansion != null) { sb.AppendLine() .Append(Expansion.Dump(indent + 1, alreadyDumped)); } } return(sb); }
public void Action(Expansion e) { if (e is OneOrMore) { if (Semanticize.EmptyExpansionExists(((OneOrMore)e).Expansion)) { CSharpCCErrors.SemanticError(e, "Expansion within \"(...)+\" can be matched by empty string."); } } else if (e is ZeroOrMore) { if (Semanticize.EmptyExpansionExists(((ZeroOrMore)e).Expansion)) { CSharpCCErrors.SemanticError(e, "Expansion within \"(...)*\" can be matched by empty string."); } } else if (e is ZeroOrOne) { if (Semanticize.EmptyExpansionExists(((ZeroOrOne)e).Expansion)) { CSharpCCErrors.SemanticError(e, "Expansion within \"(...)?\" can be matched by empty string."); } } }
public static void production_addexpansion(BnfProduction p, Expansion e) { e.Parent = p; p.Expansion = e; }
public static IList <MatchInfo> genFirstSet(IList <MatchInfo> partialMatches, Expansion exp) { if (exp is RegularExpression) { IList <MatchInfo> retval = new List <MatchInfo>(); for (int i = 0; i < partialMatches.Count; i++) { MatchInfo m = (MatchInfo)partialMatches[i]; MatchInfo mnew = new MatchInfo(); for (int j = 0; j < m.firstFreeLoc; j++) { mnew.match[j] = m.match[j]; } mnew.firstFreeLoc = m.firstFreeLoc; mnew.match[mnew.firstFreeLoc++] = ((RegularExpression)exp).Ordinal; if (mnew.firstFreeLoc == MatchInfo.laLimit) { sizeLimitedMatches.Add(mnew); } else { retval.Add(mnew); } } return(retval); } else if (exp is NonTerminal) { NormalProduction prod = ((NonTerminal)exp).Production; if (prod is CodeProduction) { return(new List <MatchInfo>()); } else { return(genFirstSet(partialMatches, prod.Expansion)); } } else if (exp is Choice) { IList <MatchInfo> retval = new List <MatchInfo>(); Choice ch = (Choice)exp; foreach (Expansion e in ch.Choices) { IList <MatchInfo> v = genFirstSet(partialMatches, e); listAppend(retval, v); } return(retval); } else if (exp is Sequence) { IList <MatchInfo> v = partialMatches; Sequence seq = (Sequence)exp; foreach (Expansion unit in seq.Units) { v = genFirstSet(v, unit); if (v.Count == 0) { break; } } return(v); } else if (exp is OneOrMore) { IList <MatchInfo> retval = new List <MatchInfo>(); IList <MatchInfo> v = partialMatches; OneOrMore om = (OneOrMore)exp; while (true) { v = genFirstSet(v, om.Expansion); if (v.Count == 0) { break; } listAppend(retval, v); } return(retval); } else if (exp is ZeroOrMore) { IList <MatchInfo> retval = new List <MatchInfo>(); listAppend(retval, partialMatches); IList <MatchInfo> v = partialMatches; ZeroOrMore zm = (ZeroOrMore)exp; while (true) { v = genFirstSet(v, zm.Expansion); if (v.Count == 0) { break; } listAppend(retval, v); } return(retval); } else if (exp is ZeroOrOne) { IList <MatchInfo> retval = new List <MatchInfo>(); listAppend(retval, partialMatches); listAppend(retval, genFirstSet(partialMatches, ((ZeroOrOne)exp).Expansion)); return(retval); } else if (exp is TryBlock) { return(genFirstSet(partialMatches, ((TryBlock)exp).Expansion)); } else if (considerSemanticLA && exp is Lookahead && ((Lookahead)exp).ActionTokens.Count != 0 ) { return(new List <MatchInfo>()); } else { IList <MatchInfo> retval = new List <MatchInfo>(); listAppend(retval, partialMatches); return(retval); } }
public static IList <MatchInfo> genFollowSet(IList <MatchInfo> partialMatches, Expansion exp, long generation) { if (exp.MyGeneration == generation) { return(new List <MatchInfo>()); } exp.MyGeneration = generation; if (exp.Parent == null) { IList <MatchInfo> retval = new List <MatchInfo>(); listAppend(retval, partialMatches); return(retval); } else if (exp.Parent is NormalProduction) { IList <NonTerminal> parents = ((NormalProduction)exp.Parent).Parents; IList <MatchInfo> retval = new List <MatchInfo>(); //System.out.println("1; gen: " + generation + "; exp: " + exp); for (int i = 0; i < parents?.Count; i++) { IList <MatchInfo> v = genFollowSet(partialMatches, parents[i], generation); listAppend(retval, v); } return(retval); } else if (exp.Parent is Sequence) { Sequence seq = (Sequence)exp.Parent; IList <MatchInfo> v = partialMatches; for (int i = exp.Ordinal + 1; i < seq.Units.Count; i++) { v = genFirstSet(v, seq.Units[i]); if (v.Count == 0) { return(v); } } IList <MatchInfo> v1 = new List <MatchInfo>(); IList <MatchInfo> v2 = new List <MatchInfo>(); listSplit(v, partialMatches, v1, v2); if (v1.Count != 0) { //System.out.println("2; gen: " + generation + "; exp: " + exp); v1 = genFollowSet(v1, seq, generation); } if (v2.Count != 0) { //System.out.println("3; gen: " + generation + "; exp: " + exp); v2 = genFollowSet(v2, seq, Expansion.NextGenerationIndex++); } listAppend(v2, v1); return(v2); } else if (exp.Parent is OneOrMore || exp.Parent is ZeroOrMore) { IList <MatchInfo> moreMatches = new List <MatchInfo>(); listAppend(moreMatches, partialMatches); IList <MatchInfo> v = partialMatches; while (true) { v = genFirstSet(v, exp); if (v.Count == 0) { break; } listAppend(moreMatches, v); } IList <MatchInfo> v1 = new List <MatchInfo>(); IList <MatchInfo> v2 = new List <MatchInfo>(); listSplit(moreMatches, partialMatches, v1, v2); if (v1.Count != 0) { //System.out.println("4; gen: " + generation + "; exp: " + exp); v1 = genFollowSet(v1, (Expansion)exp.Parent, generation); } if (v2.Count != 0) { //System.out.println("5; gen: " + generation + "; exp: " + exp); v2 = genFollowSet(v2, (Expansion)exp.Parent, Expansion.NextGenerationIndex++); } listAppend(v2, v1); return(v2); } else { //System.out.println("6; gen: " + generation + "; exp: " + exp); return(genFollowSet(partialMatches, (Expansion)exp.Parent, generation)); } }
public static void choiceCalc(Choice choice) { int first = firstChoice(choice); // dbl[i] and dbr[i] are lists of size limited matches for choice i // of choice. dbl ignores matches with semantic lookaheads (when force_la_check // is false), while dbr ignores semantic lookahead. IList <MatchInfo>[] dbl = new IList <MatchInfo> [choice.Choices.Count]; IList <MatchInfo>[] dbr = new IList <MatchInfo> [choice.Choices.Count]; int[] minLA = new int[choice.Choices.Count - 1]; MatchInfo[] overlapInfo = new MatchInfo[choice.Choices.Count - 1]; int[] other = new int[choice.Choices.Count - 1]; MatchInfo m; IList <MatchInfo> v; bool overlapDetected; for (int la = 1; la <= Options.getChoiceAmbiguityCheck(); la++) { MatchInfo.laLimit = la; LookaheadWalk.considerSemanticLA = !Options.getForceLaCheck(); for (int i = first; i < choice.Choices.Count - 1; i++) { LookaheadWalk.sizeLimitedMatches = new List <MatchInfo>(); m = new MatchInfo(); m.firstFreeLoc = 0; v = new List <MatchInfo>(); v.Add(m); LookaheadWalk.genFirstSet(v, (Expansion)choice.Choices[i]); dbl[i] = LookaheadWalk.sizeLimitedMatches; } LookaheadWalk.considerSemanticLA = false; for (int i = first + 1; i < choice.Choices.Count; i++) { LookaheadWalk.sizeLimitedMatches = new List <MatchInfo>(); m = new MatchInfo(); m.firstFreeLoc = 0; v = new List <MatchInfo>(); v.Add(m); LookaheadWalk.genFirstSet(v, (Expansion)choice.Choices[i]); dbr[i] = LookaheadWalk.sizeLimitedMatches; } if (la == 1) { for (int i = first; i < choice.Choices.Count - 1; i++) { Expansion exp = (Expansion)choice.Choices[i]; if (Semanticize.EmptyExpansionExists(exp)) { CSharpCCErrors.Warning(exp, "This choice can expand to the empty token sequence " + "and will therefore always be taken in favor of the choices appearing later."); break; } else if (CodeCheck(dbl[i])) { CSharpCCErrors.Warning(exp, "CSHARPCODE non-terminal will force this choice to be taken " + "in favor of the choices appearing later."); break; } } } overlapDetected = false; for (int i = first; i < choice.Choices.Count - 1; i++) { for (int j = i + 1; j < choice.Choices.Count; j++) { if ((m = overlap(dbl[i], dbr[j])) != null) { minLA[i] = la + 1; overlapInfo[i] = m; other[i] = j; overlapDetected = true; break; } } } if (!overlapDetected) { break; } } for (int i = first; i < choice.Choices.Count - 1; i++) { if (explicitLA((Expansion)choice.Choices[i]) && !Options.getForceLaCheck()) { continue; } if (minLA[i] > Options.getChoiceAmbiguityCheck()) { CSharpCCErrors.Warning("Choice conflict involving two expansions at"); Console.Error.Write(" line " + ((Expansion)choice.Choices[i]).Line); Console.Error.Write(", column " + ((Expansion)choice.Choices[i]).Column); Console.Error.Write(" and line " + ((Expansion)choice.Choices[other[i]]).Line); Console.Error.Write(", column " + ((Expansion)choice.Choices[other[i]]).Column); Console.Error.WriteLine(" respectively."); Console.Error.WriteLine(" A common prefix is: " + image(overlapInfo[i])); Console.Error.WriteLine(" Consider using a lookahead of " + minLA[i] + " or more for earlier expansion."); } else if (minLA[i] > 1) { CSharpCCErrors.Warning("Choice conflict involving two expansions at"); Console.Error.Write(" line " + ((Expansion)choice.Choices[i]).Line); Console.Error.Write(", column " + ((Expansion)choice.Choices[i]).Column); Console.Error.Write(" and line " + ((Expansion)choice.Choices[other[i]]).Line); Console.Error.Write(", column " + ((Expansion)choice.Choices[other[i]]).Column); Console.Error.WriteLine(" respectively."); Console.Error.WriteLine(" A common prefix is: " + image(overlapInfo[i])); Console.Error.WriteLine(" Consider using a lookahead of " + minLA[i] + " for earlier expansion."); } } }
public bool GoDeeper(Expansion e) { return(!(e is RegularExpression)); }
public bool GoDeeper(Expansion e) { return(true); }
public bool GoDeeper(Expansion e) { return(!(e is RegularExpression) && !(e is Lookahead)); }
public Choice(Expansion expansion) { Line = expansion.Line; Column = expansion.Column; choices.Add(expansion); }
public ZeroOrOne(Token token, Expansion expansion) { Line = token.beginLine; Column = token.beginColumn; Expansion = expansion; }
internal static void PostOrderWalk(Expansion node, ITreeWalkerOp opObj) { if (opObj.GoDeeper(node)) { if (node is Choice) { foreach (var choice in ((Choice)node).Choices) { PostOrderWalk(choice, opObj); } } else if (node is Sequence) { foreach (var unit in ((Sequence)node).Units) { PostOrderWalk(unit, opObj); } } else if (node is OneOrMore) { PostOrderWalk(((OneOrMore)node).Expansion, opObj); } else if (node is ZeroOrMore) { PostOrderWalk(((ZeroOrMore)node).Expansion, opObj); } else if (node is ZeroOrOne) { PostOrderWalk(((ZeroOrOne)node).Expansion, opObj); } else if (node is Lookahead) { Expansion nestedE = ((Lookahead)node).Expansion; if (!(nestedE is Sequence && ((Sequence)nestedE).Units[0] == node)) { PostOrderWalk(nestedE, opObj); } } else if (node is TryBlock) { PostOrderWalk(((TryBlock)node).Expansion, opObj); } else if (node is RChoice) { foreach (var choice in ((RChoice)node).Choices) { PostOrderWalk(choice, opObj); } } else if (node is RSequence) { foreach (var unit in ((RSequence)node).Units) { PostOrderWalk(unit, opObj); } } else if (node is ROneOrMore) { PostOrderWalk(((ROneOrMore)node).RegularExpression, opObj); } else if (node is RZeroOrMore) { PostOrderWalk(((RZeroOrMore)node).RegularExpression, opObj); } else if (node is RZeroOrOne) { PostOrderWalk(((RZeroOrOne)node).RegularExpression, opObj); } else if (node is RRepetitionRange) { PostOrderWalk(((RRepetitionRange)node).RegularExpression, opObj); } } opObj.Action(node); }