From 11acfa2e75350d31a665b8ee99925916c2c8b5f1 Mon Sep 17 00:00:00 2001 From: pacien Date: Thu, 22 Feb 2018 23:52:17 +0100 Subject: Remove token value copy --- src/tpc.lex | 43 ++++++++++++++++++------------------------- src/tpc.y | 27 +++++---------------------- 2 files changed, 23 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/tpc.lex b/src/tpc.lex index eaeea89..63d4391 100644 --- a/src/tpc.lex +++ b/src/tpc.lex @@ -14,37 +14,30 @@ int lineno = 1; %% [ \t\r]+ ; -\n { lineno++;} -"/*" { BEGIN COMMENT;} +\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; } +"*"|"/"|% { return DIVSTAR; } +"+"|- { return ADDSUB; } +"<"|"<="|">"|>= { return ORDER; } +==|!= { return EQ; } +int { return TYPE; } +float { return TYPE; } +double { return TYPE; } +char { return TYPE; } void { return VOID; } const { return CONST; } -if { return IF;} -else { return ELSE;} +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++;} +[a-zA-Z_][a-zA-Z0-9_]* { return IDENT; } +[0-9]+ { return NUM; } +'\\?.' { return CARACTERE; } +. { return yytext[0]; } +"*/" { BEGIN INITIAL; } +\n { lineno++; } . ; %% diff --git a/src/tpc.y b/src/tpc.y index 11635f9..31452fe 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -12,24 +12,8 @@ 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 +%token CARACTERE NUM IDENT ORDER EQ ADDSUB DIVSTAR +%token OR AND CONST IF WHILE RETURN VOID TYPE %left ',' %precedence ')' @@ -127,12 +111,11 @@ ListExp : ListExp ',' ListExp ; %% -void yyerror(char *s){ - fprintf(stderr, "Line no%d: %s\n", lineno, s); +void yyerror(char *msg){ + fprintf(stderr, "%s at line %d\n", msg, lineno); } int main(int argc, char **argv) { - yyparse(); - return 0; + return yyparse(); } -- cgit v1.2.3 From 806dd4ff649a1fb9a28dc469cc17d7dd5dbc1da5 Mon Sep 17 00:00:00 2001 From: pacien Date: Fri, 23 Feb 2018 01:11:34 +0100 Subject: Adapt for new grammar --- src/tpc.lex | 9 +-- src/tpc.y | 215 +++++++++++++++++++++++++++++++++++------------------------- 2 files changed, 129 insertions(+), 95 deletions(-) (limited to 'src') diff --git a/src/tpc.lex b/src/tpc.lex index 63d4391..8b824c0 100644 --- a/src/tpc.lex +++ b/src/tpc.lex @@ -16,15 +16,13 @@ int lineno = 1; [ \t\r]+ ; \n { lineno++; } "/*" { BEGIN COMMENT; } -&& { return AND; } +"&&" { return AND; } "||" { return OR; } "*"|"/"|% { return DIVSTAR; } "+"|- { return ADDSUB; } -"<"|"<="|">"|>= { return ORDER; } +"<"|"<="|">"|">=" { return ORDER; } ==|!= { return EQ; } int { return TYPE; } -float { return TYPE; } -double { return TYPE; } char { return TYPE; } void { return VOID; } const { return CONST; } @@ -32,6 +30,9 @@ if { return IF; } else { return ELSE; } while { return WHILE; } return { return RETURN; } +print { return PRINT; } +readc { return READC; } +reade { return READE; } [a-zA-Z_][a-zA-Z0-9_]* { return IDENT; } [0-9]+ { return NUM; } '\\?.' { return CARACTERE; } diff --git a/src/tpc.y b/src/tpc.y index 31452fe..a44a9aa 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -12,103 +12,136 @@ void yyerror(char *); %} -%token CARACTERE NUM IDENT ORDER EQ ADDSUB DIVSTAR -%token OR AND CONST IF WHILE RETURN VOID TYPE +%token CARACTERE NUM IDENT +%token ADDSUB DIVSTAR +%token ORDER EQ OR AND +%token IF WHILE RETURN +%token CONST VOID TYPE +%token PRINT READC READE %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 - ; +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 ';' + | READE '(' IDENT ')' ';' + | READC '(' IDENT ')' ';' + | PRINT '(' Exp ')' ';' + | 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 ',' Exp + | Exp + ; %% void yyerror(char *msg){ -- cgit v1.2.3