Formal Languages and Automata Theory, SS Project (due Week 14)

Similar documents
Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

TREES. Trees - Introduction

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

An Introduction to Trees

The Stack and Queue Types

Stack and Its Implementation

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

CE 221 Data Structures and Algorithms

7.1 Introduction. A (free) tree T is A simple graph such that for every pair of vertices v and w there is a unique path from v to w

Lab 7 1 Due Thu., 6 Apr. 2017

Infix to Postfix Conversion

BBM 201 DATA STRUCTURES

BBM 201 DATA STRUCTURES

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Stack Abstract Data Type

STACKS AND QUEUES. Problem Solving with Computers-II

Linear Data Structure

Stacks, Queues and Hierarchical Collections

F453 Module 7: Programming Techniques. 7.2: Methods for defining syntax

COMP 250 Fall binary trees Oct. 27, 2017

March 13/2003 Jayakanth Srinivasan,

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms


Programming, Data Structures and Algorithms Prof. Hema Murthy Department of Computer Science and Engineering Indian Institute of Technology, Madras

LECTURE 17. Expressions and Assignment

12 Abstract Data Types

SimpleCalc. which can be entered into a TI calculator, like the one on the right, like this:

This book is licensed under a Creative Commons Attribution 3.0 License

Top of the Stack. Stack ADT

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

March 20/2003 Jayakanth Srinivasan,

Prefix/Infix/Postfix Notation

Stacks Calculator Application. Alexandra Stefan

Top of the Stack. Stack ADT

Lecture Data Structure Stack

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

Stacks II. Adventures in Notation. stacks2 1

CS 206 Introduction to Computer Science II

PA3 Design Specification

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

Stacks and their Applications

Postfix (and prefix) notation

Stacks. Revised based on textbook author s notes.

CS W3134: Data Structures in Java

Content: Learning Objectives

EE 368. Week 6 (Notes)

DEEPIKA KAMBOJ UNIT 2. What is Stack?

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

A Simple Syntax-Directed Translator

Tree. A path is a connected sequence of edges. A tree topology is acyclic there is no loop.

6-TREE. Tree: Directed Tree: A directed tree is an acyclic digraph which has one node called the root node

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

CSE 214 Computer Science II Stack

Adapted By Manik Hosen

Trees. T.U. Cluj-Napoca -DSA Lecture 2 - M. Joldos 1

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

Chapter 10: Trees. A tree is a connected simple undirected graph with no simple circuits.

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

LECTURE 13 BINARY TREES

CDA 3103 Computer Organization Homework #7 Solution Set

INSTITUTE OF AERONAUTICAL ENGINEERING

Lecture 4 Stack and Queue

Binary Trees, Binary Search Trees

Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Data Structures. Trees. By Dr. Mohammad Ali H. Eljinini. M.A. Eljinini, PhD

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

Chapter Summary. Introduction to Trees Applications of Trees Tree Traversal Spanning Trees Minimum Spanning Trees

syntax tree - * * * * * *

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Data Structures. 1. Each entry in a linked list is a called a (a)link (b)node (c)data structure (d)none of the above

COMPUTER ARCHITECTURE AND PARALEL PROCESSING STUDY NOTES

CSI33 Data Structures

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Monday, April 15, We will lead up to the Analysis and Synthesis algorithms involved by first looking at three simpler ones.

3137 Data Structures and Algorithms in C++

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

13 BINARY TREES DATA STRUCTURES AND ALGORITHMS INORDER, PREORDER, POSTORDER TRAVERSALS

-The Hacker's Dictionary. Friedrich L. Bauer German computer scientist who proposed "stack method of expression evaluation" in 1955.

Problem with Scanning an Infix Expression

Introduction to Computers and Programming. Concept Question

Why Do We Need Trees?

Binary Trees and Binary Search Trees

Lecture 12 ADTs and Stacks

CSCI 204 Introduction to Computer Science II. Lab 6: Stack ADT

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

CS8391-DATA STRUCTURES

17CS33:Data Structures Using C QUESTION BANK

Transcription:

