Skip to content

Commit

Permalink
Finished calculator
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasanchez committed Nov 14, 2020
1 parent 5d7fefa commit caf0edd
Show file tree
Hide file tree
Showing 11 changed files with 491 additions and 75 deletions.
1 change: 1 addition & 0 deletions 08-CalcInfAuto/auto/scanner.l
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bool allOK = true;
[a-zA-Z]+ { scanner_GetNextToken(*yytext, VARIABLE); totalTokens_g++;}
[+*-]+ { scanner_GetNextToken(*yytext, OPERATOR); totalTokens_g++;}
[(] { scanner_GetNextToken(*yytext, LBRACKET); totalTokens_g++;}
[)] { scanner_GetNextToken(*yytext, RBRACKET); totalTokens_g++;}
[\n|<<EOF>>] { return 0;}
. { puts("Undefined Symbol: Exiting..."); exit(-1);}
%%
5 changes: 0 additions & 5 deletions 08-CalcInfAuto/inc/Calc.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@

#include "scanner.h"

/*Calls Flex Scanner*/
int yylex();

/*Calculator Functions*/

/*Show welcome message*/
int calculator_create();

Expand Down
5 changes: 4 additions & 1 deletion 08-CalcInfAuto/inc/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ int parser_delete();
bool parser_GetNextToken(int , token_id_t);

/*Prints all token parsed*/
int parser_print_results();
int parser_print_results();

/*Tells if parse has error*/
bool parser_has_error();
5 changes: 4 additions & 1 deletion 08-CalcInfAuto/inc/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@
------------------------------------------------------------------------------------ */

#pragma once
#include "parser.h"
#include "solver.h"

/*Calls Flex Scanner*/
int yylex();

/*Loads new token from scanner buffer*/
int scanner_GetNextToken(int , token_id_t);
67 changes: 67 additions & 0 deletions 08-CalcInfAuto/inc/solver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/* ---------------------------------------------------------------------------------
solver.h
solver for infix automatic-scanner calculator.
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/13/2020
------------------------------------------------------------------------------------ */

#pragma once

#include "parser.h"

/*Token handler*/
typedef struct Token{
int token;
token_id_t type;
}token_t;

/*Object that can solve expressions*/
typedef struct Solver{
/*Current token buffered*/
token_t * token;
/*Queue*/
t_queue * output_queue;
/*Stack*/
stack_t * operator_stack;
/*Value obtained after all calculations were applied*/
int final_result;
}solver_t;

/*Creates a solver*/
int solver_create();

/*Frees memory usage form solver*/
int solver_delete();

/*Stores next token*/
int solver_GetNextToken(int, token_id_t);

/*Solves expression*/
int solver_update();

