Skip to content

Create README.md#1

Open
shubhamnarlawar77 wants to merge 1 commit intoSameeranjoshi:masterfrom
shubhamnarlawar77:shubhamnarlawar77-patch-1
Open

Create README.md#1
shubhamnarlawar77 wants to merge 1 commit intoSameeranjoshi:masterfrom
shubhamnarlawar77:shubhamnarlawar77-patch-1

Conversation

@shubhamnarlawar77
Copy link
Copy Markdown

@shubhamnarlawar77 shubhamnarlawar77 commented Jun 27, 2018

  • Mini Interpreter
    Write a small C program that is representative of a miniature
    interpreter. The program
    should take as input from file - a list, evaluate it and output the
    result.

For eg:
(= a 1)
(= b 2)
(+ (* a b) 3)

The result of evaluation should be 5.
Note that the lists can be arbitrarily nested:
(+ (* a (- c  d)) (* k x))
The program should gracefully handle nested lists.

Each element of this "list" is either an integer, a variable name, an
operator like +, -
or a list itself, that is, it's definition is recursive.

More formally:
list: element
    |  element list

element:  number | variable | operator | '( list ')'

The above specification is called Context Free Grammar, which is a
mathematical notation of specifying language grammars [2].

For operators just consider the standard arithmetic operators: +, -, *, /,
Bonus points for multi-name variables like "foo", but single-letter
variables are also fine to
begin with.

Write the program in two parts:
a) The first part should correctly parse the input and build the
data-structure to represent the evaluation. This data structure is
called abstract syntax tree or AST for short [2].
b) In the second part, traverse the AST and evaluate the expression.
Note that a single AST should represent single expression.

For example: (= a (+ 2 3))
can be represented as following tree:

                 =
            a            +
                      2       3

The first phase should build this tree, and the second phase should
then do the tree-walk and evaluate the expression.
The notation of "lists" is more commonly known as s-expression [3] and
is the basis of functional languages like LISP.  Make the program
robust. It should be able to deal
with incorrect syntax like
(+ 1) and gracefully exit with an error message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant