Grammar.cs :  » Script » IronRuby » gpcc » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Script » IronRuby 
IronRuby » gpcc » Grammar.cs
// Copyright (c) Microsoft Corporation. All rights reserved.




using System;
using System.Collections.Generic;


namespace gpcc{
  public class Grammar
  {
        public const string DefaultValueTypeName = "ValueType";

    public List<Production> productions = new List<Production>();
        public string unionType;
    public int NumActions = 0;
        public string header;  // before first %%
        public string prologCode;  // between %{ %} 
        public string epilogCode;  // after last %%
    public NonTerminal startSymbol;
    public Production rootProduction;
    public Dictionary<string, NonTerminal> nonTerminals = new Dictionary<string, NonTerminal>();
    public Dictionary<string, Terminal> terminals = new Dictionary<string, Terminal>();
        
        public bool IsPartial = false;
        public string OutFName = null;
        public string TokFName = null;
        public string Namespace;
        public string Visibility = "public";
        public string ParserName = "Parser";
        public string TokenName = "Tokens";
        public string ValueTypeName = null;
        public string LocationTypeName = "LexLocation";
        public string PartialMark { get { return (IsPartial ? " partial" : ""); } }


        public Grammar()
        {
            LookupTerminal(GrammarToken.Symbol, "NONE");
            LookupTerminal(GrammarToken.Symbol, "ERROR");
            LookupTerminal(GrammarToken.Symbol, "END_OF_FILE");
        }


    public Terminal LookupTerminal(GrammarToken token, string name)
    {
      if (!terminals.ContainsKey(name))
        terminals[name] = new Terminal(token == GrammarToken.Symbol, name);

      return terminals[name];
    }


    public NonTerminal LookupNonTerminal(string name)
    {
      if (!nonTerminals.ContainsKey(name))
        nonTerminals[name] = new NonTerminal(name);

      return nonTerminals[name];
    }


    public void AddProduction(Production production)
    {
      productions.Add(production);
      production.num = productions.Count;
    }


    public void CreateSpecialProduction(NonTerminal root)
    {
      rootProduction = new Production(LookupNonTerminal("$accept"));
      AddProduction(rootProduction);
      rootProduction.rhs.Add(root);
            rootProduction.rhs.Add(LookupTerminal(GrammarToken.Symbol, "END_OF_FILE"));
    }

        void MarkReachable()
        {
            Stack<NonTerminal> work = new Stack<NonTerminal>();
            rootProduction.lhs.reached = true; // by definition.
            work.Push(startSymbol);
            startSymbol.reached = true;
            while (work.Count > 0)
            {
                NonTerminal nonT = work.Pop();
                foreach (Production prod in nonT.productions)
                {
                    foreach (Symbol smbl in prod.rhs)
                    {
                        NonTerminal rhNt = smbl as NonTerminal;
                        if (rhNt != null && !rhNt.reached)
                        {
                            rhNt.reached = true;
                            work.Push(rhNt);
                        }
                    }
                }
            }
        }

        public bool CheckGrammar()
        {
            bool ok = true;
            NonTerminal nt;
            MarkReachable();
            foreach (KeyValuePair<string, NonTerminal> pair in nonTerminals)
            {
                nt = pair.Value;
                if (!nt.reached)
                    Console.Error.WriteLine(
                        "WARNING: NonTerminal symbol \"{0}\" is unreachable", pair.Key);

                if (nt.productions.Count == 0)
                {
                    ok = false;
                    Console.Error.WriteLine(
                        "FATAL: NonTerminal symbol \"{0}\" has no productions", pair.Key);
                }
            }
            return ok;    
        }
  }
}







www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.