/*Print results*/
int solver_print();
44 changes: 22 additions & 22 deletions 08-CalcInfAuto/leaks.log
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
==2575== Memcheck, a memory error detector
==2575== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2575== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2575== Command: ./Calculator.exe
==2575== Parent PID: 2555
==2575==
==2575==
==2575== HEAP SUMMARY:
==2575== in use at exit: 16,482 bytes in 4 blocks
==2575== total heap usage: 14 allocs, 10 frees, 18,642 bytes allocated
==2575==
==2575== LEAK SUMMARY:
==2575== definitely lost: 0 bytes in 0 blocks
==2575== indirectly lost: 0 bytes in 0 blocks
==2575== possibly lost: 0 bytes in 0 blocks
==2575== still reachable: 16,482 bytes in 4 blocks
==2575== suppressed: 0 bytes in 0 blocks
==2575== Reachable blocks (those to which a pointer was found) are not shown.
==2575== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==2575==
==2575== For counts of detected and suppressed errors, rerun with: -v
==2575== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
==1772== Memcheck, a memory error detector
==1772== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==1772== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==1772== Command: ./Calculator.exe
==1772== Parent PID: 1750
==1772==
==1772==
==1772== HEAP SUMMARY:
==1772== in use at exit: 16,482 bytes in 4 blocks
==1772== total heap usage: 29 allocs, 25 frees, 18,866 bytes allocated
==1772==
==1772== LEAK SUMMARY:
==1772== definitely lost: 0 bytes in 0 blocks
==1772== indirectly lost: 0 bytes in 0 blocks
==1772== possibly lost: 0 bytes in 0 blocks
==1772== still reachable: 16,482 bytes in 4 blocks
==1772== suppressed: 0 bytes in 0 blocks
==1772== Reachable blocks (those to which a pointer was found) are not shown.
==1772== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==1772==
==1772== For counts of detected and suppressed errors, rerun with: -v
==1772== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
6 changes: 6 additions & 0 deletions 08-CalcInfAuto/src/Calc.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
int calculator_create(){

parser_create();
solver_create();

return puts(" :: == A Simple Infix Calculator With Flex-Generated Scanner == :: ");
}
Expand All @@ -45,10 +46,15 @@ int calculator_read(){

int calculator_update(){
parser_print_results();
if(! parser_has_error()){
solver_update();
solver_print();
}
return OK;
}

int calculator_delete(){
parser_delete();
solver_delete();
return puts("\n- :: © 2020 TOMAS SANCHEZ - <tosanchez@est.frba.utn.edu.ar> | :: | All rights reserved :: -");
}
91 changes: 48 additions & 43 deletions 08-CalcInfAuto/src/lex.yy.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,19 +372,19 @@ static void yynoreturn yy_fatal_error ( const char* msg );
YY_FATAL_ERROR( "token too large, exceeds YYLMAX" ); \
yy_flex_strncpy( yytext, (yytext_ptr), yyleng + 1 ); \
(yy_c_buf_p) = yy_cp;
#define YY_NUM_RULES 7
#define YY_END_OF_BUFFER 8
#define YY_NUM_RULES 8
#define YY_END_OF_BUFFER 9
/* This struct is not used in this scanner,
but its presence is necessary. */
struct yy_trans_info
{
flex_int32_t yy_verify;
flex_int32_t yy_nxt;
};
static const flex_int16_t yy_accept[16] =
static const flex_int16_t yy_accept[17] =
{ 0,
0, 0, 8, 6, 5, 4, 3, 1, 5, 2,
2, 3, 1, 2, 0
0, 0, 9, 7, 6, 4, 5, 3, 1, 6,
2, 2, 3, 1, 2, 0
} ;

static const YY_CHAR yy_ec[256] =
Expand All @@ -393,16 +393,16 @@ static const YY_CHAR yy_ec[256] =
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 3,
1, 4, 4, 1, 4, 1, 1, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 1, 1, 6,
1, 6, 1, 1, 7, 7, 7, 7, 8, 8,
7, 7, 7, 7, 7, 7, 7, 7, 8, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
1, 1, 1, 1, 1, 1, 7, 7, 7, 7,

7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, 7, 1, 6, 1, 1, 1, 1, 1, 1,
4, 5, 5, 1, 5, 1, 1, 6, 6, 6,
6, 6, 6, 6, 6, 6, 6, 1, 1, 7,
1, 7, 1, 1, 8, 8, 8, 8, 9, 9,
8, 8, 8, 8, 8, 8, 8, 8, 9, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
1, 1, 1, 1, 1, 1, 8, 8, 8, 8,

8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, 8, 1, 7, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Expand All @@ -419,41 +419,41 @@ static const YY_CHAR yy_ec[256] =
1, 1, 1, 1, 1
} ;

static const YY_CHAR yy_meta[9] =
static const YY_CHAR yy_meta[10] =
{ 0,
1, 1, 1, 1, 1, 1, 2, 2
1, 1, 1, 1, 1, 1, 1, 2, 2
} ;

static const flex_int16_t yy_base[17] =
static const flex_int16_t yy_base[18] =
{ 0,
0, 0, 14, 15, 15, 15, 9, 7, 15, 0,
0, 7, 5, 0, 15, 7
0, 0, 15, 16, 16, 16, 16, 9, 7, 16,
0, 0, 7, 5, 0, 16, 8
} ;

static const flex_int16_t yy_def[17] =
static const flex_int16_t yy_def[18] =
{ 0,
15, 1, 15, 15, 15, 15, 15, 15, 15, 16,
16, 15, 15, 16, 0, 15
16, 1, 16, 16, 16, 16, 16, 16, 16, 16,
17, 17, 16, 16, 17, 0, 16
} ;

