-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsintaxis_ver1.y
More file actions
69 lines (57 loc) · 1.14 KB
/
sintaxis_ver1.y
File metadata and controls
69 lines (57 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//printf("%.*s\n",strlen($3)-2,$3+1);
void fixString(char *str);
void yyerror (char *s);
%}
%union
{
int integer;
float floatnum;
char *charac;
char *id;
char *string;
}
%start LINEA
%token print
%token <floatnum> FLOAT
%token <integer> CTE_INT
%token <charac> CTE_CHAR
%token <id> ID
%token <string> STRING
%type <integer> LINEA EXP DESPLIEGUE
%%
LINEA :EXP ';' {printf("%d\n",$1);}
|DESPLIEGUE ';' {;}
|LINEA EXP ';' {printf("%d\n",$2);}
|LINEA DESPLIEGUE ';' {;}
;
EXP :CTE_INT {$$ = $1;}
|CTE_INT '+' EXP {$$ = $1 + $3;}
|CTE_INT '-' EXP {$$ = $1 - $3;}
|CTE_INT '*' EXP {$$ = $1 * $3;}
|CTE_INT '/' EXP {$$ = $1 / $3;}
;
DESPLIEGUE :print '(' EXP ')' {printf("%d\n",$3);}
|print '(' STRING ')' {fixString($3);
printf("%s\n",$3);}
|print '(' CTE_CHAR ')' {fixString($3);
printf("%s\n",$3);}
;
%%
void fixString(char *str)
{
size_t len=strlen(str);
memmove(str,str+1,len);
str[len-2]=0;
}
void yyerror (char *s)
{
printf("%s\n", s);
}
int main(void)
{
return yyparse();
}