lec02 - CS 3110 Lecture 2 Syntax and evaluation of OCaml...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 3110 Lecture 2 Syntax and evaluation of OCaml programs Summary of topics: OCaml syntax Expression, terms, types, and values Errors Evaluation and rewrite rules Namespaces and scope Qualified identifiers and libraries OCaml syntax In the previous recitation, you should've seen a few simple expression and declaration forms for OCaml. The syntax of this fragment of the language can be summarized as follows: (note that ~ is unary, - is binary). syntactic class syntactic variable(s) and grammar rule(s) examples identifiers x, f a , x , y , x_y , foo1000 , . .. constants c ... -2 , -1 , 0 , 1 , 2 (integers) 1.0 , -0.001 , 3.141 (floats) true , false (booleans) "hello" , "" , "!" (strings) 'A' , ' ' , '\n' (characters) unary operator u - , not binary operators b + , * , - , > , < , >= , <= , ^ , . .. terms e ::= x | c | u e | e 1 b e 2 | if e then e else e | let d 1 and ... and d n in e | e ( e 1 , ... , e n ) foo , ~0.001 , not b , 2 + 2 , declarations d ::= x = e | f ( x 1 , . .., x n ): t = e one = 1< square(x):int = x*x types t ::= int | float | bool | string | char | t 1 * ... * t n -> t int , string , int->int , bool*int->bool A program in ML, like any other language, is made up of various kinds of expressions . The table above
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
describes how to construct some of those expression. That is, it specifies some of the syntax of ML. Some of these expressions, such as identifiers, constants, and operators, we have described only by example. These expressions are all single tokens . Other expressions, such as terms, declarations, and types, are described by grammar rules . These rules are written in a form known as BNF, for B ackus- N aur F orm (named after its inventors). Each rule describes various ways to build a particular kind of expression, separated by vertical bars. For example, a term may be an identifier, a constant, any unary operator u followed by any expression e ( u e ), any two terms e 1 and e 2 separated by any binary operator b , and so on. Notice that we use the letter u to represent any unary operator and the letter e to represent any term. These are examples of syntactic variables or metavariables . A syntactic variable is not an OCaml program variable; it is just a generic name for a certain syntactic construct. For instance,
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

lec02 - CS 3110 Lecture 2 Syntax and evaluation of OCaml...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online