static const flex_int16_t yy_nxt[24] =
static const flex_int16_t yy_nxt[26] =
{ 0,
4, 5, 6, 7, 8, 9, 10, 11, 14, 13,
12, 13, 12, 15, 3, 15, 15, 15, 15, 15,
15, 15, 15
4, 5, 6, 7, 8, 9, 10, 11, 12, 15,
14, 13, 14, 13, 16, 3, 16, 16, 16, 16,
16, 16, 16, 16, 16
} ;

static const flex_int16_t yy_chk[24] =
static const flex_int16_t yy_chk[26] =
{ 0,
1, 1, 1, 1, 1, 1, 1, 1, 16, 13,
12, 8, 7, 3, 15, 15, 15, 15, 15, 15,
15, 15, 15
1, 1, 1, 1, 1, 1, 1, 1, 1, 17,
14, 13, 9, 8, 3, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16
} ;

/* Table of booleans, true if rule could match eol. */
static const flex_int32_t yy_rule_can_match_eol[8] =
static const flex_int32_t yy_rule_can_match_eol[9] =
{ 0,
0, 0, 0, 0, 1, 0, 0, };
0, 0, 0, 0, 0, 1, 0, 0, };

static yy_state_type yy_last_accepting_state;
static char *yy_last_accepting_cpos;
Expand Down Expand Up @@ -727,13 +727,13 @@ YY_DECL
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 16 )
if ( yy_current_state >= 17 )
yy_c = yy_meta[yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
++yy_cp;
}
while ( yy_base[yy_current_state] != 15 );
while ( yy_base[yy_current_state] != 16 );

yy_find_action:
yy_act = yy_accept[yy_current_state];
Expand Down Expand Up @@ -788,22 +788,27 @@ YY_RULE_SETUP
{ scanner_GetNextToken(*yytext, LBRACKET); totalTokens_g++;}
YY_BREAK
case 5:
/* rule 5 can match eol */
YY_RULE_SETUP
#line 15 "auto/scanner.l"
{ return 0;}
{ scanner_GetNextToken(*yytext, RBRACKET); totalTokens_g++;}
YY_BREAK
case 6:
/* rule 6 can match eol */
YY_RULE_SETUP
#line 16 "auto/scanner.l"
{ puts("Undefined Symbol: Exiting..."); exit(-1);}
{ return 0;}
YY_BREAK
case 7:
YY_RULE_SETUP
#line 17 "auto/scanner.l"
{ puts("Undefined Symbol: Exiting..."); exit(-1);}
YY_BREAK
case 8:
YY_RULE_SETUP
#line 18 "auto/scanner.l"
ECHO;
YY_BREAK
#line 807 "lex.yy.c"
#line 812 "lex.yy.c"
case YY_STATE_EOF(INITIAL):
yyterminate();

Expand Down Expand Up @@ -1100,7 +1105,7 @@ static int yy_get_next_buffer (void)
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 16 )
if ( yy_current_state >= 17 )
yy_c = yy_meta[yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
Expand Down Expand Up @@ -1128,11 +1133,11 @@ static int yy_get_next_buffer (void)
while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
{
yy_current_state = (int) yy_def[yy_current_state];
if ( yy_current_state >= 16 )
if ( yy_current_state >= 17 )
yy_c = yy_meta[yy_c];
}
yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
yy_is_jam = (yy_current_state == 15);
yy_is_jam = (yy_current_state == 16);

return yy_is_jam ? 0 : yy_current_state;
}
Expand Down Expand Up @@ -1818,6 +1823,6 @@ void yyfree (void * ptr )
free( (char *) ptr ); /* see yyrealloc() for (char *) cast */
}

#line 17 "auto/scanner.l"
#line 18 "auto/scanner.l"

4 changes: 2 additions & 2 deletions 08-CalcInfAuto/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ bool __token_is_valid__(token_id_t new, token_id_t previous){
}
}

bool expression_is_valid(){
return lBrackets == rBrackets;
bool parser_has_error(){
return !oParser->valid_expression;
}

int parser_print_results(){
Expand Down
4 changes: 3 additions & 1 deletion 08-CalcInfAuto/src/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
#include "../inc/scanner.h"

int scanner_GetNextToken(int value, token_id_t type){
parser_GetNextToken(value, type);
if(parser_GetNextToken(value, type))
solver_GetNextToken(value, type);
return OK;
}
Loading

0 comments on commit caf0edd

Please sign in to comment.