Answered step by step
Verified Expert Solution
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 work for the grammar 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 catchall 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
aan "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.SplitstrTrim @s mapTokens
let getTokenList str: string Regex.SplitstrTrim @s mapTokens
Begin Parsing Process
let startParsing str
Display our list of tokens...
printfn $
Initial String: sstr
Try to parse the list of tokens and display the results.
try
let tokenList getTokenList str
printfn $"Tokens Before Parsing: AtokenList
let parsedList parse tokenList
if parsedListLength then
printfn $"Parsing Failed because we have extra tokens! AparsedList
printfn $"Extra Tokens:tAparsedList
else
printfn "Done!"
If an exception failwith is thrown, display the error message.
with Failure msg
printfn $"Error: smsg
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
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started