Answered step by step
Verified Expert Solution
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 stmtlist tokens
match stmts with
if List.length remainingTokens then
sprintf "Unexpected token: A remainingTokens.
else
and stmtlist tokens: Token list : Token list Token list
match tokens with
Empty statement list
let stmt remainingTokens stmt tokens
match stmt with
No statement parsed
let stmts remainingTokens stmtlist remainingTokens
stmt @ stmts remainingTokens
and stmt tokens: Token list : Token list Token list
match tokens with
ID :: tl idtail ID :: tl Begin with an identifier
READ :: tl readstmt tl
WRITE :: tl writestmt tl
IF :: tl ifstmt tl
DO :: tl dostmt tl
WHILE :: tl whilestmt tl
tokens
and idtail tokens: Token list : Token list Token list
match tokens with
ID :: ASSIGN :: assignstmt tokens
ID :: LPAREN :: funcall tokens
tokens
and assignstmt tokens: Token list : Token list Token list
match tokens with
ID :: ASSIGN :: let exprTokens, remainingTokens expr Listtail Listtail 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 tokens For simplicity, consider an expression as ID : ID
LPAREN :: tl let subExpr, remainingTokens expr Listtail tokens in
match remainingTokens with
RPAREN :: rest LPAREN :: subExpr @ RPAREN rest
tokens
tokens
and funcall tokens: Token list : Token list Token list
match tokens with
ID :: LPAREN :: tl let paramList, remainingTokens paramlist Listtail Listtail tokens in
match remainingTokens with
RPAREN :: rest LPAREN @ paramList @ RPAREN rest
tokens
tokens
and paramlist 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 whilestmt tokens: Token list : Token list Token list
match tokens with
WHILE :: tl let condTokens, remainingTokens cond Listtail tokens in
match remainingTokens with
DO :: rest WHILE :: condTokens @ DO rest
tokens
tokens
and dostmt tokens: Token list : Token list Token list
match tokens with
DO :: tl let stmtList, remainingTokens stmtlist Listtail tokens in
match remainingTokens with
UNTIL :: tail DO :: stmtList @ UNTIL tail
tokens
tokens
and ifstmt 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
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