Top Banner
Pad.Dr.D.Y.Patil Institute of Engineering and Technology, Department of Information Technology (LAB Manual) Class: TE (IT) (2008 Revised Syllabus) Software Design Lab Staff In-charge Prof. Rupesh Mahajan
81
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SDL Lab+Manual+2008+Course

PadDrDYPatil Institute of Engineering and Technology

Department of Information Technology

(LAB Manual)

Class TE (IT)(2008 Revised Syllabus)

Software Design Lab

Staff In-charge

Prof Rupesh Mahajan

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 01

Aim Implementation of TWO Pass assembler with hypothetical Instruction setInstruction set should include all types of assembly language statements suchas Imperative Declarative and Assembler Directive While designing stressshould be given ona) How efficiently Mnemonic opcode table could be implemented so asto enable faster retrieval on op-codeb) Implementation of symbol table for faster retrieval( Concepts in DSF should be applied while design)

Objective To learn the basic translation process of assembly language to machine

Language

Theory -

A language translator bridges an execution gap to machine language of computer system An assembler is a language translator whose source language is assembly language

Language processing activity consists of two phases Analysis phase and synthesis phase Analysis of source program consists of three components Lexical rules syntax rules and semantic rules Lexical rules govern the formation of valid statements in source language Semantic rules associate the formation meaning with valid statements of language Synthesis phase is concerned with construction of target language statements which have the same meaning as source language statements This consists of memory allocation and code generation

Data structures

Intermediate codeData access Over view of two pass assemblyControl transfer

Department of Information Technology

Pass I Pass II

Pad DrDYPatil Institute Of Technology Pimpri

Analysis of source program statements may not be immediately followed by synthesis of equivalent target statements This is due to forward references issue concerning memory requirements and organization of Language Processor (LP)

Forward reference of a program entity is a reference to the entity which precedes its definition in the program While processing a statement containing a forward reference language processor does not posses all relevant information concerning referenced entity This creates difficulties in synthesizing the equivalent target statements This problem can be solved by postponing the generation of target code until more information concerning the entity is available This also reduces memory requirements of LP and simplifies its organization This leads to multi-pass model of language processing

Language Processor Pass -

It is the processing of every statement in a source program or its equivalent representation to perform language-processing function

Assembly Language statements -

There are three types of statements Imperative Declarative Assembly directives An imperative statement indicates an action to be performed during the execution of assembled program Each imperative statement usually translates into one machine instruction Declarative statement eg DS reserves areas of memory and associates names with them DC constructs memory word containing constants Assembler directives instruct the assembler to perform certain actions during assembly of a program eg STARTltconstantgt directive indicates that the first word of the target program generated by assembler should be placed at memory word with address ltconstantgt

Function Of Analysis And Synthesis Phase

Analysis Phase -

Isolate the label operation code and operand fields of a statementEnter the symbol found in label field (if any) and address of next available machine word into symbol tableValidate the mnemonic operation code by looking it up in the mnemonics tableDetermine the machine storage requirements of the statement by considering the mnemonic operation code and operand fields of the statementCalculate the address of the address of the first machine word following the target code generated for this statement (Location Counter Processing) Synthesis PhaseObtain the machine operation code corresponding to the mnemonic operation code by searching the mnemonic tableObtain the address of the operand from the symbol table

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Synthesize the machine instruction or the machine form of the constant as the case may be

Design of a Two Pass Assembler -

Tasks performed by the passes of two-pass assembler are as follows

Pass I -

Separate the symbol mnemonic opcode and operand fieldsDetermine the storage-required foe every assembly language statement and update the location counterBuild the symbol table and the literal tableConstruct the intermediate code for every assembly language statement

Pass II -

Synthesize the target code by processing the intermediate code generated during

Data structures required for pass I1 Source file containing assembly program2 MOT A table of mnemonic op-codes and related information

It has the following fields Mnemonic Such as ADD END DC TYPE IS for imperative DL for declarative and AD for Assembler directive OP- code Operation code indicating the operation to be performed Length Length of instruction required for Location Counter Processing Hash table Implementation of MOT to minimize the search time required for searching the instruction

Index Mnemonic TYPE OP-Code Length Link

0 ADD IS 01 01 -11 BC IS 07 01 -12 COMP IS 06 01 -13 DIV IS 08 01 54 EQU AD 03 - 75 DC DL 01 - 66 DS DL 02 - -17 END AD 05 - -1

Hash Function used is ASCII Value of the First letter of Mnemonic ndash 65 This helps in retrieving the op- code and other related information in minimum time For Example the instruction starting with alphabet lsquoArsquo will be found at index location 0 lsquoBrsquo at index 1 so on and so forth If more instructions exist with same alphabet then the instruction is

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

stored at empty location and the index of that instruction is stored in the link field Thus instructions starting with alphabet lsquoDrsquo will be stored at index locations 35and 6 Those starting with E will be stored at 4 and 7 and the process continues

1 SYMTB The symbol tableFields are Symbol name Address (LC Value) Initialize all values in the address fields to -1 and when symbol gets added when it appears in label field replace address value with current LC The symbol if it used but not defined will have address value -1 which will be used for error detection

4 LITTAB and POOLTAB Literal table stores the literals used in the program and POOLTAB stores the pointers to the literals in the current literal pool

Literal Address= lsquo5rsquo= lsquo1rsquo= lsquo1rsquo

5 Intermediate form used Variant 1 Variant 2 Students are supposed to write the variant used by them

Data Structure used by Pass II1 OPTAB A table of mnemonic opcodes and related information2 SYMTAB The symbol table 3 LITTAB A table of literals used in the program4 Intermediate code generated by Pass I5 Output file containing Target code error listing

Algorithm 1 Open the source file in input mode2 if end of file of source file go to step 83 Read the next line of the source program4 Separate the line into words These words could be stored in array of strings5 Search for first word is mnemonic opcode table if not present it is a label add this as a symbol in symbol table with current LC And then search for second word in mnemonic opcode table6 If instruction is found case 1 imperative statement case 2 Declarative statement case 3 Assembler Directive Generate Intermediate code and write to Intermediate code file7 go to step 2

Department of Information Technology

Symbol AddressLoop 204Next 214

Pad DrDYPatil Institute Of Technology Pimpri

8 Close source file and open intermediate code file9 If end of file ( Intermediate code) go to step 1310 Read next line from intermediate code file11 Write opcode register code and address of memory( to be fetched from literal or symbol table depending on the case) onto target file This is to be done only for Imperative statement12 go to step 9 13 Close all files14 Display symbol table literal table and target file

Imperative statement case 1 If opcode gt= 1 ampamp opcode lt=8 ( Instruction requires register operand)

a Set type as IS get opcode get register code and make entry into symbol or literal table as the case may be In case of symbol used as operand LC field is not known so LC could be -1 Perform LC processing LC++ Updating of symbol table should consider error handling

2 if opcode is 00 ( stop) Set all fields of Intermediate call as 00 LC++

3 else register operand not required ( Read and Print)Same as case 1 only register code is not required so set it to zero Here again update the symbol table LC++

On similar lines we can identify the cases for declarative and assembler directive statements based on opcode

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for first pass of two pass assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for Pass II of II pass Assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

List of hypothetical instructions

Instruction Assembly Remarks

Opcode mnemonic

00 STOP stop execution01 ADD first operand modified condition code set

02 SUB first operand modified condition code set03 MULT first operand modified condition code set04 MOVER register memory05 MOVEM memory register06 COMP sets condition code 07 BC branch on condition code 08 DIV analogous to SUB09 READ first operand is not used10 PRINT first operand is not used

Sample Input amp Output -

SAMPLE INPUT FILE SAMPLE OUTPUT FILE OF INTERMEDIATE CODE Using Variant One START 202 MOVER AREG =rsquo5 202) (IS04)(1)(L1) MOVEM AREG A 203) (IS05)(1)(S1) LOOP MOVER AREG A 204) (IS04)(1)(S1)

MOVER CREG B 205) (IS04)(3)(S3) ADD CREG = lsquo1rsquo 206) (IS01)(3)(L2)

MOVEM CREG B 207) (IS05)(2)(S3) SUB CREGA 208) (IS02)(3)(S1)

BC ANY NEXT 209) (IS07)(6)(S4) LTORG 210) (AD 04)

211) ADD CREGB rsquo 212) (IS01)(3)(S2)

BC LE LOOP 213) (IS07)(2)(S2) NEXT SUB AREG = lsquo1rsquo 214) (IS02)(1)(L3) BC LT BACK 215) (IS07)(1)(S5) STOP 216) (IS00) ORIGIN 219 217) (AD03) MULT CREG B 219) (IS03)(3)(S3) A DS 1 220) (DL02) (C1) BACK EQU LOOP 221) (AD02) B DS 1 221) (DL02) (C1)

END 222) (AD05)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

SYMBOL TABLE

Index Symbol Address1 A 2202 LOOP 2043 B 2214 NEXT 2145 BACK 204

POOL TABLE LITERAL TABLE

LIT_Ind Index LITERAL ADDRESS01 1 5 21003 2 1 211

3 1 222

SAMPLE INPUT FILE SAMPLE OUTPUT FILE FOR TARGET CODE START 202 MOVER AREG =rsquo5 202) 04 1 210 MOVEM AREG A 203) 05 1 220 LOOP MOVER AREG A 204) 04 1 220

MOVER CREG B 205) 04 3 221 ADD CREG = lsquo1rsquo 206) 01 3 211

MOVEM CREG B 207) 05 3 221 SUB CREG A 208) 03 3 220

BC ANY NEXT 209) 07 6 214 LTORG 210) 00 0 005

211) 00 0 001 ADD CREG B rsquo 212) 01 3 221

BC LE LOOP 213) 07 2 204 NEXT SUB AREG = lsquo1rsquo 214) 02 1 222 BC LT BACK 215) 07 1 220 STOP 216) 00 0 000 ORIGIN 219 217) MULT CREG B 219) 03 3 221 A DS 1 220) BACK EQU LOOP 221) B DS 1 221)

END 222) 00 0 001

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Instructions to the Students -Students are supposed to write about the organizations of the different data structures such as array link list etcAssumptions and limitations if any should be clearly mentionedStudents are supposed to create two output files one without errors amp one with errors indicating the type of error At least following errors must be handled

Errors -

Forward reference(Symbol used but not defined) -

This error occurs when some symbol is used but it is not defined into the program

Duplication of Symbol -

This error occurs when some symbol is declared more than once in the program

Mnemonic error

If there is invalid instruction then this error will occur

Register error -

If there is invalid register then this error will occur

Operand error -

This error will occur when there is an error in the operand field

Instructions Student must use incremental approach at the time of

developmentieconsider simple input first and then go for complex input

Platform DOS ( language ndash C)------------------------------------------------------------------------------------------------------------

FAQs

1 Explain what is meant by pass of an assembler2 Explain the need for two pass assembler3 Explain terms such as Forward Reference and backward reference4 Explain various types of errors that are handled in two different passes5 Explain the need of Intermediate Code generation and the variants used6 State various tables used and their significance in the design of two pass

Assembler------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 2: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 01

Aim Implementation of TWO Pass assembler with hypothetical Instruction setInstruction set should include all types of assembly language statements suchas Imperative Declarative and Assembler Directive While designing stressshould be given ona) How efficiently Mnemonic opcode table could be implemented so asto enable faster retrieval on op-codeb) Implementation of symbol table for faster retrieval( Concepts in DSF should be applied while design)

Objective To learn the basic translation process of assembly language to machine

Language

Theory -

A language translator bridges an execution gap to machine language of computer system An assembler is a language translator whose source language is assembly language

Language processing activity consists of two phases Analysis phase and synthesis phase Analysis of source program consists of three components Lexical rules syntax rules and semantic rules Lexical rules govern the formation of valid statements in source language Semantic rules associate the formation meaning with valid statements of language Synthesis phase is concerned with construction of target language statements which have the same meaning as source language statements This consists of memory allocation and code generation

Data structures

Intermediate codeData access Over view of two pass assemblyControl transfer

Department of Information Technology

Pass I Pass II

Pad DrDYPatil Institute Of Technology Pimpri

Analysis of source program statements may not be immediately followed by synthesis of equivalent target statements This is due to forward references issue concerning memory requirements and organization of Language Processor (LP)

Forward reference of a program entity is a reference to the entity which precedes its definition in the program While processing a statement containing a forward reference language processor does not posses all relevant information concerning referenced entity This creates difficulties in synthesizing the equivalent target statements This problem can be solved by postponing the generation of target code until more information concerning the entity is available This also reduces memory requirements of LP and simplifies its organization This leads to multi-pass model of language processing

Language Processor Pass -

It is the processing of every statement in a source program or its equivalent representation to perform language-processing function

Assembly Language statements -

There are three types of statements Imperative Declarative Assembly directives An imperative statement indicates an action to be performed during the execution of assembled program Each imperative statement usually translates into one machine instruction Declarative statement eg DS reserves areas of memory and associates names with them DC constructs memory word containing constants Assembler directives instruct the assembler to perform certain actions during assembly of a program eg STARTltconstantgt directive indicates that the first word of the target program generated by assembler should be placed at memory word with address ltconstantgt

Function Of Analysis And Synthesis Phase

Analysis Phase -

Isolate the label operation code and operand fields of a statementEnter the symbol found in label field (if any) and address of next available machine word into symbol tableValidate the mnemonic operation code by looking it up in the mnemonics tableDetermine the machine storage requirements of the statement by considering the mnemonic operation code and operand fields of the statementCalculate the address of the address of the first machine word following the target code generated for this statement (Location Counter Processing) Synthesis PhaseObtain the machine operation code corresponding to the mnemonic operation code by searching the mnemonic tableObtain the address of the operand from the symbol table

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Synthesize the machine instruction or the machine form of the constant as the case may be

Design of a Two Pass Assembler -

Tasks performed by the passes of two-pass assembler are as follows

Pass I -

Separate the symbol mnemonic opcode and operand fieldsDetermine the storage-required foe every assembly language statement and update the location counterBuild the symbol table and the literal tableConstruct the intermediate code for every assembly language statement

Pass II -

Synthesize the target code by processing the intermediate code generated during

Data structures required for pass I1 Source file containing assembly program2 MOT A table of mnemonic op-codes and related information

It has the following fields Mnemonic Such as ADD END DC TYPE IS for imperative DL for declarative and AD for Assembler directive OP- code Operation code indicating the operation to be performed Length Length of instruction required for Location Counter Processing Hash table Implementation of MOT to minimize the search time required for searching the instruction

Index Mnemonic TYPE OP-Code Length Link

0 ADD IS 01 01 -11 BC IS 07 01 -12 COMP IS 06 01 -13 DIV IS 08 01 54 EQU AD 03 - 75 DC DL 01 - 66 DS DL 02 - -17 END AD 05 - -1

