Skip to content

kingletbv/carburetta

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Carburetta - Fused Scanner & Parser Generator

Carburetta is a fused scanner & parser generator for C and C++. It aims to make parsing of smaller languages trivial and larger languages doable.

Please note that this project is not accepting pull requests, both due to the intricacies of the code making isolated changes impractical, and the impracticality of determining contribution ownership.

To access Carburetta's manual and learn more about the product, visit the website at https://carburetta.com/.

Getting Started

#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;
}

Usage

$ carburetta inputfile.cbrt --c parser.c --h

Will read the input grammar in inputfile.cbrt and generate the C file parser.c containing the parser code, and a header file parser.h containing the declarations necessary for calling the parser from another source file.