Institute of Engineering Pulchowk Campus A Report for C++ Project Functional Calculation Interpreter Submitted By: Prasanna Koirala (069/BCT/531) Neeraj Adhikari (069/BCT/527) Submitted To: Mr. Daya Sagar Baral Department of Electronics and Computer Engineering Acknowledgements We must first thank Mr Daya Sagar Baral, who is our lecturer on Object Oriented Programming with C++, and with whose supervision we are about to start the project. We believe that with his good guidance and supervision, the project will be successfully accomplished. We also would like to thank our friends for suggesting ideas and providing positive criticism for our project. And finally, we thank Bjarne Stroustrup for creating the powerful and efficient C++ programming language which started era of object-oriented programming and as a result of well-designed software. Abstract This report is to be presented to the department of Electronics and Computers Engineering, Pulchowk engineering campus. This report is written under the course of Object Oriented Programming. The target of this project is to implement the Programming concepts taught throughout the semester. This report has a full explanation of our project FCI (Functional Calculation Interpreter). The GUI is designed in GTKmm(GTK minus minus). GTKmm is the official c++ interface of the famous GUI library GTK+. It is cross platform. The expression parser uses a recursive descent algorithm. This means there are corresponding Functions for corresponding priority level of the operators. It somewhat form a parse tree like algorithm. Features of Object Oriented Programming, like classes, abstraction, encapsulation and data hiding are fully used in this project. Vectors play a handy role in the project as well, they are used to store the variables and the functions declared in the code. We also include some file handling in this project. We can read and write from and to the file. The project’s GUI is linked with the parser and function part. Input is taken from the GUI and sent to be parsed for the parser, the output that the parser return is again fed to the GUI’s output window. Table of contents 1. Objective 2. Introduction 3. Application 4. Literature Survey 5. Existing System 6. Methodology 7. Implementation 7.1 Block diagram 8. Results 9. Limitation and Future Enhancements 10. Conclusion and recommendation 11. References 1. Objectives The objectives of this software project are listed below: • To understand and apply concepts of object-oriented software design and programming to an actual project, and to reinforce the experience of writing modular and manageable software in the object-oriented language C++. • To create software that has applications for our personal and other types of use, and in that process learn about object-oriented programming concepts. • Another objective of the project is also that when it is finished, it will be able to provide a platform for day-to-day calculations and computations required by an engineering student, and also facilitate the writing of short and simple ‘programs’ in a simple way. • To help us understand more about the functional programming paradigm. Since we make a project that interprets this paradigm, we will know about their actual working mechanism. 2. Introduction In this age of computers and computation, there are a large number of programming languages that vary in many different ways. The programming language, C++ is an object-oriented and compiled language that is powerful and enhances the modularity and manageability of software. To explore the capacities and strengths of the C++ and its object-oriented style, we have decided to build an interpreter for a simple calculation and programming language. The language will not be strictly a programming language in the strict sense but more an evaluator of mathematical expressions with the ability to do computation. The language interpreted will be somewhat functional in its approach. By functional, we mean the concept of treating computation as the evaluation of expressions and functions, which is an approach inspired by lambda calculus. The language interpreted by the interpreter will not be a full-fledged language like C or C++ capable of being used to create big programs but instead will be more of an advanced calculator, with some features of programming languages. This is because the main task of the interpreter is to evaluate expressions. A simplification on the approach is that in this language, variables and functions are not treated as totally different things, but both are first-class entities. 3. Application This project will have a huge set of applications in engineering and mathematical field. It is especially helpful for us students when we need to repeatedly do specific types of calculations, and when a calculator is not powerful enough for the job and also writing a program and compiling it is cumbersome. This tool is the bridge between a calculator and a programming language. In this package, the user also gets some of the capabilities of a programming language but in a simple way, and also from the expression evaluation perspective. For example, if-else construct can be used in this language but it acts more like the ternary operator in C, because it is actually an expression. Looping is accomplished by recursive functions with a specified base case. Functions are supported that accept some arguments and return an output based on some operations onto them. Functions could be used together with if-else and recursion, this simple fact makes this tool very powerful. This tool is Turing complete. As this software meets the demands of common users and also provides a unique platform for complicated calculations. That is why this project is a good one from the utility point of view. 4. Literature Survey This section will cover the introduction to lambda calculus and functional programming. How they are related and what are the perks and losses of using functional programming. Lambda calculus, Source: Wikipedia Lambda calculus (also written as λ-calculus) is a formal system in mathematical logic and computer science for expressing computation based on function abstraction and application using variable binding and substitution. First formulated by Alonzo Church to formalize the concept of effective computability, lambda calculus found early successes in the area of computability theory. Lambda calculus is a conceptually simple universal model of computation (Turing showed in 1937 that Turing machines equaled the lambda calculus in expressiveness). The name derives from the Greek letter lambda (λ) used to denote binding a variable in a function. Functional Programming, Source: Wikipedia In computer science, functional programming is a programming paradigm, a style of building the structure and elements of computer programs, which treats computation as the evaluation of mathematical functions and avoids state and mutable data. Functional programming emphasizes functions that produce results that depend only on their inputs and not on the program state—i.e. pure mathematical functions. It is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) both times. Eliminating side effects, i.e. changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming. Functional programming paradigm uses lambda calculus. It treats everything as a function i.e. it doesn’t have any side-effects what so ever. A variable, for example is a function with no argument and constant output. This will help the user to link mathematics with programming in a more direct way. This makes the code short in terms of line numbers and more readable. It has some demerits considering the code writing. Writing a code in functional programming language is quite difficult in comparison to writing them in any other programming paradigm as the programmer needs to be very selective about what he writes. 5. Existing System To say that Functional Programming Languages like Haskell are the existing analogy to our system would not be completely true. It is not a full fledge functional programming interpreter but also not only a simple calculator. It is a unique kind of software that combines functionality of a calculator with that of a programming language to give something which is simple and sufficient for small yet more practical and needed tasks. Existing programs of this kind are not quite abundant but are rare. Most of the similar systems we encounter are either very basic calculators that only do simple operations or else advanced full-fledged programming languages which can do such kind of computations very efficiently. An example of such a system is Mathematica, which is a proprietary software developed by Wolfram, Inc. and also MATLAB or GNU Octave, which are primarily used for matrix operations and numerical operations. These systems are very efficient and complicated too. As we can write a program with this tool, a programmable calculator can be considered the most matching analogy for this tool. 6. Methodology The methods and ideas that will be used to implement this software are: • The GUI takes the input from the user, sends it to the expression parser and prints the result in the output section. • The expression parser converts the list of tokens obtained to a suitable form and evaluates the expression. Evaluating an expression generally consists of replacing the identifiers by their respective values and evaluation each part of the expression in a step-by-step fashion until the whole is evaluated. • General calculations that include addition, subtraction etc, are allowed. • Relational operators are allowed as well, as they are very important part of ifelse constructs. However, they could be used independently as well. • If-Else constructs are also allowed in the language, and they are also nothing but expressions, which are evaluated according to a condition. • Functions are allowed. The user may define any number (zero to sufficient) of parameters with in a function. • Recursion is allowed. There are many problems that require iteration. Iteration in functional programming is done through the help of recursion. • Functions along with if-else and recursion makes a complete package, and they are allowed as well. 7. Implementation The parser uses a recursive descent algorithm. This is a top-down process in which the parser attempts to verify that the syntax of the input stream is correct as it is read from left to right. A basic operation necessary for this involves reading characters from the input stream and matching then with terminals from the grammar that describes the syntax of the input. Our recursive descent parsers will look ahead one character and advance the input stream reading pointer when proper matches occur. The grammar for the parser is followed below: Expression [Assignment] [Rvalue] Assignment Lvalue = Rvalue Lvalue Functions (parameterized or non parameterized) Rvalue Part [relational-op] Part Part Term [+,-] Term Term Factor [*, /, %, ^] Factor Factor [- , +] Atom Atom Number, Function or Expression The functions are a different story. Every function has a name, parameters and definition. F(x) = x*x will define a function with name F parameter x and definition x*x. While evaluating, it will replace the definition with the parameter provided. And send the replaced definition to the parser to evaluate. 7.1 Block Diagram Expression GUI Tokenizer Definition Output Parser Error handling 8. Results The project was more or less as complete as mentioned in our proposal. Everything from GUI to parsing to function went successfully. Speaking roughly, our project was successful. There are some limitations though which are defined in the section following. 9. Limitations and Future Enhancement Our Functional Computation Interpreter is still incomplete. The limitations are as follows: • It lacks dynamic data types. That means we don’t have data types such as long, string, character and many more. We have only used a single data type that is double. Double can work on both integer as well as floating point number. So it was the most eligible tool we had in out hand. • A ‘if’ statement must have ‘then’ and ‘else’ statements. Simple ‘then’ only ‘if’ could have been written as well. Multiple else statement is not included in out project i.e. with ‘if’, only one ‘else’ could be written. We intend to enhance the project in the near future with the following features, • Every limitations mentioned above will be sorted out. • Graphs of the functions, their derivative and integrated values will be included in the future versions. 10. Conclusion and Recommendation This project was an extremely good adventure. We learned many things from this journey. We learned team work and different programming skills. In addition, we grasped the concepts of an important library i.e. GTKmm. And most of all, we successfully implemented our system to work with recursion. 11. References Secrets of Object Oriented programming by Er. D.S. Baral The art of c++ by Herbert Schildt www.Gtkmm.org www.Wikipedia.org Linux man pages. A lot of internet that had many useful websites in it. Every one of them are not possible to list down.