Hash Function used is ASCII Value of the First letter of Mnemonic ndash 65 This helps in retrieving the op- code and other related information in minimum time For Example the instruction starting with alphabet lsquoArsquo will be found at index location 0 lsquoBrsquo at index 1 so on and so forth If more instructions exist with same alphabet then the instruction is

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

stored at empty location and the index of that instruction is stored in the link field Thus instructions starting with alphabet lsquoDrsquo will be stored at index locations 35and 6 Those starting with E will be stored at 4 and 7 and the process continues

1 SYMTB The symbol tableFields are Symbol name Address (LC Value) Initialize all values in the address fields to -1 and when symbol gets added when it appears in label field replace address value with current LC The symbol if it used but not defined will have address value -1 which will be used for error detection

4 LITTAB and POOLTAB Literal table stores the literals used in the program and POOLTAB stores the pointers to the literals in the current literal pool

Literal Address= lsquo5rsquo= lsquo1rsquo= lsquo1rsquo

5 Intermediate form used Variant 1 Variant 2 Students are supposed to write the variant used by them

Data Structure used by Pass II1 OPTAB A table of mnemonic opcodes and related information2 SYMTAB The symbol table 3 LITTAB A table of literals used in the program4 Intermediate code generated by Pass I5 Output file containing Target code error listing

Algorithm 1 Open the source file in input mode2 if end of file of source file go to step 83 Read the next line of the source program4 Separate the line into words These words could be stored in array of strings5 Search for first word is mnemonic opcode table if not present it is a label add this as a symbol in symbol table with current LC And then search for second word in mnemonic opcode table6 If instruction is found case 1 imperative statement case 2 Declarative statement case 3 Assembler Directive Generate Intermediate code and write to Intermediate code file7 go to step 2

Department of Information Technology

Symbol AddressLoop 204Next 214

Pad DrDYPatil Institute Of Technology Pimpri

8 Close source file and open intermediate code file9 If end of file ( Intermediate code) go to step 1310 Read next line from intermediate code file11 Write opcode register code and address of memory( to be fetched from literal or symbol table depending on the case) onto target file This is to be done only for Imperative statement12 go to step 9 13 Close all files14 Display symbol table literal table and target file

Imperative statement case 1 If opcode gt= 1 ampamp opcode lt=8 ( Instruction requires register operand)

a Set type as IS get opcode get register code and make entry into symbol or literal table as the case may be In case of symbol used as operand LC field is not known so LC could be -1 Perform LC processing LC++ Updating of symbol table should consider error handling

2 if opcode is 00 ( stop) Set all fields of Intermediate call as 00 LC++

3 else register operand not required ( Read and Print)Same as case 1 only register code is not required so set it to zero Here again update the symbol table LC++

On similar lines we can identify the cases for declarative and assembler directive statements based on opcode

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for first pass of two pass assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for Pass II of II pass Assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

List of hypothetical instructions

Instruction Assembly Remarks

Opcode mnemonic

00 STOP stop execution01 ADD first operand modified condition code set

02 SUB first operand modified condition code set03 MULT first operand modified condition code set04 MOVER register memory05 MOVEM memory register06 COMP sets condition code 07 BC branch on condition code 08 DIV analogous to SUB09 READ first operand is not used10 PRINT first operand is not used

Sample Input amp Output -

SAMPLE INPUT FILE SAMPLE OUTPUT FILE OF INTERMEDIATE CODE Using Variant One START 202 MOVER AREG =rsquo5 202) (IS04)(1)(L1) MOVEM AREG A 203) (IS05)(1)(S1) LOOP MOVER AREG A 204) (IS04)(1)(S1)

MOVER CREG B 205) (IS04)(3)(S3) ADD CREG = lsquo1rsquo 206) (IS01)(3)(L2)

MOVEM CREG B 207) (IS05)(2)(S3) SUB CREGA 208) (IS02)(3)(S1)

BC ANY NEXT 209) (IS07)(6)(S4) LTORG 210) (AD 04)

211) ADD CREGB rsquo 212) (IS01)(3)(S2)

BC LE LOOP 213) (IS07)(2)(S2) NEXT SUB AREG = lsquo1rsquo 214) (IS02)(1)(L3) BC LT BACK 215) (IS07)(1)(S5) STOP 216) (IS00) ORIGIN 219 217) (AD03) MULT CREG B 219) (IS03)(3)(S3) A DS 1 220) (DL02) (C1) BACK EQU LOOP 221) (AD02) B DS 1 221) (DL02) (C1)

END 222) (AD05)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

SYMBOL TABLE

Index Symbol Address1 A 2202 LOOP 2043 B 2214 NEXT 2145 BACK 204

POOL TABLE LITERAL TABLE

LIT_Ind Index LITERAL ADDRESS01 1 5 21003 2 1 211

3 1 222

SAMPLE INPUT FILE SAMPLE OUTPUT FILE FOR TARGET CODE START 202 MOVER AREG =rsquo5 202) 04 1 210 MOVEM AREG A 203) 05 1 220 LOOP MOVER AREG A 204) 04 1 220

MOVER CREG B 205) 04 3 221 ADD CREG = lsquo1rsquo 206) 01 3 211

MOVEM CREG B 207) 05 3 221 SUB CREG A 208) 03 3 220

BC ANY NEXT 209) 07 6 214 LTORG 210) 00 0 005

211) 00 0 001 ADD CREG B rsquo 212) 01 3 221

BC LE LOOP 213) 07 2 204 NEXT SUB AREG = lsquo1rsquo 214) 02 1 222 BC LT BACK 215) 07 1 220 STOP 216) 00 0 000 ORIGIN 219 217) MULT CREG B 219) 03 3 221 A DS 1 220) BACK EQU LOOP 221) B DS 1 221)

END 222) 00 0 001

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Instructions to the Students -Students are supposed to write about the organizations of the different data structures such as array link list etcAssumptions and limitations if any should be clearly mentionedStudents are supposed to create two output files one without errors amp one with errors indicating the type of error At least following errors must be handled

Errors -

Forward reference(Symbol used but not defined) -

This error occurs when some symbol is used but it is not defined into the program

Duplication of Symbol -

This error occurs when some symbol is declared more than once in the program

Mnemonic error

If there is invalid instruction then this error will occur

Register error -

If there is invalid register then this error will occur

Operand error -

This error will occur when there is an error in the operand field

Instructions Student must use incremental approach at the time of

developmentieconsider simple input first and then go for complex input

Platform DOS ( language ndash C)------------------------------------------------------------------------------------------------------------

FAQs

1 Explain what is meant by pass of an assembler2 Explain the need for two pass assembler3 Explain terms such as Forward Reference and backward reference4 Explain various types of errors that are handled in two different passes5 Explain the need of Intermediate Code generation and the variants used6 State various tables used and their significance in the design of two pass

Assembler------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 3: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Analysis of source program statements may not be immediately followed by synthesis of equivalent target statements This is due to forward references issue concerning memory requirements and organization of Language Processor (LP)

Forward reference of a program entity is a reference to the entity which precedes its definition in the program While processing a statement containing a forward reference language processor does not posses all relevant information concerning referenced entity This creates difficulties in synthesizing the equivalent target statements This problem can be solved by postponing the generation of target code until more information concerning the entity is available This also reduces memory requirements of LP and simplifies its organization This leads to multi-pass model of language processing

Language Processor Pass -

It is the processing of every statement in a source program or its equivalent representation to perform language-processing function

Assembly Language statements -

There are three types of statements Imperative Declarative Assembly directives An imperative statement indicates an action to be performed during the execution of assembled program Each imperative statement usually translates into one machine instruction Declarative statement eg DS reserves areas of memory and associates names with them DC constructs memory word containing constants Assembler directives instruct the assembler to perform certain actions during assembly of a program eg STARTltconstantgt directive indicates that the first word of the target program generated by assembler should be placed at memory word with address ltconstantgt

Function Of Analysis And Synthesis Phase

Analysis Phase -

Isolate the label operation code and operand fields of a statementEnter the symbol found in label field (if any) and address of next available machine word into symbol tableValidate the mnemonic operation code by looking it up in the mnemonics tableDetermine the machine storage requirements of the statement by considering the mnemonic operation code and operand fields of the statementCalculate the address of the address of the first machine word following the target code generated for this statement (Location Counter Processing) Synthesis PhaseObtain the machine operation code corresponding to the mnemonic operation code by searching the mnemonic tableObtain the address of the operand from the symbol table

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Synthesize the machine instruction or the machine form of the constant as the case may be

Design of a Two Pass Assembler -

Tasks performed by the passes of two-pass assembler are as follows

Pass I -

Separate the symbol mnemonic opcode and operand fieldsDetermine the storage-required foe every assembly language statement and update the location counterBuild the symbol table and the literal tableConstruct the intermediate code for every assembly language statement

Pass II -

Synthesize the target code by processing the intermediate code generated during

Data structures required for pass I1 Source file containing assembly program2 MOT A table of mnemonic op-codes and related information

It has the following fields Mnemonic Such as ADD END DC TYPE IS for imperative DL for declarative and AD for Assembler directive OP- code Operation code indicating the operation to be performed Length Length of instruction required for Location Counter Processing Hash table Implementation of MOT to minimize the search time required for searching the instruction

Index Mnemonic TYPE OP-Code Length Link

0 ADD IS 01 01 -11 BC IS 07 01 -12 COMP IS 06 01 -13 DIV IS 08 01 54 EQU AD 03 - 75 DC DL 01 - 66 DS DL 02 - -17 END AD 05 - -1

Hash Function used is ASCII Value of the First letter of Mnemonic ndash 65 This helps in retrieving the op- code and other related information in minimum time For Example the instruction starting with alphabet lsquoArsquo will be found at index location 0 lsquoBrsquo at index 1 so on and so forth If more instructions exist with same alphabet then the instruction is

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

stored at empty location and the index of that instruction is stored in the link field Thus instructions starting with alphabet lsquoDrsquo will be stored at index locations 35and 6 Those starting with E will be stored at 4 and 7 and the process continues

1 SYMTB The symbol tableFields are Symbol name Address (LC Value) Initialize all values in the address fields to -1 and when symbol gets added when it appears in label field replace address value with current LC The symbol if it used but not defined will have address value -1 which will be used for error detection

4 LITTAB and POOLTAB Literal table stores the literals used in the program and POOLTAB stores the pointers to the literals in the current literal pool

Literal Address= lsquo5rsquo= lsquo1rsquo= lsquo1rsquo

5 Intermediate form used Variant 1 Variant 2 Students are supposed to write the variant used by them

Data Structure used by Pass II1 OPTAB A table of mnemonic opcodes and related information2 SYMTAB The symbol table 3 LITTAB A table of literals used in the program4 Intermediate code generated by Pass I5 Output file containing Target code error listing

Algorithm 1 Open the source file in input mode2 if end of file of source file go to step 83 Read the next line of the source program4 Separate the line into words These words could be stored in array of strings5 Search for first word is mnemonic opcode table if not present it is a label add this as a symbol in symbol table with current LC And then search for second word in mnemonic opcode table6 If instruction is found case 1 imperative statement case 2 Declarative statement case 3 Assembler Directive Generate Intermediate code and write to Intermediate code file7 go to step 2

Department of Information Technology

Symbol AddressLoop 204Next 214

Pad DrDYPatil Institute Of Technology Pimpri

8 Close source file and open intermediate code file9 If end of file ( Intermediate code) go to step 1310 Read next line from intermediate code file11 Write opcode register code and address of memory( to be fetched from literal or symbol table depending on the case) onto target file This is to be done only for Imperative statement12 go to step 9 13 Close all files14 Display symbol table literal table and target file

Imperative statement case 1 If opcode gt= 1 ampamp opcode lt=8 ( Instruction requires register operand)

a Set type as IS get opcode get register code and make entry into symbol or literal table as the case may be In case of symbol used as operand LC field is not known so LC could be -1 Perform LC processing LC++ Updating of symbol table should consider error handling

2 if opcode is 00 ( stop) Set all fields of Intermediate call as 00 LC++

3 else register operand not required ( Read and Print)Same as case 1 only register code is not required so set it to zero Here again update the symbol table LC++

On similar lines we can identify the cases for declarative and assembler directive statements based on opcode

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for first pass of two pass assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for Pass II of II pass Assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

List of hypothetical instructions

Instruction Assembly Remarks

Opcode mnemonic

00 STOP stop execution01 ADD first operand modified condition code set

02 SUB first operand modified condition code set03 MULT first operand modified condition code set04 MOVER register memory05 MOVEM memory register06 COMP sets condition code 07 BC branch on condition code 08 DIV analogous to SUB09 READ first operand is not used10 PRINT first operand is not used

Sample Input amp Output -

SAMPLE INPUT FILE SAMPLE OUTPUT FILE OF INTERMEDIATE CODE Using Variant One START 202 MOVER AREG =rsquo5 202) (IS04)(1)(L1) MOVEM AREG A 203) (IS05)(1)(S1) LOOP MOVER AREG A 204) (IS04)(1)(S1)

MOVER CREG B 205) (IS04)(3)(S3) ADD CREG = lsquo1rsquo 206) (IS01)(3)(L2)

MOVEM CREG B 207) (IS05)(2)(S3) SUB CREGA 208) (IS02)(3)(S1)

BC ANY NEXT 209) (IS07)(6)(S4) LTORG 210) (AD 04)

211) ADD CREGB rsquo 212) (IS01)(3)(S2)

BC LE LOOP 213) (IS07)(2)(S2) NEXT SUB AREG = lsquo1rsquo 214) (IS02)(1)(L3) BC LT BACK 215) (IS07)(1)(S5) STOP 216) (IS00) ORIGIN 219 217) (AD03) MULT CREG B 219) (IS03)(3)(S3) A DS 1 220) (DL02) (C1) BACK EQU LOOP 221) (AD02) B DS 1 221) (DL02) (C1)

END 222) (AD05)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

SYMBOL TABLE

Index Symbol Address1 A 2202 LOOP 2043 B 2214 NEXT 2145 BACK 204

POOL TABLE LITERAL TABLE

LIT_Ind Index LITERAL ADDRESS01 1 5 21003 2 1 211

3 1 222

SAMPLE INPUT FILE SAMPLE OUTPUT FILE FOR TARGET CODE START 202 MOVER AREG =rsquo5 202) 04 1 210 MOVEM AREG A 203) 05 1 220 LOOP MOVER AREG A 204) 04 1 220

MOVER CREG B 205) 04 3 221 ADD CREG = lsquo1rsquo 206) 01 3 211

MOVEM CREG B 207) 05 3 221 SUB CREG A 208) 03 3 220

BC ANY NEXT 209) 07 6 214 LTORG 210) 00 0 005

211) 00 0 001 ADD CREG B rsquo 212) 01 3 221

BC LE LOOP 213) 07 2 204 NEXT SUB AREG = lsquo1rsquo 214) 02 1 222 BC LT BACK 215) 07 1 220 STOP 216) 00 0 000 ORIGIN 219 217) MULT CREG B 219) 03 3 221 A DS 1 220) BACK EQU LOOP 221) B DS 1 221)

