├── .gitignore ├── README.md ├── calculator ├── calculator.l ├── calculator.tab.h └── calculator.y /.gitignore: -------------------------------------------------------------------------------- 1 | **/*.c -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Linux-Terminal-Calculator 2 | This project, compiled in Kali Linux terminal, offers basic math functions. Designed with Lex and Yacc tools, it facilitates arithmetic operations, including addition, subtraction, multiplication, division, and modulus. 3 | 4 | 5 | ## Table of Contents 📚 6 | # Table of Contents 📚 7 | - [Overview](https://github.com/NisalWick2002/Linux-Terminal-Calculator?tab=readme-ov-file#overview-) 8 | - [Features](https://github.com/NisalWick2002/Linux-Terminal-Calculator?tab=readme-ov-file#features-%EF%B8%8F) 9 | - [Steps](https://github.com/NisalWick2002/Linux-Terminal-Calculator?tab=readme-ov-file#Steps-%EF%B8%8F) 10 | 11 | ## Overview 📋 12 | Overview: 13 | The Linux Terminal Calculator is a command-line calculator designed to provide users with a simple yet powerful mathematical tool within the Linux environment. Developed using Lex and Yacc tools, it supports a wide range of arithmetic operations, including addition, subtraction, multiplication, division, and modulus calculations. 14 | 15 | ## Features ⚙️ 16 | Basic arithmetic operations: Addition, subtraction, multiplication, division. 17 | Modulus calculations. 18 | Command-line interface for ease of use. 19 | Compiled using Lex and Yacc tools in the Kali Linux terminal. 20 | Designed to offer a seamless mathematical experience within the Linux Terminal environment. 21 | 22 | ## Steps 🛠️ 23 | 1. Download and Unzip: Download the zip file from the GitHub repository and extract it to a convenient location on your Linux system 24 | 2. Open Terminal: Open the terminal and navigate to the directory where you extracted the downloaded folder. 25 | 3. Compile Lexer and Parser: Execute the following commands in the terminal: 26 | flex calculator.l 27 | bison -d calculator.y 28 | gcc -o calculator lex.yy.c calculator.tab.c -lfl -lm 29 | 4. Run Calculator: Launch the calculator by typing the following command in the terminal: 30 | ./calculator 31 | 5. Perform Calculations: Input your mathematical expressions at the command prompt and press Enter to see the results. 32 | -------------------------------------------------------------------------------- /calculator: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NisalWick2002/Linux-Terminal-Calculator/13f12cbd5af03b0ce64e4b66428bbf7bbedb2c7b/calculator -------------------------------------------------------------------------------- /calculator.l: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include "calculator.tab.h" 4 | extern int yylval; 5 | extern int yylex(); 6 | %} 7 | 8 | %% 9 | 10 | [0-9]+ { 11 | yylval = atoi(yytext); 12 | return NUMBER; 13 | } 14 | 15 | [\t] return 0; 16 | [\n] return 0; 17 | . return yytext[0]; 18 | 19 | %% 20 | 21 | int yywrap() { 22 | return 1; 23 | } 24 | -------------------------------------------------------------------------------- /calculator.tab.h: -------------------------------------------------------------------------------- 1 | /* A Bison parser, made by GNU Bison 3.8.2. */ 2 | 3 | /* Bison interface for Yacc-like parsers in C 4 | 5 | Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2021 Free Software Foundation, 6 | Inc. 7 | 8 | This program is free software: you can redistribute it and/or modify 9 | it under the terms of the GNU General Public License as published by 10 | the Free Software Foundation, either version 3 of the License, or 11 | (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 | GNU General Public License for more details. 17 | 18 | You should have received a copy of the GNU General Public License 19 | along with this program. If not, see . */ 20 | 21 | /* As a special exception, you may create a larger work that contains 22 | part or all of the Bison parser skeleton and distribute that work 23 | under terms of your choice, so long as that work isn't itself a 24 | parser generator using the skeleton or a modified version thereof 25 | as a parser skeleton. Alternatively, if you modify or redistribute 26 | the parser skeleton itself, you may (at your option) remove this 27 | special exception, which will cause the skeleton and the resulting 28 | Bison output files to be licensed under the GNU General Public 29 | License without this special exception. 30 | 31 | This special exception was added by the Free Software Foundation in 32 | version 2.2 of Bison. */ 33 | 34 | /* DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual, 35 | especially those whose name start with YY_ or yy_. They are 36 | private implementation details that can be changed or removed. */ 37 | 38 | #ifndef YY_YY_CALCULATOR_TAB_H_INCLUDED 39 | # define YY_YY_CALCULATOR_TAB_H_INCLUDED 40 | /* Debug traces. */ 41 | #ifndef YYDEBUG 42 | # define YYDEBUG 0 43 | #endif 44 | #if YYDEBUG 45 | extern int yydebug; 46 | #endif 47 | 48 | /* Token kinds. */ 49 | #ifndef YYTOKENTYPE 50 | # define YYTOKENTYPE 51 | enum yytokentype 52 | { 53 | YYEMPTY = -2, 54 | YYEOF = 0, /* "end of file" */ 55 | YYerror = 256, /* error */ 56 | YYUNDEF = 257, /* "invalid token" */ 57 | NUMBER = 258 /* NUMBER */ 58 | }; 59 | typedef enum yytokentype yytoken_kind_t; 60 | #endif 61 | 62 | /* Value type. */ 63 | #if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED 64 | typedef int YYSTYPE; 65 | # define YYSTYPE_IS_TRIVIAL 1 66 | # define YYSTYPE_IS_DECLARED 1 67 | #endif 68 | 69 | 70 | extern YYSTYPE yylval; 71 | 72 | 73 | int yyparse (void); 74 | 75 | 76 | #endif /* !YY_YY_CALCULATOR_TAB_H_INCLUDED */ 77 | -------------------------------------------------------------------------------- /calculator.y: -------------------------------------------------------------------------------- 1 | %{ 2 | #include 3 | #include "calculator.tab.h" 4 | 5 | extern int yylex(); 6 | void yyerror(const char *s); 7 | %} 8 | 9 | %token NUMBER 10 | %left '+' '-' 11 | %left '*' '/' '%' 12 | %left '(' ')' 13 | 14 | %% 15 | 16 | ArithmeticExpression: E { 17 | printf("\nResult=%d\n", $$); 18 | return 0; 19 | }; 20 | 21 | E: E '+' E { $$ = $1 + $3; } 22 | | E '-' E { $$ = $1 - $3; } 23 | | E '*' E { $$ = $1 * $3; } 24 | | E '/' E { $$ = $1 / $3; } 25 | | E '%' E { $$ = $1 % $3; } 26 | | '(' E ')' { $$ = $2; } 27 | | NUMBER { $$ = $1; } 28 | ; 29 | 30 | %% 31 | 32 | int main() { 33 | yyparse(); 34 | return 0; 35 | } 36 | 37 | void yyerror(const char *s) { 38 | printf("\nError: %s\n", s); 39 | } 40 | --------------------------------------------------------------------------------