Code Monkey home page Code Monkey logo

carburetta's Introduction

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.

carburetta's People

Contributors

martijnboekhorst avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.