END 222) 00 0 001

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Instructions to the Students -Students are supposed to write about the organizations of the different data structures such as array link list etcAssumptions and limitations if any should be clearly mentionedStudents are supposed to create two output files one without errors amp one with errors indicating the type of error At least following errors must be handled

Errors -

Forward reference(Symbol used but not defined) -

This error occurs when some symbol is used but it is not defined into the program

Duplication of Symbol -

This error occurs when some symbol is declared more than once in the program

Mnemonic error

If there is invalid instruction then this error will occur

Register error -

If there is invalid register then this error will occur

Operand error -

This error will occur when there is an error in the operand field

Instructions Student must use incremental approach at the time of

developmentieconsider simple input first and then go for complex input

Platform DOS ( language ndash C)------------------------------------------------------------------------------------------------------------

FAQs

1 Explain what is meant by pass of an assembler2 Explain the need for two pass assembler3 Explain terms such as Forward Reference and backward reference4 Explain various types of errors that are handled in two different passes5 Explain the need of Intermediate Code generation and the variants used6 State various tables used and their significance in the design of two pass

Assembler------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 4: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Synthesize the machine instruction or the machine form of the constant as the case may be

Design of a Two Pass Assembler -

Tasks performed by the passes of two-pass assembler are as follows

Pass I -

Separate the symbol mnemonic opcode and operand fieldsDetermine the storage-required foe every assembly language statement and update the location counterBuild the symbol table and the literal tableConstruct the intermediate code for every assembly language statement

Pass II -

Synthesize the target code by processing the intermediate code generated during

Data structures required for pass I1 Source file containing assembly program2 MOT A table of mnemonic op-codes and related information

It has the following fields Mnemonic Such as ADD END DC TYPE IS for imperative DL for declarative and AD for Assembler directive OP- code Operation code indicating the operation to be performed Length Length of instruction required for Location Counter Processing Hash table Implementation of MOT to minimize the search time required for searching the instruction

Index Mnemonic TYPE OP-Code Length Link

0 ADD IS 01 01 -11 BC IS 07 01 -12 COMP IS 06 01 -13 DIV IS 08 01 54 EQU AD 03 - 75 DC DL 01 - 66 DS DL 02 - -17 END AD 05 - -1

Hash Function used is ASCII Value of the First letter of Mnemonic ndash 65 This helps in retrieving the op- code and other related information in minimum time For Example the instruction starting with alphabet lsquoArsquo will be found at index location 0 lsquoBrsquo at index 1 so on and so forth If more instructions exist with same alphabet then the instruction is

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

stored at empty location and the index of that instruction is stored in the link field Thus instructions starting with alphabet lsquoDrsquo will be stored at index locations 35and 6 Those starting with E will be stored at 4 and 7 and the process continues

1 SYMTB The symbol tableFields are Symbol name Address (LC Value) Initialize all values in the address fields to -1 and when symbol gets added when it appears in label field replace address value with current LC The symbol if it used but not defined will have address value -1 which will be used for error detection

4 LITTAB and POOLTAB Literal table stores the literals used in the program and POOLTAB stores the pointers to the literals in the current literal pool

Literal Address= lsquo5rsquo= lsquo1rsquo= lsquo1rsquo

5 Intermediate form used Variant 1 Variant 2 Students are supposed to write the variant used by them

Data Structure used by Pass II1 OPTAB A table of mnemonic opcodes and related information2 SYMTAB The symbol table 3 LITTAB A table of literals used in the program4 Intermediate code generated by Pass I5 Output file containing Target code error listing

Algorithm 1 Open the source file in input mode2 if end of file of source file go to step 83 Read the next line of the source program4 Separate the line into words These words could be stored in array of strings5 Search for first word is mnemonic opcode table if not present it is a label add this as a symbol in symbol table with current LC And then search for second word in mnemonic opcode table6 If instruction is found case 1 imperative statement case 2 Declarative statement case 3 Assembler Directive Generate Intermediate code and write to Intermediate code file7 go to step 2

Department of Information Technology

Symbol AddressLoop 204Next 214

Pad DrDYPatil Institute Of Technology Pimpri

8 Close source file and open intermediate code file9 If end of file ( Intermediate code) go to step 1310 Read next line from intermediate code file11 Write opcode register code and address of memory( to be fetched from literal or symbol table depending on the case) onto target file This is to be done only for Imperative statement12 go to step 9 13 Close all files14 Display symbol table literal table and target file

Imperative statement case 1 If opcode gt= 1 ampamp opcode lt=8 ( Instruction requires register operand)

a Set type as IS get opcode get register code and make entry into symbol or literal table as the case may be In case of symbol used as operand LC field is not known so LC could be -1 Perform LC processing LC++ Updating of symbol table should consider error handling

2 if opcode is 00 ( stop) Set all fields of Intermediate call as 00 LC++

3 else register operand not required ( Read and Print)Same as case 1 only register code is not required so set it to zero Here again update the symbol table LC++

On similar lines we can identify the cases for declarative and assembler directive statements based on opcode

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for first pass of two pass assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for Pass II of II pass Assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

List of hypothetical instructions

Instruction Assembly Remarks

Opcode mnemonic

00 STOP stop execution01 ADD first operand modified condition code set

02 SUB first operand modified condition code set03 MULT first operand modified condition code set04 MOVER register memory05 MOVEM memory register06 COMP sets condition code 07 BC branch on condition code 08 DIV analogous to SUB09 READ first operand is not used10 PRINT first operand is not used

Sample Input amp Output -

SAMPLE INPUT FILE SAMPLE OUTPUT FILE OF INTERMEDIATE CODE Using Variant One START 202 MOVER AREG =rsquo5 202) (IS04)(1)(L1) MOVEM AREG A 203) (IS05)(1)(S1) LOOP MOVER AREG A 204) (IS04)(1)(S1)

MOVER CREG B 205) (IS04)(3)(S3) ADD CREG = lsquo1rsquo 206) (IS01)(3)(L2)

MOVEM CREG B 207) (IS05)(2)(S3) SUB CREGA 208) (IS02)(3)(S1)

BC ANY NEXT 209) (IS07)(6)(S4) LTORG 210) (AD 04)

211) ADD CREGB rsquo 212) (IS01)(3)(S2)

BC LE LOOP 213) (IS07)(2)(S2) NEXT SUB AREG = lsquo1rsquo 214) (IS02)(1)(L3) BC LT BACK 215) (IS07)(1)(S5) STOP 216) (IS00) ORIGIN 219 217) (AD03) MULT CREG B 219) (IS03)(3)(S3) A DS 1 220) (DL02) (C1) BACK EQU LOOP 221) (AD02) B DS 1 221) (DL02) (C1)

END 222) (AD05)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

SYMBOL TABLE

Index Symbol Address1 A 2202 LOOP 2043 B 2214 NEXT 2145 BACK 204

POOL TABLE LITERAL TABLE

LIT_Ind Index LITERAL ADDRESS01 1 5 21003 2 1 211

3 1 222

SAMPLE INPUT FILE SAMPLE OUTPUT FILE FOR TARGET CODE START 202 MOVER AREG =rsquo5 202) 04 1 210 MOVEM AREG A 203) 05 1 220 LOOP MOVER AREG A 204) 04 1 220

MOVER CREG B 205) 04 3 221 ADD CREG = lsquo1rsquo 206) 01 3 211

MOVEM CREG B 207) 05 3 221 SUB CREG A 208) 03 3 220

BC ANY NEXT 209) 07 6 214 LTORG 210) 00 0 005

211) 00 0 001 ADD CREG B rsquo 212) 01 3 221

BC LE LOOP 213) 07 2 204 NEXT SUB AREG = lsquo1rsquo 214) 02 1 222 BC LT BACK 215) 07 1 220 STOP 216) 00 0 000 ORIGIN 219 217) MULT CREG B 219) 03 3 221 A DS 1 220) BACK EQU LOOP 221) B DS 1 221)

END 222) 00 0 001

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Instructions to the Students -Students are supposed to write about the organizations of the different data structures such as array link list etcAssumptions and limitations if any should be clearly mentionedStudents are supposed to create two output files one without errors amp one with errors indicating the type of error At least following errors must be handled

Errors -

Forward reference(Symbol used but not defined) -

This error occurs when some symbol is used but it is not defined into the program

Duplication of Symbol -

This error occurs when some symbol is declared more than once in the program

Mnemonic error

If there is invalid instruction then this error will occur

Register error -

If there is invalid register then this error will occur

Operand error -

This error will occur when there is an error in the operand field

Instructions Student must use incremental approach at the time of

developmentieconsider simple input first and then go for complex input

Platform DOS ( language ndash C)------------------------------------------------------------------------------------------------------------

FAQs

1 Explain what is meant by pass of an assembler2 Explain the need for two pass assembler3 Explain terms such as Forward Reference and backward reference4 Explain various types of errors that are handled in two different passes5 Explain the need of Intermediate Code generation and the variants used6 State various tables used and their significance in the design of two pass

Assembler------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 5: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

stored at empty location and the index of that instruction is stored in the link field Thus instructions starting with alphabet lsquoDrsquo will be stored at index locations 35and 6 Those starting with E will be stored at 4 and 7 and the process continues

1 SYMTB The symbol tableFields are Symbol name Address (LC Value) Initialize all values in the address fields to -1 and when symbol gets added when it appears in label field replace address value with current LC The symbol if it used but not defined will have address value -1 which will be used for error detection

4 LITTAB and POOLTAB Literal table stores the literals used in the program and POOLTAB stores the pointers to the literals in the current literal pool

Literal Address= lsquo5rsquo= lsquo1rsquo= lsquo1rsquo

5 Intermediate form used Variant 1 Variant 2 Students are supposed to write the variant used by them

Data Structure used by Pass II1 OPTAB A table of mnemonic opcodes and related information2 SYMTAB The symbol table 3 LITTAB A table of literals used in the program4 Intermediate code generated by Pass I5 Output file containing Target code error listing

Algorithm 1 Open the source file in input mode2 if end of file of source file go to step 83 Read the next line of the source program4 Separate the line into words These words could be stored in array of strings5 Search for first word is mnemonic opcode table if not present it is a label add this as a symbol in symbol table with current LC And then search for second word in mnemonic opcode table6 If instruction is found case 1 imperative statement case 2 Declarative statement case 3 Assembler Directive Generate Intermediate code and write to Intermediate code file7 go to step 2

Department of Information Technology

Symbol AddressLoop 204Next 214

Pad DrDYPatil Institute Of Technology Pimpri

8 Close source file and open intermediate code file9 If end of file ( Intermediate code) go to step 1310 Read next line from intermediate code file11 Write opcode register code and address of memory( to be fetched from literal or symbol table depending on the case) onto target file This is to be done only for Imperative statement12 go to step 9 13 Close all files14 Display symbol table literal table and target file

Imperative statement case 1 If opcode gt= 1 ampamp opcode lt=8 ( Instruction requires register operand)

a Set type as IS get opcode get register code and make entry into symbol or literal table as the case may be In case of symbol used as operand LC field is not known so LC could be -1 Perform LC processing LC++ Updating of symbol table should consider error handling

2 if opcode is 00 ( stop) Set all fields of Intermediate call as 00 LC++

3 else register operand not required ( Read and Print)Same as case 1 only register code is not required so set it to zero Here again update the symbol table LC++

On similar lines we can identify the cases for declarative and assembler directive statements based on opcode

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for first pass of two pass assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for Pass II of II pass Assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

List of hypothetical instructions

Instruction Assembly Remarks

Opcode mnemonic

00 STOP stop execution01 ADD first operand modified condition code set

02 SUB first operand modified condition code set03 MULT first operand modified condition code set04 MOVER register memory05 MOVEM memory register06 COMP sets condition code 07 BC branch on condition code 08 DIV analogous to SUB09 READ first operand is not used10 PRINT first operand is not used

Sample Input amp Output -

SAMPLE INPUT FILE SAMPLE OUTPUT FILE OF INTERMEDIATE CODE Using Variant One START 202 MOVER AREG =rsquo5 202) (IS04)(1)(L1) MOVEM AREG A 203) (IS05)(1)(S1) LOOP MOVER AREG A 204) (IS04)(1)(S1)

MOVER CREG B 205) (IS04)(3)(S3) ADD CREG = lsquo1rsquo 206) (IS01)(3)(L2)

MOVEM CREG B 207) (IS05)(2)(S3) SUB CREGA 208) (IS02)(3)(S1)

BC ANY NEXT 209) (IS07)(6)(S4) LTORG 210) (AD 04)

211) ADD CREGB rsquo 212) (IS01)(3)(S2)

BC LE LOOP 213) (IS07)(2)(S2) NEXT SUB AREG = lsquo1rsquo 214) (IS02)(1)(L3) BC LT BACK 215) (IS07)(1)(S5) STOP 216) (IS00) ORIGIN 219 217) (AD03) MULT CREG B 219) (IS03)(3)(S3) A DS 1 220) (DL02) (C1) BACK EQU LOOP 221) (AD02) B DS 1 221) (DL02) (C1)

END 222) (AD05)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

SYMBOL TABLE

Index Symbol Address1 A 2202 LOOP 2043 B 2214 NEXT 2145 BACK 204

POOL TABLE LITERAL TABLE

LIT_Ind Index LITERAL ADDRESS01 1 5 21003 2 1 211

3 1 222

SAMPLE INPUT FILE SAMPLE OUTPUT FILE FOR TARGET CODE START 202 MOVER AREG =rsquo5 202) 04 1 210 MOVEM AREG A 203) 05 1 220 LOOP MOVER AREG A 204) 04 1 220

MOVER CREG B 205) 04 3 221 ADD CREG = lsquo1rsquo 206) 01 3 211

MOVEM CREG B 207) 05 3 221 SUB CREG A 208) 03 3 220

BC ANY NEXT 209) 07 6 214 LTORG 210) 00 0 005

211) 00 0 001 ADD CREG B rsquo 212) 01 3 221

BC LE LOOP 213) 07 2 204 NEXT SUB AREG = lsquo1rsquo 214) 02 1 222 BC LT BACK 215) 07 1 220 STOP 216) 00 0 000 ORIGIN 219 217) MULT CREG B 219) 03 3 221 A DS 1 220) BACK EQU LOOP 221) B DS 1 221)

END 222) 00 0 001

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Instructions to the Students -Students are supposed to write about the organizations of the different data structures such as array link list etcAssumptions and limitations if any should be clearly mentionedStudents are supposed to create two output files one without errors amp one with errors indicating the type of error At least following errors must be handled

Errors -

Forward reference(Symbol used but not defined) -

This error occurs when some symbol is used but it is not defined into the program

Duplication of Symbol -

This error occurs when some symbol is declared more than once in the program

Mnemonic error

If there is invalid instruction then this error will occur

Register error -

If there is invalid register then this error will occur

Operand error -

This error will occur when there is an error in the operand field

