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