From 0a28cfda01af50a13d9e04baf55061ba54d0909d Mon Sep 17 00:00:00 2001 From: kappa Date: Sun, 6 Jan 2019 21:13:13 +0100 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D1=99?= =?UTF-8?q?=D0=B5=D0=BD=20memory-leak=20=D0=B2=D0=B5=D0=B7=D0=B0=D0=BD=20?= =?UTF-8?q?=D0=B7=D0=B0=20=D0=BB=D0=B5=D0=BA=D1=81=D0=B5=D1=80=D1=81=D0=BA?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=B5=D0=BE=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80?= =?UTF-8?q?=D0=BF=D1=80=D0=B5=D1=82=D0=B5=D1=80=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 4 ++-- cirilisp.c | 7 +++---- lexer.c | 18 ++++++++++++++++++ lexer.h | 8 +++++--- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index 0aa1313..58d3b59 100644 --- a/Makefile +++ b/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 diff --git a/cirilisp.c b/cirilisp.c index f31fa38..b0b266b 100644 --- a/cirilisp.c +++ b/cirilisp.c @@ -27,18 +27,17 @@ int main(int argc, char **argv) } token *tokenList, *current; - + current = tokenList = lexLine(input); int i = 0; while (current != NULL) { - printf("Токен бр. %d: \"%s\", тип:%s\n", i, -current->lexeme, current->type == number ? "number" : (current->type == symbol -? "symbol" : "parenthesis")); + printf("Токен бр. %d: \"%s\", тип:%s\n", i, current->lexeme, current->type == number ? "number" : (current->type == symbol ? "symbol" : "parenthesis")); current = current->next; ++i; } + freeLexedLine(tokenList); free(input); } } diff --git a/lexer.c b/lexer.c index 6a8f847..2be3ab8 100644 --- a/lexer.c +++ b/lexer.c @@ -94,3 +94,21 @@ token *lex1Token(char *input, int *i) return result; } + +void freeLexedLine(token *list) +{ + if (list == NULL) + { + return; + } + else + { + freeLexedLine(list->next); + if (list->lexeme != NULL) + { + free(list->lexeme); + } + free(list); + return; + } +} diff --git a/lexer.h b/lexer.h index 944f869..f218a0b 100644 --- a/lexer.h +++ b/lexer.h @@ -6,7 +6,7 @@ typedef enum number, symbol, parenthesis -} tokenType ; +} tokenType; typedef struct _Token { @@ -15,6 +15,8 @@ typedef struct _Token struct _Token *next; } token; -/* функција lexLine као аргумент добија ред са стандардног улаза, а као излаз -враћа лексичке елементе у повезаној листи */ token *lexLine(char *input); +/* као аргумент добија ред са стандардног улаза, а као излаз +враћа лексичке елементе у повезаној листи */ +void freeLexedLine(token *list); +/* ослобађа меморију коју је заузела листа токена узета из корисничког улаза */