Related questions
Using PLY, write an interpreter for the language of "LISP expressions".
A LISP expression is defined as follows:
- A number (integer as well as fractional) is a LISP expression.
- if E1 and E2 are LISP expressions then so are (+ E1 E2), (- E1 E2), (* E1 E2), and (/ E1 E2).
- if L is a LIST expression (defined below) then (car L) is a LISP expression.
- if B is a BOOLEAN expression and E1 and E2 are LISP expressions then (if B E1 E2) is a LISP expression.
A LIST Expression is defined as follows:
- if E1, E2, ..., En are LISP expressions where n>=0 then (E1 E2 ... En) is a LIST expression.
- if L is a LIST expression then (cdr L) is a LIST expression.
- if E is a LISP expression and L is a LIST expression then (cons E L) is a LIST expression.
A BOOLEAN expression is defined as follows:
- True and False are BOOLEAN expressions.
- if E1 and E2 are LISP expressions then (> E1 E2), (>= E1 E2), (< E1 E2), (<= E1 E2), (= E1 E2), and (<> E1 E2) are BOOLEAN expressions.
- if B1 and B2 are BOOLEAN expressions then so are (not B1), (and B1 B2), and (or B1 B2).
Here are examples of valid LISP expressions:34 True (20 30 40) (+ 20 30) (* (+ 1 2) (/ 8 4)) (* (car (2 4 (+ 2 4) 8)) (/ 27 9)) (+ (car (2 3 4)) (car (cdr (cdr (9 8 7 6))))) (if (> 2 3) 40 50) (and (> 2 3) (> 3 2)) (cdr (cons (+ 2 3) (4 5 6))) Here is a sample run:Mac-mini:hw1 raj$ python3 LISP.py LISP: 34; 34.0 LISP: (+ 20 30); 50.0 LISP: (/ 9 (- 2 2)); DIVIDE BY ZERO! LISP: (* (+ 1 2) (/ 8 4)); 6.0 LISP: (* (car (2 4 (+ 2 4) 8)) (/ 27 9)); 6.0 LISP: (+ (car (2 3 4)) (car (cdr (cdr (9 8 7 6))))); 9.0 LISP: (+ 2 3 4); Syntax error in input! LISP: (* (car 4) 22); Syntax error in input! LISP: (+ 3 (car (cdr (cdr (cdr (1 2)))))); Cannot CDR on an empty list! LISP: (if (> 2 3) 40 50); 50.0 LISP: (or (> 2 3) (> 3 2)); True LISP: (and (> 2 3) (> 3 2)); False LISP: (cdr (1 2 3 4)); (2.0 3.0 4.0) LISP: (cdr (cons (+ 2 3) (4 5 6))); (4.0 5.0 6.0) LISP: (cdr (cdr (1 2 3 4))); (3.0 4.0) LISP: (cdr (cdr (3 4))); () LISP: (cdr (cdr (3))); Cannot CDR on an empty list! LISP: (car (cdr (1 2 3 4))); 2.0 LISP: exit; Mac-mini:hw1 raj$
WHAT TO SUBMIT?
LISP.py, LISPLexer.py, LISPParser.py. If you chose to implement the expression tree using LISPNode, then you must submit LISPNode.py as well.
Trending nowThis is a popular solution!
Step by stepSolved in 2 steps
- Please IN C++ Write the simplest program that will demonstrate iteration vs recursion using the following guidelines - Write two primary helper functions - one iterative (IsArrayPrimeIter) and one recursive (IsArrayPrimeRecur) - each of which Takes an array of integers and its size as input params and returns a bool such that 'true' ==> all elements of the array are prime, so the array is prime, 'false' ==> at least one element in array is not prime, so array is not prime. Print out a message "Entering <function_name>" as the first executed statement of each function. Perform the code to test whether every element of the array is a Prime number. Print out a message "Leaving <function_name>" as the last executed statement before returning from the function. Remember - there will be nested loops for the iterative function and there can be no loops at all in the recursive function. For the recursive function - define one other helper function (IsPrimeRecur) which...arrow_forwardDefine a LISP function MONTH->INTEGER which takes as argument a symbol that should be thename of a month, and which returns the number of the month. For example:(MONTH->INTEGER 'MARCH) => 3 (MONTH->INTEGER 'JUNE) => 6If the argument is not a symbol that is the name of a month, the function should return the symbolERROR. For example:(MONTH->INTEGER 'C) => ERROR (MONTH->INTEGER 7) => ERROR(MONTH->INTEGER 'QUOTE) =>ERROR (MONTH->INTEGER '(MAY)) =>ERRORarrow_forwardcode in ada with Ada.Numerics.Generic_Elementary_Functions; package body Number_Theory is -- Instantiate the library for floating point math using Floating_Type. package Floating_Functions is new Ada.Numerics.Generic_Elementary_Functions(Floating_Type); use Floating_Functions; function Factorial(N : in Factorial_Argument_Type) return Positive is begin -- TODO: Finish me! -- -- 0! is 1 -- N! is N * (N-1) * (N-2) * ... * 1 return 1; end Factorial; function Is_Prime(N : in Prime_Argument_Type) return Boolean is Upper_Bound : Prime_Argument_Type; Current_Divisor : Prime_Argument_Type; begin -- Handle 2 as a special case. if N = 2 then return True; end if; Upper_Bound := N - 1; Current_Divisor := 2; while Current_Divisor < Upper_Bound loop if N rem Current_Divisor = 0 then return False; end if; Upper_Bound := N / Current_Divisor; end loop;...arrow_forward
- the code is in ada with Ada.Numerics.Generic_Elementary_Functions; package body Number_Theory is -- Instantiate the library for floating point math using Floating_Type. package Floating_Functions is new Ada.Numerics.Generic_Elementary_Functions(Floating_Type); use Floating_Functions; function Factorial(N : in Factorial_Argument_Type) return Positive is begin -- TODO: Finish me! -- -- 0! is 1 -- N! is N * (N-1) * (N-2) * ... * 1 return 1; end Factorial; function Is_Prime(N : in Prime_Argument_Type) return Boolean is Upper_Bound : Prime_Argument_Type; Current_Divisor : Prime_Argument_Type; begin -- Handle 2 as a special case. if N = 2 then return True; end if; Upper_Bound := N - 1; Current_Divisor := 2; while Current_Divisor < Upper_Bound loop if N rem Current_Divisor = 0 then return False; end if; Upper_Bound := N / Current_Divisor; end loop;...arrow_forwarddef optimize_tail_calls(original_scheme_eval): """Return a properly tail recursive version of an eval function.""" def optimized_eval(expr, env, tail=False): """Evaluate Scheme expression EXPR in Frame ENV. If TAIL, return an Unevaluated containing an expression for further evaluation. """ if tail and not scheme_symbolp(expr) and not self_evaluating(expr): return Unevaluated(expr, env) result = Unevaluated(expr, env) # fill in code code here while isinstance(result, Unevaluated): #code here return optimized_eval scheme_eval = optimize_tail_calls(scheme_eval)arrow_forwardMust be new solution and run on GNU Common Lisp! Using Lisp, write a program that solves the Missionaries and Cannibals problem that uses a DFS( depth first search). It should use (mac start end). Start is the current state (which can be (3 3 l) and End is the goal state (which can be (0 0 r). This should output the sequences of moves needed to reach the end state from the start state. This should print nil if there is no solution. For example, the call should be something like this! Call: (mac '(3 3 l) '(0 0 r)) Output: ((3 3 l) (2 2 r) (3 2 l) (3 0 r) (3 1 l) (1 1 r) (2 2 l) (0 2 r) (0 3 l) (0 1 r) (1 1 l) (0 0 r))arrow_forward
- Using the following variable bindings: (setq x ‘(a b c d)) (setq y ‘(1 2 3 4)) Using only variables x and y, car, cdr, and cons, make Lisp expressions to generate: (d c b a)arrow_forwardHaskellarrow_forwardUsing the following variable bindings: (setq x ‘(a b c d)) (setq y ‘(1 2 3 4)) Using only variables x and y, car, cdr, and cons, write Lisp expressions to generate: ((a c) (1 2)) . a)arrow_forward
- Text book imageDatabase System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationText book imageStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONText book imageDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- Text book imageC How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONText book imageDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningText book imageProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education