From 66f66d2363e41274589ccad098269e379e348632 Mon Sep 17 00:00:00 2001 From: pacien Date: Tue, 20 Feb 2018 23:40:23 +0100 Subject: Import from TD11 --- src/tpc.lex | 50 ++++++++++++++++++++++ src/tpc.y | 139 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 189 insertions(+) create mode 100644 src/tpc.lex create mode 100644 src/tpc.y diff --git a/src/tpc.lex b/src/tpc.lex new file mode 100644 index 0000000..eaeea89 --- /dev/null +++ b/src/tpc.lex @@ -0,0 +1,50 @@ +%{ +/** + * UPEM / Compilation / Projet + * Pacien TRAN-GIRARD, Adam NAILI + */ + +#include "tpc.tab.h" + +int lineno = 1; +%} + +%option noinput nounput noyywrap +%x COMMENT + +%% +[ \t\r]+ ; +\n { lineno++;} +"/*" { BEGIN COMMENT;} +&& { return AND; } +"||" { return OR; } +"*"|"/"|% { yylval.divstar=yytext[0]; return DIVSTAR; } +"+"|- { yylval.addsub=yytext[0]; return ADDSUB; } +"<"|"<="|">"|>= { strcpy(yylval.comp, yytext); return ORDER; } +==|!= { strcpy(yylval.comp, yytext); return EQ; } +int { strcpy(yylval.type, yytext); return TYPE; } +float { strcpy(yylval.type, yytext); return TYPE; } +double { strcpy(yylval.type, yytext); return TYPE; } +char { strcpy(yylval.type, yytext); return TYPE; } +void { return VOID; } +const { return CONST; } +if { return IF;} +else { return ELSE;} +while { return WHILE; } +return { return RETURN; } +[a-zA-Z_][a-zA-Z0-9_]* { strcpy(yylval.ident, yytext); return IDENT; } +[0-9]+ { sscanf(yytext, "%d", &(yylval.num)); return NUM; } +'\\?.' { if (strlen(yytext)==3) + yylval.caractere=yytext[1]; + else switch(yytext[2]) { + case 'n': yylval.caractere='\n'; break; + case 't': yylval.caractere='\t'; break; + case '\'': yylval.caractere='\''; break; + } + return CARACTERE; } +. { return yytext[0];} +"*/" { BEGIN INITIAL;} +\n { lineno++;} +. ; +%% + diff --git a/src/tpc.y b/src/tpc.y new file mode 100644 index 0000000..88e31fb --- /dev/null +++ b/src/tpc.y @@ -0,0 +1,139 @@ +%{ +/* + * UPEM / Compilation / Projet + * Pacien TRAN-GIRARD, Adam NAILI + */ + +#include + +extern int lineno; +int yylex(); +void yyerror(char *); + +%} + +%union { + char caractere; + int num; + char ident[64]; + char type[16]; + char comp[3]; + char addsub; + char divstar; +} + +%token CARACTERE +%token NUM +%token IDENT +%token ORDER EQ +%token ADDSUB +%token DIVSTAR +%token OR AND CONST IF WHILE RETURN VOID +%token TYPE + +%left ',' +%precedence ')' +%precedence ELSE + +%% +Prog : DeclConsts DeclVars DeclFoncts + ; +DeclConsts : DeclConsts CONST ListConst ';' + | + ; +ListConst : ListConst ',' IDENT '=' Litteral + | IDENT '=' Litteral + ; +Litteral : NombreSigne + | CARACTERE + ; +NombreSigne : NUM + | ADDSUB NUM + ; +DeclVars : + DeclVars TYPE Declarateurs ';' + | + ; +Declarateurs : Declarateurs ',' Declarateur + | Declarateur + ; +Declarateur : IDENT + | IDENT '[' NUM ']' + ; +DeclFoncts : DeclFoncts DeclFonct + | DeclFonct + ; +DeclFonct : EnTeteFonct Corps + ; +EnTeteFonct : TYPE IDENT '(' Parametres ')' + | VOID IDENT '(' Parametres ')' + ; +Parametres : VOID + | ListTypVar + ; +ListTypVar : ListTypVar ',' TYPE IDENT + | TYPE IDENT + ; +Corps : '{' DeclConsts DeclVars SuiteInstr '}' + ; +SuiteInstr : SuiteInstr Instr + | + ; +Instr : Exp ';' + | RETURN Exp ';' + | RETURN ';' + | ';' + | IF '(' Exp ')' Instr + | IF '(' Exp ')' Instr ELSE Instr + | WHILE '(' Exp ')' Instr + | '{' SuiteInstr '}' + ; +Exp : LValue '=' Exp + | EB + ; +EB : EB OR TB + | TB + ; +TB : TB AND FB + | FB + ; +FB : FB EQ M + | M + ; +M : M ORDER E + | E + ; +E : E ADDSUB T + | T + ; +T : T DIVSTAR F + | F + ; +F : ADDSUB F + | '!' F + | '(' Exp ')' + | LValue + | NUM + | CARACTERE + | IDENT '(' Arguments ')' + ; +LValue : IDENT + | IDENT '[' Exp ']' + ; +Arguments : ListExp + | + ; +ListExp : ListExp ',' ListExp + | Exp + ; +%% + +void yyerror(char *s){ + fprintf(stderr, "Line no%d: %s\n", lineno, s); +} + +int main(int argc, char **argv) { + yyparse(); + return 0; +} + -- cgit v1.2.3