Instructions Student must use incremental approach at the time of

developmentieconsider simple input first and then go for complex input

Platform DOS ( language ndash C)------------------------------------------------------------------------------------------------------------

FAQs

1 Explain what is meant by pass of an assembler2 Explain the need for two pass assembler3 Explain terms such as Forward Reference and backward reference4 Explain various types of errors that are handled in two different passes5 Explain the need of Intermediate Code generation and the variants used6 State various tables used and their significance in the design of two pass

Assembler------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 6: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

8 Close source file and open intermediate code file9 If end of file ( Intermediate code) go to step 1310 Read next line from intermediate code file11 Write opcode register code and address of memory( to be fetched from literal or symbol table depending on the case) onto target file This is to be done only for Imperative statement12 go to step 9 13 Close all files14 Display symbol table literal table and target file

Imperative statement case 1 If opcode gt= 1 ampamp opcode lt=8 ( Instruction requires register operand)

a Set type as IS get opcode get register code and make entry into symbol or literal table as the case may be In case of symbol used as operand LC field is not known so LC could be -1 Perform LC processing LC++ Updating of symbol table should consider error handling

2 if opcode is 00 ( stop) Set all fields of Intermediate call as 00 LC++

3 else register operand not required ( Read and Print)Same as case 1 only register code is not required so set it to zero Here again update the symbol table LC++

On similar lines we can identify the cases for declarative and assembler directive statements based on opcode

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for first pass of two pass assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for Pass II of II pass Assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

List of hypothetical instructions

Instruction Assembly Remarks

Opcode mnemonic

00 STOP stop execution01 ADD first operand modified condition code set

02 SUB first operand modified condition code set03 MULT first operand modified condition code set04 MOVER register memory05 MOVEM memory register06 COMP sets condition code 07 BC branch on condition code 08 DIV analogous to SUB09 READ first operand is not used10 PRINT first operand is not used

Sample Input amp Output -

SAMPLE INPUT FILE SAMPLE OUTPUT FILE OF INTERMEDIATE CODE Using Variant One START 202 MOVER AREG =rsquo5 202) (IS04)(1)(L1) MOVEM AREG A 203) (IS05)(1)(S1) LOOP MOVER AREG A 204) (IS04)(1)(S1)

MOVER CREG B 205) (IS04)(3)(S3) ADD CREG = lsquo1rsquo 206) (IS01)(3)(L2)

MOVEM CREG B 207) (IS05)(2)(S3) SUB CREGA 208) (IS02)(3)(S1)

BC ANY NEXT 209) (IS07)(6)(S4) LTORG 210) (AD 04)

211) ADD CREGB rsquo 212) (IS01)(3)(S2)

BC LE LOOP 213) (IS07)(2)(S2) NEXT SUB AREG = lsquo1rsquo 214) (IS02)(1)(L3) BC LT BACK 215) (IS07)(1)(S5) STOP 216) (IS00) ORIGIN 219 217) (AD03) MULT CREG B 219) (IS03)(3)(S3) A DS 1 220) (DL02) (C1) BACK EQU LOOP 221) (AD02) B DS 1 221) (DL02) (C1)

END 222) (AD05)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

SYMBOL TABLE

Index Symbol Address1 A 2202 LOOP 2043 B 2214 NEXT 2145 BACK 204

POOL TABLE LITERAL TABLE

LIT_Ind Index LITERAL ADDRESS01 1 5 21003 2 1 211

3 1 222

SAMPLE INPUT FILE SAMPLE OUTPUT FILE FOR TARGET CODE START 202 MOVER AREG =rsquo5 202) 04 1 210 MOVEM AREG A 203) 05 1 220 LOOP MOVER AREG A 204) 04 1 220

MOVER CREG B 205) 04 3 221 ADD CREG = lsquo1rsquo 206) 01 3 211

MOVEM CREG B 207) 05 3 221 SUB CREG A 208) 03 3 220

BC ANY NEXT 209) 07 6 214 LTORG 210) 00 0 005

211) 00 0 001 ADD CREG B rsquo 212) 01 3 221

BC LE LOOP 213) 07 2 204 NEXT SUB AREG = lsquo1rsquo 214) 02 1 222 BC LT BACK 215) 07 1 220 STOP 216) 00 0 000 ORIGIN 219 217) MULT CREG B 219) 03 3 221 A DS 1 220) BACK EQU LOOP 221) B DS 1 221)

END 222) 00 0 001

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Instructions to the Students -Students are supposed to write about the organizations of the different data structures such as array link list etcAssumptions and limitations if any should be clearly mentionedStudents are supposed to create two output files one without errors amp one with errors indicating the type of error At least following errors must be handled

Errors -

Forward reference(Symbol used but not defined) -

This error occurs when some symbol is used but it is not defined into the program

Duplication of Symbol -

This error occurs when some symbol is declared more than once in the program

Mnemonic error

If there is invalid instruction then this error will occur

Register error -

If there is invalid register then this error will occur

Operand error -

This error will occur when there is an error in the operand field

Instructions Student must use incremental approach at the time of

developmentieconsider simple input first and then go for complex input

Platform DOS ( language ndash C)------------------------------------------------------------------------------------------------------------

FAQs

1 Explain what is meant by pass of an assembler2 Explain the need for two pass assembler3 Explain terms such as Forward Reference and backward reference4 Explain various types of errors that are handled in two different passes5 Explain the need of Intermediate Code generation and the variants used6 State various tables used and their significance in the design of two pass

Assembler------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 7: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for first pass of two pass assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for Pass II of II pass Assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

List of hypothetical instructions

Instruction Assembly Remarks

Opcode mnemonic

00 STOP stop execution01 ADD first operand modified condition code set

02 SUB first operand modified condition code set03 MULT first operand modified condition code set04 MOVER register memory05 MOVEM memory register06 COMP sets condition code 07 BC branch on condition code 08 DIV analogous to SUB09 READ first operand is not used10 PRINT first operand is not used

Sample Input amp Output -

SAMPLE INPUT FILE SAMPLE OUTPUT FILE OF INTERMEDIATE CODE Using Variant One START 202 MOVER AREG =rsquo5 202) (IS04)(1)(L1) MOVEM AREG A 203) (IS05)(1)(S1) LOOP MOVER AREG A 204) (IS04)(1)(S1)

MOVER CREG B 205) (IS04)(3)(S3) ADD CREG = lsquo1rsquo 206) (IS01)(3)(L2)

MOVEM CREG B 207) (IS05)(2)(S3) SUB CREGA 208) (IS02)(3)(S1)

BC ANY NEXT 209) (IS07)(6)(S4) LTORG 210) (AD 04)

211) ADD CREGB rsquo 212) (IS01)(3)(S2)

BC LE LOOP 213) (IS07)(2)(S2) NEXT SUB AREG = lsquo1rsquo 214) (IS02)(1)(L3) BC LT BACK 215) (IS07)(1)(S5) STOP 216) (IS00) ORIGIN 219 217) (AD03) MULT CREG B 219) (IS03)(3)(S3) A DS 1 220) (DL02) (C1) BACK EQU LOOP 221) (AD02) B DS 1 221) (DL02) (C1)

END 222) (AD05)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

SYMBOL TABLE

Index Symbol Address1 A 2202 LOOP 2043 B 2214 NEXT 2145 BACK 204

POOL TABLE LITERAL TABLE

LIT_Ind Index LITERAL ADDRESS01 1 5 21003 2 1 211

3 1 222

SAMPLE INPUT FILE SAMPLE OUTPUT FILE FOR TARGET CODE START 202 MOVER AREG =rsquo5 202) 04 1 210 MOVEM AREG A 203) 05 1 220 LOOP MOVER AREG A 204) 04 1 220

MOVER CREG B 205) 04 3 221 ADD CREG = lsquo1rsquo 206) 01 3 211

MOVEM CREG B 207) 05 3 221 SUB CREG A 208) 03 3 220

BC ANY NEXT 209) 07 6 214 LTORG 210) 00 0 005

211) 00 0 001 ADD CREG B rsquo 212) 01 3 221

BC LE LOOP 213) 07 2 204 NEXT SUB AREG = lsquo1rsquo 214) 02 1 222 BC LT BACK 215) 07 1 220 STOP 216) 00 0 000 ORIGIN 219 217) MULT CREG B 219) 03 3 221 A DS 1 220) BACK EQU LOOP 221) B DS 1 221)

END 222) 00 0 001

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Instructions to the Students -Students are supposed to write about the organizations of the different data structures such as array link list etcAssumptions and limitations if any should be clearly mentionedStudents are supposed to create two output files one without errors amp one with errors indicating the type of error At least following errors must be handled

Errors -

Forward reference(Symbol used but not defined) -

This error occurs when some symbol is used but it is not defined into the program

Duplication of Symbol -

This error occurs when some symbol is declared more than once in the program

Mnemonic error

If there is invalid instruction then this error will occur

Register error -

If there is invalid register then this error will occur

Operand error -

This error will occur when there is an error in the operand field

Instructions Student must use incremental approach at the time of

developmentieconsider simple input first and then go for complex input

Platform DOS ( language ndash C)------------------------------------------------------------------------------------------------------------

FAQs

1 Explain what is meant by pass of an assembler2 Explain the need for two pass assembler3 Explain terms such as Forward Reference and backward reference4 Explain various types of errors that are handled in two different passes5 Explain the need of Intermediate Code generation and the variants used6 State various tables used and their significance in the design of two pass

Assembler------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 8: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Algorithm for Pass II of II pass Assembler

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

List of hypothetical instructions

Instruction Assembly Remarks

Opcode mnemonic

00 STOP stop execution01 ADD first operand modified condition code set

02 SUB first operand modified condition code set03 MULT first operand modified condition code set04 MOVER register memory05 MOVEM memory register06 COMP sets condition code 07 BC branch on condition code 08 DIV analogous to SUB09 READ first operand is not used10 PRINT first operand is not used

Sample Input amp Output -

SAMPLE INPUT FILE SAMPLE OUTPUT FILE OF INTERMEDIATE CODE Using Variant One START 202 MOVER AREG =rsquo5 202) (IS04)(1)(L1) MOVEM AREG A 203) (IS05)(1)(S1) LOOP MOVER AREG A 204) (IS04)(1)(S1)

MOVER CREG B 205) (IS04)(3)(S3) ADD CREG = lsquo1rsquo 206) (IS01)(3)(L2)

MOVEM CREG B 207) (IS05)(2)(S3) SUB CREGA 208) (IS02)(3)(S1)

BC ANY NEXT 209) (IS07)(6)(S4) LTORG 210) (AD 04)

211) ADD CREGB rsquo 212) (IS01)(3)(S2)

BC LE LOOP 213) (IS07)(2)(S2) NEXT SUB AREG = lsquo1rsquo 214) (IS02)(1)(L3) BC LT BACK 215) (IS07)(1)(S5) STOP 216) (IS00) ORIGIN 219 217) (AD03) MULT CREG B 219) (IS03)(3)(S3) A DS 1 220) (DL02) (C1) BACK EQU LOOP 221) (AD02) B DS 1 221) (DL02) (C1)

END 222) (AD05)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

SYMBOL TABLE

Index Symbol Address1 A 2202 LOOP 2043 B 2214 NEXT 2145 BACK 204

POOL TABLE LITERAL TABLE

LIT_Ind Index LITERAL ADDRESS01 1 5 21003 2 1 211

3 1 222

SAMPLE INPUT FILE SAMPLE OUTPUT FILE FOR TARGET CODE START 202 MOVER AREG =rsquo5 202) 04 1 210 MOVEM AREG A 203) 05 1 220 LOOP MOVER AREG A 204) 04 1 220

MOVER CREG B 205) 04 3 221 ADD CREG = lsquo1rsquo 206) 01 3 211

MOVEM CREG B 207) 05 3 221 SUB CREG A 208) 03 3 220

BC ANY NEXT 209) 07 6 214 LTORG 210) 00 0 005

211) 00 0 001 ADD CREG B rsquo 212) 01 3 221

BC LE LOOP 213) 07 2 204 NEXT SUB AREG = lsquo1rsquo 214) 02 1 222 BC LT BACK 215) 07 1 220 STOP 216) 00 0 000 ORIGIN 219 217) MULT CREG B 219) 03 3 221 A DS 1 220) BACK EQU LOOP 221) B DS 1 221)

END 222) 00 0 001

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Instructions to the Students -Students are supposed to write about the organizations of the different data structures such as array link list etcAssumptions and limitations if any should be clearly mentionedStudents are supposed to create two output files one without errors amp one with errors indicating the type of error At least following errors must be handled

Errors -

Forward reference(Symbol used but not defined) -

This error occurs when some symbol is used but it is not defined into the program

Duplication of Symbol -

This error occurs when some symbol is declared more than once in the program

Mnemonic error

If there is invalid instruction then this error will occur

Register error -

If there is invalid register then this error will occur

Operand error -

This error will occur when there is an error in the operand field

Instructions Student must use incremental approach at the time of

developmentieconsider simple input first and then go for complex input

Platform DOS ( language ndash C)------------------------------------------------------------------------------------------------------------

FAQs

1 Explain what is meant by pass of an assembler2 Explain the need for two pass assembler3 Explain terms such as Forward Reference and backward reference4 Explain various types of errors that are handled in two different passes5 Explain the need of Intermediate Code generation and the variants used6 State various tables used and their significance in the design of two pass

Assembler------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 9: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

List of hypothetical instructions

Instruction Assembly Remarks

Opcode mnemonic

00 STOP stop execution01 ADD first operand modified condition code set

02 SUB first operand modified condition code set03 MULT first operand modified condition code set04 MOVER register memory05 MOVEM memory register06 COMP sets condition code 07 BC branch on condition code 08 DIV analogous to SUB09 READ first operand is not used10 PRINT first operand is not used

Sample Input amp Output -

SAMPLE INPUT FILE SAMPLE OUTPUT FILE OF INTERMEDIATE CODE Using Variant One START 202 MOVER AREG =rsquo5 202) (IS04)(1)(L1) MOVEM AREG A 203) (IS05)(1)(S1) LOOP MOVER AREG A 204) (IS04)(1)(S1)

MOVER CREG B 205) (IS04)(3)(S3) ADD CREG = lsquo1rsquo 206) (IS01)(3)(L2)

MOVEM CREG B 207) (IS05)(2)(S3) SUB CREGA 208) (IS02)(3)(S1)

BC ANY NEXT 209) (IS07)(6)(S4) LTORG 210) (AD 04)

211) ADD CREGB rsquo 212) (IS01)(3)(S2)

BC LE LOOP 213) (IS07)(2)(S2) NEXT SUB AREG = lsquo1rsquo 214) (IS02)(1)(L3) BC LT BACK 215) (IS07)(1)(S5) STOP 216) (IS00) ORIGIN 219 217) (AD03) MULT CREG B 219) (IS03)(3)(S3) A DS 1 220) (DL02) (C1) BACK EQU LOOP 221) (AD02) B DS 1 221) (DL02) (C1)

