# Solution For Parse Lisp Expression

The “Parse Lisp Expression” problem on LeetCode (https://leetcode.com/problems/parse-lisp-expression/) requires you to implement a Lisp interpreter that can evaluate expressions in a simplified version of the Lisp language.

The problem statement provides various examples of Lisp expressions that need to be evaluated, such as:

`(add 1 2) (mult 3 (add 2 3)) (let x 2 (mult x 5))`

Your task is to implement a function `evaluate` that takes a string parameter `expression` representing a Lisp expression and returns its evaluation. You should assume that the input expression is well-formed.

To solve this problem, you can use recursion to evaluate the different types of expressions in the Lisp language. There are three types of Lisp expressions:

1. Integer: an integer constant, such as “123”
2. Symbol: a variable or operator, such as “add”, “mult”, or “x”
3. List: a parenthesized expression that contains one or more sub-expressions

Here’s a step-by-step approach to solving the problem:

1. Define a function `parse` that takes a string representing a Lisp expression and returns a list of tokens. To do this, you can use regular expressions to split the input string into separate tokens. Here’s an example implementation:

“`python
import re

def parse(expression: str) -> List[str]:
tokens = [] for token in re.findall(r'(|)|[^\s()]+’, expression):
tokens.append(token)
“`

This function uses the regular expression `r'\(|\)|[^\s()]+'` to split the input string into separate tokens, treating parentheses as separate tokens. For example, the expression “(add 1 2)” would be parsed into the list [“(“, “add”, “1”, “2”, “)”].

1. Define a function `evaluate` that takes a list of tokens representing a Lisp expression and returns its evaluation. To do this, you can use recursion to evaluate the different types of expressions. Here’s an example implementation:

```python def evaluate(tokens: List[str], env: Dict[str, int] = None) -> int: if not tokens: return 0 if env is None: env = {} token = tokens.pop(0) if token == '(': if tokens[0] == 'add': tokens.pop(0) arg1 = evaluate(tokens, env) arg2 = evaluate(tokens, env) return arg1 + arg2 elif tokens[0] == 'mult': tokens.pop(0) arg1 = evaluate(tokens, env) arg2 = evaluate(tokens, env) return arg1 * arg2 elif tokens[0] == 'let': tokens.pop(0) new_env = env.copy() while tokens[0] != ')': var = tokens.pop(0) if tokens[0] == '(': new_env[var] = evaluate(tokens, new_env) else: new_env[var] = int(tokens.pop(0)) tokens.pop(0) # pop final closing bracket return evaluate(tokens, new_env) else: if token in env: return env[token] return int(token)```

This function takes a list of tokens that represent a Lisp expression, as well as an optional environment dictionary that contains the current variable bindings. It returns an integer that represents the evaluation of the input expression.

The function begins by popping the first token from the token list and determining its type. If the token is an opening parenthesis, then it recursively evaluates the sub-expressions within the parentheses. If the sub-expression is a built-in function (i.e., “add” or “mult”), then it evaluates the arguments and applies the function. If the sub-expression is a “let” expression, then it creates a new environment dictionary with the variable bindings specified in the expression, and recursively evaluates the remaining expression with the new environment.

If the token is not an opening parenthesis, then it checks if it is a variable in the current environment dictionary. If it is, then it returns the value associated with the variable. Otherwise, it assumes that the token is an integer constant and returns its value.

1. Define the main function `parse_lisp_expression` that takes a string parameter `expression` and returns its evaluation. Here’s an example implementation:

“`python
from typing import List, Dict

def parse_lisp_expression(expression: str) -> int:
tokens = parse(expression)
return evaluate(tokens)

“`

This function simply parses the input string into a list of tokens and then evaluates it using the `evaluate` function.

1. Test your implementation using the examples provided in the problem statement. For example:

“`python
assert parse_lisp_expression(“(add 1 2)”) == 3
assert parse_lisp_expression(“(mult 3 (add 2 3))”) == 15
assert parse_lisp_expression(“(let x 2 (mult x 5))”) == 10
assert parse_lisp_expression(“(let x 2 (mult x (let x 3 y 4 (add x y))))”) == 14
assert parse_lisp_expression(“(let x 1 y 2 x (add x y) (add x y))”) == 5

“`

Overall, the `Parse Lisp Expression` problem on LeetCode requires you to implement a Lisp interpreter that can evaluate expressions in a simplified version of the Lisp language. You can solve this problem by using recursion to evaluate the different types of Lisp expressions: integers, symbols, and lists. The `parse` function tokenizes the input string, and the `evaluate` function recursively evaluates the tokens using the current environment dictionary. With these functions, you can implement the `parse_lisp_expression` function that parses and evaluates the input expression, and then test your implementation using the examples provided.

## Step by Step Implementation For Parse Lisp Expression

```/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* public interface NestedInteger {
*
*     // Constructor initializes an empty nested list.
*     public NestedInteger();
*
*     // Constructor initializes a single integer.
*     public NestedInteger(int value);
*
*     // @return true if this NestedInteger holds a single integer, rather than a nested list.
*     public boolean isInteger();
*
*     // @return the single integer that this NestedInteger holds, if it holds a single integer
*     // Return null if this NestedInteger holds a nested list
*     public Integer getInteger();
*
*     // Set this NestedInteger to hold a single integer.
*     public void setInteger(int value);
*
*     // Set this NestedInteger to hold a nested list and adds a nested integer to it.
*
*     // @return the nested list that this NestedInteger holds, if it holds a nested list
*     // Return null if this NestedInteger holds a single integer
*     public List getList();
* }
*/
class Solution {
public NestedInteger deserialize(String s) {

}
}```
```# Given an expression s = "(add 1 2)" # Output: 3 # Explanation: The expression is evaluated as "(add (add 1 2))". # We evaluate the sub-expression "(add 1 2)", get 3, then evaluate the main expression.

def parse_lisp_expression(s):
# TODO: Write your code here
pass```
```// We can use a stack to keep track of the current expression we are evaluating.
// Whenever we encounter an open parentheses, we push a new node onto the stack.
// The node will be used to keep track of the current expression and the result of the expression.
// Whenever we encounter a closed parentheses, we pop the node from the stack and update the result of the node.
// If we encounter a symbol, we update the node accordingly.
// Finally, we return the result of the root node.

/**
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* function NestedInteger() {
*
*     Return true if this NestedInteger holds a single integer, rather than a nested list.
*     @return {boolean}
*     this.isInteger = function() {
*         ...
*     };
*
*     Return the single integer that this NestedInteger holds, if it holds a single integer
*     Return null if this NestedInteger holds a nested list
*     @return {integer}
*     this.getInteger = function() {
*         ...
*     };
*
*     Set this NestedInteger to hold a single integer equal to value.
*     @return {void}
*     this.setInteger = function(value) {
*         ...
*     };
*
*     Set this NestedInteger to hold a nested list and adds a nested integer elem to it.
*     @return {void}
*         ...
*     };
*
*     Return the nested list that this NestedInteger holds, if it holds a nested list
*     Return null if this NestedInteger holds a single integer
*     @return {NestedInteger[]}
*     this.getList = function() {
*         ...
*     };
* };
*/
/**
* @param {string} expression
* @return {NestedInteger}
*/
var parseLispExpression = function(expression) {
// use a stack to keep track of the current expression
let stack = [];
// use a variable to keep track of the current index
let i = 0;

// helper function to skip over whitespace
const skipWhitespace = () => {
while (i < expression.length && expression[i] === ' ') i++;
}

// helper function to get the current character
const getChar = () => {
return expression[i];
}

// helper function to get the next character
const getNextChar = () => {
return expression[i + 1];
}

// helper function to get the next token
const getNextToken = () => {
// skip over any whitespace
skipWhitespace();
// we will return the token as a string
let token = "";
// if the current character is an open parentheses, we return it as a token
if (getChar() === '(') {
token += getChar();
i++;
// if the current character is a closed parentheses, we return it as a token
} else if (getChar() === ')') {
token += getChar();
i++;
// otherwise, we need to get the entire token
} else {
// we keep looping until we encounter a space or a parentheses
while (i < expression.length && getChar() !== ' ' && getChar() !== '(' && getChar() !== ')') {
// we add the current character to the token
token += getChar();
// and move on to the next character
i++;
}
// we return the token
}
}

// helper function to evaluate an expression
const evalExpr = (expr) => {
// we will use a stack to keep track of the nested expressions
let stack = [];
// we will use a variable to keep track of the current index
let i = 0;
// we will use a variable to keep track of the result
let result;
// we loop through the expression
while (i < expr.length) {
// we get the next token
let token = getNextToken(expr, i);
// if the token is an open parentheses, we push a new node onto the stack
if (token === '(') {
stack.push(new NestedInteger());
// if the token is a closed parentheses, we pop the node from the stack and update the result
} else if (token === ')') {
// we get the node from the top of the stack
let node = stack.pop();
// if the stack is empty, this is the root node so we update the result
if (stack.length === 0) {
result = node;
// otherwise, we add the node to the parent node
} else {
}
// otherwise, the token is a symbol so we update the node accordingly
} else {
// we get the node from the top of the stack
let node = stack[stack.length - 1];
// if the token is '-', we need to negate the next token
if (token === '-') {
// we get the next token
let nextToken = getNextToken(expr, i);
// we convert the token to a number
let num = Number(nextToken);
// we negate the number
num = -num;
// we update the node with the number
node.setInteger(num);
// otherwise, we just update the node with the token
} else {
node.setInteger(Number(token));
}
}
}
// we return the result
return result;
}

// we loop through the expression
while (i < expression.length) {
// we get the next token
let token = getNextToken();
// if the token is an open parentheses, we push a new node onto the stack
if (token === '(') {
stack.push(new NestedInteger());
// if the token is a closed parentheses, we pop the node from the stack and update the result
} else if (token === ')') {
// we get the node from the top of the stack
let node = stack.pop();
// if the stack is empty, this is the root node so we update the result
if (stack.length === 0) {
result = node;
// otherwise, we add the node to the parent node
} else {
}
// otherwise, the token is a symbol so we update the node accordingly
} else {
// we get the node from the top of the stack
let node = stack[stack.length - 1];
// if the token is '-', we need to negate the next token
if (token === '-') {
// we get the next token
let nextToken = getNextToken();
// we convert the token to a number
let num = Number(nextToken);
// we negate the number
num = -num;
// we update the node with the number
node.setInteger(num);
// otherwise, we just update the node with the token
} else {
node.setInteger(Number(token));
}
}
}
// we return the result
return result;
};```
```This is a recursive solution to the leetcode problem parse-lisp-expression. The idea is to keep track of the current scope (a stack of maps) and to evaluate expressions in this scope.

We start by scanning the input string. When we encounter an open parentheses, we create a new scope. When we encounter a closed parentheses, we pop the current scope off the stack. When we encounter an identifier, we look up its value in the current scope. When we encounter an expression, we evaluate it in the current scope.

The eval function takes an expression and a scope. It first tokenizes the expression (splits it on whitespace). It then looks at the first token to determine what kind of expression it is. If it is a symbol, it looks up the symbol's value in the scope. If it is a list, it evaluates each element of the list in the scope. Otherwise, it evaluates the expression as a numeric value.

#include  #include  #include  #include  #include  using namespace std; // Tokenizes a string on whitespace. vector tokenize(string s) { vector tokens; string token; for (char c : s) { if (isspace(c)) { if (!token.empty()) { tokens.push_back(token); token.clear(); } } else { token.push_back(c); } } if (!token.empty()) { tokens.push_back(token); } return tokens; } // Evaluates an expression in a given scope. int eval(string expr, stack>& scope) { vector tokens = tokenize(expr); // Look at the first token to determine the type of expression. if (tokens[0] == "(") { // This is a list expression. We evaluate each element of the list // in the current scope. int result = 0; for (int i = 1; i < tokens.size() - 1; i++) { result = eval(tokens[i], scope); } return result; } else if (tokens[0] == "add") { // This is an addition expression. We evaluate the left and right // operands in the current scope and add them together. int left = eval(tokens[1], scope); int right = eval(tokens[2], scope); return left + right; } else if (tokens[0] == "mult") { // This is a multiplication expression. We evaluate the left and // right operands in the current scope and multiply them together. int left = eval(tokens[1], scope); int right = eval(tokens[2], scope); return left * right; } else if (tokens[0] == "let") { // This is a let expression. We create a new scope, bind the left // operand to the value of the right operand in this scope, and // evaluate the expression in this new scope. stack> new_scope = scope; int value = eval(tokens[2], scope); new_scope.top()[tokens[1]] = value; return eval(tokens[3], new_scope); } else { // This is a symbol. We look up its value in the current scope. return scope.top()[tokens[0]]; } } int main() { // Parse the input string. string input = "(let x 2 (mult x 5))"; // Keep track of the current scope (a stack of maps). stack> scope; // Push an empty map onto the stack to represent the global scope. scope.push(map()); // Evaluate the expression in the global scope. int result = eval(input, scope); // Print the result. cout << result << endl; return 0; }```
`// We can use a stack to keep track of the current scope as we parse // the input. When we encounter an open parenthesis, we push a new // scope onto the stack. When we encounter a close parenthesis, we // pop the top scope off the stack and update the current scope with // the result of the expression. using System; using System.Collections.Generic; using System.Linq; public class Solution { private enum TokenType { INT, SYMBOL, OPEN_PAREN, CLOSE_PAREN } private class Token { public TokenType Type; public int IntVal; public string SymbolVal; public Token(TokenType type, int intVal = 0, string symbolVal = "") { Type = type; IntVal = intVal; SymbolVal = symbolVal; } } // We'll use a simple recursive descent parser to parse the input. // Each method in the parser corresponds to a type of token. // // expr ::= add | mul | let // add ::= expr '+' expr // mul ::= expr '*' expr // let ::= '(' 'let' '(' id ')' expr ')' // // We'll use a global index into the input to keep track of // our position in the input. private static int _index; // Parse an expression. private static int ParseExpr() { if (_index >= _tokens.Count) { throw new Exception("Unexpected end of input"); } Token token = _tokens[_index]; switch (token.Type) { case TokenType.INT: return ParseInt(token); case TokenType.SYMBOL: return ParseSymbol(token); case TokenType.OPEN_PAREN: return ParseLet(token); default: throw new Exception(\$"Unexpected token: {token.Type}"); } } // Parse an integer. private static int ParseInt(Token token) { _index++; return token.IntVal; } // Parse a symbol. private static int ParseSymbol(Token token) { _index++; if (!_scope.ContainsKey(token.SymbolVal)) { throw new Exception(\$"Undefined symbol: {token.SymbolVal}"); } return _scope[token.SymbolVal]; } // Parse a let expression. private static int ParseLet(Token token) { _index++; // Skip the '(' token = _tokens[_index]; if (token.Type != TokenType.OPEN_PAREN) { throw new Exception("Expected '('"); } _index++; // Skip the 'let' token = _tokens[_index]; if (token.Type != TokenType.SYMBOL || token.SymbolVal != "let") { throw new Exception("Expected 'let'"); } _index++; // Skip the '(' token = _tokens[_index]; if (token.Type != TokenType.OPEN_PAREN) { throw new Exception("Expected '('"); } _index++; // Parse the symbol token = _tokens[_index]; if (token.Type != TokenType.SYMBOL) { throw new Exception("Expected symbol"); } string symbol = token.SymbolVal; _index++; // Skip the ')' token = _tokens[_index]; if (token.Type != TokenType.CLOSE_PAREN) { throw new Exception("Expected ')'"); } _index++; // Parse the value int val = ParseExpr(); _scope[symbol] = val; // Skip the ')' token = _tokens[_index]; if (token.Type != TokenType.CLOSE_PAREN) { throw new Exception("Expected ')'"); } _index++; return val; } // The current scope. private static Dictionary _scope = new Dictionary(); // The list of tokens. private static List _tokens; // Tokenize the input. private static void Tokenize(string input) { _tokens = new List(); for (int i = 0; i < input.Length; i++) { switch (input[i]) { case ' ': break; case '(': _tokens.Add(new Token(TokenType.OPEN_PAREN)); break; case ')': _tokens.Add(new Token(TokenType.CLOSE_PAREN)); break; case '+': _tokens.Add(new Token(TokenType.SYMBOL, symbolVal: "+")); break; case '*': _tokens.Add(new Token(TokenType.SYMBOL, symbolVal: "*")); break; default: // Assume it's an integer int val = 0; while (i < input.Length && char.IsDigit(input[i])) { val = val * 10 + (input[i] - '0'); i++; } i--; _tokens.Add(new Token(TokenType.INT, intVal: val)); break; } } } // Parse and evaluate the input. public static int Eval(string input) { Tokenize(input); _index = 0; return ParseExpr(); } }`

Scroll to Top
[gravityforms id="5" description="false" titla="false" ajax="true"]