Question
Using ANTLR4 system, write an interpreter for the language of LISP expressions. A LISP expression is defined as follows: A number is a LISP expression.
Using ANTLR4 system, write an interpreter for the language of "LISP expressions".
A LISP expression is defined as follows:
A number is a LISP expression. (We can assume positive integers)
if E1 and E2 are LISP expressions then so are (+ E1 E2), (- E1 E2), (* E1 E2), and (/ E1 E2).
if L is a ListExpression (defined below) then (car L) is a LISP expression.
A ListExpression is defined as follows:
if E1, E2, ..., En are LISP expressions where n>1 then (E1 E2 ... En) is a ListExpression.
if L is a ListExpression then (cdr L) is a ListExpression.
Here are examples of valid LISP expressions:
34 (+ 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)))))
Here is a sample run:
MacBook-Pro:lisp raj$ java LISP LISP> 34; The value is 34.0 LISP> (+ 20 30); The value is 50.0 LISP> (* (+ 1 2) (/ 8 4)); The value is 6.0 LISP> (* (car (2 4 (+ 2 4) 8)) (/ 27 9)); The value is 6.0 LISP> (+ (car (2 3 4)) (car (cdr (cdr (9 8 7 6))))); The value is 9.0 LISP> (+ 2 3 4); SYNTAX ERROR LISP> (* (car 4) 22); line 1:8 no viable alternative at input '4' SYNTAX ERROR LISP> exit;
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