END 222) (AD05)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

SYMBOL TABLE

Index Symbol Address1 A 2202 LOOP 2043 B 2214 NEXT 2145 BACK 204

POOL TABLE LITERAL TABLE

LIT_Ind Index LITERAL ADDRESS01 1 5 21003 2 1 211

3 1 222

SAMPLE INPUT FILE SAMPLE OUTPUT FILE FOR TARGET CODE START 202 MOVER AREG =rsquo5 202) 04 1 210 MOVEM AREG A 203) 05 1 220 LOOP MOVER AREG A 204) 04 1 220

MOVER CREG B 205) 04 3 221 ADD CREG = lsquo1rsquo 206) 01 3 211

MOVEM CREG B 207) 05 3 221 SUB CREG A 208) 03 3 220

BC ANY NEXT 209) 07 6 214 LTORG 210) 00 0 005

211) 00 0 001 ADD CREG B rsquo 212) 01 3 221

BC LE LOOP 213) 07 2 204 NEXT SUB AREG = lsquo1rsquo 214) 02 1 222 BC LT BACK 215) 07 1 220 STOP 216) 00 0 000 ORIGIN 219 217) MULT CREG B 219) 03 3 221 A DS 1 220) BACK EQU LOOP 221) B DS 1 221)

END 222) 00 0 001

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Instructions to the Students -Students are supposed to write about the organizations of the different data structures such as array link list etcAssumptions and limitations if any should be clearly mentionedStudents are supposed to create two output files one without errors amp one with errors indicating the type of error At least following errors must be handled

Errors -

Forward reference(Symbol used but not defined) -

This error occurs when some symbol is used but it is not defined into the program

Duplication of Symbol -

This error occurs when some symbol is declared more than once in the program

Mnemonic error

If there is invalid instruction then this error will occur

Register error -

If there is invalid register then this error will occur

Operand error -

This error will occur when there is an error in the operand field

Instructions Student must use incremental approach at the time of

developmentieconsider simple input first and then go for complex input

Platform DOS ( language ndash C)------------------------------------------------------------------------------------------------------------

FAQs

1 Explain what is meant by pass of an assembler2 Explain the need for two pass assembler3 Explain terms such as Forward Reference and backward reference4 Explain various types of errors that are handled in two different passes5 Explain the need of Intermediate Code generation and the variants used6 State various tables used and their significance in the design of two pass

Assembler------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 10: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

SYMBOL TABLE

Index Symbol Address1 A 2202 LOOP 2043 B 2214 NEXT 2145 BACK 204

POOL TABLE LITERAL TABLE

LIT_Ind Index LITERAL ADDRESS01 1 5 21003 2 1 211

3 1 222

SAMPLE INPUT FILE SAMPLE OUTPUT FILE FOR TARGET CODE START 202 MOVER AREG =rsquo5 202) 04 1 210 MOVEM AREG A 203) 05 1 220 LOOP MOVER AREG A 204) 04 1 220

MOVER CREG B 205) 04 3 221 ADD CREG = lsquo1rsquo 206) 01 3 211

MOVEM CREG B 207) 05 3 221 SUB CREG A 208) 03 3 220

BC ANY NEXT 209) 07 6 214 LTORG 210) 00 0 005

211) 00 0 001 ADD CREG B rsquo 212) 01 3 221

BC LE LOOP 213) 07 2 204 NEXT SUB AREG = lsquo1rsquo 214) 02 1 222 BC LT BACK 215) 07 1 220 STOP 216) 00 0 000 ORIGIN 219 217) MULT CREG B 219) 03 3 221 A DS 1 220) BACK EQU LOOP 221) B DS 1 221)

END 222) 00 0 001

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Instructions to the Students -Students are supposed to write about the organizations of the different data structures such as array link list etcAssumptions and limitations if any should be clearly mentionedStudents are supposed to create two output files one without errors amp one with errors indicating the type of error At least following errors must be handled

Errors -

Forward reference(Symbol used but not defined) -

This error occurs when some symbol is used but it is not defined into the program

Duplication of Symbol -

This error occurs when some symbol is declared more than once in the program

Mnemonic error

If there is invalid instruction then this error will occur

Register error -

If there is invalid register then this error will occur

Operand error -

This error will occur when there is an error in the operand field

Instructions Student must use incremental approach at the time of

developmentieconsider simple input first and then go for complex input

Platform DOS ( language ndash C)------------------------------------------------------------------------------------------------------------

FAQs

1 Explain what is meant by pass of an assembler2 Explain the need for two pass assembler3 Explain terms such as Forward Reference and backward reference4 Explain various types of errors that are handled in two different passes5 Explain the need of Intermediate Code generation and the variants used6 State various tables used and their significance in the design of two pass

Assembler------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 11: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Instructions to the Students -Students are supposed to write about the organizations of the different data structures such as array link list etcAssumptions and limitations if any should be clearly mentionedStudents are supposed to create two output files one without errors amp one with errors indicating the type of error At least following errors must be handled

Errors -

Forward reference(Symbol used but not defined) -

This error occurs when some symbol is used but it is not defined into the program

Duplication of Symbol -

This error occurs when some symbol is declared more than once in the program

Mnemonic error

If there is invalid instruction then this error will occur

Register error -

If there is invalid register then this error will occur

Operand error -

This error will occur when there is an error in the operand field

Instructions Student must use incremental approach at the time of

developmentieconsider simple input first and then go for complex input

Platform DOS ( language ndash C)------------------------------------------------------------------------------------------------------------

FAQs

1 Explain what is meant by pass of an assembler2 Explain the need for two pass assembler3 Explain terms such as Forward Reference and backward reference4 Explain various types of errors that are handled in two different passes5 Explain the need of Intermediate Code generation and the variants used6 State various tables used and their significance in the design of two pass

Assembler------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 12: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 02

Aim Implementation of Macro Processor Following cases to be considereda) Macro without any parametersb) Macro with Positional Parametersc) Macro with Key word parametersd) Macro with positional and keyword parameters( Conditional expansion nested macro implementation not expected)

Objective1 To understand macro facility features and its use in assembly language

programming 2 To study how the macro definition is processed and how macro call results in the

expansion of code

Theory

An assembly language macro facility is to extend the set of operations provided in an assembly languageIn order that programmers can repeat identical parts of their program macro facility can be used This permits the programmer to define an abbreviation for a part of program amp use this abbreviation in the program This abbreviation is treated as macro definition amp saved by the macro processor For all occurrences the abbreviation ie macro call macro processor substitutes the definition

Macro definition part

It consists of 1 Macro Prototype Statement - this declares the name of macro amp types of

parameters2 Model statement - It is statement from which assembly language statement is

generated during macro expansion3 Preprocessor Statement - It is used to perform auxiliary function during macro

expansion

Macro Call amp Expansion

The operation defined by a macro can be used by writing a macro name in the mnemonic field and its operand in the operand field Appearance of the macro name in the mnemonic field leads to a macro call Macro call replaces such statements by sequence of statement comprising the macro This is known as macro expansion

Macro Facilities

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 13: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

1 Use of AIF amp AGO allows us alter the flow of control during expansion2 Loops can be implemented using expansion time variables

Design Procedure1 Definition processing - Scan all macro definitions and for each macro definition

enter the macro name in macro name table (MNT) Store entire macro definition in macro definition table (MDT) and add auxiliary information in MNT such as no of positional parameters (PP) no of key word parameters (KP) macro definition table position (MDTP) etc

2 Macro expansion - Examine all statement in assembly source program to detect the macro calls For each macro call locate the macro in MNT retrieve MDTP establish the correspondence between formal amp actual parameters and expand the macro

Data structures required for macro definition processing -

1 Macro Name Table [MNT] - Fields- Name of Macro pp (no of positional parameters) kp( no of keyword parameters) MDTP ( Macro Definition Table Pointer) Keyword Parameters Default Table Position (KPDTP) 2 Parameter Name Table [PNTAB] -

Fields - Parameter Name 3 Keyword parameter Default Table [KPDTAB] -

Fields - Parameter Name Default value4 Macro Definition Table [MDT] -

Model Statement are stored in the intermediate code from as Opcode Operands

Algorithm for definition processingBefore processing any definition initialize KPDTAB_ptr MDT_ptr to 0 and

MNT_ptr to -1 These table pointers are common to all macro definitions For each macro definition perform the following steps

1 Initialize PNTAB ndash ptr to 0 amp fields of MNT pp kp to 0 and increment MNT_ptr by 1

2 For macro prototype statement from MNT entrya Entry name into name fieldb For each position parameter field i Enter name in parameter name tableii Increment PNTAB ndash ptr by 1iii Increment pp by 1c KPDTP KPDTAB - ptr

d For each keyword parameteri Enter name amp default value in KPDTABii Increment KPTAB ndashptr by 1iii Enter name in PNTAB amp increment PNTAB ndash ptr by 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 14: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

iv Increment kp by 1e MDTP MDT ndash ptr ( current MDT Ptr)

3 Read next statement a Model statement

i For parameter generate specification (p n)ii Record intermediate code in MDTiii Increment MDT - ptr by 1

end b If MEND statement Begin

Enter MEND in MDT increment MDT_ptr by 1 If kp == 0 then KPDTP = 0 Return to main logic ie step 6 of main logic

Data structures required for expansion processing -

1 Actual parameter table APTAB2 Macro expansion counter MEC

Algorithm for macro expansion

1 Initialization i MEC MDTP from MNTii Create APTAB with pp amp kp entries and set APTAB ptr accordinglyiii Copy keyword parameter defaults from KPDTAB in APTAB[pp] to

APTAB[pp + kp -1]iv Process actual positional parameters in call and copy them in APTAB from 0

to pp-1v For keyword parameter specification search name in parameter name field of

KPDTAB get matching entry in q amp enter value in APTAB [ pp + q ndash KPDTP ]

2 While Statement pointed by MEC in MDT is not MENDi If model statement then replace operands of the form (p n) by values in

APTABii Increment MEC by oneiii Write the model statement on expanded code file

3 Exit from macro expansion

Main Program Logic 1 Initialize KPDTAB_ptr MDT_ptr to 0 and MNT_ptr to -1 These table

pointers are common to all macro definitions ( There could be more than one macro definition in program)

2 Read the statement from source file one line at time3 Separate the words from that line and count the no of words Save the

separated words in the array say word which is a array of strings

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 15: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

4 If count for words in a line is one then check if that only word matches with ldquoMACROrdquo keyword if MACRO keyword found then perform definition processing

5 If it does not match then check whether first word of the line matches with any of the entries in the macro name table (Search the complete macro name table for presence of macro call) if so then perform macro expansion routine

6 If no Macro call or no definition then enter the line as it is in the expanded code file

7 If not end of file go to step 3

Sample Input -

The assembly language program with macro definitions amp macro calls

MACRO MAC1 MOVER AREG M ADD BREG M MOVEM CREG M MEND MACRO EVAL ampXampYampZ MOVER AREG ampX SUB AREG ampY ADD AREG ampZ MOVER AREG ampZ MEND MACRO CALC ampXampYampOP=MULTampLAB=ampLAB MOVER AREG ampX ampOP AREG ampY MOVEM AREG ampX MEND START MOVEM AREG B EVAL A B C ADD AREG N MOVEM AREG N CALC P Q LAB=LOOP MOVEM AREG N MAC1 CALC PQOP=DIV LAB=NEXTM DS 1A DS 5B DS 1 C DS 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 16: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

N DS 1P DS 1Q DS 1 END

Sample output -

Macro name Table

Name pp kp mdtp kpdtpMAC1 0 0 0 0EVAL 3 0 4 0CALC 2 2 9 1

MACRO DEFINITION TABLE

Index Statements of the macros0 MOVER AREG M1 ADD BREG M2 MOVEM CREG M 3 MEND4 MOVER AREG (P0)5 SUB AREG (P1)6 ADD AREG (P2) 7 MOVER AREG (P2)8 MEND9 (P3) MOVER AREG (P0) 10 (P2) AREG (P1) 11 MOVEM AREG (P0) 12 MEND

Expanded code with no macro definition amp macro calls

START

MOVEM AREG B + MOVER AREG A expanded code of EVAL +SUB AREG B +ADD AREG C +MOVER AREG C ADD AREG N MOVEM AREG N + LOOP MOVER AREG P expanded code for CALC +MULT AREG Q +MOVEM AREG P MOVEM AREGN

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 17: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

+MOVER AREG M expanded code for MAC1 +ADD BREG M +MOVEM CREG M + NEXT MOVER AREG P expanded code for CALC +DIV AREG Q +MOVEM AREG PA DS 5B DS 1 C DS 1N DS 1P DS 1Q DS 1 END

Students are supposed to display PNTAB and APTAB also

Instructions Regarding testing of the program -

1 Students are expected to take minimum two macro definitions2 Positional amp keywords parameters are to be handled3 The code generated from macro expansion should be preceded

with + sign4 Students should write about error handling At least following

errors should be handled i) Positional parameters must precede the keyword

parametersii) Mismatch in total no of parameters in formal and

actual parameters5 Students should write about error handling6 Students are supposed to write assumptions limitations if any

Future Enhancement ndash ( NOT Mandatory)

The macro is defined within the macro or the call is within a call lt- Macro definition within macrolt- Macro call within a macro lt- Processing of AIF and AGO Instructions

The students should use incremental approach of development so that consider simple input first and then go for implementation of complex input

Platform DOS ( language ndash C)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 18: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

------------------------------------------------------------------------------------------------------------

FAQS

1 Define the term macro2 Distinguish between macro and a subroutine3 Define and Distinguish between parameters that can be used in macros4 State various tables used in processing the macro5 Explain the role of stack in nested macros

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 19: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 03

Aim Regular Expression to DFA ( To be taken from compiler point of view) The implementation to be done as per the algorithm covered in the book ldquoCompiler ndash Design and Principlesrdquo By Aho ndash Ullman Sethi

Objective - To understand the role of regular expressions and finite automata in applications such as Compilers

Theory -

Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language

Compiler is a program which converts the given source program in high-level language into an equivalent machine language While doing so it detects errors and reports errors This process is quite complex and it is divided into number of phases such as Lexical Analysis Syntax and Semantic Analysis Intermediate Code generation Code Generation and Code OptimizationThe lexical analysis phase of compiler reads the source program character by character and then groups these characters into tokens which are further passed to next phase which is nothing but parsing or syntax or semantic analysis After syntax and semantic analysis Intermediate Code is generated which is followed by actual code generationLexical Analyzer recognizes the tokens from series of characters A ldquoCrdquo program consists of tokens such as Identifiers Integers Floating Point Numbers Punctuation symbols relational and logical and arithmetic operators keywords and comments (to be removed) To identify these tokens lexical analyzer needs the specification of each of these symbols The set of words belonging to a particular token type is a regular language Hence each of these token types can be specified using regular expressions For example consider the token Identifier In most of the programming languages an identifier is a word which begins with an alphabet (capital or small) followed by zero or more letters or digits (09) This can be defined by the regular expression (letter) ( letter | digit) where letter = A|B|C|helliphellip|Z| a| b |c |helliphellip|z and digit = 0|1|2|hellip|9 One can specify all token types using regular expressions These regular expressions are then converted to DFArsquos in the form of DFA transition table Lexical analyzer reads a character from a source program and based on the current state and current symbol read makes a transition to some other state When it reaches a final state of DFA it groups the series of characters so far read and outputs the token foundFormal definition of Regular expressionThe class of regular expressions over sum is defined recursively as follows

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 20: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

1 The letters φ and Є are regular expressions over sum 2 Every letter lsquoarsquo c sumis a regular expression over sum 3 If lsquoR1rsquo and lsquoR are regular expressions over sum then so are lsquo(R1|R2)rsquo lsquo(R1R2)rsquo and (R1) Where lsquo|rsquo indicates alternative or parallel paths lsquorsquo Indicates concatenation lsquorsquo indicates closure4 The regular expressions are only those that are obtained using rules (1) and (2)Formal definition of DFAThe formal definition of finite automata is denoted by a tuple ( Q sumd qo f)Where Q Finite set of table sum finite input alphabet qo Initial state of FAqo qo Q F set of final states F c Q d Transition function called as state function mapping Q sum Q ie d= Qsum QA FA is called deterministic (DFA) if from every vertex of its transition graph there is an unique input symbol which takes the vertex state to the required next state

DFA is constructed directly from an augmented regular expression ( r ) We begin by constructing a syntax tree T for ( r ) and then we compute four functions Nullable Firstpos Lastpos and Followpos The functions Nullable Firstpos Lastpos are defined on the nodes of a syntax tree and are used to compute Followpos which is defined on set of positions We can short circuit the construction of NFA by building the DFA whose states correspond to the sets of positions in the tree Positions in particular encode the information regarding when one position can follow another Each symbol in an input string to a DFA can be matched by certain positions An input symbol lsquocrsquo can only be matched by positions at which there is a lsquocrsquo but not every position with a lsquocrsquo can necessarily match a particular occurrences of lsquocrsquo in input stream

Algorithm The steps in algorithm are

1 Accept the given regular expression with end of character as 2 Covert the regular expressions to its equivalent postfix form manually ( students

need not write the code for converting infix to postfix but they can directly accept postfix form of the infix expression)

3 Construct a syntax tree from the postfix expression obtained in step 24 Assign positions to leaf nodes5 Compute following functions

Nullable Firstpos Lastpos Followpos

Computation of Nullables All nodes except the nodes are not nullable

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 21: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Also if some leaf node is for έ then it is also nullable Firstpos (Firstposition) At each node n of the syntax tree of a regular expression we define a function firstpos(n) that gives the set of first positions that can match first symbol of a string generated by sub expression rooted at lsquonrsquo Lastpos (lastposition) At each node n of the syntax tree of a regular expression we define a function lastpos(n) that gives the set of last positions that can match last symbol of a string generated by sub expression rooted at lsquonrsquoTo compute firstposition and last position we need to know which nodes are the roots of sub expression that generate languages that include the empty string Such nodes are Nullable We define nullable(n) to be true if node lsquonrsquo is nullable false otherwise

Computation of Followpos Followpos(i) tells us what positions can follow position i in the syntax tree This can be computed as follows

1 if n is a lsquorsquo (cat) Node with a left child C1 and right child C2 and i is a position in the Lastpos(C1) then all positions in Firstpos(C2) are in Followpos(i)

2 if n is a (closure) Node and i is a position in the Lastpos(n) then all positions in Firstpos(n) are Followpos(i)

6 Construct DFA from Follow Pos

Note Step 5 can be done during construction of tree since you are building the tree from bottom to top and when computations at some root of sub tree are to be done information of sub tree is available So no need to do any traversalData Structures

Node Structure for Parse Tree Leftchild and Rightchild pointers to the node structure Nullable Boolean TypeData Character TypeFistpos and Lastpos set of integersPos integer (this may or may not be part of tree node)

Stack Stack is required to build the tree This can be implemented either using link list (preferable) or as an array Item or data that will be pushed into or popped out from stack is pointer to the node structure of a Parse Tree and not just a single character

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 22: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Computations of Firstpos and Lastpos

Node n Nullable(n) Firstpos(n) Lastpos(n)N is a leaf labeled έ

true oslash oslash

N

Nullable(c1) orNullable (c2)

Firstpos (C1) U Firstpos(C2)

Lastpos (C1) U Lasttpos(C2)

N is a leaf labeled with position i

false i i

nNullable(c1) andNullable (c2)

If nullable (C1) thenFirstpos (C1) U Firstpos(C2) else Firstpos(C1)

If nullable (C2) thenLastpos (C1) U Lastpos(C2) else Lastpos(C2)

ntrue Firstpos (C1) Lastpos (C1)

Algorithm for construction of DFA transition table2 Initially the only unmarked state in Dstates is firstpos(root) where root is the

root of a syntax tree3 While there is an unmarked state T in Dstates do

Begin Mark T For each input symbol a do Begin

Let U be the set of positions that are in Followpos(P) for some P in T such that the symbol at position P is a

If U is not empty and is not in Dstates then add U as an unmarked state to Dstates Dtran [Ta] = U End End

Department of Information Technology

C2C1

C2C1

| |

C1

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 23: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input Enter postfix form of the regular expression (r) and use dot operator for concatenation and lsquo|rsquo for OR and for closure For Example (r ) is (a| b) a b b then equivalent Postfix Expression is ab|abb

Sample output

Post Order Traversal Traversal of the tree is ab|abb

Students are Expected to Draw the Parse Tree and Show the Values Firstpos Lastpos and the values of Positions for leaf nodes

Node Position Nullable Firstpositon Lastposition a 1 0 1 1 b 2 0 2 2 | (or) 0 0 12 12 (closure) 0 1 12 12 a 3 0 3 3 (cat) 0 0 123 3 b 4 0 4 4 (cat) 0 0 123 4 b 5 0 5 5 (cat) 0 0 123 5 6 0 6 6 0 0 123 6

Follow Positions

Leaf Node position

Symbol at that position

Follow Positions

1 a 1232 b 1233 a 44 b 55 b 6

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 24: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

DFA ( graphical output is not expected at all)

Input Symbol

State a bA 123

B A

B (1234

B C

C1235

B D

D1236

B A

b

01start a b b

Instructions to the students for testing Test your program for following regular expressions

i) (a|b)ii) (a|b)iii) (a|b)abb(a|b)

Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Define regular expression and DFA2 Give applications where regular expressions could be used3 Explain the significance of firstpositionlastposition and followpositin in the

algorithm4 Any alternate method to convert RE to DFA If yes compare the methods from

space and time complexity point of view------------------------------------------------------------------------------------------------------------

Department of Information Technology

A DCB

a

a

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 25: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 04

Aim Write a program in C to implement Lexical Analyzer for the subset of C

Objective

1 To understand the basic principles in compilation2 To study lexical analysis phase of compiler

Theory

Compiler takes input as a source program amp produces output as an equivalent sequence of machine instructions This process consists of two-step processing of source program

1 Analysis of source program2 Synthesis of target program

Analysis step

It consists of three sub steps1 Lexical Analysis - Determine lexical constituents in source program2 Syntax Analysis - Determine structure of source string3 Semantic Analysis - Determine meaning of source string

Synthesis Step

It deals with memory allocation amp code generation The actions in analysis phase are uniquely defined for a given language But synthesis step consists of many action instances where actions depend on the aspect concerning the execution environment of the compilereg - Operating system interfaces target machine features such as instruction set addressing modes etc

Lexical analysis The action of scanning the source program into proper syntactic classes is known as lexical analysis

Task of Lexical Analysis

1 To scan the program into basic elements or tokens of the language2 To build the Uniform symbol table (table of tokens)3 To build the symbol amp literal table4 To remove white spaces amp comments5 To detect errors such as invalid identifier or constant

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 26: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Data structures Databases required

1 Source program - Original Source program which is scanned by compiler as string of characters

2 Terminal Table - A permanent database that has entry for each terminal symbols such as arithmetic operators keywords punctuation characters such as lsquorsquo lsquorsquoetc Fields Name of the symbol

3 Literal Table - This table is created during lexical analysis so as to describe all literals in the program

Fields Name of the literal4 Identifier Table - Created during lexical analysis and describes all identifiers in

the program

Fields Name of the identifier

5 Uniform Symbol Table - Created during lexical analysis to represent the program as a string of tokens rather than of individual characters Each uniform symbol contains the identification of the table to which it belongs( IDN - Identifier table LIT - Literal Table TRM - Terminal Symbol Table)and index within that table

6 Buffer - One buffer or two buffer schemes to load source program part by part to reduce disk io

Keywords can be stored either in the terminal table or in the keyword table

Basic steps in algorithm

1 Initialize line no to 12 Read the source program line by line ( Buffer is line)3 For each line separate the tokens such as

i) identifier function name keywords Follow the transition diagram to detect this ie letter followed by letter or digit Search in keyword table for existence of keyword otherwise it is identifier or function name

ii) Integer Constant Digit followed by digitiii) All types of operators such as gt gt= ++ + - etciv) Remove comments of the type v) Remove all white spaces

4 Assign a line no and increment a line number5 Repeat steps 2-4 till end of file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 27: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Sample Input A program in C language

main() int i no sum max sum =0 max = -32767 for ( i = 0 ilt 10 i++) scanf(ldquodrdquo ampno) sum = sum + no if ( max gt no) max = no printf(ldquosum = d max = dnrdquo sum max) getch()

Very Important Do not have include statement as part of C program over here Otherwise you have to do pre-processor handling

Sample Output

Terminal Table

() +- =ampltgtldquo++

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 28: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Keyword Table

forifintfloat

Identifier Table

identifier Attributemain Function nameinosummaxscanf Function nameprintf Function namegetch Function name

Literal Table

Literal attribute0 Numeric constant-32767 Numeric constant10 Numeric constantldquodrdquo String constantldquosum = d max = dnrdquo String constant

Uniform Symbol Table

Type Indexmain IDN 0( TRM 0) TRM 1 TRM 2int KEY 0i IDN 1 TRM 8no IDN 2 TRM 8sum IDN 3 TRM 8

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 29: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

max IDN 4 TRM 9sum IDN 3= TRM 60 LIT 0 TRM 9max IDN 4= TRM 6-32767 LIT 1 TRM 9for KEY 0( TRM 0i IDN I= TRM 60 LIT 0

Instructions1 Students are supposed to draw the transition state diagrams for identifying

literals identifiers relational operators comments etc2 Students should use a buffer to load a part of source program to avoid large

disk io3 Efficient search algorithms should be implemented for optimum time

complexity4 Students should handle lexical errors such as invalid identifier or literal5 Any assumptions should be clearly mentioned

Reference Systems Programming by Donavan

Instructions The students should clearly show in their program the processing of Comment lines tabs white spaces new line characters etc in lexical analysis processPlatform DOS ( Language ndash C)------------------------------------------------------------------------------------------------------------FAQrsquos

1 What are the different phases of the compiler2 What is the role of lexical analysis in the process of compilation3 What are the tables data structures used as input and output of lexical analysis4 How keywords and identifiers are distinguished and processed5 What types of errors are generated by lexical analysis phase of compiler

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 30: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 05

Aim Design and Implement Screen editor in C Language

Objective To study different terms of editors types of editors Design a screen editor

Theory Write in brief Role of Editor Types of Editor Use the concept of buffer to implement screen area Students can also use video memory concept for this assignment

Input

Output ( Expected )

The above source program is to be stored in a separate C file and the expected outputs are

Movement of cursor (up down left right) Building up menu with New File File saveFile open Providing Keyboard shortcuts Delete and backspace

Algorithm Students are expected to draw a flow chart or algorithm of the program written according to the assumptions and applied logic

Instructions The students are expected to know the various types and functions of editors in detail Platform DOS ( Language ndash C)

------------------------------------------------------------------------------------------------------------FAQs

1 Which are different types of screen editor2 What do you mean by scan code

------------------------------------------------------------------------------------------------------------

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 31: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 06

Aim Implementation of Calculator using LEX and YACC

Objective To study the process of lexical analysis and parsing

TheoryDuring the first phase the compiler reads the input and converts strings in the source to tokens With regular expressions we can specify patterns to lex so it can generate code that will allow it to scan and match strings in the input Each pattern specified in the input to lex has an associated action Typically an action returns a token that represents the matched string for subsequent use by the parser Initially we will simply print the matched string rather than return a token value The following represents a simple pattern composed of a regular expression that scans for identifiers Lex will read this pattern and produce C code for a lexical analyzer that scans for identifiers

letter(letter|digit) This pattern matches a string of characters that begins with a single letter followed by zero or more letters or digits This example nicely illustrates operations allowed in regular expressions

repetition expressed by the ldquordquo operator

alternation expressed by the ldquo|rdquo operator

History of Lex amp Yacc

1048698 Lex amp Yacc were developed at Bell Laboratories in the 70rsquos1048698Yacc was developed as the first of the two by Stephen C Johnson1048698 Lex was designed by Mike E Lesk and Eric Schmidt to work with Yacc1048698 Standard UNIX utilities

Lex amp Yacc1048698Programming Tools for writers of compilers and interpreters1048698Also interesting for non-compilerwriters1048698Any application looking for patterns in its input or having aninputcommand language is acandiate for LexYacc

lex and yacc help you write programs that transform structured inputndash lex -- generates a lexical analyzerbull divides a stream of input characters into meaningful units (lexemes) identifies them (token) and may pass the token to a parser generator yaccbull lex specifications are regular expressions ndash yacc -- generates a parserbull may do syntax checking only or create an interpreterbull yacc specifications are grammar components

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 32: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Lex

1048698 The Unix program ldquolexrdquo is a ldquoLexical Analyzer Generatorrdquondash Takes a high-level description of lexical tokens and actionsndash Generates C subroutines that implement the lexical analysisbull The name of the resulting subroutine is ldquoyylexrdquo1048698 Generally yylex is linked to other routines such as the parsing procedures generated by YACC

Yacc and Lex

Organization of a Lex program

ltdeclarationsgtlttranslation rulesgtltauxiliary proceduresgt

1048698 Translation rules consist of a sequence of patterns associated with actions1048698 Lex reads the file and generates a scannerndash Repeatedly locates the ldquolongest prefix of the input that is matched by one or more of the patternsrdquondash When the action is found lex executes the associated actionndash In the case of a tiebull Use whichever regexp uses the most characters

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 33: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

bull If same number of characters the first rule wins

Regular Expressions in Lex1048698 References to a single characterndash x the character xndash x an x even if x is an operatorndash x an x even if x is an operatorndash (x) an xndash [xy] the character x or yndash [x-z] the character x y or zndash [ˆx] any character except xndash any character except newline1048698 Repetitions and optionsndash x an optional xndash x 012 instances of xndash x+ 123 instances of x

Yacc Introduction

1048698 Yacc is a theoretically complicated but ldquoeasyrdquo to use program that parses input files to verify that they correspond to a certain language1048698 Your main program calls yyparse() to parse the input file1048698 The compiled YACC program automatically calls yylex() which is in lexyyc1048698 You really need a Makefile to keep it all straight1048698Yacc takes a grammar that you specify (in BNF form) and produces a parser that recognizes valid sentences in your language1048698Can generate interpreters also if you include an action for each statement that is executed when the statement is recognized (completed)

The Yacc Parser1048698 Parser reads tokens if token does not complete a rule it is pushed on a stack and the parser switches to a new state reflecting the token it just read1048698 When it finds all tokens that constitute the right hand side of a rule it pops of the right hand symbols from the stack and pushes the left hand symbol on the stack (called a reduction)1048698 Whenever yacc reduces a rule it executes the user code associated with the rule1048698 Parser is referred to as a shiftreduce parser1048698 yacc cannot run alone -- it needs lex

Organization of a Yacc file1048698Definition section cndash Declarations of tokens used in grammar the types of values used on the parser stack and other odds and endsndash For example token PLUS MINUS TIMES DIVIDE

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 34: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

ndash Declaration of non-terminals unionetc1048698 Rules sectionndash A list of grammar rules in BNF formndash Examplendash Each rule may or may not have an associated action (actions Communication between Lex and Yacc1048698 Whenever Lex returns a token to the parser that has an associated value the lexer must store the value in the global variable yylval before it returns1048698 The variable yylval is of the type YYSTYPE this type is defined in the file yytabh (created by yacc using the option lsquondashdrsquo)1048698 By default it is integer1048698 If you want to have tokens of multiple valued types you have to list all the values using the union declaration

Yacc file for the calculator example(calcy)

token NUMBER PLUS MINUS TIMES DIVIDEleft MINUS PLUSleft TIMES DIVIDEnonassoc UMINUSstatement expression printf(=dn$1)expression expression PLUS expression $$ = $1 + $3| expression MINUS expression $$ = $1 - $3| expression TIMES expression $$ = $1 $3| expression DIVIDE expression if ($3 == 0)yyerror(divide by zero)else$$ = $1 $3| - expression prec UMINUS $$ = -$2| ( expression ) $$ = $2| NUMBER $$ = $1

How it works

1048698 yacc creates a C file that represents the parser for a grammar1048698 yacc requires input from a lexical analyzer lexical analyzer no longer calls yylex because yacc does that1048698 Each token is passed to yacc as it is produced and handled by yacc yaccdefines the the token names in the parser as C preprocessor names in ytabh

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 35: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Overall structure of lex and Yacc for program execution is shown below

Input Numbers

Output Result of the operation performed on operands

------------------------------------------------------------------------------------------------------------FAQs

1 What is the role of lex 2 What do you mean by YACC 3 How lex and yacc communicates

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 36: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Assignment 07

Aim Implementation of DLL

Objective1 To write an application in VC++ to create DLL2 To learn to use AppWizard to create MFC-based DLLs3 To understand the significance of dynamic linking over static linking as

linking statically is the most expensive method in terms of disk space and memory requirements

Theory

Procedure

The first step is to start Visual C++ and create our DLL project

1) Start Visual C++ and choose File | New from the menu

This will open the New dialog This is the starting point for creating all kinds of projects Make sure that you are on the Projects tab

2) Select Win32 Dynamic-Link Library as the project type and name the project MakeTempFilenameDLL

We want to create a Win32 DLL not a MFC DLL MFC-based DLLs can also be very useful and offer a lot more functionality however they require the overhead of the MFC library DLLs (over 1 MB) just to get started That is too much overhead for our simple DLL needs

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 37: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

So the New dialog should now look something like the one above Click OK to continue to the next step

3) Choose A simple DLL project and click Finish

The next screen you will see looks like this

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 38: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

For this project we will just start with a simple DLL After you click the Finish button you will see the New Project Information dialog which summarizes your settings Click OK to complete the project creation Your new project will now be ready to work on from the Visual C++ design environment At this point your project will compile and your DLL can be created however it will not do anything because we have not created and exported any functions yet That is the next step

Add the Function

The next step is to actually add the function to the DLL that we want to be able to call from other programs

1) Open the MakeTempFilenameDLLcpp file for editing

Select the FileView tab on the Workspace bar Then open the Source Files folder Now double-click on the MakeTempFilenameDLLcpp file You will now be able to edit the file

2) Add the RetrieveTempFilename function

Next we want to add the actual code for the function Our function will be called RetrieveTempFilename It will accept two string arguments The first is szDirectory which is the directory that we want to generate the unique filename in The second

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 39: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

argument szPrefix is a string that will be the prefix of the temporary filename The GetTempFileName API function will use the first three characters of this string when generating the unique filename The function will return the unique filename if successful or the string ERROR if it fails

Because we want to return a string we need to make the returned string a global variable This is necessary because if we declare the string inside of the function it will be destroyed when the function is finished We need to create a string that will last for the life of the DLL

You can copy and paste the code above right into your file below the DllMain function If you build your project now it will compile successfully but the function will still not be callable from outside programs That is because the Visual C++ compiler decorates the function names so that they would not be recognizable to the calling program Although there are several ways around this problem we will use an Exportdef file

Add an Exportdef File

Adding an Exportdef file will allow us to tell the compiler the proper names of our DLL functions that should be exposed

1) Add a new text file to your project called Exportdef

Choose File | New from the menu within Visual C++ and select the Files tab Select Text File as the file type and type in Exportdef as the filename Click OK to create the file

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 40: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

2) Add the LIBRARY and EXPORTS sections to the Exportdef file

All that we need to do now is to add a few sections to the Exportdef file and we will be in business The first section is the LIBRARY declaration with our DLL name The second is the EXPORTS section which declares our exported functions

LIBRARY dl

EXPORTSadd1 1

Copy and paste the above lines into the Exportdef file Now you can build your project First of all make sure you are building the Release build by selecting Build | Select Active Configuration choosing MakeTempFilenameDLL - Win32 Release and clicking OK Then just hit F7 to build the DLL It should compile and link with no troubles

Using the DLL from VB appication

At this point you have a fully-functional DLL that can be called from Visual Basic another C++ application or any other program capable of loading can calling DLL

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 41: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

functions Here we will focus on how to call our new function from any of the Indigo Rose development tools that use the Lua scripting language

Private Declare Function add1 Lib CssdlDebugdldll (ByVal a As Double ByVal b As Double) As Double

InputInput is given through VB environment

OutputYou will be creating a project file for Dynamic Link Library project The

compiler and linking settings will be configured to generate a DLL file The project will be blank now you must add or create your source files manuallyThe VC++ dll will send back the output to VB environment

InstructionsThe students may adopt any method and alternative for this particular

assignment You are given a flexibility to choose the functions for which u are going to create the DLL

Hence u are expected to illustrate all assumptions and necessary theoretical details

------------------------------------------------------------------------------------------------------------FAQs

1 What do you mean by DLL 2 Which are different types of DLL

------------------------------------------------------------------------------------------------------------

Assignment 08

Aim Study of Recursive descent parser and shift Reduce parser

Objective To understand the top down and bottom up parsing

Theory

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 42: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Top-down parsing

The top down construction of a parse tree is done by starting with the root labeled with the starting non-terminal and repeatedly performing the following two steps-

1 at node n labeled with non-terminal Aselect one of the productions for A and constructchildren at n for the symbols on the right side of the production

2 Find the next node at which the subtree is constructed For some grammars the above steps can be implemented during a single left to right scan of the input string The current token being scanned on the input is often called as the lookahead symbol Initially the lookahead symbol is the first ie the leftmost token of the input string

Let us consider the following grammar

A -gt BA| a| aa

B -gt BB| b

and now consider the input string ldquo bbaardquo

The top-down parsing would look like this in different steps-

1 A

input bbaa the highlighted character indicates the lookahead

2 A

B A

3 A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 43: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

B A

B B

4 A

B A

B B

b

5 input bbaa A

B A

B B

b b

6 input bbaa A

B A

B B a a

b b

Here we have assumed that at the first attempt the parser would know which production to use to get the right output but in general the selection of a production of a non-terminal may involve trial and error that is we may have to try a production and backtrack to try another production if the first is found to be unsuitable A production is unsuitable if after using the production we cannot complete the tree to match the input string we will discuss this parsing in the next section

RECURSIVE-DESCENT PARSING

This is general form of top-down parsing called recursive descent parsing where backtracking may be involved This is a bad type of parsing which involves repeated trying to get the correct outputThis can also be termed as brute-force type of parsing Presently this type of parsing is outdated just because there are much better methods of parsing which we will be discussing later

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 44: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Consider the grammarS -gt cAd | bdA -gt ab | a and the input string is ldquocadrdquo

To construct the tree we create an initial tree of just one node SThe input pointer points to c and we use the first production for s to get the expanded tree

S

c A d

The leftmost leaf labeled c matches the fist symbol of the input and hence we advance the pointer to the second symbol of the input which is a we now expand A by its first production to obtain the followingtree

S

c A d

a b

now we have a match for the second symbol of the input and hence advabce the pointer to d and compare it with the next leaf b which does not match we report failure and go back to see whether there is an alternative production for A

In going back to A we must backtrack the input pointer to afinding another production we try out the next configuration

S

c A d

a

Now the leaf a matches with the second symbol of the input and the the third leaf d matches with the third symbol of the inputAnd because the input string is consumed we halt and denote the successful completion of parsing

BOTTOM-UP PARSING In Bottom-up parsing we start with the sentence and try to apply the production rules in reverse in order to finish up with the start symbol of the grammar This corresponds to starting at the leaves of the parse tree and working back to the rootIt can be thought of

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 45: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

a process of reducing the string in question to the start symbol of the grammarBottom-up parsing is also known as shift-reduce parsing

Suppose we have a grammar

S -gt aABeA -gtAbc | bB -gtd

And the input string isabbcde

Then an instance of bottom-up parsing can be given as

aAde -gt aABe -gt S

Here in the first step we used the derivation B -gt d and then the derivationS -gt aABe

Thus this process is like tracing out the right most derivations in reverse

RIGHT DERIVATION

Expanding the rightmost non-terminal

In context of the above example right derivations can be shown as

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Here in each step we have expanded the rightmost terminal

HANDLEA handle is a substring that matches the right hand side of a production and replacingRHS by LHS must be step in the reverse rightmost derivation that ultimately leads tothe start symbolIf replacing a string does not ultimately lead to the start symbol it canrsquotbe a handleIn the above example Abc is an example of a handle

S -gt aABe -gt aAde -gt aAbcde -gt abbcde

Consider the grammar

E -gt E + E | E E | id

Right sentential form Handle Production Ruleid1 + id2 id3 id1 E -gt id

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 46: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

E + id2 id3 id2 E -gt idE + E id3 id3 E -gt idE + E E E E E -gt E EE + E E + E E -gt E +EE

Here it can be noted that string appearing to the right of a handle contains only terminal symbols

Since here the grammar is ambiguous the choices for the handles can be different depending upon right derivations usedIn bottom up parsing using handles the main problems are

1 Identifying the handle2 Identifying the rule to reduce

This process can be made algorithmic using a stack implementation

Decision 1 shift the next input symbol onto the top of the stackDecision 2 Reduce the symbol at the top of the stack Here the parser knows that the right end of the handle is at the top the stack It must then locate the left end of the handle within the stack and replace it with the non-terminal

Thus it is because of this approach that it is also called bottom-up parsingThe main two problems stated above are termed as shift-reduce and reduce-reduceconflicts

Here are the actions of a shift-reduce parsers for the string id1 + id2 id3 for the grammar defined above

STACK INPUT ACTION 1 $ id1 + id2 id3$ shift2 $id3 + id2 id3 $ Reduce(E -gt id) 3 $E + id2 id3 $ Shift4 $E + id2 id3 $ Shift5 $E + id2 id3 $ Reduce (E-gtid)6 $E + E id3 $ Shift7 $E + E id3 $ Shift8 $E + E id3 $ Reduce (E-gtid)9 $E + E E $ Reduce (E-gtE E)10$E + E $ Reduce (E-gtE + E)11$E $ accept

Note in 6 E + E could have been reduced to E instead of shifting ( a shift-reduce conflict)But we chose to shift because it would have produced the start symbol(11) even if the whole input was not consumed

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 47: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

There are CFGrsquos for which shift-reduce parsing cannot be used For such grammars theShift-reduce or reduce-reduce conflicts cannot be resolved

------------------------------------------------------------------------------------------------------------FAQs1 What is parsing2 Differentiate between Top down and bottom uo parsing3 What do you mean by leftmost and rightmost derivation ------------------------------------------------------------------------------------------------------------

Assignment 09

Aim- Write a program to implement a Tower of Hanoi (Recursive implementation) in C C++ language

Objective-

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 48: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

To understand and implement Recursive algorithm using the Tower of Hanoi problem and study Divide and Conquer strategy

Theory- Divide and conquer strategy

Divide Divide the problem into a number of subproblem that are smaller instances of the same problem

Conquer Conquer the subproblem by solving them recursively If the subproblem sizes are small enough just solve the subproblems in a straight forward manner

Combine Combine the solutions to the sub problems into the solutions for the original problem

When the subproblems are large enough to solve recursively we call that the recursive case Once the subprobles become small enough that we no longer recurse recursion ldquobottoms outrdquo and that we have gone down to the base case In combine step we solve subproblems that are quite not same as original

Recurrences go hand in hand with divide-and-conquer paradigm because they give us a natural way to characterize the running times of divide-and-conquer algorithmsAlgorithm DAndC(P) If small((P) then return S(P) Else Divide P into smaller instances say P1P2P3helliphelliphellipPk kgt=1 Apply DAndC to each of thse subproblems Return Combine(DAndC(P1)DAndC(P2)helliphellipDAndC(Pk)) Tower of Hanoi problem Assume that there are lsquonrsquo number of disks on tower A The disks are of decreasing size and are stacked on the tower in decreasing order of size bottom to top Besides this tower there are two other towers labeled B and C The disks from tower A are to be moved to tower B using tower C as intermediate storage The disks are to be moved one at a time In addition at no time can a disk be on top of a smaller diskExample

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 49: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

Assume that there are n number of disks on tower A To get the largest disk to the bottom of tower B we move the remaining n-1 disks to tower C and then move the largest disk to tower B Now we are left with the task of moving the disks from tower C to tower B To do this we have tower A and B available The fact that the tower B has a disk can be ignored as the disk is larger than the disks being moved from tower C and so any disk can be placed on top of it Algorithm TowerofHanoi(nxyz)Move the top n disks from tower x to tower y If(n gt= 1)then TowerofHanoi(n-1xzy) Write(ldquomove top disk from towerrdquoxrdquoto top of towerrdquoy) TowerofHanoi(n-1zyx)

The recursive nature of the solution is apparent from the above algorithm Our solution for an n-disk problem is formulated in terms of solutions to two(n-1)disk problems AnalysisRecurrence relation

T(n)=g(n)helliphellip n is small=T(n1)+T(n2)+helliphellip+T(nk)+f(n)

In generalT(n)=T(1) =aT(nb)+f(n) n=2k

Exampletn = 0

=t(n-1) + 1+t(n-1)

Where first t(n-1) =movement of (n-1) disks from tower A to tower C 1 is for the movement of the bottommost disk from tower A to tower B

Second t(n-1) = movement of the (n-1) disks from tower C to tower B tn = 2t(n-1) + 1

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 50: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Thus

tn - 2tn-1 = 1 b=1p(n)=1

Characteristic polynomial is (x-2) (x-1)tn = c1 1n + c2 2n

t1= 2t0 + 1t0 = 0 hellipOriginal recursive relationt1 = 1when n=0tn = c1 + c2 = 0 t0 is 0When n=1 tn = c1 + 2c2 =1 t1 is 0

-c2 = -1c2 = 1c1 = -1tn = 2n-1 helliphelliptower of hanoi

tn = number of disk movements

Input and Output

Input=n=noof disksOutput=t(n)=no of disk movements

Equation tn = 2n-1

1) When n=2t(2)= 22 -1 = 3

