Интерпретер само лескира основне математичке операције
This commit is contained in:
parent
8d89aa622a
commit
e560ef8a3c
4
Makefile
4
Makefile
|
@ -7,8 +7,8 @@ VERSION = 0.0
|
|||
PREFIX = /usr/local
|
||||
|
||||
# флегови за C компајлер и линкер
|
||||
# CFLAGS = -g -std=c99 -pedantic -Wall -O0
|
||||
CFLAGS = -std=c99 -pedantic -Wall -O1
|
||||
CFLAGS = -g -std=c99 -pedantic -Wall -O0
|
||||
# CFLAGS = -std=c99 -pedantic -Wall -O1
|
||||
LDFLAGS = -lm -lc
|
||||
|
||||
CC = cc
|
||||
|
|
|
@ -10,7 +10,8 @@ int main(int argc, char **argv)
|
|||
/* Омогућава библиотекама коришћеним у интерпретеру да протумаче српску ћирилицу */
|
||||
if (setlocale(LC_ALL, "sr_RS.utf8") == NULL)
|
||||
{
|
||||
fprintf(stderr, "locale couldn't be set to \"sr_RS.utf8\", check if you've enabled it on your system\n");
|
||||
fprintf(stderr, "locale couldn't be set to \"sr_RS.utf8\",\
|
||||
check if you've enabled it on your system\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
|
@ -26,6 +27,7 @@ int main(int argc, char **argv)
|
|||
}
|
||||
|
||||
token *tokenList, *current;
|
||||
|
||||
current = tokenList = lexLine(input);
|
||||
int i = 0;
|
||||
while (current != NULL)
|
||||
|
@ -33,6 +35,8 @@ int main(int argc, char **argv)
|
|||
printf("Токен бр. %d: \"%s\", тип:%s\n", i,
|
||||
current->lexeme, current->type == number ? "number" : (current->type == symbol
|
||||
? "symbol" : "parenthesis"));
|
||||
current = current->next;
|
||||
++i;
|
||||
}
|
||||
|
||||
free(input);
|
||||
|
|
13
lexer.c
13
lexer.c
|
@ -5,7 +5,7 @@
|
|||
|
||||
#include "lexer.h"
|
||||
|
||||
token *lex1token(char *input, int *i);
|
||||
token *lex1Token(char *input, int *i);
|
||||
/* враћа показивач на једну token структуру, која означава један одређен токен,
|
||||
чита улазни стринг од i-тог карактера, и мења i тако да оно затим индексира
|
||||
следећи токен или крај стринга*/
|
||||
|
@ -18,8 +18,7 @@ token *lexLine(char *input)
|
|||
new = &root;
|
||||
while (i < n)
|
||||
{
|
||||
*new = lex1token(input, &i);
|
||||
new = &((*new)->next);
|
||||
*new = lex1Token(input, &i);
|
||||
if ((*new)->type == undefined)
|
||||
{
|
||||
/* уколико се у реду нађе токен који је лексички погрешан, штампа се место тог
|
||||
|
@ -35,19 +34,20 @@ token *lexLine(char *input)
|
|||
}
|
||||
return NULL;
|
||||
}
|
||||
new = &((*new)->next);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
regex_t regNumber, regSymbol, regParenthesis, regSpace;
|
||||
|
||||
token *lex1token(char *input, int *i)
|
||||
token *lex1Token(char *input, int *i)
|
||||
{
|
||||
token *result = malloc(sizeof(token));
|
||||
result->next = NULL;
|
||||
|
||||
regcomp(®Space, "^[:space:]*", REG_EXTENDED);
|
||||
regcomp(®Number, "^[-+]?[:digit:]+", REG_EXTENDED);
|
||||
regcomp(®Space, "^[[:space:]]*", REG_EXTENDED);
|
||||
regcomp(®Number, "^[-+]?[[:digit:]]+", REG_EXTENDED);
|
||||
regcomp(®Symbol, "^[-+/*]", REG_EXTENDED);
|
||||
/* за сада подржава само симболе -, +, * и / */
|
||||
regcomp(®Parenthesis, "^[()]", REG_EXTENDED);
|
||||
|
@ -74,6 +74,7 @@ token *lex1token(char *input, int *i)
|
|||
else
|
||||
{
|
||||
result->type = undefined;
|
||||
result->lexeme = NULL;
|
||||
goto skipStringCopy;
|
||||
}
|
||||
result->lexeme = malloc((a[0].rm_eo + 1) * sizeof(char));
|
||||
|
|
Loading…
Reference in a new issue