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 = Tok_Int of int | Tok_LParen | Tok_RParen |
Tok_Mult | Tok_Plus | Tok_EOF
Lookahead Function
This returns the head of the passed-in token list, or throws a failure if the list was empty.
let lookahead toks = match toks with
h::t -> h
|_-> raise (Failure("Empty input to lookahead"))
string_of_token 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 string_of_token tok = match tok with
| Tok_Int(i)-> string_of_int i
| Tok_Mult ->"*"
| Tok_Plus ->"+"
| Tok_LParen ->"("
| Tok_RParen ->")"
| Tok_EOF ->""
let rec string_of_list conv lst =
match lst with
|[]->""
| h::[]-> conv h
| h::t ->(conv h)^""^(string_of_list conv t)
match_token Function
This returns the tail of the passed-in token list if the passed-in 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 match_token (toks : token list)(tok : token) : token list =
match toks with
|[]-> raise (Failure(string_of_token tok))
| h::t when h = tok -> t
| h::_-> raise (Failure(
Printf.sprintf "Expected %s from input %s, got %s"
(string_of_token tok)
(string_of_list string_of_token toks)
(string_of_token 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 non-terminal 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 Tok_EOF exists. Like in the discussion example, all valid token lists passed to the parser will end with Tok_EOF. Since both examples have no explicit handling of this, it should remain in the passed back list. parse_S [Tok_Int 5; Tok_EOF] should return ([Tok_EOF], Num 5) 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 Tok_EOF.
NOTE: Can be neither or both
A)
let rec parse_S toks =
let (token_lst_1, expression1)= parse_T toks in
match lookahead token_lst_1 with
| Tok_Plus -> let token_lst_2= match_token token_lst_1 Tok_Plus in
let (token_lst_3, expression2)= parse_S token_lst_2 in
(token_lst_2, Add(expression1, expression2))
| Tok_Mult -> let token_lst_2= match_token token_lst_1 Tok_Mult in
let (token_lst_3, expression2)= parse_S token_lst_1 in
(token_lst_3, Mult(expression1, expression2))
|_->(token_lst_1, expression1)
and parse_T toks =
match lookahead toks with
| Tok_Int(n)-> let token_lst = match_token toks (Tok_Int n) in
(token_lst, Num n)
| Tok_LParen -> let token_lst_1= match_token toks Tok_LParen in
let (token_lst_2, expression)= parse_S token_lst_1 in
let final_token_lst = match_token token_lst_1 Tok_RParen in
(final_token_lst, Paren expression)
|_-> failwith "Unexpected token in parse_T"
B)
let rec parse_S toks =
let (toks1, lhs)= parse_T toks in
match lookahead toks1 with
| Tok_Plus ->
let toks2= match_token toks1 Tok_Plus in
let (toks3, rhs)= parse_S toks2 in
(toks3, Add(lhs, rhs))
| Tok_Mult ->
let toks2= match_token toks1 Tok_Mult in
let (toks3, rhs)= parse_S toks2 in
(toks3, Mult(lhs, rhs))
|_->(toks1, lhs)
and parse_T toks = match lookahead toks with
| Tok_Int(n)->
let toks1= match_token toks (Tok_Int n) in
(toks1, Num n)
| Tok_LParen ->
let toks1= match_token toks Tok_LParen in
let (toks2, expr)= parse_S toks1 in
(toks2, 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 blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!