First draft

This commit is contained in:
Arnaud Vergnet 2021-03-12 10:48:48 +01:00
parent 63975af86f
commit 6e00634d28
4 changed files with 195 additions and 0 deletions

19
Makefile Normal file
View file

@ -0,0 +1,19 @@
LEX = flex
YACC = bison -d
CC = gcc
compilateur: as.tab.c lex.yy.c
$(CC) -o compilateur as.tab.c lex.yy.c
as.tab.c: as.y
$(YACC) as.y
lex.yy.c: al.lex
$(LEX) al.lex
build: compilateur
clean:
rm as.tab.c as.tab.h lex.yy.c compilateur

54
al.lex Normal file
View file

@ -0,0 +1,54 @@
%{
#include "as.tab.h"
%}
D [0-9]
%%
{D}+(e{D}+)? {
yylval.nombre = atoi(yytext);
return tNB;
}
"+" { return tADD; }
"-" { return tSUB; }
"*" { return tMUL; }
"/" { return tDIV; }
"=" { return tEQ; }
"(" { return tPO; }
")" { return tPF; }
";" { return tPV; }
"," { return tVIRG; }
"{" { return tAO; }
"}" { return tAF; }
"!" { return tNOT; }
"==" { return tEQ2; }
"!=" { return tNOTEQ; }
"<" { return tINF; }
"<=" { return tINFEQ; }
">" { return tSUP; }
">=" { return tSUPEQ; }
"&&" { return tAND; }
"||" { return tOR; }
[ \t\n]+ { }
"int" { return tINT; }
"const" { return tCONST; }
"main" { return tMAIN; }
"if" { return tIF; }
"else" { return tELSE; }
"elsif" { return tELSIF; }
"while" { return tWHILE; }
"printf" { return tPRINTF; }
[a-zA-Z][a-zA-Z0-9_]* { return tID; }
%%
int yywrap() {
return 1;
}
// int main () {
// yylex();
// return 1;
// }

115
as.y Normal file
View file

@ -0,0 +1,115 @@
%{
#include "stdio.h"
#include "stdlib.h"
%}
%union {
int nombre;
}
%token<nombre> tNB
%token tADD
%token tSUB
%token tMUL
%token tDIV
%token tPO
%token tPF
%token tPV
%token tVIRG
%token tAO
%token tAF
%token tEQ
%token tEQ2
%token tNOTEQ
%token tNOT
%token tINF
%token tINFEQ
%token tSUP
%token tSUPEQ
%token tAND
%token tOR
%token tINT
%token tCONST
%token tMAIN
%token tIF
%token tELSE
%token tELSIF
%token tWHILE
%token tPRINTF
%token tID
%left tADD
%left tSUB
%left tMUL
%left tDIV
// %type<nombre> E
%%
S : Main ;
Main : tINT tMAIN tPO Params tPF Body ;
Params : | Param SuiteParams ;
Param : tINT tID ;
SuiteParams : tVIRG Param SuiteParams ;
Body : tAO Instructions tAF ;
Instructions : Instruction Instructions | ;
Instruction : Aff | Printf | If ;
Aff : tID tEQ E tPV ;
E : tNB | tID | E tADD E | E tMUL E | E tSUB E | E tDIV E | tPO E tPF | tSUB E ;
If : IfSimple | IfElse ;
IfSimple : tIF tPO Cond tPF Body ;
IfElse : IfSimple tELSE Body ;
Cond : Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | E tINFEQ E | E tSUP E | E tSUPEQ E | E tNOTEQ E | tNOT Cond | tNOT tPO Cond tPF ;
Printf : tPRINTF tPO tID tPF tPV
// If : tIF tPO Cond tPF Body ;
//
// Cond : Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | tNOT Cond ;
// Invocation : tID tPO Args tPF ;
// Args : .... cf params
// Return : tRET E tPV ;
/* S -> E ; S
* S ->
*/
// S : E tPV
// { printf("RES: %d\n", $1); }
// S
// | { printf("END\n"); }
// ;
//
// E : E tADD E { $$ = $1 + $3; }
// | E tSUB E { $$ = $1 - $3; }
// | tOB E tCB { $$ = $2; }
// | tNB { $$ = $1; }
// ;
%%
void yyerror(char const* err) {
printf("%s\n", err);
exit(1);
}
void main(void) {
yyparse();
}

7
test.c Normal file
View file

@ -0,0 +1,7 @@
int main(){
x = 3 + 6;
printf(x);
if (x == x && y != 5 + 6 || 1 + 2 == 2 + 1 && !(x == x)) {
printf(x);
}
}