private ParserBuilder(
            IEnumerable <Rule> rules,
            IEnumerable <KeyValuePair <IReadOnlyList <Symbol>, Rule> > ambiguityResolutions)
        {
            this.rules = rules.GroupBy(r => r.Produced)
                         .ToDictionary(g => g.Key, g => (IReadOnlyList <Rule>)g.ToArray());
            this.baseFirstFollow  = new FirstFollowCalculator(this.rules.SelectMany(kvp => kvp.Value).ToArray());
            this.firstFollow      = new InternalFirstFollowProvider(this.baseFirstFollow);
            this.remainingSymbols = new Queue <NonTerminal>(this.baseFirstFollow.NonTerminals);

            this.ambiguityResolutions = ambiguityResolutions.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, (IEqualityComparer <IReadOnlyList <Symbol> >)EqualityComparers.GetSequenceComparer <Symbol>());
        }
Exemple #2
0
        private LeftRecursionRewriter(
            ImmutableDictionary <NonTerminal, ImmutableList <Rule> > rulesByProduced,
            ImmutableDictionary <Rule, Rule> ruleMapping,
            ImmutableDictionary <NonTerminal, NonTerminal> aliases,
            ImmutableHashSet <Rule> rightAssociativeRules)
        {
            this.rulesByProduced       = rulesByProduced;
            this.aliases               = aliases;
            this.ruleMapping           = ruleMapping;
            this.rightAssociativeRules = rightAssociativeRules;
            this.firstFollow           = new FirstFollowCalculator(ruleMapping.Keys.ToArray());

            var missings = this.rulesByProduced.SelectMany(kvp => kvp.Value)
                           .Where(r => !this.ruleMapping.ContainsKey(r))
                           .ToList();

            if (missings.Any())
            {
                throw new ArgumentException($"Unmapped: {string.Join(", ", missings)}");
            }
        }
 public InternalFirstFollowProvider(FirstFollowCalculator provider)
 {
     this.originalGrammarProvider = provider;
 }
Exemple #4
0
 public ParserGenerator(IEnumerable <Rule> grammar)
 {
     this.rules       = grammar.ToArray();
     this.firstFollow = new FirstFollowCalculator(this.rules);
 }
Exemple #5
0
 public LookaheadGrammarHelper(IEnumerable <Rule> rules)
 {
     this.rules           = rules.ToArray();
     this.rulesByProduced = this.rules.ToLookup(r => r.Produced);
     this.firstFollow     = new FirstFollowCalculator(this.rules);
 }