Created
November 27, 2017 12:30
-
-
Save guenodz/6ae25c509f52505b3a96421c30d41b16 to your computer and use it in GitHub Desktop.
Sample lex+yacc program
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#include <stdlib.h> | |
#include "y.tab.h" | |
%} | |
%% | |
[0-9]+ { | |
yylval = atoi(yytext); | |
return ENTIER; | |
} | |
[-+\n] return *yytext; | |
[ \t] ; /* skip whitespace */ | |
. yyerror("invalid character"); | |
%% |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#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