What is Carburetta?

Carburetta is an open source fused scanner parser generator for the C and C++ languages. Fused means it can generate both the scanner and the parser from the same input which simplifies parsing languages. This is beneficial for the following reasons:

  • You won't have to manually ferry data and types back and forth between the scanner and the parser.
  • The nuances of the scanner are available to the parser. If the type of a "[0-9]+" patterned token is "int" then that is also the type the token appears as in the parser's grammar. No longer a need to shovel everything into a single "token" data type only to "unshovel" it later.

When choosing Carburetta as your parser generator, not only will you be more productive because you choose to implement a language with a parser generator, but you'll be more productive because you chose Carburetta as your parser generator.

Open source license

Carburetta is open source, licensed under the Apache license version 2.0:

  • You can use it for commercial purposes.
  • You can modify it without sharing your changes.
  • No need for any small business source code escrow constructions.
  • There are no license fees, it is available for no charge.

Being open source does not mean we just throw it over the fence for your enjoyment. For businesses that need it, support, training and consultancy are available. Please express your interest by sending an email to carburetta@kinglet.nl summarizing your needs; we can take then explore how we can help.

Whether you need to move an existing solution into the future or create a new language to suit your needs, or just need independent advice on your best options going forward, it can be difficult to find the expertise you need.

What it looks like

Below you can find a minimalist calculator example. Note how the scanner and parser are beautifully, cohesively integrated, sharing type information into a simple, expressive, whole:

#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
 
%scanner%
%prefix calc_
 
: [\ \n]+; /* skip whitespace */ 
 
PLUS: \+;
MINUS: \-;
ASTERISK: \*;
SLASH: /;
TILDE: ~;
BANG: !;
PAR_OPEN: \(;
PAR_CLOSE: \);
INTEGER: [0-9]+ {
  $$ = atoi($text);
}
 
%token PLUS MINUS ASTERISK SLASH TILDE BANG PAR_OPEN PAR_CLOSE INTEGER
%nt grammar expr term factor value
 
 
%grammar%
 
%type grammar expr term factor value: int
 
%token_type int
%constructor $$ = 0;
%params int *final_result
 
grammar: expr { printf("Outcome: %d\n", $0); *final_result = $0; }
 
expr: term                      { $$ = $0; }
expr: expr PLUS term            { $$ = $0 + $2; }
expr: expr MINUS term           { $$ = $0 - $2; }
 
term: factor                    { $$ = $0; }
term: term ASTERISK factor      { $$ = $0 * $2; }
term: term SLASH factor         { $$ = $0 / $2; }
 
factor: value                   { $$ = $0; }
factor: TILDE factor            { $$ = ~$1; }
factor: BANG factor             { $$ = !$1; }
factor: MINUS factor            { $$ = -$1; }
factor: PAR_OPEN expr PAR_CLOSE { $$ = $1; }
 
value: INTEGER                  { $$ = $0; }
 
%%
 
int main(int argc, char **argv) {
  struct calc_stack stack;
  calc_stack_init(&stack);
 
  int final_result;
  static char s[300];
 
  fprintf(stderr, "Enter an expression (-1 to terminate):\n");
 
  do {
    if (!fgets(s, sizeof(s), stdin)) {
      break;
    }
    calc_stack_reset(&stack);
    calc_scan(&stack, s, strlen(s), 1, &final_result);
  } while (final_result != -1);
  calc_stack_cleanup(&stack);
 
  return final_result;
}
 

Learning more

Play with the above example by going to the Download section, it includes the example in the carburetta/examples/calc folder. Try and modify it, add functions! Add variables! Add statements! Control flow!

Alternatively, you can read about the calculator example in the manual by going to the Documentation section. To jump straight to the section about the calculator, you can do so here.