Skip to content

Instantly share code, notes, and snippets.

@RavuAlHemio
Created October 14, 2012 17:02
Show Gist options
  • Save RavuAlHemio/3889179 to your computer and use it in GitHub Desktop.
Save RavuAlHemio/3889179 to your computer and use it in GitHub Desktop.
flex/bison example buildable with MSVC
cl = C:\Development\VS\VC\bin\cl.exe
flex = C:\Development\MinGW\msys\1.0\bin\flex.exe
bison = C:\Development\MinGW\msys\1.0\bin\bison.exe
cflags = /nologo /DYY_NO_UNISTD_H
lflags = /nologo
rule cc
description = CC $out
command = $cl $cflags /c /Fo$out $in
rule link
description = LINK $out
command = $cl $lflags /Fe$out $in
rule lex
description = LEX $outsrc
command = $flex -o $outsrc --header-file=$outhdr $in
rule yacc
description = YACC $outsrc
command = $bison -o $outsrc --defines=$outhdr $in
build lexer.c lexer.h: lex lexer.l
outsrc = lexer.c
outhdr = lexer.h
build parser.c parser.h: yacc parser.y
outsrc = parser.c
outhdr = parser.h
build parser.obj: cc parser.c | lexer.h
build lexer.obj: cc lexer.c
build parsertest.obj: cc parsertest.c
build parsertest.exe: link parser.obj lexer.obj parsertest.obj
%{
#include "lycommon.h"
%}
%%
"-" { return '-'; }
"+" { return '+'; }
"(" { return '('; }
")" { return ')'; }
"0" { return '0'; }
"1" { return '1'; }
. { yyerror("unexpected character %c\n", yytext[0]); }
#ifndef __LYCOMMON_H__
#define __LYCOMMON_H__
int yyparse(void);
int yyerror(const char *msg, ...);
int yywrap(void);
#endif
%token '-' '+' '(' ')' '0' '1'
%start Ausdruck
%{
#include "lycommon.h"
#include "lexer.h"
/* slightly evil MSVC hacks... */
#define __STDC__
#define malloc
#define free
%}
%%
Ausdruck : OptMinus Wert
| OptMinus Wert MehrPlusWert
| OptMinus Wert MehrMinusWert
;
OptMinus :
| '-'
;
MehrPlusWert : '+' Wert
| MehrPlusWert '+' Wert
;
MehrMinusWert : '-' Wert
| MehrMinusWert '-' Wert
;
Wert : Zahl
| '(' Ausdruck ')'
;
Zahl : '0'
| '1' Mehr0Oder1
;
Mehr0Oder1 :
| Mehr0Oder1 '0'
| Mehr0Oder1 '1'
;
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "lycommon.h"
int yyerror(const char *msg, ...)
{
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
exit(1);
}
int yywrap(void)
{
return 1;
}
int main(void)
{
yyparse();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment