SlideShare ist ein Scribd-Unternehmen logo
1 von 13
In the
Notes on Programming Language Syntax
page, an example parser for a simple language is given, using C
syntax. Write the parser using F#, but you may only use
functional programming and immutable date. Create the list of
tokens as a discriminated union, which (in the simplest case)
looks like an enumeration.
type TERMINAL =
IF|THEN|ELSE|BEGIN|END|PRINT|SEMICOLON|ID|EOF
With this type declared, you can use the terminals like you
would use enumerated values in Java.
Use immutable data. The C-code example uses mutable data.
Pass the program into the start symbol function. Pass the input
yet to be processed to each non-terminal function.
The main function might look like this:
let test_program program =
let result = program |> S
match result with
| [] -> failwith "Early termination or missing EOF"
| x::xs -> if x = EOF then accept() else error()
You do not have to parse input strings. Assume that the parsing
has been done. Pass a list of tokens that represent a program
into the start symbol. Try these program examples:
[IF;ID;THEN;BEGIN;PRINT;ID;SEMICOLON;PRINT;ID;END;
ELSE;PRINT;ID;EOF]
[IF;ID;THEN;IF;ID;THEN;PRINT;ID;ELSE;PRINT;ID;ELSE;B
EGIN;PRINT;ID;END;EOF]
Causes error:
[IF;ID;THEN;BEGIN;PRINT;ID;SEMICOLON;PRINT;ID;SEMI
COLON;END;ELSE;PRINT;ID;EOF]
Print an accept message when the input is valid and completely
consumed. Generate appropriate error messages for incorrect
symbols, not enough input, and too much input.
Once you have the parser recognizing input, generate a parse
tree using a discriminated type.
Implement a parser using functional programming and
immutable data for the unambiguous grammar for arithmetic
expressions, from the
Notes on Programming Language Syntax.
E -> E + T | E - T | T
T -> T * F | T / F | F
F -> i | (E)
Use the suggestion in the notes to get around the fact that this
grammar appears to need more than one lookahead token.
Once you have the parser recognizing input, generate a parse
tree using a discriminated type.
Recall that an F# function that takes two arguments can be
coded in either uncurried form (in which case it takes a pair as
its input) or curried form (in which case it takes the first
argument and returns a function that takes the second
argument). In fact it is easy to convert from one form to the
other in F#. To this end, define an F# function
curry f
that converts an uncurried function to a curried function, and
an F# function
uncurry f
that does the opposite conversion. For example,
> (+);;
val it : (int -> int -> int) =
[email protected]
>
> let plus = uncurry (+);;
val plus : (int * int -> int)
> plus (2,3);;
val it : int = 5
> let cplus = curry plus;;
val cplus : (int -> int -> int)
> let plus3 = cplus 3;;
val plus3 : (int -> int)
> plus3 10;;
val it : int = 13
What are the types of
curry
and
uncurry
?
Given vectors
u = (u
1
, u
2
,..., u
n
)
and
v = (v
1
, v
2
,..., v
n
)
, the
inner product
of
u
and
v
is defined to be
u
1
*v
1
+ u
2
*v
2
+ ... + u
n
*v
n
. Write a curried F# function
inner
that takes two vectors represented as
int list
and returns their inner product.
Throw an exception if the lists do not have the same length.
Do not use any built-in or borrowed functions. Write it from
scratch.
Use tail recursion.
> inner [1;2;3] [4;5;6];; val it : int = 32
Given an
m
-by-
n
matrix
A
and an
n
-by-
p
matrix
B
, the product of
A
and
B
is an
m
-by-
p
matrix whose entry in position (
i,j
) is the inner product of row
i
of
A
with column
j
of
B
. For example,
/ 0 1 
/ 1 2 3  * | 3 2 | = / 9 11
4 5 6 /  1 2 /  21 26 /
Write an uncurried F# function to do matrix multiplication:
> multiply ([[1;2;3];[4;5;6]], [[0;1];[3;2];[1;2]]);;
val it : int list list = [[9; 11]; [21; 26]]
Assume that the dimensions of the matrices are appropriate.
Hint
: Use
transpose
(from
Homework 1
),
inner
, and
List.map
.
Evaluate the asymptotic time complexity of this function:
let rec oddeven = function
| [] -> []
| x::xs -> if x % 2 = 0
then oddeven xs @ [x]
else x :: oddeven xs
Two powerful
List
functions provided by F# are
List.fold
and
List.foldBack
. These are similar to
List.reduce
and
List.reduceBack
, but more general. Both take a binary function
f
, an initial value
i
, and a list
[x1;x2;x3;...;xn]
. Then
List.fold
returns
(f ... (f (f (f i x1) x2) x3) ... xn)
while
List.foldBack
returns
(f x1 (f x2 (f x3 ... (f xn i) ... )))
In spite of this complicated behavior, they can be implemented
very simply:
> let rec fold f a = function
| [] -> a
| x::xs -> fold f (f a x) xs;;
val fold : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a
> let rec foldBack f xs a =
match xs with
| [] -> a
| y::ys -> f y (foldBack f ys a);;
val foldBack : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b
(Note that they don't take their arguments in the same order.)
Each of these functions can be used to implement
flatten
, which "flattens" a list of lists:
let flatten1 xs = List.fold (@) [] xs
let flatten2 xs = List.foldBack (@) xs []
For example,
> flatten1 [[1;2];[];[3];[4;5;6]];;
val it : int list = [1; 2; 3; 4; 5; 6]
Compare the efficiency of
flatten1 xs
and
flatten2 xs
, both in terms of
asymptotic time compexity
and
experimentally
. To make the analysis simpler, assume that
xs
is a list of the form
[[1];[2];[3];...;[n]]
.
Interpreter 0
In this problem, we begin our exploration of the use of F# for
language-oriented programming
. You will write an F# program to evaluate arithmetic
expressions written in the language given by the following
context-free grammar:
E -> n | -E | E + E | E - E | E * E | E / E | (E)
In the above,
n
is an integer literal,
-E
is the negation of
E
, the next four terms are the sum, difference, product, and
quotient of expressions, and
(E)
is used to control the order of evaluation of expressions, as in
the expression
3*(5-1)
.
Rather than working directly with the concrete syntax above, we
will imagine that we have a parser that parses input into an
abstract syntax tree
, as is standard in real compilers. Hence your interpreter will
take an input of the following discriminated union type:
type Exp =
Num of int
| Neg of Exp
| Sum of Exp * Exp
| Diff of Exp * Exp
| Prod of Exp * Exp
| Quot of Exp * Exp
Note how this definition mirrors the grammar given above. For
instance, the constructor
Num
makes an integer into an
Exp
, and the constructor
Sum
makes a pair of
Exp
's into an
Exp
representing their sum. Interpreting abstract syntax trees is
much easier than trying to interpret concrete syntax directly.
Note that there is no need for a constructor corresponding to
parentheses, as the example given above would simply be
represented by
Prod(Num 3, Diff(Num 5, Num 1))
which represents the parse tree which looks like
Your job is to write an F# function
evaluate
that takes an abstract syntax tree and returns the result of
evaluating it. Most of the time, evaluating a tree will produce an
integer, but we must address the possibility of dividing by zero.
This could be handled by raising an exception, but instead we
choose to make use of the built-in F# type
type 'a option = None | Some of 'a
Thus
evaluate
will have type
Exp -> int option
, allowing it to return
Some m
in the case of a successful evaluation, and
None
in the case of an evaluation that fails due to dividing by zero.
For example,
> evaluate (Prod(Num 3, Diff(Num 5, Num 1)));;
val it : int option = Some 12
> evaluate (Diff(Num 3, Quot(Num 5, Prod(Num 7, Num
0))));;
val it : int option = None
Naturally,
evaluate e
should use recursion to evaluate each of
e
's sub-expressions; it should also use
match
to distinguish between the cases of successful or failed sub-
evaluations. To get you started, here is the beginning of the
definition of
evaluate
:
let rec evaluate = function
| Num n -> Some n
| Neg e -> match evaluate e with
| ...
Record
Create a record type for Name, Credits and GPA.
Create a record instance with the values "Jones", 109, 3.85.
Discriminated Union
Create a discriminated union for Coordinates that can be a
Tuple, Threeple or Fourple that represent tuples of size two,
three and four. The type for the union should be polymorphic.
Instantiate a Tuple of integers, a Threeple of floats and a
Fourple of strings.
Create a function that has a parameter of a binary function and
Coordinate. Apply the function to the Coordinate like
List.reduce. Call the function with (+) for each of the
Coordinates in part (b).
Binary Tree
Write a function that searches for a value in a binary tree and
then removes that node. Be sure to handle all of these cases:
The value is not in the tree.
Both children are Lf.
One child node is a Br the other is a Lf.
Both children are Br nodes.
Draw a memory diagram with before and after views for the
case where both children are Br nodes.
In the Notes on Programming Language Syntax page, an example par.docx

Weitere ähnliche Inhalte

Ähnlich wie In the Notes on Programming Language Syntax page, an example par.docx

Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manualNitesh Dubey
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlsana mateen
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionssana mateen
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxabhi353063
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharpDaniele Pozzobon
 
Introduction to Python Values, Variables Data Types Chapter 2
Introduction to Python  Values, Variables Data Types Chapter 2Introduction to Python  Values, Variables Data Types Chapter 2
Introduction to Python Values, Variables Data Types Chapter 2Raza Ul Mustafa
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework HelpC++ Homework Help
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and stringsRai University
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Keyappasami
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsRai University
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-aneebkmct
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and stringsRai University
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsRai University
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsRai University
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...venkatapranaykumarGa
 

Ähnlich wie In the Notes on Programming Language Syntax page, an example par.docx (20)

Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
 
Chapter 6 Intermediate Code Generation
Chapter 6   Intermediate Code GenerationChapter 6   Intermediate Code Generation
Chapter 6 Intermediate Code Generation
 
Strings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perlStrings,patterns and regular expressions in perl
Strings,patterns and regular expressions in perl
 
Unit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressionsUnit 1-strings,patterns and regular expressions
Unit 1-strings,patterns and regular expressions
 
This first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docxThis first assignment will focus on coding in Python, applying kno.docx
This first assignment will focus on coding in Python, applying kno.docx
 
Functional programming with FSharp
Functional programming with FSharpFunctional programming with FSharp
Functional programming with FSharp
 
Introduction to Python Values, Variables Data Types Chapter 2
Introduction to Python  Values, Variables Data Types Chapter 2Introduction to Python  Values, Variables Data Types Chapter 2
Introduction to Python Values, Variables Data Types Chapter 2
 
qb unit2 solve eem201.pdf
qb unit2 solve eem201.pdfqb unit2 solve eem201.pdf
qb unit2 solve eem201.pdf
 
C++ Programming Homework Help
C++ Programming Homework HelpC++ Programming Homework Help
C++ Programming Homework Help
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Diploma ii cfpc u-4 function, storage class and array and strings
Diploma ii  cfpc u-4 function, storage class and array and stringsDiploma ii  cfpc u-4 function, storage class and array and strings
Diploma ii cfpc u-4 function, storage class and array and strings
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
Btech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and stringsBtech i pic u-4 function, storage class and array and strings
Btech i pic u-4 function, storage class and array and strings
 
Functions torage class and array and strings-
Functions torage class and array and strings-Functions torage class and array and strings-
Functions torage class and array and strings-
 
function, storage class and array and strings
 function, storage class and array and strings function, storage class and array and strings
function, storage class and array and strings
 
Mcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and stringsMcai pic u 4 function, storage class and array and strings
Mcai pic u 4 function, storage class and array and strings
 
Lecture 9
Lecture 9Lecture 9
Lecture 9
 
Bsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and stringsBsc cs i pic u-4 function, storage class and array and strings
Bsc cs i pic u-4 function, storage class and array and strings
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
14-Intermediate code generation - Variants of Syntax trees - Three Address Co...
 

Mehr von mecklenburgstrelitzh

Discussion - Week 3Elements of the Craft of WritingThe narra.docx
Discussion - Week 3Elements of the Craft of WritingThe narra.docxDiscussion - Week 3Elements of the Craft of WritingThe narra.docx
Discussion - Week 3Elements of the Craft of WritingThe narra.docxmecklenburgstrelitzh
 
Discussion - Microbial ClassificationGive names of bacteria in.docx
Discussion - Microbial ClassificationGive names of bacteria in.docxDiscussion - Microbial ClassificationGive names of bacteria in.docx
Discussion - Microbial ClassificationGive names of bacteria in.docxmecklenburgstrelitzh
 
Discussion (Chapter 7) What are the common challenges with which se.docx
Discussion (Chapter 7) What are the common challenges with which se.docxDiscussion (Chapter 7) What are the common challenges with which se.docx
Discussion (Chapter 7) What are the common challenges with which se.docxmecklenburgstrelitzh
 
Discussion - Big Data Visualization toolsSeveral Big Data Visu.docx
Discussion - Big Data Visualization toolsSeveral Big Data Visu.docxDiscussion - Big Data Visualization toolsSeveral Big Data Visu.docx
Discussion - Big Data Visualization toolsSeveral Big Data Visu.docxmecklenburgstrelitzh
 
Discussion - 1 Pick 2 different department team members and descri.docx
Discussion - 1  Pick 2 different department team members and descri.docxDiscussion - 1  Pick 2 different department team members and descri.docx
Discussion - 1 Pick 2 different department team members and descri.docxmecklenburgstrelitzh
 
Discussion (Chapter 7) What are the common challenges with which .docx
Discussion (Chapter 7) What are the common challenges with which .docxDiscussion (Chapter 7) What are the common challenges with which .docx
Discussion (Chapter 7) What are the common challenges with which .docxmecklenburgstrelitzh
 
Discussion (Chapter 7) What are the common challenges with whic.docx
Discussion (Chapter 7) What are the common challenges with whic.docxDiscussion (Chapter 7) What are the common challenges with whic.docx
Discussion (Chapter 7) What are the common challenges with whic.docxmecklenburgstrelitzh
 
Discussion (Chapter 6) List and briefly describe the nine-step .docx
Discussion (Chapter 6) List and briefly describe the nine-step .docxDiscussion (Chapter 6) List and briefly describe the nine-step .docx
Discussion (Chapter 6) List and briefly describe the nine-step .docxmecklenburgstrelitzh
 
Discussion (Chapter 5) What is the relationship between Naïve Bayes.docx
Discussion (Chapter 5) What is the relationship between Naïve Bayes.docxDiscussion (Chapter 5) What is the relationship between Naïve Bayes.docx
Discussion (Chapter 5) What is the relationship between Naïve Bayes.docxmecklenburgstrelitzh
 
Discussion (Chapter 4) What are the privacy issues with data mini.docx
Discussion (Chapter 4) What are the privacy issues with data mini.docxDiscussion (Chapter 4) What are the privacy issues with data mini.docx
Discussion (Chapter 4) What are the privacy issues with data mini.docxmecklenburgstrelitzh
 
Discussion (Chapter 3) Why are the originalraw data not readily us.docx
Discussion (Chapter 3) Why are the originalraw data not readily us.docxDiscussion (Chapter 3) Why are the originalraw data not readily us.docx
Discussion (Chapter 3) Why are the originalraw data not readily us.docxmecklenburgstrelitzh
 
Discussion (Chapter 5) What is the relationship between Naïve B.docx
Discussion (Chapter 5) What is the relationship between Naïve B.docxDiscussion (Chapter 5) What is the relationship between Naïve B.docx
Discussion (Chapter 5) What is the relationship between Naïve B.docxmecklenburgstrelitzh
 
Discussion (Chapter 10 in the textbook or see the ppt) For ea.docx
Discussion (Chapter 10 in the textbook  or see the ppt) For ea.docxDiscussion (Chapter 10 in the textbook  or see the ppt) For ea.docx
Discussion (Chapter 10 in the textbook or see the ppt) For ea.docxmecklenburgstrelitzh
 
Discussion (Chapter 1) Compare and contrast predictive analytics wi.docx
Discussion (Chapter 1) Compare and contrast predictive analytics wi.docxDiscussion (Chapter 1) Compare and contrast predictive analytics wi.docx
Discussion (Chapter 1) Compare and contrast predictive analytics wi.docxmecklenburgstrelitzh
 
Discussion (400 words discussion + 150 words student response)Co.docx
Discussion (400 words discussion + 150 words student response)Co.docxDiscussion (400 words discussion + 150 words student response)Co.docx
Discussion (400 words discussion + 150 words student response)Co.docxmecklenburgstrelitzh
 
Discussion (150-200 words) Why do you think so much emphasis is pla.docx
Discussion (150-200 words) Why do you think so much emphasis is pla.docxDiscussion (150-200 words) Why do you think so much emphasis is pla.docx
Discussion (150-200 words) Why do you think so much emphasis is pla.docxmecklenburgstrelitzh
 
discussion (11)explain the concept of information stores as th.docx
discussion (11)explain the concept of information stores as th.docxdiscussion (11)explain the concept of information stores as th.docx
discussion (11)explain the concept of information stores as th.docxmecklenburgstrelitzh
 
Discussion #5 How progressive was the Progressive EraThe Progres.docx
Discussion #5 How progressive was the Progressive EraThe Progres.docxDiscussion #5 How progressive was the Progressive EraThe Progres.docx
Discussion #5 How progressive was the Progressive EraThe Progres.docxmecklenburgstrelitzh
 
Discussion #4, Continued Work on VygotskyA. Why is it important .docx
Discussion #4, Continued Work on VygotskyA. Why is it important .docxDiscussion #4, Continued Work on VygotskyA. Why is it important .docx
Discussion #4, Continued Work on VygotskyA. Why is it important .docxmecklenburgstrelitzh
 
Discussion #4 What are the most common metrics that make for an.docx
Discussion #4 What are the most common metrics that make for an.docxDiscussion #4 What are the most common metrics that make for an.docx
Discussion #4 What are the most common metrics that make for an.docxmecklenburgstrelitzh
 

Mehr von mecklenburgstrelitzh (20)

Discussion - Week 3Elements of the Craft of WritingThe narra.docx
Discussion - Week 3Elements of the Craft of WritingThe narra.docxDiscussion - Week 3Elements of the Craft of WritingThe narra.docx
Discussion - Week 3Elements of the Craft of WritingThe narra.docx
 
Discussion - Microbial ClassificationGive names of bacteria in.docx
Discussion - Microbial ClassificationGive names of bacteria in.docxDiscussion - Microbial ClassificationGive names of bacteria in.docx
Discussion - Microbial ClassificationGive names of bacteria in.docx
 
Discussion (Chapter 7) What are the common challenges with which se.docx
Discussion (Chapter 7) What are the common challenges with which se.docxDiscussion (Chapter 7) What are the common challenges with which se.docx
Discussion (Chapter 7) What are the common challenges with which se.docx
 
Discussion - Big Data Visualization toolsSeveral Big Data Visu.docx
Discussion - Big Data Visualization toolsSeveral Big Data Visu.docxDiscussion - Big Data Visualization toolsSeveral Big Data Visu.docx
Discussion - Big Data Visualization toolsSeveral Big Data Visu.docx
 
Discussion - 1 Pick 2 different department team members and descri.docx
Discussion - 1  Pick 2 different department team members and descri.docxDiscussion - 1  Pick 2 different department team members and descri.docx
Discussion - 1 Pick 2 different department team members and descri.docx
 
Discussion (Chapter 7) What are the common challenges with which .docx
Discussion (Chapter 7) What are the common challenges with which .docxDiscussion (Chapter 7) What are the common challenges with which .docx
Discussion (Chapter 7) What are the common challenges with which .docx
 
Discussion (Chapter 7) What are the common challenges with whic.docx
Discussion (Chapter 7) What are the common challenges with whic.docxDiscussion (Chapter 7) What are the common challenges with whic.docx
Discussion (Chapter 7) What are the common challenges with whic.docx
 
Discussion (Chapter 6) List and briefly describe the nine-step .docx
Discussion (Chapter 6) List and briefly describe the nine-step .docxDiscussion (Chapter 6) List and briefly describe the nine-step .docx
Discussion (Chapter 6) List and briefly describe the nine-step .docx
 
Discussion (Chapter 5) What is the relationship between Naïve Bayes.docx
Discussion (Chapter 5) What is the relationship between Naïve Bayes.docxDiscussion (Chapter 5) What is the relationship between Naïve Bayes.docx
Discussion (Chapter 5) What is the relationship between Naïve Bayes.docx
 
Discussion (Chapter 4) What are the privacy issues with data mini.docx
Discussion (Chapter 4) What are the privacy issues with data mini.docxDiscussion (Chapter 4) What are the privacy issues with data mini.docx
Discussion (Chapter 4) What are the privacy issues with data mini.docx
 
Discussion (Chapter 3) Why are the originalraw data not readily us.docx
Discussion (Chapter 3) Why are the originalraw data not readily us.docxDiscussion (Chapter 3) Why are the originalraw data not readily us.docx
Discussion (Chapter 3) Why are the originalraw data not readily us.docx
 
Discussion (Chapter 5) What is the relationship between Naïve B.docx
Discussion (Chapter 5) What is the relationship between Naïve B.docxDiscussion (Chapter 5) What is the relationship between Naïve B.docx
Discussion (Chapter 5) What is the relationship between Naïve B.docx
 
Discussion (Chapter 10 in the textbook or see the ppt) For ea.docx
Discussion (Chapter 10 in the textbook  or see the ppt) For ea.docxDiscussion (Chapter 10 in the textbook  or see the ppt) For ea.docx
Discussion (Chapter 10 in the textbook or see the ppt) For ea.docx
 
Discussion (Chapter 1) Compare and contrast predictive analytics wi.docx
Discussion (Chapter 1) Compare and contrast predictive analytics wi.docxDiscussion (Chapter 1) Compare and contrast predictive analytics wi.docx
Discussion (Chapter 1) Compare and contrast predictive analytics wi.docx
 
Discussion (400 words discussion + 150 words student response)Co.docx
Discussion (400 words discussion + 150 words student response)Co.docxDiscussion (400 words discussion + 150 words student response)Co.docx
Discussion (400 words discussion + 150 words student response)Co.docx
 
Discussion (150-200 words) Why do you think so much emphasis is pla.docx
Discussion (150-200 words) Why do you think so much emphasis is pla.docxDiscussion (150-200 words) Why do you think so much emphasis is pla.docx
Discussion (150-200 words) Why do you think so much emphasis is pla.docx
 
discussion (11)explain the concept of information stores as th.docx
discussion (11)explain the concept of information stores as th.docxdiscussion (11)explain the concept of information stores as th.docx
discussion (11)explain the concept of information stores as th.docx
 
Discussion #5 How progressive was the Progressive EraThe Progres.docx
Discussion #5 How progressive was the Progressive EraThe Progres.docxDiscussion #5 How progressive was the Progressive EraThe Progres.docx
Discussion #5 How progressive was the Progressive EraThe Progres.docx
 
Discussion #4, Continued Work on VygotskyA. Why is it important .docx
Discussion #4, Continued Work on VygotskyA. Why is it important .docxDiscussion #4, Continued Work on VygotskyA. Why is it important .docx
Discussion #4, Continued Work on VygotskyA. Why is it important .docx
 
Discussion #4 What are the most common metrics that make for an.docx
Discussion #4 What are the most common metrics that make for an.docxDiscussion #4 What are the most common metrics that make for an.docx
Discussion #4 What are the most common metrics that make for an.docx
 

Kürzlich hochgeladen

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxannathomasp01
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 

Kürzlich hochgeladen (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptxCOMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
COMMUNICATING NEGATIVE NEWS - APPROACHES .pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 

In the Notes on Programming Language Syntax page, an example par.docx

  • 1. In the Notes on Programming Language Syntax page, an example parser for a simple language is given, using C syntax. Write the parser using F#, but you may only use functional programming and immutable date. Create the list of tokens as a discriminated union, which (in the simplest case) looks like an enumeration. type TERMINAL = IF|THEN|ELSE|BEGIN|END|PRINT|SEMICOLON|ID|EOF With this type declared, you can use the terminals like you would use enumerated values in Java. Use immutable data. The C-code example uses mutable data. Pass the program into the start symbol function. Pass the input yet to be processed to each non-terminal function. The main function might look like this: let test_program program = let result = program |> S match result with | [] -> failwith "Early termination or missing EOF" | x::xs -> if x = EOF then accept() else error() You do not have to parse input strings. Assume that the parsing has been done. Pass a list of tokens that represent a program into the start symbol. Try these program examples: [IF;ID;THEN;BEGIN;PRINT;ID;SEMICOLON;PRINT;ID;END; ELSE;PRINT;ID;EOF] [IF;ID;THEN;IF;ID;THEN;PRINT;ID;ELSE;PRINT;ID;ELSE;B EGIN;PRINT;ID;END;EOF]
  • 2. Causes error: [IF;ID;THEN;BEGIN;PRINT;ID;SEMICOLON;PRINT;ID;SEMI COLON;END;ELSE;PRINT;ID;EOF] Print an accept message when the input is valid and completely consumed. Generate appropriate error messages for incorrect symbols, not enough input, and too much input. Once you have the parser recognizing input, generate a parse tree using a discriminated type. Implement a parser using functional programming and immutable data for the unambiguous grammar for arithmetic expressions, from the Notes on Programming Language Syntax. E -> E + T | E - T | T T -> T * F | T / F | F F -> i | (E) Use the suggestion in the notes to get around the fact that this grammar appears to need more than one lookahead token. Once you have the parser recognizing input, generate a parse tree using a discriminated type. Recall that an F# function that takes two arguments can be coded in either uncurried form (in which case it takes a pair as its input) or curried form (in which case it takes the first argument and returns a function that takes the second argument). In fact it is easy to convert from one form to the other in F#. To this end, define an F# function curry f that converts an uncurried function to a curried function, and an F# function uncurry f that does the opposite conversion. For example, > (+);;
  • 3. val it : (int -> int -> int) = [email protected] > > let plus = uncurry (+);; val plus : (int * int -> int) > plus (2,3);; val it : int = 5 > let cplus = curry plus;; val cplus : (int -> int -> int) > let plus3 = cplus 3;; val plus3 : (int -> int) > plus3 10;; val it : int = 13 What are the types of curry and uncurry ? Given vectors u = (u 1 , u 2 ,..., u n
  • 4. ) and v = (v 1 , v 2 ,..., v n ) , the inner product of u and v is defined to be u 1 *v 1 + u 2 *v 2 + ... + u n *v n . Write a curried F# function inner that takes two vectors represented as int list and returns their inner product. Throw an exception if the lists do not have the same length. Do not use any built-in or borrowed functions. Write it from scratch.
  • 5. Use tail recursion. > inner [1;2;3] [4;5;6];; val it : int = 32 Given an m -by- n matrix A and an n -by- p matrix B , the product of A and B is an m -by- p matrix whose entry in position ( i,j ) is the inner product of row i of A with column j of B . For example, / 0 1 / 1 2 3 * | 3 2 | = / 9 11
  • 6. 4 5 6 / 1 2 / 21 26 / Write an uncurried F# function to do matrix multiplication: > multiply ([[1;2;3];[4;5;6]], [[0;1];[3;2];[1;2]]);; val it : int list list = [[9; 11]; [21; 26]] Assume that the dimensions of the matrices are appropriate. Hint : Use transpose (from Homework 1 ), inner , and List.map . Evaluate the asymptotic time complexity of this function: let rec oddeven = function | [] -> [] | x::xs -> if x % 2 = 0 then oddeven xs @ [x] else x :: oddeven xs Two powerful List functions provided by F# are List.fold and List.foldBack . These are similar to List.reduce and
  • 7. List.reduceBack , but more general. Both take a binary function f , an initial value i , and a list [x1;x2;x3;...;xn] . Then List.fold returns (f ... (f (f (f i x1) x2) x3) ... xn) while List.foldBack returns (f x1 (f x2 (f x3 ... (f xn i) ... ))) In spite of this complicated behavior, they can be implemented very simply: > let rec fold f a = function | [] -> a | x::xs -> fold f (f a x) xs;; val fold : ('a -> 'b -> 'a) -> 'a -> 'b list -> 'a > let rec foldBack f xs a = match xs with
  • 8. | [] -> a | y::ys -> f y (foldBack f ys a);; val foldBack : ('a -> 'b -> 'b) -> 'a list -> 'b -> 'b (Note that they don't take their arguments in the same order.) Each of these functions can be used to implement flatten , which "flattens" a list of lists: let flatten1 xs = List.fold (@) [] xs let flatten2 xs = List.foldBack (@) xs [] For example, > flatten1 [[1;2];[];[3];[4;5;6]];; val it : int list = [1; 2; 3; 4; 5; 6] Compare the efficiency of flatten1 xs and flatten2 xs , both in terms of asymptotic time compexity and experimentally . To make the analysis simpler, assume that
  • 9. xs is a list of the form [[1];[2];[3];...;[n]] . Interpreter 0 In this problem, we begin our exploration of the use of F# for language-oriented programming . You will write an F# program to evaluate arithmetic expressions written in the language given by the following context-free grammar: E -> n | -E | E + E | E - E | E * E | E / E | (E) In the above, n is an integer literal, -E is the negation of E , the next four terms are the sum, difference, product, and quotient of expressions, and (E) is used to control the order of evaluation of expressions, as in the expression 3*(5-1) . Rather than working directly with the concrete syntax above, we will imagine that we have a parser that parses input into an abstract syntax tree , as is standard in real compilers. Hence your interpreter will take an input of the following discriminated union type: type Exp = Num of int | Neg of Exp
  • 10. | Sum of Exp * Exp | Diff of Exp * Exp | Prod of Exp * Exp | Quot of Exp * Exp Note how this definition mirrors the grammar given above. For instance, the constructor Num makes an integer into an Exp , and the constructor Sum makes a pair of Exp 's into an Exp representing their sum. Interpreting abstract syntax trees is much easier than trying to interpret concrete syntax directly. Note that there is no need for a constructor corresponding to parentheses, as the example given above would simply be represented by Prod(Num 3, Diff(Num 5, Num 1)) which represents the parse tree which looks like Your job is to write an F# function evaluate that takes an abstract syntax tree and returns the result of evaluating it. Most of the time, evaluating a tree will produce an integer, but we must address the possibility of dividing by zero. This could be handled by raising an exception, but instead we
  • 11. choose to make use of the built-in F# type type 'a option = None | Some of 'a Thus evaluate will have type Exp -> int option , allowing it to return Some m in the case of a successful evaluation, and None in the case of an evaluation that fails due to dividing by zero. For example, > evaluate (Prod(Num 3, Diff(Num 5, Num 1)));; val it : int option = Some 12 > evaluate (Diff(Num 3, Quot(Num 5, Prod(Num 7, Num 0))));; val it : int option = None Naturally, evaluate e should use recursion to evaluate each of e 's sub-expressions; it should also use match to distinguish between the cases of successful or failed sub- evaluations. To get you started, here is the beginning of the definition of evaluate : let rec evaluate = function
  • 12. | Num n -> Some n | Neg e -> match evaluate e with | ... Record Create a record type for Name, Credits and GPA. Create a record instance with the values "Jones", 109, 3.85. Discriminated Union Create a discriminated union for Coordinates that can be a Tuple, Threeple or Fourple that represent tuples of size two, three and four. The type for the union should be polymorphic. Instantiate a Tuple of integers, a Threeple of floats and a Fourple of strings. Create a function that has a parameter of a binary function and Coordinate. Apply the function to the Coordinate like List.reduce. Call the function with (+) for each of the Coordinates in part (b). Binary Tree Write a function that searches for a value in a binary tree and then removes that node. Be sure to handle all of these cases: The value is not in the tree. Both children are Lf. One child node is a Br the other is a Lf. Both children are Br nodes. Draw a memory diagram with before and after views for the case where both children are Br nodes.