736. Parse Lisp Expression
You are given a string expression representing a Lisp-like expression to return the integer value of.
The syntax for these expressions is given as follows.
- An expression is either an integer, let expression, add expression, mult expression, or an assigned variable. Expressions always evaluate to a single integer.
- (An integer could be positive or negative.)
- A let expression takes the form “ ( l e t v 1 e 1 v 2 e 2 . . . v n e n e x p r ) (let \ v_1 e_1 v_2 e_2 ... v_n e_n expr) (let v1e1v2e2...vnenexpr)”, where let is always the string “let”, then there are one or more pairs of alternating variables and expressions, meaning that the first variable v 1 v_1 v1 is assigned the value of the expression e 1 e_1 e1, the second variable v 2 v_2 v2 is assigned the value of the expression e 2 e_2 e2, and so on sequentially; and then the value of this let expression is the value of the expression expr.
- An add expression takes the form “ ( a d d e 1 e 2 ) (add \ e_1 e_2) (add e1e2)” where add is always the string “add”, there are always two expressions e 1 e_1 e1, e 2 e_2 e2 and the result is the addition of the evaluation of e 1 e_1 e1 and the evaluation of e 2 e_2 e2.
- A mult expression takes the form “ ( m u l t e 1 e 2 ) (mult \ e_1 e_2) (mult e1e2)” where mult is always the string “mult”, there are always two expressions e 1 e_1 e1, e 2 e_2 e2 and the result is the multiplication of the evaluation of e 1 e_1 e1 and the evaluation of e 2 e_2 e2.
- For this question, we will use a smaller subset of variable names. A variable starts with a lowercase letter, then zero or more lowercase letters or digits. Additionally, for your convenience, the names “add”, “let”, and “mult” are protected and will never be used as variable names.
- Finally, there is the concept of scope. When an expression of a variable name is evaluated, within the context of that evaluation, the innermost scope (in terms of parentheses) is checked first for the value of that variable, and then outer scopes are checked sequentially. It is guaranteed that every expression is legal. Please see the examples for more details on the scope.
Example 1:
Input: expression = “(let x 2 (mult x (let x 3 y 4 (add x y))))”
Output: 14
Explanation: In the expression (add x y), when checking for the value of the variable x,
we check from the innermost scope to the outermost in the context of the variable we are trying to evaluate.
Since x = 3 is found first, the value of x is 3.
Example 2:
Input: expression = “(let x 3 x 2 x)”
Output: 2
Explanation: Assignment in let statements is processed sequentially.
Example 3:
Input: expression = “(let x 1 y 2 x (add x y) (add x y))”
Output: 5
Explanation: The first (add x y) evaluates as 3, and is assigned to x.
The second (add x y) evaluates as 3+2 = 5.
Constraints:
- 1 <= expression.length <= 2000
- There are no leading or trailing spaces in expression.
- All tokens are separated by a single space in expression.
- The answer and all intermediate calculations of that answer are guaranteed to fit in a 32-bit integer.
- The expression is guaranteed to be legal and evaluate to an integer.
From: LeetCode
Link: 736. Parse Lisp Expression
Solution:
Ideas:
- Recursive Parser: Uses recursive descent parsing to handle nested Lisp expressions with proper tokenization
- Scope Stack Management: Maintains a stack of scopes where each scope contains variable-value pairs, with new scopes pushed for each let expression
- Variable Lookup Order: Searches for variables from innermost to outermost scope, and within each scope searches backwards to get the most recent assignment
- Expression Types: Handles four types - numbers (return value), variables (lookup value), binary operations (add/mult), and let expressions
- Sequential Assignment: The critical fix was ensuring that within the same let expression, later variable assignments override earlier ones (e.g., in (let x 3 x 2 x), the second x 2 overrides x 3)
- Memory Management: Properly allocates and frees tokens during parsing to avoid memory leaks
Code:
// Structure to represent variable-value pairs in scope
typedef struct {
char var[100];
int val;
} VarVal;
// Structure to represent a scope (list of variable-value pairs)
typedef struct {
VarVal vars[100];
int count;
} Scope;
// Global scope stack
Scope scopes[100];
int scopeCount = 0;
// Function to add a variable to current scope
void addVar(char* var, int val) {
strcpy(scopes[scopeCount-1].vars[scopes[scopeCount-1].count].var, var);
scopes[scopeCount-1].vars[scopes[scopeCount-1].count].val = val;
scopes[scopeCount-1].count++;
}
// Function to lookup a variable value (from innermost to outermost scope)
// Within each scope, we need to find the LAST occurrence of the variable
int lookupVar(char* var) {
for (int i = scopeCount - 1; i >= 0; i--) {
// Search backwards within the scope to get the most recent assignment
for (int j = scopes[i].count - 1; j >= 0; j--) {
if (strcmp(scopes[i].vars[j].var, var) == 0) {
return scopes[i].vars[j].val;
}
}
}
return 0; // Should never reach here for valid expressions
}
// Function to push a new scope
void pushScope() {
scopes[scopeCount].count = 0;
scopeCount++;
}
// Function to pop current scope
void popScope() {
scopeCount--;
}
// Function to check if a string is a number
int isNumber(char* str) {
int i = 0;
if (str[0] == '-') i = 1;
if (str[i] == '\0') return 0;
while (str[i] != '\0') {
if (!isdigit(str[i])) return 0;
i++;
}
return 1;
}
// Function to parse and evaluate expression
int parseExpression(char* expr, int* pos);
// Function to get next token
char* getNextToken(char* expr, int* pos) {
// Skip whitespace
while (expr[*pos] == ' ') (*pos)++;
if (expr[*pos] == '\0') return NULL;
int start = *pos;
if (expr[*pos] == '(' || expr[*pos] == ')') {
(*pos)++;
char* token = (char*)malloc(2);
token[0] = expr[start];
token[1] = '\0';
return token;
}
// Read until space or parenthesis
while (expr[*pos] != '\0' && expr[*pos] != ' ' && expr[*pos] != '(' && expr[*pos] != ')') {
(*pos)++;
}
int len = *pos - start;
char* token = (char*)malloc(len + 1);
strncpy(token, expr + start, len);
token[len] = '\0';
return token;
}
int parseExpression(char* expr, int* pos) {
char* token = getNextToken(expr, pos);
if (token == NULL) return 0;
// If it's a number, return its value
if (isNumber(token)) {
int val = atoi(token);
free(token);
return val;
}
// If it's not '(', it must be a variable
if (strcmp(token, "(") != 0) {
int val = lookupVar(token);
free(token);
return val;
}
free(token);
// It's a '(', so we expect an operation
char* op = getNextToken(expr, pos);
if (strcmp(op, "add") == 0) {
int val1 = parseExpression(expr, pos);
int val2 = parseExpression(expr, pos);
// Skip closing ')'
char* closing = getNextToken(expr, pos);
free(closing);
free(op);
return val1 + val2;
}
else if (strcmp(op, "mult") == 0) {
int val1 = parseExpression(expr, pos);
int val2 = parseExpression(expr, pos);
// Skip closing ')'
char* closing = getNextToken(expr, pos);
free(closing);
free(op);
return val1 * val2;
}
else if (strcmp(op, "let") == 0) {
pushScope();
// Parse variable-value pairs until we hit the final expression
while (1) {
// Check if next token starts an expression (either '(' or a number or final variable)
int savedPos = *pos;
char* nextToken = getNextToken(expr, pos);
*pos = savedPos; // Reset position
// If it's '(' or a number, it's the final expression
if (strcmp(nextToken, "(") == 0 || isNumber(nextToken)) {
free(nextToken);
break;
}
// Check if it's the final variable by looking ahead
int tempPos = *pos;
char* var = getNextToken(expr, &tempPos);
char* possibleExpr = getNextToken(expr, &tempPos);
// If the next token after variable is ')', then this variable is the final expression
if (strcmp(possibleExpr, ")") == 0) {
free(var);
free(possibleExpr);
free(nextToken);
break;
}
// Otherwise, it's a variable-value pair
*pos = savedPos; // Reset position
char* variable = getNextToken(expr, pos);
int value = parseExpression(expr, pos);
addVar(variable, value);
free(variable);
free(possibleExpr);
free(nextToken);
}
// Parse the final expression
int result = parseExpression(expr, pos);
// Skip closing ')'
char* closing = getNextToken(expr, pos);
free(closing);
popScope();
free(op);
return result;
}
free(op);
return 0;
}
int evaluate(char* expression) {
scopeCount = 0;
int pos = 0;
return parseExpression(expression, &pos);
}