Click here to Skip to main content
15,897,273 members
Articles / Programming Languages / Javascript

Implementing Programming Languages Using C# 4.0

Rate me:
Please Sign up or sign in to vote.
4.92/5 (115 votes)
12 Jul 2012MIT17 min read 242K   3.6K   249  
An introduction to creating programming language tools using C# 4.0.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Diggins.Jigsaw.Sandbox
{
    class LeftRecursion
    {
        /*
        public class DecomposeSeqRule : Rule
        {
            public Rule Iteration { get { return Children[0]; } }
            public Rule RightMost { get { return Children[1]; } }
            public DecomposeSeqRule(Rule iteration, Rule rightMost)
                : base(iteration, rightMost)
            { }

            public bool Decompose(Node node)
            {
                if (node.Count < 2 || (node[node.Count - 1].Label != RightMost.Name))
                    return false;

                var n = new Node(Name);
                return true;
            }
        }

        public class LeftRecursionRule : Rule
        {
            public Rule baseRule;
            public Rule iterativeRule;
            public Rule rightHandRule;

            public LeftRecursionRule(Rule baseRule, params LeftRecursionOptionRule[] choices)
            {
                rightHandRule = new ChoiceRule(choices.Select(x => x.Right).ToArray());
                iterativeRule = new SeqRule(baseRule, new ZeroOrMoreRule(rightHandRule));
            }

            public override string Definition
            {
                get { return "???"; }
            }

            protected override bool InternalMatch(ParserState state)
            {
            }

            public static Node Decompose(List<Node> nodes)
        {
            if (nodes.Count < 2)
                return nodes[0];

            List<Node> leftGroup = nodes.GetRange(0, nodes.Count - 1);
            Node rightNode = nodes[nodes.Count - 1];
          


            Decompose(leftGroup);
        }
        }
        /*
        public static Rule IncExpr      = DecomposeSeq(PostfixBase, Inc);
        public static Rule DecExpr      = DecomposeSeq(PostfixBase, Dec);
        public static Rule FieldExpr    = DecomposeSeq(PostfixBase, Field);
        public static Rule IndexExpr    = DecomposeSeq(PostfixBase, Index);        
        public static Rule CallExpr     = DecomposeSeq(PostfixBase, ArgList);
        public static Rule PostfixExpr  = DecomposeChoice(UnaryExpr, IncExpr, DecExpr, FieldExpr, IndexExpr, CallExpr);
         */

        /*
        public static Rule PostIncExpr = LeftRecursiveChoice(() => PostfixExpr, Inc);
        public static Rule PostDecExpr = LeftRecursiveChoice(() => PostfixExpr, Dec);
        public static Rule FieldExpr = LeftRecursiveChoice(() => PostfixExpr, Field);
        public static Rule IndexExpr = LeftRecursiveChoice(() => PostfixExpr, Index);        
        public static Rule CallExpr = LeftRecursiveChoice(() => PostfixExpr, ArgList);
        public static Rule PostfixExpr = LeftRecursion(UnaryExpr, PostIncExpr, PostDecExpr, FieldExpr, IndexExpr, CallExpr);
         */

        /*
        public static Rule PostfixBaseExpr = Recursive(() => PostfixExpr);
        public static Rule PostIncExpr = PostfixBaseExpr + Inc;
        public static Rule PostDecExpr = PostfixBaseExpr + Dec;
        public static Rule FieldExpr = PostfixBaseExpr + Field;
        public static Rule IndexExpr = PostfixBaseExpr + Index;        
        public static Rule CallExpr = PostfixBaseExpr + ArgList;
        */ 

        //public static Rule PostfixExpr = Node(UnaryExpr | PostIncExpr | PostDecExpr | FieldExpr | IndexExpr | CallExpr);

        //public static Rule PostfixExpr = LeftRecursiveChoice(UnaryExpr, PostIncExpr, PostDecExpr, FieldExpr, IndexExpr, CallExpr);

        //public static Rule PostfixExpr = IterationToRecursion(UnaryExpr, ZeroOrMore(PostfixOp), (PostIncExpr | PostDecExpr | FieldExpr | IndexExpr | CallExpr));

        //public static Rule PostfixExpr = Node(UnaryExpr | PostIncExpr | PostDecExpr | FieldExpr | IndexExpr | CallExpr);

        /*
        public static Rule PostfixBaseExpr = Node(UnaryExpr + WS + Postfixes);        
        public static Rule IncExpr = DecomposableSeq(PostfixBaseExpr, Inc);
        public static Rule DecExpr = DecomposableSeq(PostfixBaseExpr, Dec);
        public static Rule FieldExpr = DecomposableSeq(PostfixBaseExpr, Field);
        public static Rule IndexExpr = DecomposableSeq(PostfixBaseExpr, Index);        
        public static Rule CallExpr = DecomposableSeq(PostfixBaseExpr, ArgList);
        public static Rule PostfixExpr = DecompsableChoice(IncExpr | DecExpr | FieldExpr | IndexExpr | CallExpr);
         */

        /*
        public static Rule PostfixExpr = LeftGroup(UnaryExpr + ZeroOrMore(PostfixOp), FieldExpr, IndexExpr, CallExpr);
        public static Rule FieldExpr = DecomposeSeq(PostfixExpr, Field);
        public static Rule IndexExpr = DecomposeSeq(PostfixExpr, Index);        
        public static Rule CallExpr = DecomposeSeq(PostfixExpr, ArgList);
         */

        /*
        public static Rule FieldExpr    = LeftRecursiveChoice(Field);
        public static Rule IndexExpr    = LeftRecursiveChoice(Index);        
        public static Rule CallExpr     = LeftRecursiveChoice(ArgList);
        public static Rule PostfixExpr  = LeftRecursiveNode(UnaryExpr, FieldExpr | IndexExpr | CallExpr);
         */

        /*
        public static Rule FieldExpr    = LeftRecursiveChoice(Field);
        public static Rule IndexExpr    = LeftRecursiveChoice(Index);        
        public static Rule CallExpr     = LeftRecursiveChoice(ArgList);
        public static Rule PostfixExpr  = LeftRecursiveNode(UnaryExpr, FieldExpr, IndexExpr, CallExpr);
        */

        /*
        public static Rule PostfixExpr  = LeftRecursiveNode(UnaryExpr, 
            new Dictionary<Rule, string>() { { Field, "FieldExpr" }, { Index, "IndexExpr" }, { ArgList, "CallExpr" } });

        public static Rule PostfixExpr  = LeftRecursiveNode(UnaryExpr)
            .AddOption(Field, "FieldExpr")
            .AddOption(Index, "IndexExpr")
            .AddOption(ArgList, "FunCall");
         */

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer Ara 3D
Canada Canada
I am the designer of the Plato programming language and I am the founder of Ara 3D. I can be reached via email at cdiggins@gmail.com

Comments and Discussions