aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpacien2018-02-22 23:52:17 +0100
committerpacien2018-02-22 23:52:17 +0100
commit11acfa2e75350d31a665b8ee99925916c2c8b5f1 (patch)
treee26cecf3f7d5313cb487466b62dbe14e3dd92c07
parentc84e00f62d1fbd273e5a07f7105c727da0f0ff8b (diff)
downloadtpc-compiler-11acfa2e75350d31a665b8ee99925916c2c8b5f1.tar.gz
Remove token value copy
-rw-r--r--src/tpc.lex43
-rw-r--r--src/tpc.y27
2 files changed, 23 insertions, 47 deletions
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;
14 14
15%% 15%%
16[ \t\r]+ ; 16[ \t\r]+ ;
17\n { lineno++;} 17\n { lineno++; }
18"/*" { BEGIN COMMENT;} 18"/*" { BEGIN COMMENT; }
19&& { return AND; } 19&& { return AND; }
20"||" { return OR; } 20"||" { return OR; }
21"*"|"/"|% { yylval.divstar=yytext[0]; return DIVSTAR; } 21"*"|"/"|% { return DIVSTAR; }
22"+"|- { yylval.addsub=yytext[0]; return ADDSUB; } 22"+"|- { return ADDSUB; }
23"<"|"<="|">"|>= { strcpy(yylval.comp, yytext); return ORDER; } 23"<"|"<="|">"|>= { return ORDER; }
24==|!= { strcpy(yylval.comp, yytext); return EQ; } 24==|!= { return EQ; }
25int { strcpy(yylval.type, yytext); return TYPE; } 25int { return TYPE; }
26float { strcpy(yylval.type, yytext); return TYPE; } 26float { return TYPE; }
27double { strcpy(yylval.type, yytext); return TYPE; } 27double { return TYPE; }
28char { strcpy(yylval.type, yytext); return TYPE; } 28char { return TYPE; }
29void { return VOID; } 29void { return VOID; }
30const { return CONST; } 30const { return CONST; }
31if { return IF;} 31if { return IF; }
32else { return ELSE;} 32else { return ELSE; }
33while { return WHILE; } 33while { return WHILE; }
34return { return RETURN; } 34return { return RETURN; }
35[a-zA-Z_][a-zA-Z0-9_]* { strcpy(yylval.ident, yytext); return IDENT; } 35[a-zA-Z_][a-zA-Z0-9_]* { return IDENT; }
36[0-9]+ { sscanf(yytext, "%d", &(yylval.num)); return NUM; } 36[0-9]+ { return NUM; }
37'\\?.' { if (strlen(yytext)==3) 37'\\?.' { return CARACTERE; }
38 yylval.caractere=yytext[1]; 38. { return yytext[0]; }
39 else switch(yytext[2]) { 39<COMMENT>"*/" { BEGIN INITIAL; }
40 case 'n': yylval.caractere='\n'; break; 40<COMMENT>\n { lineno++; }
41 case 't': yylval.caractere='\t'; break;
42 case '\'': yylval.caractere='\''; break;
43 }
44 return CARACTERE; }
45. { return yytext[0];}
46<COMMENT>"*/" { BEGIN INITIAL;}
47<COMMENT>\n { lineno++;}
48<COMMENT>. ; 41<COMMENT>. ;
49%% 42%%
50 43
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 *);
12 12
13%} 13%}
14 14
15%union { 15%token CARACTERE NUM IDENT ORDER EQ ADDSUB DIVSTAR
16 char caractere; 16%token OR AND CONST IF WHILE RETURN VOID TYPE
17 int num;
18 char ident[64];
19 char type[16];
20 char comp[3];
21 char addsub;
22 char divstar;
23}
24
25%token <caractere> CARACTERE
26%token <num> NUM
27%token <ident> IDENT
28%token <comp> ORDER EQ
29%token <addsub> ADDSUB
30%token <divstar> DIVSTAR
31%token OR AND CONST IF WHILE RETURN VOID
32%token <type> TYPE
33 17
34%left ',' 18%left ','
35%precedence ')' 19%precedence ')'
@@ -127,12 +111,11 @@ ListExp : ListExp ',' ListExp
127 ; 111 ;
128%% 112%%
129 113
130void yyerror(char *s){ 114void yyerror(char *msg){
131 fprintf(stderr, "Line no%d: %s\n", lineno, s); 115 fprintf(stderr, "%s at line %d\n", msg, lineno);
132} 116}
133 117
134int main(int argc, char **argv) { 118int main(int argc, char **argv) {
135 yyparse(); 119 return yyparse();
136 return 0;
137} 120}
138 121