T

Tower A Tower B Tower CIn this figure two disks are kept on tower A

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 51: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Tower A Tower B Tower C

In this figure the topmost disk from tower A is moved to tower C ( 1 movement)

Tower A Tower B Tower C

In this figure the disk from A is moved to tower B and then the disk from tower C is moved to tower B total 2 movements

Thus when there are two disks to be moved and the no of disk movements is (1+2) 3

2)When n=3 T(3)=23-1 =7

When there are 3 disks to be moved the no of disk movements is 7

Graphical output is not expected if done most welcomeConclusionThe Divide and conquer strategy of the Recursive Algorithm using Tower of Hanoi is studied and implemented

------------------------------------------------------------------------------------------------------------FAQs

1 Explain divide and conquer strategy2 Explain how the recurrence relations help in doing the analysis of algorithms3 Solve the recurrence relation of tower of Hanoi using repetitive substitution

------------------------------------------------------------------------------------------------------------

Assignment 10

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 52: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm

Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree

Theory

Kruskalrsquos algorithm is another greedy algorithm for the minimum spanning tree problem that also always yields an optimal solution It is named Kruskalrsquos algorithm [Kru56] after Joseph Kruskal who discovered the algorithm when he was a second-year graduate student Kruskalrsquos algorithm looks at the minimum spanning tree for a weighted connected graph G = V E as an acyclic subgraph with |V|-1 edges for which the sum of the edges weights is the smallestThe algorithm begins by sorting the graphrsquos edges in non decreasing order of their weights Then starting with the empty subgraph it scans this sorted list adding the next edge on the list to the current subgraph if such an inclusion does not create a cycle and simply skipping the edge otherwise

Data Structures used 1 Use array reorientation of sets where value in the array is either a link to parent

field or for root node the value is total no of children with negative sign

2 We use heap to store the edge information

Pseudocode for Kruskal - Cost ndash 2 dimensional matrix cost[u][v] is cost of edge (uv) n no of verticeslsquotrsquo set of edges in minimum spanning tree ndash it us a 2D array t[1hellipn][1hellip2] initially each vertex is in different set

1 Construct a minimum heap out of edge costs using heapify

2 For( i = 1 to n) do parent[i] = -1

each vertex is in different set3 I=0 mincost=0

4 While(iltn-1) ampamp (heap not empty) to

5

6 Delete minimum cost edge (uv) from heap

7 Reheapify using adjust

8 J=find(u) k=find(v)

9 If (j=k) then

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 53: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

10

11 I++ t[i][1]=ut[i][2]=v

12 Union(jk) mincost = mincost+cost[u][v]

13

14

Algorithm SimpleUnion (I j) i amp j are roots of sets p[i] = jAlgorithm SimpleFind (i ) return root of lsquoirsquo

while (p[i] gt=0) do i=p[i]return I

If we want to perform n-1 unions then can be processed in Linear time O(n)The time required to process a find for an element at level lsquoirsquo of a tree is O(i) so total time needed to process n finds is O(n^2)One can improve the performance of our union amp find by avoiding creation of degenerating trees We apply weighting rule for Union which says that if the number of nodes in the tree with root i is less than the number of nodes in the tree with root j then make j as the parent of i otherwise make i as the parent of j Thus we avoid degeneratedskewed treesSimilarly we use CollapseFind to improve the performance of find In CollapsingFind we are required to go up to reach the root node and then reset the links Every first find has to go through intermediate nodes to go up to root node but every next find on similar node will require only one link to go up thus reducing the cost cost of find Collapsing rule says that if j is a node on the path from i to its root node and p[i] is not root[i] then set p[j] to root[i]Weighted union(Ij)

Temp=p[i]+p[j]If(p[i]gtp[j]) i has fewer nodes

p[i]=j p[j]=tempElse

p[j]=I j has fewer nodesp[i]=temp

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 54: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Collapse Find(i)

r=IWhile(p[r]gt=0)

r=p[r]while(i=r)

s=p[i]p[i]=ri=s

Analysis of Kruskal algorithm Time required to construct the initial heap is log E Removal of edge from heap requires again log E and the edge is removed in the while loop which runs for E times hence the time complexity of algorithm is O(E log E) Here since we have used sets to represent trees and we have used efficient find and union algorithms which take almost O(1) timeInput ndash Output Write this as per your implementation

Primrsquos algorithm for finding minimum cost of spanning tree

Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge

Greedy method 1 It is the most straight forward design technique Most of the problems have n

inputs amp require us to obtain a subset that satisfies some constraints 2 Any subset that satisfies these constraints is called a feasible solution 3 We find a feasible solution that either maximizes or minimizes a given objective

function4 A feasible solution that does this is called an lsquoOptimal Solutionrsquo

Greedy algorithm It works in stages considers one input at a time Inputs are considered in an order determined by some selection procedure At each stage decision is made regarding whether a particular input is in

optimal solution Inclusion of next input into partially constructed optimal solution should

always result into feasible solution If not then this input is not considered

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 55: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Selection procedure is based on some optimization measure which may be objective function

Greedy algorithm that makes use of optimization measures to generate suboptimal solution is called lsquoSubset Paradigmrsquo

General greedy algorithm Algorithm Greedy (an) a[1hellipn] contains n inputs

solution=0 initialise the solutionfor( i=1 to n )

do x= select(a)

if(feasible(solution x)) then solution= union(solution x)

return solution

Function select selects an input from a[ ] and removes it The selected inputrsquos value is assigned to xFunction feasible is Boolean valued function that determines whether x can be included into solution vector Function union combines x with solution and updates the objective function

Knapsack problem job sequencing minimum spanning trees comes under the subset paradigm

Shortest path Huffman codes comes under ordering paradigm

Minimum cost spanning trees Definition Spanning trees ndashLet G = (V E) be an undirected connected graph A subgraph t= ( V Ersquo) of G is a spanning tree if and only if t is a tree (No cycle exists)

In practical situations the edges have weights assigned to them weights are positive These weights may represent the cost of construction lengths of links etc Given such weighted graph one would like to select cities (vertices) to have minimum total cost minimum total length So one can find a spanning tree with minimum cost

Since identification of minimum spanning tree involves selection of a subset of the edges this problem fits into the Subset Paradigm

Prims algorithmrsquos outline Greedy method constructs the tree by including edge by edge Choose an edge that results in a minimum increase in sum of cost of edges so far included Also set of edges so far selected form a tree (No cycle)

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 56: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Pseudo code for Primrsquos algorithm Prim() cost[1n][1] is a cost adjacency matrix n number of vertices in the graph cost [ i ][ j ] =infin if there is no edge between I and j cost [ i ][ j ] =infin if i=j cost [ i ][ j ] = cost [ i ][ j ] = positive number if it is edge t [1n-1][12] has all the edges of minimum spanning tree mincost is minimum cost near[ ] is an array which stores vertex in tree such that cost [ j ][ near[ j ] ] is minimum among all choices for near[ j ]

1 mincost =02 For (i=2 to n) do near[ i ]=13 Near[1]=0 vertex 1 is initially in lsquotrsquo 4 For(i=1 to n-1) do5 find n-1 edges of tree6 Let j be an index such that7 Near[ j ] =0 amp cost[ j ][ near[ j ] ] is minimum8 computation of j requires linear loop 9 not shown above10 t[ i ][1] = j t[ i ][2]= near[ j ]11 mincost=mincost + cost[ j ][near [ j ] ]12 Near[ j ] =013 For( k= 1 to n ) do update near[ ]14 If(near[ k ] =0) ampamp (cost[ k ][ near[ k ] ] gt cost[ k ][ j ])15 Near[ k ]=j16 17 return mincost

Stages in Primrsquos algorithm

Complexity The time required by algorithm Prim is O(n^2) where n is number of vertices in graph G Each iteration of for loop takes O(n) time So the total time is O(n^2)If we store nodes not yet included in tree as a red-black tree then algorithm takes O(log n) timeDisadvantages of Primrsquos algorithm

1 List of edges has to be searched from beginning as new edge gets added2 If there are more than one edges having same weight then all possible spanning

trees are required to be found for final minimal tree

Sample Input amp Output Write this as per your implementation

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 57: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

Conclusion Thus we have understood the role of using Kruskal algorithm in finding the minimum cost spanning tree Also we understood the role of primrsquos algorithm with respect to greedy approach

------------------------------------------------------------------------------------------------------------

FAQs1 Explain the weighted union and collapse find rule with example

2 How the above rules improve the total complexity of Kruskal algorithm

3 Why the edge information is stored in Heap

4 Can we use any other sort technique to get sorted edges if so comment on time complexity

5 Explain heapify adjust algorithms in maintaining heap property

6 What is a red black tree7 Distinguish between Prims and Kruskal algorithm8 Explain the greedy strategy used in both algorithms

Assignment 11

Aim Implementation of 8 queen problem( Non recursive implementation) using

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 58: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

backtracking strategyObjective To study and understand the concept of backtracking

Theory

BacktrackingGeneral method-bull Useful technique for optimizing search under some constraintsbull Express the desired solution as an n-tuple (x1 xn) where each xi 2 Si Si being a finite setbull The solution is based on finding one or more vectors that maximize minimize or satisfy a criterion function P(x1 xn)bull Sorting an array a[n]ndash Find an n-tuple where the element xi is the index of ith smallest element in andash Criterion function is given by a[xi] _ a[xi+1] for 1 _ i lt nndash Set Si is a finite set of integers in the range [1n]bull Brute force approachndash Let the size of set Si be mindash There are m = m1m2 middot middot middotmn n-tuples that satisfy the criterion function Pndash In brute force algorithm you have to form all the m n-tuples to determine the optimal solutionsbull Backtrack approachndash Requires less than m trials to determine the solutionndash Form a solution (partial vector) and check at every step if this has any chance of successndash If the solution at any point seems not-promising ignore itndash If the partial vector (x1 x2 xi) does not yield an optimal solution ignore mi+1 middot middot middotmn possible test vectorseven without looking at thembull All the solutions require a set of constraints divided into two categories explicit and implicit constraintsDefinition 1 Explicit constraints are rules that restrict each xi to take on values only from a given setndash Explicit constraints depend on the particular instance I of problem being solvedndash All tuples that satisfy the explicit constraints define a possible solution space for Indash Examples of explicit constraints_ xi _ 0 or all nonnegative real numbers_ xi = 0 1_ li _ xi _ uiDefinition 2 Implicit constraints are rules that determine which of the tuples in the solution space of I satisfy thecriterion functionndash Implicit constraints describe the way in which the xis must relate to each other

bull 8-queens problem

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 59: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

ndash Place eight queens on an 8 times 8 chessboard so that no queen attacks another queenBacktracking

1 2 3 4 5 6 7 81 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q

ndash Identify data structures to solve the problem

_ First pass Define the chessboard to be an 8 times 8 array

_ Second pass Since each queen is in a different row define the chessboard solution to be an 8-tuple (x1 x8)

where xi is the column for ith queen

ndash Identify explicit constraints

_ Explicit constraints using 8-tuple formulation are Si = 1 2 3 4 5 6 7 8 1 _ i _ 8

_ Solution space of 88 8-tuples

ndash Identify implicit constraints

_ No two xi can be the same or all the queens must be in different columns

middot All solutions are permutations of the 8-tuple (1 2 3 4 5 6 7 8)

middot Reduces the size of solution space from 88 to 8 tuples

_ No two queens can be on the same diagonal

ndash The solution above is expressed as an 8-tuple as 4 6 8 2 7 1 3 5

Input as specifies number of queens (here 8)

Output for 8 queens 92 steps

------------------------------------------------------------------------------------------------------------

FAQ

Department of Information Technology

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge
Page 60: SDL Lab+Manual+2008+Course

Pad DrDYPatil Institute Of Technology Pimpri

1What is backtracking

2What is the time complexity for 8 queens problem using backtracking

------------------------------------------------------------------------------------------------------------

Department of Information Technology

  • Theory -
  • Language Processor Pass -
  • Assembly Language statements -
  • Analysis Phase -
  • Design of a Two Pass Assembler -
  • Pass I -
  • Pass II -
    • Instruction Assembly Remarks
      • Sample Input amp Output -
      • Errors -
      • Forward reference(Symbol used but not defined) -
      • Duplication of Symbol -
      • Mnemonic error
      • If there is invalid instruction then this error will occur
      • Register error -
      • If there is invalid register then this error will occur
      • Operand error -
      • FAQs
      • Data structures required for macro definition processing -
      • Data structures required for expansion processing -
      • Algorithm for macro expansion
      • Sample Input -
      • The assembly language program with macro definitions amp macro calls
      • Sample output -
      • Instructions Regarding testing of the program -
      • Future Enhancement ndash ( NOT Mandatory)
      • ------------------------------------------------------------------------------------------------------------
      • FAQS
      • Objective - To understand the role of regular expressions and finite automata in applications such as Compilers
      • Theory -
      • Regular expressions are used to specify regular languages and finite automata are used to recognize the regular languages Many computer applications such as compilers operating system utilities text editors make use of regular languages In these applications the regular expressions and finite automata are used to recognize this language
      • Objective
      • Theory
      • Synthesis Step
        • Task of Lexical Analysis
          • Add the Function
          • Add an Exportdef File
          • Using the DLL from VB appication
          • Aim - Write a program in CC++ to implement kruskalrsquos algorithm and Prims algorithm
          • Objective - To understand the role of Kruskal and Primrsquos algorithm in finding out the minimum cost spanning tree
          • Theory
          • Primrsquos algorithm for finding minimum cost of spanning tree
          • Objective of primrsquos algorithm is to understand the Greedy method to obtain a minimum cost spanning tree edge by edge