ProjetGIT_TP_Forth/LEXER/lexer.c
2020-10-01 16:27:02 +02:00

34 lines
681 B
C

#include "lexer.h"
int numberOfDelimiters(char *string)
{
int count = 0;
for (int i = 0; i < strlen(string); i++)
{
if (string[i] == ' ')
{
count++;
}
}
return count;
}
Programme *lexer(char *chaine)
{
char *token, *str;
str = strdup(chaine);
int i = 0;
int arraysize = (numberOfDelimiters(str) + 1);
char **programme = (char **)malloc(sizeof(char *) * arraysize);
while ((token = strsep(&str, " ")))
{
programme[i] = token;
i++;
}
Programme *retour = (Programme *)malloc(sizeof(Programme));
retour->tokens = programme;
retour->taille = i;
return retour;
}