1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
//%expect 0
%error-verbose
%pure-parser
%debug
%locations
%code requires
{
# include <stdio.h>
# include <stdlib.h>
# include <errno.h>
}
%code provides
{
void yyerror (const char* msg);
# define YY_DECL \
enum yytokentype yylex (YYSTYPE* yylval, YYLTYPE* yylloc)
YY_DECL;
extern FILE* yyin;
}
%%
%token
IF "if"
THEN "then"
ELSE "else"
NUL "nul"
PLUS "+"
MINUS "-"
STAR "*"
DIV "/"
EQ "="
INF "<"
SUP ">"
NEQ "<>"
INFEQ "<="
SUPEQ ">="
WHILE "while"
FOR "for"
TO "to"
DO "do"
OF "of"
BREAK "break"
PARSE_EOF 0 "end of file";
%token <string>
STRING "string";
%token <id>
ID "id";
%token <ival>
INT "integer";
%union
{
int ival;
char *string;
char *id;
};
//Ajouter les bonnes associativite et priorite pour regler certains conflits
%left "+" "-"; // priorite 0 associatif a gauche
%nonassoc ">" "<" "="; //priorite 1 non associatif
program:
exp
exp:
"integer"
| "id"
| "if" exp "then" exp
| "if" exp "then" exp "else" exp
| op
;
op:
exp "+" exp
| exp "-" exp
| exp "*" exp
| exp "/" exp
| exp "=" exp
| exp ">" exp
| exp "<" exp
;
%%
/* Epilogue. */
void yyerror (const char* msg)
{
/* Ajouter affichage d'un message d'erreur */
printf("%s\n", msg);
return;
}
int main (void)
{
unsigned int nerrors = 0;
// yyin de type FILE*, permet de specifier la source a analyser.
yyin = stdin;
yyparse ();
return nerrors;
} |
Partager