Click here to Skip to main content
12,505,104 members (62,670 online)
Click here to Skip to main content

Stats

130K views
3.1K downloads
244 bookmarked
Posted

Implementing Programming Languages Using C# 4.0

, 12 Jul 2012 MIT
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

Share

About the Author

Christopher Diggins
Software Developer Autodesk
Canada Canada
This article was written by Christopher Diggins, a computer science nerd who currently works at Autodesk as an SDK specialist.

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160919.1 | Last Updated 12 Jul 2012
Article Copyright 2011 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid