Question: Write an LL( 1) parser generator in purely functional Scheme. If you consult Figure 2.24 in our textbook (Scott), you will need to use tail

 Write an LL( 1) parser generator in purely functional Scheme. If

Write an LL( 1) parser generator in purely functional Scheme. If you consult Figure 2.24 in our textbook (Scott), you will need to use tail recursion in place of iteration. Assume that the input CFG consists of a list of lists, one per nonterminal in the grammar. The first element of each sublist should be the nonterminal; the remaining elements should be the right-hand sides of the productions for which that nonterminal is the left-hand side. You may assume that the sublist for the start symbol will be the first one in the list. If we use quoted strings to represent grammar symbols, the calculator grammar of Figure 2.16 in the textbook would look like this ((" program" (" stmt list" "S S")) ("stmt-list" ( stmt', ''stmt-list") ()) ("strnt" ('' id" ?"?"expr") ('' read" "id") (" write" expr")) (" expr" (" term" "term tail") (" term" (" factor" factor-tail")) ("term tail" (" add op" "term" "term tail") O) (" factor-tail" (" mult-op" "factor" FT") ()) ("add op")) ("mult op" () ("") (" factor" (" id") (" number") ("(" "expr" )"))) Your output should be a parse table that has this same format, except that every right-hand side is replaced by a pair (a 2-element list) whose first element is the firstt set for the corresponding production, and whose second element is the right-hand side. For the calculator grammar, the table looks like this ((" program" ((" S $" "id" "read" "write") (" stmt_list" "S S"))) ("stmt-list" ((" ?d" "read" "write") (" stmt', "stmt-list")) ((" $ $,,)( ))) ("stmt" ((" id") ("id" . . " "expr")) ((" read") (" read" "id")) ((" write") (" write" "expr"))) (" expr" (("(" "id" "number") ( term" "term tail")) ("term" (((" "id" "number") (" factor" "factor tail") (" term tail" ((" +', ") (" add op" "term" "term tall'')) ((" $ $ )', id" "read" "write") ())) (" factor tal" (("*?) (" mult op,' ''factor" factor tail")) ((" $ $ )" +" ,, "id" "read" write) 0)) ("mult op" () (" ((" "" (" factor" ("id") ("id")) ((" number") (" number")) (("(") ("(" expr" ")")))) (Hint: You may want to define a right context function that takes a nonterminal B as argument and returns a list of all pairs (A, B), where A is a nonterminal and B is a list of symbols, such that for some potentially different list of symbols ?, ? ? ? ?. This function is useful for computing FOLLOW sets. You may also want to build a tail-recursive function that re-computes FIRST and FOLLOW sets until they converge. You will find it easier if you do not include ? in either set, but rather keep a separate estimate, for each nonterminal, of whether it may generate ?.) Write an LL( 1) parser generator in purely functional Scheme. If you consult Figure 2.24 in our textbook (Scott), you will need to use tail recursion in place of iteration. Assume that the input CFG consists of a list of lists, one per nonterminal in the grammar. The first element of each sublist should be the nonterminal; the remaining elements should be the right-hand sides of the productions for which that nonterminal is the left-hand side. You may assume that the sublist for the start symbol will be the first one in the list. If we use quoted strings to represent grammar symbols, the calculator grammar of Figure 2.16 in the textbook would look like this ((" program" (" stmt list" "S S")) ("stmt-list" ( stmt', ''stmt-list") ()) ("strnt" ('' id" ?"?"expr") ('' read" "id") (" write" expr")) (" expr" (" term" "term tail") (" term" (" factor" factor-tail")) ("term tail" (" add op" "term" "term tail") O) (" factor-tail" (" mult-op" "factor" FT") ()) ("add op")) ("mult op" () ("") (" factor" (" id") (" number") ("(" "expr" )"))) Your output should be a parse table that has this same format, except that every right-hand side is replaced by a pair (a 2-element list) whose first element is the firstt set for the corresponding production, and whose second element is the right-hand side. For the calculator grammar, the table looks like this ((" program" ((" S $" "id" "read" "write") (" stmt_list" "S S"))) ("stmt-list" ((" ?d" "read" "write") (" stmt', "stmt-list")) ((" $ $,,)( ))) ("stmt" ((" id") ("id" . . " "expr")) ((" read") (" read" "id")) ((" write") (" write" "expr"))) (" expr" (("(" "id" "number") ( term" "term tail")) ("term" (((" "id" "number") (" factor" "factor tail") (" term tail" ((" +', ") (" add op" "term" "term tall'')) ((" $ $ )', id" "read" "write") ())) (" factor tal" (("*?) (" mult op,' ''factor" factor tail")) ((" $ $ )" +" ,, "id" "read" write) 0)) ("mult op" () (" ((" "" (" factor" ("id") ("id")) ((" number") (" number")) (("(") ("(" expr" ")")))) (Hint: You may want to define a right context function that takes a nonterminal B as argument and returns a list of all pairs (A, B), where A is a nonterminal and B is a list of symbols, such that for some potentially different list of symbols ?, ? ? ? ?. This function is useful for computing FOLLOW sets. You may also want to build a tail-recursive function that re-computes FIRST and FOLLOW sets until they converge. You will find it easier if you do not include ? in either set, but rather keep a separate estimate, for each nonterminal, of whether it may generate ?.)

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 Databases Questions!