Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have this code and I need i t t o work for the grammar i n the picture type Token = | NOUN |

I have this code and I need itto work for the grammar in the picture type Token =
| NOUN
| VERB
| ARTICLE
| ADJECTIVE
| ADVERB
| PREPOSITION
| COMMA
| CONJUNCTION
| EOS // End of Sentence (period, exclamation point, etc.)
| OTHER of string // Could represent and ID in a more complex language, but for now, just a catch-all for anything else.
// Member (of the type) function to get a token from a lexeme (String)
static member tokenFromLexeme = function
|","-> COMMA
| "dog" | "cat" | "tree" -> NOUN
|"a"|"an"| "the" -> ARTICLE
| "chases" -> VERB
| "small" | "tall" | "slow" | "fast" -> ADJECTIVE
| "quietly" | "quickly" -> ADVERB
|"up"| "around" -> PREPOSITION
|"."|"!"|"?"-> EOS
| "and" |"or"-> CONJUNCTION
| x -> OTHER x // aka, ID
let rec parse theList = theList |> sentence
// :
and sentence lst = lst |> np |> vp |> np |> sentenceTail
and sentenceTail lst =
match lst with
| CONJUNCTION :: xs -> sentence xs
| EOS :: xs -> failwith $"End of sentence marker found, but not at end!
Remaining Tokens {xs}"
| x :: _-> failwithf $"Expected EOS but found: {x}"
|[]-> "Done!"
//*** This uses the matchToken function that was defined above.
// ::= ARTICLE NOUN
and np lst =
let matchNoun lst =
match lst with
| NOUN :: xs -> xs
| x -> failwith $"Expected Noun, but found {x}."
in
match lst with
| ARTICLE :: xs -> xs |> adjList |> matchNoun |> pp
| x :: xs -> failwithf $"Expected article, but found {x}.
remaining tokens: {xs}"
|[]-> failwith "article should not be empty"
// ::= ADJECTIVE |\epsi
and adjList =
function
| ADJECTIVE :: xs -> xs |> adjTail
| xs -> xs //\epsi is permitted, so just resolve to what was passed if no other match.
// ::= COMMA |\epsi
and adjTail =
function
| COMMA :: xs -> xs |> adjList
| xs -> xs //\epsi is permitted, so just resolve to what was passed if no other match.
// ::= PREPOSITION |\epsi
and pp =
function
| PREPOSITION :: xs -> xs |> np
| xs -> xs //\epsi is permitted, so just resolve to what was passed if no other match.
// ::= ADVERB VERB | VERB
and vp lst =
let matchVerb lst =
match lst with
| VERB :: xs -> xs
| x -> failwithf $"Expected Verb, but found {x}."
in
match lst with
| VERB :: xs -> xs
| ADVERB :: xs -> xs |> matchVerb
| x :: xs -> failwithf $"Expected Verb Phrase, but found {x}.
Remaining tokens: {xs}"
|[]-> failwith "Unexpected end of input while processing Verb Phrase."
open System.Text.RegularExpressions
let main ()=
let mapTokens = Array.toList >> List.map Token.tokenFromLexeme
// This is very ".NET" specific. Split is part of the .NET API.
// KEEPS DELIMIER: let getTokenList (str: string)= Regex.Split(str.Trim(), @"(?=\s+)")|> mapTokens
let getTokenList (str: string)= Regex.Split(str.Trim(), @"\s+")|> mapTokens
(* Begin Parsing Process *)
let startParsing str =
// Display our list of tokens...
printfn $"
Initial String: %s{str}"
// Try to parse the list of tokens and display the results.
try
let tokenList = getTokenList str
printfn $"Tokens Before Parsing: %A{tokenList}"
let parsedList = parse tokenList
if (parsedList.Length >0) then
printfn $"Parsing Failed because we have extra tokens! %A{parsedList}"
printfn $"Extra Tokens:\t%A{parsedList}"
else
printfn "Done!"
// If an exception ("failwith") is thrown, display the error message.
with Failure msg ->
printfn $"Error: %s{msg}"
// Get the user input and start parsing
let getUserInput ()=
printf "Enter (Space Delimited) String
=>"
// A case where it's easier to use the .NET ReadLine as opposed to the more restrictive OCaml native variant.
System.Console.ReadLine()
in
// Get the user input and start parsing
getUserInput ()|> startParsing |> ignore // Just ignore the result, as we are just printing results above.
main ()
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Driven Web Sites

Authors: Mike Morrison, Joline Morrison

1st Edition

061901556X, 978-0619015565

More Books

Students also viewed these Databases questions