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