What is Carburetta?
Carburetta is an open-source fused scanner parser generator for the C and C++ languages. It is like lex (scanner) and yacc (parser) rolled into one, generating both components from the same input. This simplifies language parsing since the scanner and parser are aware of each other.
This integration offers several benefits:
- You won't have to manually pass 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 need to pack everything into a single 'token' type only to unpack it later.
Carburetta reduces the complexity of writing parsers, letting you focus on defining your language rather than managing the interaction between a scanner and parser.
What It Looks Like
This minimalist calculator example shows how Carburetta unifies scanning and parsing, ensuring smooth type integration for a more expressive grammar:
#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; }
Open-Source License
Carburetta is open-source, licensed under the Apache license version 2.0:
Learning More
You can experiment with the example by visiting the Download section, which includes it in the carburetta/examples/calc
folder.
Alternatively, read more about the calculator example in the Documentation. Jump directly to the calculator section here.
More substantial examples are also available:
- Aex-GL, an open-source implementation of OpenGL ES 2.0 with both its GLSL 1.0 shader language implementation and C preprocessor written in Carburetta. Aex-GL is available on GitHub: https://github.com/kingletbv/aex-gl
-
kc, an open-source C compiler front-end and interpreter that implements the C99 standard using Carburetta.
kc is included in the Carburetta source tree (found at
carburetta/examples/kc/
).