Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Answer Q2 the 1st screenshot and the python code is for additional information #!/local/bin/python import sys, string, tokenize tokens = iter( sys.stdin.read().split() ) cur_token =

Answer Q2 the 1st screenshot and the python code is for additional information

#!/local/bin/python import sys, string, tokenize tokens = iter( sys.stdin.read().split() ) cur_token = None class ParseError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) class EvalError(Exception): def __init__(self, value): self.value = value def __str__(self): return repr(self.value) def lookahead(): global cur_token if cur_token == None: try: cur_token = tokens.next() except: cur_token = None return cur_token def next_token(): global cur_token n = lookahead() cur_token = None return n def add( a, b ): return a + b def sub( a, b ): return a - b def mul( a, b ): return a * b def div( a, b ): return a / b def do_arith_op( op, l ): if len( l ) 1: a = f[1] elif op == "car": if len( f ) > 1: l = do_eval( f[1] ) if isinstance( l, list ) and len( l ) > 0: a = l[0] elif op == "cdr": if len( f ) > 1: l = do_eval( f[1] ) if isinstance( l, list ) and len( l ) > 0: a = l[1:] elif op == "cons": if len( f ) > 2: h = do_eval( f[1] ) t = do_eval( f[2] ) if isinstance( t, list ): a = [ h ] + t elif op == "list": a = [] for b in f[1:]: a = a + [do_eval( b )] else: raise EvalError( 'unknown proc: ' + str( op ) ) if a == None: raise EvalError( op ) return a elif str(a).isdigit(): # int return a else: # id return a def parseS(): tok = lookahead() if tok != ")": return parseAtoms() else: raise ParseError( 'S' ) def parseAtoms(): tok = lookahead() if tok == None or tok == ")": return [] else: return parseAtom() + parseAtoms() def parseAtom(): tok = lookahead() if tok == "(": l = [ parseList() ] elif tok == "'": # quote next_token() l = [[tok] + parseAtom()] elif( str(tok).isdigit() ): # integer next_token() l = [int( tok )] elif tok != None: # identifier next_token() l = [tok] else: raise ParseError( 'Atom' ) return l def parseList(): tok = next_token() if tok == '(': l = parseListBody() tok = next_token() if tok != ")": raise ParseError( 'List' ) else: raise ParseError( 'List' ) return l def parseListBody(): tok = lookahead() l = [] if tok != None: return l + parseAtoms() else: raise ParseError( 'ListBody' ) def atom2str( l ): if isinstance( l, list ): if len( l )

image text in transcribed

image text in transcribed

adds three additional operationms to the ones from the previous assignment. All three define, let nd ltbind variable names ids to values. The syntax for define is define id atom) For example, the expressions define foo 42 define bar 7 13)) bind foo to the value 42 and bind bar to 91, which is the evaluation of 7 13. Speciically define takes two parameters, an id, ich is not evaluated, and an expression, whose evaluation is then bound to the id. The evaluation of a define expression adds the binding to the current reserence environment and yieds the id. The example above yields foo and bar The syntax for let is let id atom) .. atom ..) where the denotes one or more of the preceding item. For example, the expressions let 42 ) x 13 let x 42) Cy13 ) xy) let (z7) 13) C- xyz) let (C221 (let (x7 x 13 evaluate to 55, 22, and 84, respectively. Specifically, let takes 2 or more arguments. The first argument is a list of bindings where each binding consists of an id and an expressson The remaining arguments are expressions that are evaluated using the bindings defined in the first argument. The evaluation of the let expression is the evaluation of the last argument. The bindings are only active inside the let expression CSCI3136 Summer 2018 Assignment 7 In the example ahove, the first let expression binds r to 42 and then evaluates x 13 to vield 55. the next evaluation of the overal The second let expression binds r to 42 and y to 13, evaluates x y and then evaluates binds to 7 and then evaluates the expressionsx 13 and x y z ). The evaluation of the latter expression is also the expression, which is also a let expression. This expression Lastly, the third expression binds r to the evaluation of ? * 2 21 ) and evaluates the next two expression The first expression is also a let expression, which rebinds to 7, and evaluates+ x 13 ). Then, the second expression+xi) is evaluated, using the outer binding of . Thus, the overall evaluation yields 84. The syntax for let is the same as for let. The only difference is that the as the first argument is being evaluated, all bindings that have already been evaluated are also visible to the next binding. The folliwing expression evaluates to 85, becaus r binds to 42 and y binds to r1, which is 43. let. Cx 42 ) (y(+x 1) x y) 1. 10 marks] A recursive descent parser,cluscm eval.py, (written in Python) that grammar is provided as part of this assignment. However, this parser does not implement let, let. or define. Furthermore, the evaluation phase has been decoupled from the parsing phase. That is, usn_eval.py first parses the input, creating a list of expressions, and then evaluates each of the expressions in and evaluates programs using the above context-free a separate pass Consider the Scheme binding operations define, let, let, and letrec that were discussed necessary to separate the parsing and the evaluation into two distinct passes in order to implement each of these operations. in class. For each of these operations determine whether it is ustify each of vour answers adds three additional operationms to the ones from the previous assignment. All three define, let nd ltbind variable names ids to values. The syntax for define is define id atom) For example, the expressions define foo 42 define bar 7 13)) bind foo to the value 42 and bind bar to 91, which is the evaluation of 7 13. Speciically define takes two parameters, an id, ich is not evaluated, and an expression, whose evaluation is then bound to the id. The evaluation of a define expression adds the binding to the current reserence environment and yieds the id. The example above yields foo and bar The syntax for let is let id atom) .. atom ..) where the denotes one or more of the preceding item. For example, the expressions let 42 ) x 13 let x 42) Cy13 ) xy) let (z7) 13) C- xyz) let (C221 (let (x7 x 13 evaluate to 55, 22, and 84, respectively. Specifically, let takes 2 or more arguments. The first argument is a list of bindings where each binding consists of an id and an expressson The remaining arguments are expressions that are evaluated using the bindings defined in the first argument. The evaluation of the let expression is the evaluation of the last argument. The bindings are only active inside the let expression CSCI3136 Summer 2018 Assignment 7 In the example ahove, the first let expression binds r to 42 and then evaluates x 13 to vield 55. the next evaluation of the overal The second let expression binds r to 42 and y to 13, evaluates x y and then evaluates binds to 7 and then evaluates the expressionsx 13 and x y z ). The evaluation of the latter expression is also the expression, which is also a let expression. This expression Lastly, the third expression binds r to the evaluation of ? * 2 21 ) and evaluates the next two expression The first expression is also a let expression, which rebinds to 7, and evaluates+ x 13 ). Then, the second expression+xi) is evaluated, using the outer binding of . Thus, the overall evaluation yields 84. The syntax for let is the same as for let. The only difference is that the as the first argument is being evaluated, all bindings that have already been evaluated are also visible to the next binding. The folliwing expression evaluates to 85, becaus r binds to 42 and y binds to r1, which is 43. let. Cx 42 ) (y(+x 1) x y) 1. 10 marks] A recursive descent parser,cluscm eval.py, (written in Python) that grammar is provided as part of this assignment. However, this parser does not implement let, let. or define. Furthermore, the evaluation phase has been decoupled from the parsing phase. That is, usn_eval.py first parses the input, creating a list of expressions, and then evaluates each of the expressions in and evaluates programs using the above context-free a separate pass Consider the Scheme binding operations define, let, let, and letrec that were discussed necessary to separate the parsing and the evaluation into two distinct passes in order to implement each of these operations. in class. For each of these operations determine whether it is ustify each of vour answers

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 Theory Icdt 97 6th International Conference Delphi Greece January 8 10 1997 Proceedings Lncs 1186

Authors: Foto N. Afrati ,Phokion G. Kolaitis

1st Edition

3540622225, 978-3540622222

More Books

Students also viewed these Databases questions

Question

Explain the five steps in journalizing and posting transactions.

Answered: 1 week ago

Question

(b) At what optimal point should the process be operated?

Answered: 1 week ago

Question

.

Answered: 1 week ago

Question

What are the purposes of collection messages? (Objective 5)

Answered: 1 week ago