Skip to content

Instantly share code, notes, and snippets.

@guenodz
Created November 27, 2017 12:30
Show Gist options
  • Save guenodz/6ae25c509f52505b3a96421c30d41b16 to your computer and use it in GitHub Desktop.
Save guenodz/6ae25c509f52505b3a96421c30d41b16 to your computer and use it in GitHub Desktop.
Sample lex+yacc program
%{
#include <stdlib.h>
#include "y.tab.h"
%}
%%
[0-9]+ {
yylval = atoi(yytext);
return ENTIER;
}
[-+\n] return *yytext;
[ \t] ; /* skip whitespace */
. yyerror("invalid character");
%%
%{
#include <stdio.h>
#include <string.h>
void yyerror(const char *str)
{
fprintf(stderr,"Erreur: %s\n",str);
}
yywrap()
{
return 1;
}
main()
{
return yyparse();
}
%}
%start program
%token ENTIER
%left '+'
%%
program:
program expr '\n' { printf("%d\n", $2); }
|
;
expr:
ENTIER { $$ = $1;}
| expr '+' expr { $$ = $1 + $3; }
;
%%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment