design and implementation of programming languages introduction

71
Department of CSE, MIT , Manipal DESIGN AND IMPLEMENTATION OF PROGRAMMING LANGUAGES

Upload: tiersarge

Post on 14-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 1/71

Department of CSE, MIT, Manipal

DESIGN AND

IMPLEMENTATION OFPROGRAMMING

LANGUAGES

Page 2: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 2/71

Department of CSE, MIT, Manipal

Page 3: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 3/71

Department of CSE, MIT, Manipal

Page 4: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 4/71

Department of CSE, MIT, Manipal

Page 5: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 5/71

Department of CSE, MIT, Manipal

Page 6: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 6/71

Department of CSE, MIT, Manipal

Page 7: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 7/71Department of CSE, MIT, Manipal

Introduction

• Programming languages are mediumthrough which we communicate with thecomputers.

• The languages we discuss in the courseare C, C++, Java, Scheme and Prolog

Page 8: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 8/71Department of CSE, MIT, Manipal

What is a programming

language?•  A programming language is a notationalsystem for describing computation inhuman readable and machine readable

form.

Page 9: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 9/71Department of CSE, MIT, Manipal

What is computation?

• Computation is any process carried out bycomputer.

• Computation includes all kinds of computer operations including datamanipulation, text processing andinformation storage and retrieval.

Page 10: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 10/71Department of CSE, MIT, Manipal

What is machine readability?

• There must be some compiler or interpreter which translates the language.

• Compiler/interpreter must not be toocomplex.

• Machine readability is ensured byrestricting the structure of programminglanguage to that of context free languages.

Page 11: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 11/71Department of CSE, MIT, Manipal

What is Human readability?

• Programming language must be easy tounderstand.

• Programming languages must be easy tomaintain.

Page 12: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 12/71Department of CSE, MIT, Manipal

 Abstraction in programming

languages• Data abstraction

• Control abstraction

Page 13: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 13/71Department of CSE, MIT, Manipal

Levels of abstraction

• Basic abstraction

• Structured abstraction

• Unit abstraction

Page 14: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 14/71Department of CSE, MIT, Manipal

Basic abstractions

• Collect together the most localizedmachine information.

Page 15: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 15/71Department of CSE, MIT, Manipal

Structured abstraction

• Collect more global information about thestructure of the program.

Page 16: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 16/71Department of CSE, MIT, Manipal

Unit abstractions

• Collect information about entire pieces of program.

Page 17: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 17/71Department of CSE, MIT, Manipal

Basic data abstraction

• Locations in computer memory thatcontain data value are abstracted bygiving them a name called variables.

• The kind of data value is also given aname and is called a data type.

• Eg:- int x;

Page 18: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 18/71Department of CSE, MIT, Manipal

Structured data abstraction

• The collection of related data values areabstracted thru data structures.

• Eg:-arrays, structures.

• Many languages allow type declaration

• Eg:-typedef int Intarray[10];

• Such data types are called structuredtypes.

Page 19: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 19/71

Department of CSE, MIT, Manipal

Unit data abstraction

• In a large program it is useful to collectrelated code into specific locations within aprogram either as a separate file or as a

separate language structure within a file.

• Eg:-package in java, class in C++

• The most important property of unit dataabstraction is its reusability.

• Unit abstraction become basis for language library mechanisms

Page 20: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 20/71

Department of CSE, MIT, Manipal

Basic control abstractions

• Statements which combine a few machineinstructions into a more understandableabstract statement.

• Eg:-goto

Page 21: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 21/71

Department of CSE, MIT, Manipal

Structured control abstractions

• Structured control abstractions divide aprogram into groups of instructions thatare nested within tests that govern their 

execution.

• Eg:-if, switch case, while, for, do while,subroutine,(procedure and function)

• Function return value while procedures donot.

Page 22: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 22/71

Department of CSE, MIT, Manipal

Unit control abstraction

• Focus on operation rather than the data.

• The goal is to achieve reusability andlibrary building.

Page 23: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 23/71

Department of CSE, MIT, Manipal

Parallel abstractions

•  Allow parallel execution of parts of program.

• Eg:- threads in Java

Page 24: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 24/71

Department of CSE, MIT, Manipal

Page 25: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 25/71

Department of CSE, MIT, Manipal

Page 26: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 26/71

Department of CSE, MIT, Manipal

Turing Completeness

•  A language is said to be turing completewhen it has integer variables, arithmeticand sequentially executes the statement,

which include assignment, selection andloop statements.

Page 27: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 27/71

Department of CSE, MIT, Manipal

Turing completeness

•  A programming language is turingcomplete if it has integer values, arithmeticfunctions on those values, and if it has a

mechanism for defining new functionsusing existing functions, selection, andrecursion.

Page 28: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 28/71

Department of CSE, MIT, Manipal

