2

Hello o/

I was experimenting with writing a language implementation so bump into this new thing called "recursive descent parser" at first it seemed complex but as I programmed it everything magically worked. I will attach the code tell me if I am wrong somewhere.

namespace ContextFreeGrammarDemo
{
    static class Parser
    {
        static string code = "aaaabbbb";
        static int cursor = 0;

        public static void Parse()
        {
            if (cursor >= code.Length)
                return;
            char currentChar = code[cursor];

            if (currentChar == 'a')
            {
                Console.WriteLine("a");
                cursor++;
                Parse();
                if (cursor < code.Length && code[cursor] == 'b')
                {
                    Console.WriteLine("b");
                    cursor++;
                    Parse();
                }
                else
                {
                    Console.WriteLine("Oopsie");
                    Environment.Exit(1);
                }
            }
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            Parser.Parse();
        }
    }
}
no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here
this post was submitted on 19 Dec 2025
2 points (100.0% liked)

C Sharp

1749 readers
2 users here now

A community about the C# programming language

Getting started

Useful resources

IDEs and code editors

Tools

Rules

Related communities

founded 2 years ago
MODERATORS