-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.c
114 lines (101 loc) · 3.85 KB
/
Lexer.c
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*This code is intended to perform lexical analysis. A lexer converts a sequence of characters like a
* mathematical expression composed of variables, operators, functions, etc and converts it to a sequ
* -ence of tokens. Tokens are meaningful character sequences.
* The recognized tokens here are ;
* + Decimal Numbers
* + Elementary functions
* + Variable 'x'
* + Operators
* + Parantheses */
#include "Lexer.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
/*~~~~~~~~~~~~~~~~~~~~~~~~Function to free a stack~~~~~~~~~~~~~~~~~~~~~~~~~~*/
void freeStack(Stack *Stack){
for(int n=0;n<=Stack->top; n++){
free(Stack->stack[n].text);
Stack->stack[n].text=NULL;
}
free(Stack->stack);
free(Stack);
}
Stack* lex(char* input){
Stack *tokenized=(Stack*)malloc(sizeof(Stack));
if(tokenized==NULL){
printf("Memory allocation for tokenized array failed");
exit(1);
}
memset(tokenized,0,sizeof(Stack));
tokenized->stack=calloc(50,sizeof(token));
if(tokenized->stack==NULL){
printf("Memory allocation for tokenized array falied");
exit(1);
}
int i=0;
while (*input !='\0'){
if((*input)==' '){
input++;
continue;
}
//~~~Numerals~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (isdigit(*input) || (*input)=='.'){
//recognize a number
char* start= input;
while (isdigit(*input)||(*input)=='.'){
input++;
}
char *end=input;
tokenized->stack[i].text=(char*)malloc((end - start +1)*sizeof(char));
strncpy (tokenized->stack[i].text, start, end-start);
tokenized->stack[i].text[end-start]='\0'; //Null terminate the string
tokenized->stack[i].type=TOKEN_NUMBER;
}
//~~~Elementary Functions~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if (isalpha(*input) && (*input)!='x'){
char *start=input;
while(isalpha(*input)&&(*input)!='x')
input++;
char *end=input;
tokenized->stack[i].text=(char*)malloc((end - start +1)*sizeof(char));
strncpy( tokenized->stack[i].text, start, end-start);
tokenized->stack[i].text[end-start]='\0';
tokenized->stack[i].type=TOKEN_FUNCTION;
//input++;
}
//~~~Variable 'x'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if((*input)== 'x'){
tokenized->stack[i].text=(char*)malloc(2*sizeof(char));
tokenized->stack[i].text[0]='x';
tokenized->stack[i].text[1]='\0';
tokenized->stack[i].type=TOKEN_VARIABLE;
input++;
}
//~~~Operators~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if ((*input)=='+' ||(*input)=='-' ||(*input)=='*' ||(*input)=='/' ||(*input)=='^'){
tokenized->stack[i].text=(char*)malloc(2*sizeof(char));
tokenized->stack[i].text[0]=*input;
tokenized->stack[i].text[1]='\0';
tokenized->stack[i].type=TOKEN_OPERATOR;
input++;
}
//~~~Parantheses~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
else if((*input)=='(' || (*input)==')'){
tokenized->stack[i].text=(char*)malloc(2*sizeof(char));
tokenized->stack[i].text[0]=*input;
tokenized->stack[i].text[1]='\0';
tokenized->stack[i].type=TOKEN_PARENTHESES;
input++;
}
else{
printf("MALFORMED EXPRESSION");
exit(EXIT_FAILURE);
}
i++;
tokenized->top=i-1;
}
tokenized->stack=realloc(tokenized->stack,(tokenized->top+1)*sizeof(token));
tokenized->stackSize=tokenized->top+1;
return tokenized;
}