Computational paradigms

• Imperative languages

• Functional languages

• Logical languages• Object oriented languages

• Parallel

Page 29: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 29/71

Department of CSE, MIT, Manipal

Page 30: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 30/71

Department of CSE, MIT, Manipal

Imperative language

• Imperative language is characterized byfollowing properties.

• The sequential execution of instruction

• The use of variables

• Use of assignment to change the values of variables.

• Eg:-C,Pascal,ada,Fortran

Page 31: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 31/71

Department of CSE, MIT, Manipal

Imperative language

Int fact(int n)

{

int sofar=1;while(n>0)

sofar *=n--;

return sofar;}

Page 32: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 32/71

Department of CSE, MIT, Manipal

Hall marks of Imperativelanguages

•  Assignment

• Iteration

• Order of execution is critical

Page 33: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 33/71

Department of CSE, MIT, Manipal

Functional languages

(define fact(x) (if (<= x 0) 1(* x(fact(- x 1)))))

Page 34: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 34/71

Department of CSE, MIT, Manipal

Hall mark of functionallanguages

• Single valued variables : no assignment

• Heavy use of recursion : no iteration

Page 35: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 35/71

Department of CSE, MIT, Manipal

Logical languages

fact(x,1):-x:=1

fact(x,Fact):-x>1, Newx is x-1,

fact(newx,nf),Fact is x*nf 

Page 36: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 36/71

Department of CSE, MIT, Manipal

Hallmark of logic languages

• Program expressed as rules in formallogic.

Page 37: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 37/71

Department of CSE, MIT, Manipal

Object Oriented Languagesclass Factorial

{int facto(int n)

{

int fac=1;

for(int i=n;i>0;i--)

{

fac*=i;

}

return(fac);

}

public static void main(String args[])

{Factorial f=new Factorial();

int fact=f.facto(5);

System.out.println("Factorial of 5="+fact);

}

}

Page 38: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 38/71

Department of CSE, MIT, Manipal

Hallmarks of object orientedlanguages

• All properties of imperative plus … 

• objects

Page 39: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 39/71

Department of CSE, MIT, Manipal

Pure languages

• Imperative:- (old)fortran

• Functional:- haskell

• Object oriented:- small talk

Page 40: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 40/71

Department of CSE, MIT, Manipal

Question

• Can you execute statement within an if statement without checking its condition inC/C++ and in JAVA why or why not

Page 41: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 41/71

Department of CSE, MIT, Manipal

Permissive Languages

Goto my10;

i=5;

If(i==12){my10:cout<<“I can reach here in C++”; 

}

Page 42: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 42/71

Department of CSE, MIT, Manipal

Language definition

• Rules that govern the language

•  American National Standard Institute(ANSI)

• International Organization for Standardization (ISO)

Page 43: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 43/71

Department of CSE, MIT, Manipal

Language definition

• Language definition can be divided intotwo parts

• Syntax (structure)

• Semantics (meaning).

Page 44: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 44/71

Department of CSE, MIT, Manipal

Language Syntax

• The syntax of the programming languageis like grammar of a natural language.

• Syntax of all programming languages isgiven using context-free grammars.

• if-statementif(expression) statement[else statement]

Page 45: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 45/71

Department of CSE, MIT, Manipal

Lexical structure

• The structure of words or tokens.

• Eg:-if, else, identifiers, +, ; etc

Page 46: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 46/71

Department of CSE, MIT, Manipal

Language Semantics

• The actual result of execution.

• Usually described in English but can bedone mathematically.

• Semantics can have static componentssuch as type checking, definition checkingother consistency checks prior toexecution.

Page 47: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 47/71

Department of CSE, MIT, Manipal

Semantic of If statement

•  An if statement is executed by firstevaluating its expression, which must havearithmetic or pointer type, including all side

effects, and if it compares unequal to zero,the statement following the expression isexecuted. If there is an else part, and the

expression is zero, the statement followingthe else is executed.

Page 48: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 48/71

Department of CSE, MIT, Manipal

Language Translation

• Compiler:- Two step process that translatesource code into target code then user executes the target code.

• Interpreter:-one step process where thesource code is executed directly.

• Java code is compiled to a machine

independent set of instructions called bytecode. Byte code is then interpreted by theJava Virtual Machine(JVM).

Page 49: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 49/71

Department of CSE, MIT, Manipal

Page 50: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 50/71

Department of CSE, MIT, Manipal

Phases of translation

• Source program is converted into tokensby lexical analyzer .

• Tokens are converted into structure of the

program by syntax analyzer .

• Structure of the program is converted intoproper meaningful program by semantic

analyzer .

Page 51: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 51/71

Department of CSE, MIT, Manipal

Error classification

• Lexical

• Syntax

• Static semantic

• Dynamic semantic

• logical

Page 52: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 52/71

