Implementation of a simple calculator using flex and bison

Similar documents
Macro Programming Reference Guide. Copyright 2005 Scott Martinez

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

Introduction to C Language

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

JAVA OPERATORS GENERAL

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

A Bison Manual. You build a text file of the production (format in the next section); traditionally this file ends in.y, although bison doesn t care.

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Lexical and Syntax Analysis

Arithmetic and Logic Blocks

Introduction to Lex & Yacc. (flex & bison)

C++ Programming: From Problem Analysis to Program Design, Third Edition

Operators in C. Staff Incharge: S.Sasirekha

GAS Tutorial - 6. Expression

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Scheme Quick Reference

Informatics Ingeniería en Electrónica y Automática Industrial

Lexical Considerations

Programming Project II

Scheme Quick Reference

Introduction to Programming with RAPTOR

A flow chart is a graphical or symbolic representation of a process.

flex is not a bad tool to use for doing modest text transformations and for programs that collect statistics on input.

Mentor Graphics Predefined Packages

Operators. Java operators are classified into three categories:

Language Reference Manual simplicity

JUN / 04 VERSION 7.0

SECTION II: LANGUAGE BASICS

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Basic types and definitions. Chapter 3 of Thompson

Computers Programming Course 6. Iulian Năstac

C Functions. 5.2 Program Modules in C

UNIT- 3 Introduction to C++

Syntax-Directed Translation. Introduction

2.2 Syntax Definition

CS-201 Introduction to Programming with Java

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Compiler Lab. Introduction to tools Lex and Yacc

Single row numeric functions

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

A simple syntax-directed

Review of the C Programming Language for Principles of Operating Systems

Chapter 20: Binary Trees

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Course Outline Introduction to C-Programming

CSCE 110 PROGRAMMING FUNDAMENTALS

The Graphing Calculator

DEPARTMENT OF MATHS, MJ COLLEGE

A Simple Syntax-Directed Translator

Industrial Automation course

Cooking flex with Perl

PHPoC vs PHP > Overview. Overview

Chapter 3: Operators, Expressions and Type Conversion

TDDD55 - Compilers and Interpreters Lesson 3

13 File Structures. Source: Foundations of Computer Science Cengage Learning. Objectives After studying this chapter, the student should be able to:

1001ICT Introduction To Programming Lecture Notes

Functions. Systems Programming Concepts

Parsing CSCI-400. Principles of Programming Languages.

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Review of the C Programming Language

PHPoC. PHPoC vs PHP. Version 1.1. Sollae Systems Co., Ttd. PHPoC Forum: Homepage:

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

TDDD55- Compilers and Interpreters Lesson 3

Programming refresher and intro to C programming

Prof. Navrati Saxena TA: Rochak Sachan

How to Design Programs Languages

Have examined process Creating program Have developed program Written in C Source code

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Introduction to Programming

CS52 - Assignment 8. Due Friday 4/15 at 5:00pm.

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 4

COMP322 - Introduction to C++ Lecture 02 - Basics of C++

MYSQL NUMERIC FUNCTIONS

Compiler Design (40-414)

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

1 Lexical Considerations

Project 1: Scheme Pretty-Printer

Syntax-Directed Translation. Lecture 14

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Creating a C++ Program

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Math 2250 MATLAB TUTORIAL Fall 2005

6.096 Introduction to C++ January (IAP) 2009

1. The output of lexical analyser is a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

COMPILERS AND INTERPRETERS Lesson 4 TDDD16

Introduction to Parsing. Lecture 5

UNIT 3 OPERATORS. [Marks- 12]

Variables. location where in memory is the information stored type what sort of information is stored in that memory

3+2 3*2 3/2 3^2 3**2 In matlab, use ^ or ** for exponentiation. In fortran, use only ** not ^ VARIABLES LECTURE 1: ARITHMETIC AND FUNCTIONS

EDIABAS BEST/2 LANGUAGE DESCRIPTION. VERSION 6b. Electronic Diagnostic Basic System EDIABAS - BEST/2 LANGUAGE DESCRIPTION

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Short Notes of CS201