Formal Languages and Automata Theory, SS 2018. Project (due Week 14) 1 Preliminaries The objective is to implement an algorithm for the evaluation of an arithmetic expression. As input, we have a string of characters representing an arithmetic expression. As output, we want the result of evaluating the expression. For example: input data: file eval.in contains on its first line a string of characters composed by numbers ( 0, 1,..., 9 ), operators ( +, -, *, / ) and parentheses ( (, ) ). output data: file eval.out contains a number representing the value obtained by evaluating the expression from the input file. Example: eval.in: (1 + 1) 13 + 10/2, eval.out: 31. Methods: 1. (Reversed) Polish notation 2. Binary trees 1.1 (Reversed) Polish notation Arithmetic expressions are everywhere in computer science, and not only 1. Typically, an arithmetic expression is written in infix notation: Operand1 op Operand2, where op {+,,, /, %}. Note that + and can be used as binary and unary operators, but here we assume that all our operations are binary. There are rules to indicate which operations take precedence over others, and we often use parentheses to override those rules. Example: 3 4 + 5 vs. 3 (4 + 5). It is also possible to write arithmetic expressions using: prefix notation (Polish notation): op Operand1 Operand2 postfix notation (reversed Polish notation): Operand1 Operand2 op. It is possible to use a stack to find the overall value of an expression. In case of prefix notation, if you push an operator, then its operands, you need to have forward knowledge of when the operator has all its operands. Basically one needs to keep track of when operators pushed have all their operands so that you can unwind the stack and evaluate. With suffix, when you push an operator, the operands are (should) already on the stack, simply pop the operands and evaluate. You only need a stack that can handle operands, and no other data structure is necessary. The idea is to think of an expression as a list or sequence of items, each of which is a left parenthesis, right parenthesis, operand, or operator. An operand can be a constant or the name of a variable. Presumably it would be necessary at some point to replace each variable with its value. There are two algorithms involved. One converts an infix expression to postfix form, and the other evaluates a postfix expression. Each uses a stack. 1 This section is mainly based on http://faculty.cs.niu.edu/~hutchins/csci241/eval.htm

1.1.1 Transform an infix expression to postfix notation Suppose Q is an arithmetic expression in infix notation. We will create an equivalent postfix expression P by adding items to on the right of P. The new expression P will not contain any parentheses. We will use a stack in which each item may be a left parenthesis or the symbol for an operation. Start with an empty stack. We scan Q from left to right. While (we have not reached the end of Q) If (an operand is found) Add it to P If (a left parenthesis is found) Push it onto the stack If (a right parenthesis is found) While (the stack is not empty AND the top item is not a left parenthesis) Pop the stack and add the popped value to P Pop the left parenthesis from the stack and discard it If (an operator is found) If (the stack is empty or if the top element is a left parenthesis) Push the operator onto the stack Else While (the stack is not empty AND the top of the stack is not a left parenthesis AND precedence of the operator <= precedence of the top of the stack) Pop the stack and add the top value to P Push the latest operator onto the stack While (the stack is not empty) Pop the stack and add the popped value to P At the end, if there is still a left parenthesis at the top of the stack, or if we find a right parenthesis when the stack is empty, then Q contained unbalanced parentheses and is in error. 1.1.2 Evaluate a postfix expression Suppose P is an arithmetic expression in postfix notation. We will evaluate it using a stack to hold the operands. Start with an empty stack. We scan P from left to right. While (we have not reached the end of P) If an operand is found

push it onto the stack If an operator is found Pop the stack and call the value A Pop the stack and call the value B Evaluate B op A using the operator just found. Push the resulting value onto the stack Pop the stack (this is the final value) At the end, there should be only one element left on the stack. This assumes the postfix expression is valid. 1.1.3 Example Example 1. Let E being the following infix expression: 5 (6 + 2) 12/4. The equivalent postfix expression E is: 562 + 124/. It was obtained using the algorithms above Other examples Infix Notation Prefix Notation Postfix Notation A + B +AB AB+ A + B C +A BC ABC + (A + B) C + ABC AB + C (a b) c c ab ab c a b (c + d) a b + cd abcd +

1.2 Binary Trees Expression tree is a binary tree in which each internal node corresponds to operator and each leaf node corresponds to operand. For example, the tree for the expression 3 + ((5 + 9) 2) is: Inorder traversal ((Left, Root, Right)) of expression tree produces infix version of given postfix expression (same with preorder ((Root, Left, Right)) traversal it gives prefix expression). Also in this case, there are two algorithms involved for evaluating an expression: construction of expression tree and evaluation of the expression represented by expression tree. 1.2.1 Construction of Expression Tree For constructing expression tree we use a stack. We loop through input expression and do following for every character: If character is operand push that into stack If character is operator pop two values from stack make them its child and push current node again. At the end the only element of stack will be root of expression tree. 1.2.2 Evaluating the expression represented by expression tree solve(t) Let t be the expression tree If t is not null then If t.value is operand then return t.value A = solve(t.left) B = solve(t.right) // calculate applies operator t.value on A and B, and returns value return calculate(a, B, t.value)

2 Homework 1. Write an algorithm in your favorite programming language which performs the evaluation of an arithmetic expression using the reversed Polish notation. 2. (Optional) Same requirement as above, but by using binary trees.