Department of CSE, MIT, Manipal

Lexical error 

• Character level error such as illegalcharacter.

• Eg:- abc#, ab$

Page 53: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 53/71

Department of CSE, MIT, Manipal

Syntax error 

• Error in structure

• Eg:- missing semicolon or keyword

Page 54: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 54/71

Department of CSE, MIT, Manipal

Static semantic

• Non syntax error prior to execution

• Eg:-Undefined variables, incompatibletype.

Page 55: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 55/71

Department of CSE, MIT, Manipal

Dynamic semantic

• Non syntax error during execution.

• Eg:- division by zero.

array index out of range.

Page 56: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 56/71

Department of CSE, MIT, Manipal

Logical errors

• Programmer errors.

• Logic errors are not errors at all from thepoint of view of language translation.

E i

Page 57: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 57/71

Department of CSE, MIT, Manipal

Error reporting

•  A compiler will report lexical, syntax andstatic semantic errors. It cannot reportdynamic sematic errors.

•  An interpreter will only report lexical andsyntax errors when loading the program.Static semantic errors may not be reported

until just prior to execution.• No translator can report a logic error.

R ti i t

Page 58: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 58/71

Department of CSE, MIT, Manipal

Run time environment

• During execution language translatorsmust maintain run time environment andallocation of memory to program and data.

R ti i t

Page 59: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 59/71

Department of CSE, MIT, Manipal

Run time environment

•  An interpreter maintains Run timeenvironment internally as a part of itsmanagement of the execution of the

program.

R ti i t

Page 60: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 60/71

Department of CSE, MIT, Manipal

Run time environment

• Compiler adds suitable instructions to thetarget code to maintain run timeinformation.

Bi th f l

Page 61: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 61/71

Department of CSE, MIT, Manipal

Birth of languages

• UnixC

• Internet java

• OOApproachesC++.

I ti t l

Page 62: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 62/71

Department of CSE, MIT, Manipal

Imperative style

int numdigits(int x)

{int temp=x; n=1;

while(temp>=10){

n++;

temp=temp/10;}

return n;

}

F ti l t l

Page 63: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 63/71

Department of CSE, MIT, Manipal

Functional style

int numdigits(int x)

{

if(x<10) return 1;

else

return 1+numdigits(x/10);

}

Q ti

Page 64: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 64/71

Department of CSE, MIT, Manipal

Question

• Write down the while statement equivalentof following statement

if(e) s1 else s2

A

Page 65: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 65/71

Department of CSE, MIT, Manipal

 Answer 

X=e;

Y=1-X;

while(X)

{

s1;X=0;

}

while(Y)

{

s2;

Y=0;

}

Q ti

Page 66: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 66/71

Department of CSE, MIT, Manipal

Question

• Write a program in C/C++ to reverse astring using functional paradigm(i.e. usingrecursion).

Page 67: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 67/71

Department of CSE, MIT, Manipal

# include <stdio.h>

void reverse(char *str){

if(*str)

{

reverse(str+1);

printf("%c", *str);}

}

int main()

{

char a[] = “MIT"; 

reverse(a);

getchar();

return 0;

}

Fib i It ti

Page 68: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 68/71

Department of CSE, MIT, Manipal

Fibonacci Iterative

static int FibonacciIterative(int n){

if (n == 0) return 0;

if (n == 1) return 1;

int prevPrev = 0;int prev = 1;

int result = 0;

for (int i = 2; i <= n; i++)

{

result = prev + prevPrev;prevPrev = prev;

prev = result;

}

return result;

}

Scheme Program

Page 69: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 69/71

Department of CSE, MIT, Manipal

Scheme Program

(define (fib n)

(cond(= n 0) 0)

((<= n 2) 1)

(else (+ (fib (- n 1)) (fib (- n 2))))

)

)

Fibonacci in Prolog

Page 70: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 70/71

Department of CSE, MIT, Manipal

Fibonacci in Prolog

fibonacci(0,0).

fibonacci(1,1).

fibonacci(2,1).fibonacci(N,F) :- if (N>=3)

fibonacci(N-1,F1), fibonacci(N-2,F2), F

= F1 + F2.

References

Page 71: Design and Implementation of Programming Languages Introduction

7/29/2019 Design and Implementation of Programming Languages Introduction

http://slidepdf.com/reader/full/design-and-implementation-of-programming-languages-introduction 71/71

References

Text book

• Kenneth C. Louden and Kenneth Lambert “Programming Languages Principles and Practice” Third editionCengage Learning Publication.

Reference Books:

• Terrence W. Pratt, Masvin V. Zelkowitz “Programming

Languages design and Implementation” Fourth Edition

Pearson Education.

• Allen Tucker, Robert Noonan “Programming LanguagesPrinciples and Paradigms second edition Tata MC Graw –Hill Publication.