A simple Lexer/Tokenizer that categorizes content into numbers, operators, or keywords.

ID: 228

				
					using uCalcSoftware;

var uc = new uCalc();
// Note how the definition order matters if patterns overlap (though here they are distinct).
var t = uc.NewTransformer();

// Define patterns for a simple math language
t.FromTo("{d: {@Number}}", "[NUM:{d}]");
t.FromTo("{op: + | - | * | / }", "[OP:{op}]");
t.FromTo("print", "[CMD:PRINT]"); // Specific keyword

var code = "print 10 + 20";
Console.WriteLine(t.Transform(code));
				
			
[CMD:PRINT] [NUM:10] [OP:+] [NUM:20]
				
					#include <iostream>
#include "uCalc.h"

using namespace std;
using namespace uCalcSoftware;

int main() {
   uCalc uc;
   // Note how the definition order matters if patterns overlap (though here they are distinct).
   auto t = uc.NewTransformer();

   // Define patterns for a simple math language
   t.FromTo("{d: {@Number}}", "[NUM:{d}]");
   t.FromTo("{op: + | - | * | / }", "[OP:{op}]");
   t.FromTo("print", "[CMD:PRINT]"); // Specific keyword

   auto code = "print 10 + 20";
   cout << t.Transform(code) << endl;
}
				
			
[CMD:PRINT] [NUM:10] [OP:+] [NUM:20]
				
					Imports System
Imports uCalcSoftware
Public Module Program
   Public Sub Main()
      Dim uc As New uCalc()
      '// Note how the definition order matters if patterns overlap (though here they are distinct).
      Dim t = uc.NewTransformer()
      
      '// Define patterns for a simple math language
      t.FromTo("{d: {@Number}}", "[NUM:{d}]")
      t.FromTo("{op: + | - | * | / }", "[OP:{op}]")
      t.FromTo("print", "[CMD:PRINT]") '// Specific keyword
      
      Dim code = "print 10 + 20"
      Console.WriteLine(t.Transform(code))
   End Sub
End Module
				
			
[CMD:PRINT] [NUM:10] [OP:+] [NUM:20]