CSC 467 Lecture 13-14: Semantic Analysis

Bits, Words, and Integers

ISA 563 : Fundamentals of Systems Programming

Yacc: A Syntactic Analysers Generator

Arithmetic and Bitwise Operations on Binary Data

Transcription:

Implementation of a simple calculator using flex and bison Somasundaram Meiyappan Abstract: A simple calculator program is created using the compiler tools flex and bison - and using the C programming language. The calculator takes is simple numerical expressions as input and evaluate them to give the result of the evaluation to the user. Apart from providing some standard mathematical functions and capabilities, this implementation also lets the user to define functions that will extend the built-in capabilities of the calculator. It also generates vcg files for all trees that the program constructs giving the user a glimpse of the internals. Description: This assignment is about creating a simple calculator using flex and bison with C as the programming language. This implementation of the assignment can take in simple numerical expressions, evaluate them and give the result of the evaluation to the user. This calculator provides the user with some standard mathematical and logical operators and some standard builtin functions. The user can define his/her own functions that use these capabilities and can pass them normally just as the other expressions are entered or as files in the command line itself. These functions have to defined in the language of the calculator and will be described shortly. The program can also used to generate vcg files for all trees that it constructs giving the user a glimpse of the internals. These trees are used when evaluating an expression or function. These vcg files can be viewed using graph visualizing tools like xvcg or aisee (after changing the extension to gdl). Here is an overview of the features: 1. Standard mathematical operators like +, -, * and / that takes in two expressions with one on the left and the other on the right. eg: 4+2, 4 + 6 * 5 2..mod. operator that gives the user the remainder of a division operation. Eg.: 17.mod. 3 gives the result 2 and 17.mod. 4 gives the result 1 3. Bitwise operators -.and.,.or.,.xor. and.not. representing and, or, xor and not operations respectively. The not operation computes the complement of the input integer and operates on a 8-byte long integer. Eg.: 15.and. 3 gives 3 as the result. 4..rshift. and.lshift. operators can be used to right shift or left shift the left-hand side operand by a fixed number of bits defined by the right-hand side operand. 5. Common logical operators like <, >, <=, >=, ==,!= and! are also supported with an integer value 1 representing true and 0 representing false. This can be used to execute some expressions in a function conditionally just like predicated instructions. Eg.: cond(x,y)=(x)*y*10+(!(x)*y*100) returns 100 times y when x is zero and 10 times y when x is non-zero. 6. sin, cos, tan and their inverse functions asin, acos, atan- are supported as well. Sinusoidal functions assume that the inputs are in radians and the inverse sinusoidal functions give the output in radians as well. 7. Other functions like log, ln, ceil, floor are also supported. All built-in functions take only one input parameter; but this is not a limitation of the implementation and just that built-in functions that take in more than one parameter are not available. 8. x^y to represent x raised to the power y. eg.: 3^2 gives the result 9

9. x gives the absolute value of x. eg.: -3 gives the result 3. 10. All expressions are evaluated are evaluated from left to right with +, - having a lower precedence than the other operators. Eg.: 2 3 * 4 gives the result 10. 11. Parenthesis can be used to force the interpreter to evaluate that expression prior to executing the ones outside it. Eg.: (2-3)*4 gives the result -4. 12. User can define functions using the above operators and functions like in the following example. f(x,y,z)=2*sin(x)*cos(y)+ x-pi/4 +tan(z+pi/4) 13. Generate VCG files representing all the user-defined functions and expression defined/evaluated. This is used to show the user the internal representation of the various expressions. This is accomplished using the -g option when starting the program in the command line. 14. Comments can be enclosed within /*. */ following C style comments 15. If an expression or a function definition takes more than a line, the user can optionally use \ to signal the interpreter to use the next line as a part of this line. 16. Usage: mycalc.exe [-Lfilename -g] -Lfilename: Used to add new functions to the function library -g: Writes.vcg files of all functions and expressions Code organization and system diagram: The following diagram gives the overall organization of the program. Front end (calc_frontend.c) Lexical analyser (calc.l) Parser (calc.y) Tree Builder (calc_treebuilder.c) Execution Engine (calc_runtime.c) This section describes the code organization. lex_src : contains the *.l files that are inputs to flex, the lexical analyzer generator o calc.l yacc_src: contains the *.y files that are input to bison, the parser generator o calc.y src: contains the *.c source files o calc_runtime.c

