Question: The token type is as follows: type token = Tok _ Int of int | Tok _ LParen | Tok _ RParen | Tok _
The token type is as follows:
type token TokInt of int TokLParen TokRParen
TokMult TokPlus TokEOF
Lookahead Function
This returns the head of the passedin token list, or throws a failure if the list was empty.
let lookahead toks match toks with
h::t h
raise FailureEmpty input to lookahead"
stringoftoken Helpers
These convert tokens into strings and are used for debugging. You do not need to understand them deeply, but the code is here if you want it
let stringoftoken tok match tok with
TokInti stringofint i
TokMult
TokPlus
TokLParen
TokRParen
TokEOF
let rec stringoflist conv lst
match lst with
h:: conv h
h::t conv hstringoflist conv t
matchtoken Function
This returns the tail of the passedin token list if the passedin token matches the head of the token list. If it does not match, or an empty list was passed in a failure is thrown.
let matchtoken toks : token listtok : token : token list
match toks with
raise Failurestringoftoken tok
h::t when h tok t
h:: raise Failure
Printf.sprintf "Expected s from input s got s
stringoftoken tok
stringoflist stringoftoken toks
stringoftoken h
Consider the following CFG and AST type:
S T S T S T
T n S
n is any int
type ast Num of int Paren of ast Mult of ast ast
Add of ast ast
Which of the following code snippets is correct when parsing nonterminal S
Note: This returns the rest of the tokens, and then the tree. That is the type of these functions is token list token list ast
See that TokEOF exists. Like in the discussion example, all valid token lists passed to the parser will end with TokEOF. Since both examples have no explicit handling of this, it should remain in the passed back list. parseS TokInt ; TokEOF should return TokEOF Num where the first thing in the tuple represents the remaining part of the list after successful parsing and the second thing in the tuple is the AST created by the parser.
A correctly functioning parser will never leave anything in the remaining token list except TokEOF.
NOTE: Can be neither or both
A
let rec parseS toks
let tokenlst expression parseT toks in
match lookahead tokenlst with
TokPlus let tokenlst matchtoken tokenlst TokPlus in
let tokenlst expression parseS tokenlst in
tokenlst Addexpression expression
TokMult let tokenlst matchtoken tokenlst TokMult in
let tokenlst expression parseS tokenlst in
tokenlst Multexpression expression
tokenlst expression
and parseT toks
match lookahead toks with
TokIntn let tokenlst matchtoken toks TokInt n in
tokenlst Num n
TokLParen let tokenlst matchtoken toks TokLParen in
let tokenlst expression parseS tokenlst in
let finaltokenlst matchtoken tokenlst TokRParen in
finaltokenlst Paren expression
failwith "Unexpected token in parseT
B
let rec parseS toks
let toks lhs parseT toks in
match lookahead toks with
TokPlus
let toks matchtoken toks TokPlus in
let toks rhs parseS toks in
toks Addlhs rhs
TokMult
let toks matchtoken toks TokMult in
let toks rhs parseS toks in
toks Multlhs rhs
toks lhs
and parseT toks match lookahead toks with
TokIntn
let toks matchtoken toks TokInt n in
toks Num n
TokLParen
let toks matchtoken toks TokLParen in
let toks expr parseS toks in
toks Paren expr
failwith "Expected a number or parentheses"
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
