Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/ / Token Type type Token = | ID of string | ASSIGN | SEMICOLON | EOF | READ | WRITE | IF | THEN

// Token Type
type Token =
| ID of string
| ASSIGN
| SEMICOLON
| EOF
| READ
| WRITE
| IF
| THEN
| ELSE
| ENDIF
| COMMA
| WHILE
| DO
| UNTIL
| LPAREN
| RPAREN
| LT
| GT
| EQ
| PLUS
| MINUS
| TIMES
| DIV
// Lexical analyzer function
let lexer (input: string) : Token list =
// Split input string into tokens
input.Split([|''; '\t'|], System.StringSplitOptions.RemoveEmptyEntries)
|> Array.map (fun token ->
match token with
|":"-> ASSIGN
|";"-> SEMICOLON
| "read" -> READ
| "write" -> WRITE
|"if"-> IF
| "then" -> THEN
| "else" -> ELSE
| "endif" -> ENDIF
|","-> COMMA
| "while" -> WHILE
|"do"-> DO
| "until" -> UNTIL
|"("-> LPAREN
|")"-> RPAREN
|">"-> GT
|"<"-> LT
|"=="-> EQ
|"+"-> PLUS
|"-"-> MINUS
|"*"-> TIMES
|"/"-> DIV
|_ as s -> ID s
)
|> Array.toList
let rec parse (input: string) : string =
let tokens = lexer input
match tokens with
|[]->""
|_-> let stmts, remainingTokens = stmt_list tokens
match stmts with
|[]-> if List.length remainingTokens >0 then
sprintf "Unexpected token: %A" remainingTokens.[0]
else
""
|_->""
and stmt_list (tokens: Token list) : Token list * Token list =
match tokens with
|[]->[],[]// Empty statement list
|_-> let stmt1, remainingTokens1= stmt tokens
match stmt1 with
|[]->[],[]// No statement parsed
|_-> let stmts2, remainingTokens2= stmt_list remainingTokens1
stmt1 @ stmts2, remainingTokens2
and stmt (tokens: Token list) : Token list * Token list =
match tokens with
|(ID _) :: tl -> id_tail (ID "" :: tl)// Begin with an identifier
| READ :: tl -> read_stmt tl
| WRITE :: tl -> write_stmt tl
| IF :: tl -> if_stmt tl
| DO :: tl -> do_stmt tl
| WHILE :: tl -> while_stmt tl
|_->[], tokens
and id_tail (tokens: Token list) : Token list * Token list =
match tokens with
|(ID _) :: ASSIGN :: _-> assign_stmt tokens
|(ID _) :: LPAREN :: _-> fun_call tokens
|_->[], tokens
and assign_stmt (tokens: Token list) : Token list * Token list =
match tokens with
|(ID _) :: ASSIGN :: _-> let exprTokens, remainingTokens = expr (List.tail (List.tail tokens)) in
match exprTokens with
| SEMICOLON :: rest ->[ASSIGN] @ exprTokens @ [SEMICOLON], rest
|_->[], tokens
|_->[], tokens
and expr (tokens: Token list) : Token list * Token list =
match tokens with
|(ID _) :: tl -> List.splitAt 3 tokens // For simplicity, consider an expression as ID := ID
| LPAREN :: tl -> let subExpr, remainingTokens = expr (List.tail tokens) in
match remainingTokens with
| RPAREN :: rest -> LPAREN :: subExpr @ [RPAREN], rest
|_->[], tokens
|_->[], tokens
and fun_call (tokens: Token list) : Token list * Token list =
match tokens with
|(ID _) :: LPAREN :: tl -> let paramList, remainingTokens = param_list (List.tail (List.tail tokens)) in
match remainingTokens with
| RPAREN :: rest ->[LPAREN] @ paramList @ [RPAREN], rest
|_->[], tokens
|_->[], tokens
and param_list (tokens: Token list) : Token list * Token list =
match tokens with
|[]->[],[]// Empty parameter list
|_-> let exprTokens, remainingTokens = expr tokens in
match remainingTokens with
| COMMA :: rest -> exprTokens @ [COMMA], rest // More parameters
|_-> exprTokens, remainingTokens // Last parameter
and while_stmt (tokens: Token list) : Token list * Token list =
match tokens with
| WHILE :: tl -> let condTokens, remainingTokens = cond (List.tail tokens) in
match remainingTokens with
| DO :: rest -> WHILE :: condTokens @ [DO], rest
|_->[], tokens
|_->[], tokens
and do_stmt (tokens: Token list) : Token list * Token list =
match tokens with
| DO :: tl -> let stmtList, remainingTokens = stmt_list (List.tail tokens) in
match remainingTokens with
| UNTIL :: tail -> DO :: stmtList @ [UNTIL], tail
|_->[], tokens
|_->[], tokens
and if_stmt (tokens: Token list) : Token list * Token list =
match tokens with
| IF :: tl -> let c

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 Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

8th Edition

013460153X, 978-0134601533

More Books

Students also viewed these Databases questions