Skip to content

Instantly share code, notes, and snippets.

@gnosis23
Created March 19, 2016 05:19
Show Gist options
  • Select an option

  • Save gnosis23/aba86edd47513c90ddd6 to your computer and use it in GitHub Desktop.

Select an option

Save gnosis23/aba86edd47513c90ddd6 to your computer and use it in GitHub Desktop.
language start pieces
%option noyywrap yylineno
%{
#include "lang.tab.h"
%}
DIGIT ([0-9])
LETTER ([a-zA-Z])
%%
"-" |
"(" |
"," |
"=" |
")" { return yytext[0]; }
"if" { return IF; }
"then" { return THEN; }
"else" { return ELSE; }
"let" { return LET;}
"in" { return IN;}
"zero?" { return ZERO;}
{LETTER}({DIGIT}|{LETTER}|"_"|"-"|"?")* { yylval.str = yytext; return VAR; }
[+-]?{DIGIT}{DIGIT}* { yylval.i = atoi(yytext); return NUMBER; }
"%".* /*ignored*/
[ \t\r\n] /*ignored*/
%%
%{
#include <stdio.h>
#include "let.h"
%}
%union {
struct ast *a;
struct boolexp *b;
char *str;
int i;
}
%token <i> NUMBER
%token <str> VAR
%token IF THEN ELSE LET IN ZERO
%token EOL
%type <a> exp
%type <b> boolexp
%%
prog: stmt prog
|
;
stmt: exp {
struct ast *a = $1;
printf("exp = %d\n", eval(a));
freeAll(a);
}
;
exp: NUMBER { $$ = new_num($1); }
| VAR { printf("var %s\n", $1); $$=0; }
| '-' '(' exp ',' exp ')' {
$$ = new_diff_exp($3, $5);
}
| IF boolexp THEN exp ELSE exp {
$$ = new_if_exp($2, $4, $6);
}
| LET VAR '=' exp IN exp {
printf("let var = exp in exp\n");
}
;
boolexp: ZERO '(' exp ')' { $$ = new_boolexp($3); }
;
%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment