-
Notifications
You must be signed in to change notification settings - Fork 0
Learn
Tristin Porter edited this page Jan 9, 2026
·
3 revisions
Welcome to CDTk! This guide introduces the basics of building a compiler in 3 steps:
- TokenSet: Recognize lexical patterns (tokens).
- RuleSet: Structure grammar rules.
- MapSet: Define how parsed input transforms into output.
CDTk simplifies compiler design with its unified and fully-typed pipeline:
- TokenSet: Identify and categorize input patterns (e.g., numbers, keywords, operators).
- RuleSet: Combine tokens into meaningful structures using grammar rules.
- MapSet: Convert parsed structures into your desired output (e.g., code, JSON, config).
class Number { }
class Identifier { }
class AssignmentNode { }
var tokens = new TokenSet
{
new Token<Number>(@"\d+"),
new Token<Identifier>(@"[A-Za-z_][A-Za-z0-9_]*")
};
var rules = new RuleSet
{
new Rule<AssignmentNode>("variable:@Identifier '=' value:@Number")
.Returns<AssignmentNode>("variable", "value")
};
var mappings = new MapSet
{
new Map<AssignmentNode>("int {variable} = {value};")
};
var compiler = new Compiler()
.WithTokens(tokens)
.WithRules(rules)
.WithTarget(mappings)
.Build();
var result = compiler.Compile("x = 42;");
Console.WriteLine(result.Output); // Outputs: int x = 42;When you call compiler.Compile(input), CDTk processes the input systematically:
-
Lexing (TokenSet): Converts input text into tokens (e.g.,
Identifier("x"), Equals("="), Number("42")). - Parsing (RuleSet): Organizes tokens into an AST (Abstract Syntax Tree).
-
Code Generation (MapSet): Transforms the AST into output (e.g.,
int x = 42;).
Learn More: Check out the Pipeline Overview.
Tokens define basic patterns in your language. For example:
var tokens = new TokenSet
{
new Token<Identifier>(@"[A-Za-z_][A-Za-z0-9_]*"), // Variables
new Token<Number>(@"\d+"), // Numbers
new Token<Equals>("="), // Assignment operator
new Token<Whitespace>(@"\s+").Ignore() // Ignored whitespace
};Learn More: See Tokens.
Rules describe your grammar in a declarative syntax:
var rules = new RuleSet
{
new Rule<AssignmentNode>("variable:@Identifier '=' value:@Number")
.Returns<AssignmentNode>("variable", "value")
};Learn More: See Rules.
Define templates for code generation or output transformation:
var mappings = new MapSet
{
new Map<AssignmentNode>("int {variable} = {value};") // Outputs C syntax
};Learn More: See Mapping.
Combine the TokenSet, RuleSet, and MapSet into Compiler:
var compiler = new Compiler()
.WithTokens(tokens)
.WithRules(rules)
.WithTarget(mappings)
.Build();
var result = compiler.Compile("x = 42;");
Console.WriteLine(result.Output); // Outputs: int x = 42;If you encounter issues:
-
Unexpected Tokens: Adjust your
TokenSetdefinitions. -
Grammar Errors: Refine your
RuleSetfor clarity. -
Missing Outputs: Confirm
MapSetcovers all node types.
Learn More: See the Debugging Guide.
CDTk offers advanced features and infinite customization: