Skip to content

Commit

Permalink
Started Parser for automatic calc
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasanchez committed Nov 12, 2020
1 parent 3466ba0 commit eb29624
Show file tree
Hide file tree
Showing 13 changed files with 1,091 additions and 20 deletions.
12 changes: 7 additions & 5 deletions 08-CalcInfAuto/auto/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
/*C - Code*/
#include "scanner.h"
int totalTokens_g;
bool allOK = true;
%}

%option noyywrap

%%
[0-9]+ { scanner_GetNextToken(yytext, OPERAND); totalTokens_g++;}
[a-zA-Z]+ { scanner_GetNextToken(yytext, OPERAND); totalTokens_g++;}
[+*-]+ { scanner_GetNextToken(yytext, OPERATOR); totalTokens_g++;}
\n { return 0;}
. { puts("Syntax Error, closing ..."); exit(-1);}
[0-9]+ { scanner_GetNextToken(atoi(yytext), OPERAND); totalTokens_g++;}
[a-zA-Z]+ { scanner_GetNextToken(*yytext, VARIABLE); totalTokens_g++;}
[+*-]+ { scanner_GetNextToken(*yytext, OPERATOR); totalTokens_g++;}
[(] { scanner_GetNextToken(**text, LBRACKET)}
[\n|<<EOF>>] { return 0;}
. { puts("Undefined Symbol: Exiting..."); exit(-1);}
%%
68 changes: 68 additions & 0 deletions 08-CalcInfAuto/inc/all.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* ---------------------------------------------------------------------------------
all.h
Infix Automatic Calculator header
MIT License
Copyright (c) 2020 Tomas Sanchez
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
last modified: 11/12/2020
------------------------------------------------------------------------------------ */

#pragma once

#include <stdio.h>
#include <string.h>

#include "stack.h"
#include "queue.h"

/* Verbose stdout */
#define VERBOSE 0
#define SCANNER 0
#define PARSER 0
#define SOLVER 0

/*Error handling*/
#define OK 0
#define ERROR -1


/*Defines size of scanner number buffer*/
#define buffer_size 32

/*Lexical ID*/
typedef enum TokenIDs{
OPERAND,
VARIABLE,
OPERATOR,
LBRACKET,
RBRACKET
}token_id_t;

/*Parsed tokens data struct*/
typedef struct pToken{
bool valid;
token_id_t type;
unsigned int value;
}ptoken_t;
186 changes: 186 additions & 0 deletions 08-CalcInfAuto/inc/list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/*
* Copyright (C) 2012 Sistemas Operativos - UTN FRBA. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef LIST_H_
#define LIST_H_

#include "node.h"
#include <stdbool.h>

typedef struct {
t_link_element *head;
int elements_count;
} t_list;

/**
* @NAME: list_create
* @DESC: Crea una lista
*/
t_list * list_create();

/**
* @NAME: list_destroy
* @DESC: Destruye una lista sin liberar
* los elementos contenidos en los nodos
*/
void list_destroy(t_list *);

/**
* @NAME: list_destroy_and_destroy_elements
* @DESC: Destruye una lista y sus elementos
*/
void list_destroy_and_destroy_elements(t_list*, void(*element_destroyer)(void*));

/**
* @NAME: list_add
* @DESC: Agrega un elemento al final de la lista
*/
int list_add(t_list *, void *element);

/**
* @NAME: list_add_in_index
* @DESC: Agrega un elemento en una posicion determinada de la lista
*/
void list_add_in_index(t_list *, int index, void *element);


/**
* @NAME: list_get
* @DESC: Retorna el contenido de una posicion determianda de la lista
*/
void *list_get(t_list *, int index);

/**
* @NAME: list_take
* @DESC: Retorna una nueva lista con
* los primeros n elementos
*/
t_list* list_take(t_list*, int count);

/**
* @NAME: list_take_and_remove
* @DESC: Retorna una nueva lista con
* los primeros n elementos, eliminando
* del origen estos elementos
*/
t_list* list_take_and_remove(t_list*, int count);


/**
* @NAME: list_replace
* @DESC: Coloca un elemento en una de la posiciones
* de la lista retornando el valor anterior
*/
void *list_replace(t_list*, int index, void* element);

/**
* @NAME: list_replace_and_destroy_element
* @DESC: Coloca un valor en una de la posiciones
* de la lista liberando el valor anterior
*/
void list_replace_and_destroy_element(t_list*, int index, void* element, void(*element_destroyer)(void*));

/**
* @NAME: list_remove
* @DESC: Remueve un elemento de la lista de
* una determinada posicion y lo retorna.
*/
void *list_remove(t_list *, int index);

/**
* @NAME: list_remove_and_destroy_element
* @DESC: Remueve un elemento de la lista de una
* determinada posicion y libera la memoria.
*/
void list_remove_and_destroy_element(t_list *, int index, void(*element_destroyer)(void*));

/**
* @NAME: list_remove_by_condition
* @DESC: Remueve el primer elemento de la lista
* que haga que condition devuelva != 0.
*/
void *list_remove_by_condition(t_list *, bool(*condition)(void*));

/**
* @NAME: list_remove_and_destroy_by_condition
* @DESC: Remueve y destruye el primer elemento de
* la lista que hagan que condition devuelva != 0.
*/
void list_remove_and_destroy_by_condition(t_list *, bool(*condition)(void*), void(*element_destroyer)(void*));

/**
* @NAME: list_clean
* @DESC: Quita todos los elementos de la lista.
*/
void list_clean(t_list *);

/**
* @NAME: list_clean
* @DESC: Quita y destruye todos los elementos de la lista
*/
void list_clean_and_destroy_elements(t_list *self, void(*element_destroyer)(void*));

/**
* @NAME: list_iterate
* @DESC: Itera la lista llamando al closure por cada elemento
*/
void list_iterate(t_list *, void(*closure)(void*));

/**
* @NAME: list_find
* @DESC: Retorna el primer valor encontrado, el cual haga que condition devuelva != 0
*/
void *list_find(t_list *, bool(*closure)(void*));

/**
* @NAME: list_size
* @DESC: Retorna el tamaño de la lista
*/
int list_size(t_list *);

/**
* @NAME: list_is_empty
* @DESC: Verifica si la lista esta vacia
*/
int list_is_empty(t_list *);

/**
* @NAME: list_sort
* @DESC: Ordena la lista segun el comparador
* El comparador devuelve si el primer parametro debe aparecer antes que el
* segundo en la lista
*/
void list_sort(t_list *, bool (*comparator)(void *, void *));

/**
* @NAME: list_sorted
* @DESC: Retorna una lista nueva ordenada segun el comparador
* El comparador devuelve si el primer parametro debe aparecer antes que el
* segundo en la lista
*/
t_list* list_sorted(t_list *, bool (*comparator)(void *, void *));

/**
* @NAME: list_fold
* @DESC: Devuelve un valor que resulta de aplicar la
* operacion entre todos los elementos de la lista, partiendo desde el primero.
*
* La funcion 'operation' debe recibir 2 dos valores, uno del tipo del valor initial (seed)
* y otro del tipo de los elementos de la lista.
*/
void* list_fold(t_list* self, void* seed, void*(*operation)(void*, void*));

#endif /*LIST_H_*/
42 changes: 42 additions & 0 deletions 08-CalcInfAuto/inc/node.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2012 Sistemas Operativos - UTN FRBA. All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef NODE_H_
#define NODE_H_


struct link_element{
void *data;
struct link_element *next;
};
typedef struct link_element t_link_element;

struct double_link_element{
struct double_link_element *previous;
void *data;
struct double_link_element *next;
};
typedef struct double_link_element t_double_link_element;

struct hash_element{
char *key;
unsigned int hashcode;
void *data;
struct hash_element *next;
};
typedef struct hash_element t_hash_element;

#endif /*NODE_H_*/
47 changes: 47 additions & 0 deletions 08-CalcInfAuto/inc/parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* ---------------------------------------------------------------------------------
parser.h
Manual Parser for Infix Calculator w/ Automatic Scanner.
MIT License
Copyright (c) 2020 Tomas Sanchez
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
last modified: 11/12/2020
------------------------------------------------------------------------------------ */

#pragma once
#include "all.h"

/*Object Parser*/
typedef struct Parser{
t_list * token_list;
ptoken_t * read_token;
token_id_t previous_token;
bool valid_expression;
}parser_t;

/*Starts oParser*/
int parser_create();

/*Deletes oParser freeing memory usage*/
int parser_delete();
Loading

0 comments on commit eb29624

Please sign in to comment.