This repository has been archived by the owner on Jul 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomplejo_cal.l
98 lines (95 loc) · 2.19 KB
/
complejo_cal.l
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
%option noyywrap
%{
#include <stdio.h>
#include <stdlib.h>
#include "complejo_cal.h"
#include "Symbol.h"
#include "y.tab.h"
extern Complejo *creaComplejo(double real, double img);
void RmWs(char* str);
extern YYSTYPE yylval;
%}
op [-+*\/()=:;]
ws [ \t]+
digits [0-9]
number (0|[1-9]+{digits}*)\.?{digits}*
im [i]
simplenum {ws}*{ws}*{number}
completenum {ws}*[-]*{ws}*{number}{ws}*[+|-]{ws}*{number}{ws}*{im}{ws}*
complexnum simplenum|completenum
VAR [_]*[a-zA-Z][a-zA-Z0-9_]*
ARG \${number}
STRING \"(\\.|[^"\\])*\"
EXIT exit[\(][\)]
%%
> {return GT;}
>= {return GE;}
\< {return LT;}
\<= {return LE;}
== {return EQ;}
!= {return NE;}
not {return NOT;}
or {return OR;}
and {return AND;}
if {return IF;}
while {return WHILE;}
else {return ELSE;}
print {return PRINT;}
for {return FOR;}
def {return FUNC;}
input {return INPUT;}
proc {return PROC;}
return {return RETURN;}
{ARG} {int n=0;sscanf(yytext,"$%d",&n);yylval.narg =n;return ARG;}
{STRING} {
String s = makeString(yytext);
yylval.sym = installString("",STRING,s);
return STRING;
}
{completenum} {
double r, im;
RmWs(yytext);
sscanf(yytext,"%lf %lf",&r,&im);
yylval.sym = installComplejo("",complexnum,creaComplejo(r,im));
return complexnum;}
{simplenum} {
double r;
RmWs(yytext);
sscanf(yytext,"%lf",&r);
yylval.sym = installComplejo("",complexnum,creaComplejo(r,0));
return complexnum;}
{VAR} {
Symbol *s;
if((s=lookup(yytext))==0)
s = installComplejo(yytext,INDEF,creaComplejo(0,0));
yylval.sym = s;
return s->type==INDEF?VAR:s->type;
}
^\n$ {return *yytext;}
{op} |
\n {return *yytext;}
{ws} { /* Do nothing */ }
[\{|\}|\[|\]|,] {return *yytext;}
. { printf("%c",*yytext);}
{EXIT} {return EXIT;}
%%
/* function provided to student to remove */
/* all the whitespaces from a string. */
/* input : a string of chars */
/* output: nothing */
/* side effect: whitespace in the */
/* original string removed */
/* return value: none */
void RmWs(char* str) {
int i = 0, j = 0;
char temp[strlen(str) + 1];
strcpy(temp, str);
while (temp[i] != '\0') {
while (temp[i] == ' ')
i++;
str[j] = temp[i];
i++;
j++;
}
str[j] = '\0';
}