o calc_treebuilder.c o calc_frontend.c inc: contains the *.h header files o more_types.h o calc_struct.h o calc_operators.h o calc_treebuilder.h Lexical analyser and Parser: The lexical analyzer, that is generated using flex, emits tokens to the parser for use in its grammar. The lexical analyzer uses some services from the calc_frontend.c file to get some tokens. For example: the function isthisuserdefinedfunction searches the function table, that has the list of all functions defined by the user, and tells the parser that this is a user-defined function and also passes the unique index of the function. Another example is the isthisanoperator function, which not only tells the lexer that the passed in string is an operator, but also gives the token of that corresponds to that operator. Some unrecognized stream of input are reported as error directly by the lexical analyzer whereas some other unrecognized characters are passed to the parser which signals that we have an error. This file also hosts the main function. The parser, that is generated using bison, uses the tokens from the lexical analyzer in reducing the various productions that are in grammar of the language accepted by the program. The parser uses the services provided by the calc_treebuilder.c file to build its parse-trees that are later used in execution. All allocations are kept track of locally and the memory is released either on successful execution or on error conditions. In the case of user-defined functions, the memory is not released as the functions are intended to be visible for this entire instance of the program. Trees involved and evaluation of expressions: The parser generates trees for all expressions and then reduces them following certain rules to evaluate them. Parse trees are also generated for user-defined functions and parameters are passed into these trees at appropriate places for evaluating the function. Examples of the internal trees and expressions are given below. The trees are evaluated by post-order traversal of the tree. In the last tree below, g(0,0,0) is represented with g as the parent of three children, one corresponding to each parameter. The edge, with label 1, points to the first parameter 0 and so on. This also means that the value of g(0,0,0) will be computed first and then 45*3 will be computed and then (g(0,0,0)) + (45*3) will be computed. f(x,y,z)=2*sin(x)*cos(y)+ x-pi/4 +tan(z+pi/4) gets translated into

g(x,y,z)=(x+y+z)*2+(f(x,y,z)^2) gets translated into The user defined function g(0,0,0)+ 45 * 3 gets translated into

For operators, please note that the left hand side of the operand actually is the edge labeled 1 and not the one that is labeled 2 as above. This could be slightly confusing for operations like / and -. Please take note of this. An entry in the table of functions has the following form struct TblFunctions{ char *FuncName; /* Null-terminated string that gives the name of the function */ short nargs; /* Number of arguments that this function takes */ enum EnumDataType *pargtype; /* List of the data type of the parameters: from left to right */ union NodeValue *pargs; /* List of pointers to the arguments' value holder */ /* Note that these place-holders should be used in TreeNode */ enum EnumDataType RetType; /* Return type of the function */ struct TreeNode *RootNode; /* Root Node of the function: Only used for user-defined ones */ }; Parameters that go into to a function are actually placed in the respective locations in pargs member. And the arg0, arg1, arg2 that in the trees of f(x,y,z) and g(x,y,z) point to these locations in the function table and hence, they have the values directly. Traversing the parse-tree in pre-order generates the vcg files. Please note that the label name of the edges and they can be useful in cases like above when 45 which is edge 1 for * operator comes to the right of 3, which is edge 2. Future enhancements: With this infrastructure, variables can be allowed very easily. These variables can be used to store results of some expressions for use later in the session. This was not included in this implementation because of lack of time. User definable functions can be made to have more than 1 statement. Also, support for loops can also be added. After all, we can try to bring all the features that C has.