Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Interpretor in oCaml, ML homework

interpretor ocaml homework ml function expression evaluate

  • Please log in to reply
1 reply to this topic

#1 spreadzz

spreadzz

    CC Lurker

  • New Member
  • Pip
  • 7 posts
  • Programming Language:C#
  • Learning:Java, C#, PHP, JavaScript

Posted 10 October 2012 - 08:44 AM

Hello guys, first i am new to the forum, and second i need some help with some homework:
This is the homework:

" Write, preferably in a functional language like ML, an interpreter for the a language with the following syntax:
Prog ::= Def* Expr
Def ::= id id* = Expr
Expr ::= int | id | Expr '+' Expr | Expr '*' Expr | id Expr* | if Expr then Expr else Expr


A program is a sequence of definitions followed by an expression evaluation. A definition binds an identifier (optionally followed by parameters, in the case of functions) to an expression. An expression is an integer, an arithmetic expression (sum or product), a previously defined identifier, a function application (identifier followed by argument expressions) or a conditional expression (the 'then' branch is taken if the condition is nonzero). Function definitions may be recursive. Implement the nonrecursive case first.
Scoping is assumed to be static, i.e. the definition of an identifier is determined syntactically. It extends to the end of the program, or its next redefinition; a function declaration creates an inner scope for its parameters. Examples:
x = 5 (* definition *)
f x = x + 3 (* definition of f *)
g y = x + y (* x is bound to 5 *)
x = 3 (* redefinition *)
g 3 (* 8, x was bound to 5 at the definition of g *)

You will need to evaluate expressions in the appropriate environment. When evaluating a function in environment env you must evaluate its argument(s) in env but evaluate the function body (specifically, its free variables) in the environment of the function definition.
You do not need to write an expression parser. It suffices if your interpreter processes an ML term representing a list of declarations/expressions."

So i have to do an interpretor in ML language. So i am using oCaml becouse that`s what i use at school. And an emacs editor.

So far i understood i have to to some expressions like this

type exprersstion = I of int
| Add of expr * expr
| Multiply of expr * expr

let rec evaluate = function
| I n -> n
| Add(n1,n2) -> evaluate n1 + evaluate n2
| Multiply(n1,n2) -> evaluate n1 * evaluate n2

That`s all i understood i have to do, i am new to this kind of programming language, and don't know how to make an interpretor nither how it works. Don't really know if what i wrote is good or not, i really need a HUGE HELP here guys.

Thanks in advance!
  • 0

#2 spreadzz

spreadzz

    CC Lurker

  • New Member
  • Pip
  • 7 posts
  • Programming Language:C#
  • Learning:Java, C#, PHP, JavaScript

Posted 12 October 2012 - 02:40 AM

Nobody know ? Or i have not been clear on something ? please tel me, i can put an example of an SML interpreter...
  • 0





Also tagged with one or more of these keywords: interpretor, ocaml, homework, ml, function, expression, evaluate

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download