Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3480b08

Browse files
author
Victor
authored
initial version.
1 parent 40382dd commit 3480b08

File tree

1 file changed

+273
-0
lines changed

1 file changed

+273
-0
lines changed

‎736. Parse Lisp Expression.c

Lines changed: 273 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,273 @@
1+
/*
2+
736. Parse Lisp Expression
3+
4+
You are given a string expression representing a Lisp-like expression to return the integer value of.
5+
6+
The syntax for these expressions is given as follows.
7+
8+
An expression is either an integer, a let-expression, an add-expression, a mult-expression, or an assigned variable. Expressions always evaluate to a single integer.
9+
10+
(An integer could be positive or negative.)
11+
12+
A let-expression takes the form (let v1 e1 v2 e2 ... vn en expr), where let is always the string "let", then there are 1 or more pairs of alternating variables and expressions, meaning that the first variable v1 is assigned the value of the expression e1, the second variable v2 is assigned the value of the expression e2, and so on sequentially; and then the value of this let-expression is the value of the expression expr.
13+
14+
An add-expression takes the form (add e1 e2) where add is always the string "add", there are always two expressions e1, e2, and this expression evaluates to the addition of the evaluation of e1 and the evaluation of e2.
15+
16+
A mult-expression takes the form (mult e1 e2) where mult is always the string "mult", there are always two expressions e1, e2, and this expression evaluates to the multiplication of the evaluation of e1 and the evaluation of e2.
17+
18+
For the purposes of 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", or "mult" are protected and will never be used as variable names.
19+
20+
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 scope.
21+
22+
23+
Evaluation Examples:
24+
Input: (add 1 2)
25+
Output: 3
26+
27+
Input: (mult 3 (add 2 3))
28+
Output: 15
29+
30+
Input: (let x 2 (mult x 5))
31+
Output: 10
32+
33+
Input: (let x 2 (mult x (let x 3 y 4 (add x y))))
34+
Output: 14
35+
Explanation: In the expression (add x y), when checking for the value of the variable x,
36+
we check from the innermost scope to the outermost in the context of the variable we are trying to evaluate.
37+
Since x = 3 is found first, the value of x is 3.
38+
39+
Input: (let x 3 x 2 x)
40+
Output: 2
41+
Explanation: Assignment in let statements is processed sequentially.
42+
43+
Input: (let x 1 y 2 x (add x y) (add x y))
44+
Output: 5
45+
Explanation: The first (add x y) evaluates as 3, and is assigned to x.
46+
The second (add x y) evaluates as 3+2 = 5.
47+
48+
Input: (let x 2 (add (let x 3 (let x 4 x)) x))
49+
Output: 6
50+
Explanation: Even though (let x 4 x) has a deeper scope, it is outside the context
51+
of the final x in the add-expression. That final x will equal 2.
52+
53+
Input: (let a1 3 b2 (add a1 1) b2)
54+
Output 4
55+
Explanation: Variable names can contain digits after the first character.
56+
57+
58+
59+
Note:
60+
The given string expression is well formatted: There are no leading or trailing spaces, there is only a single space separating different components of the string, and no space between adjacent parentheses. The expression is guaranteed to be legal and evaluate to an integer.
61+
The length of expression is at most 2000. (It is also non-empty, as that would not be a legal expression.)
62+
The answer and all intermediate calculations of that answer are guaranteed to fit in a 32-bit integer.
63+
*/
64+
65+
#define TYPE_INT 0
66+
#define TYPE_ID 1
67+
68+
typedef struct {
69+
char *p;
70+
int len;
71+
} idn_t;
72+
73+
typedef struct {
74+
int type; // 0: identifier, 1: value
75+
union {
76+
idn_t id;
77+
int num;
78+
} u;
79+
} expr_t;
80+
81+
typedef struct sym_s {
82+
idn_t id;
83+
int val;
84+
int scope;
85+
struct sym_s *list;
86+
} sym_t;
87+
88+
typedef struct {
89+
char *input;
90+
sym_t *sym;
91+
} p_t;
92+
93+
#define IS_LET(E) ((E)[0] == 'l' && \
94+
(E)[1] == 'e' && \
95+
(E)[2] == 't' && \
96+
(E)[3] == ' ')
97+
#define IS_ADD(E) ((E)[0] == 'a' && \
98+
(E)[1] == 'd' && \
99+
(E)[2] == 'd' && \
100+
(E)[3] == ' ')
101+
#define IS_MULT(E) ((E)[0] == 'm' && \
102+
(E)[1] == 'u' && \
103+
(E)[2] == 'l' && \
104+
(E)[3] == 't' && \
105+
(E)[4] == ' ')
106+
#define IS_NUM(E) ((E)[0] >= '0' && \
107+
(E)[0] <= '9')
108+
109+
char *next_input(char *input) {
110+
int n = 0;
111+
char c;
112+
while (c = *(input ++)) {
113+
if (c == '(') n ++;
114+
else if (c == ')') {
115+
if (n) n --;
116+
else {
117+
*(input - 1) = 0;
118+
break;
119+
}
120+
}
121+
}
122+
if (*input == ' ') input ++;
123+
//assert(0);
124+
return input;
125+
}
126+
127+
idn_t parse_identifier(p_t *p) {
128+
char c;
129+
idn_t id;
130+
131+
id.p = p->input;
132+
do {
133+
c = *(++ p->input);
134+
} while (c != 0 && c != ' ');
135+
136+
id.len = p->input - id.p;
137+
138+
if (c == ' ') p->input ++;
139+
140+
return id;
141+
}
142+
143+
expr_t parse_num(p_t *p) {
144+
expr_t expr;
145+
int neg = 0;
146+
147+
if (*p->input == '-') {
148+
neg = 1;
149+
p->input ++;
150+
}
151+
152+
expr.type = TYPE_INT;
153+
expr.u.num = 0;
154+
do {
155+
expr.u.num = expr.u.num * 10 + *p->input - '0';
156+
p->input ++;
157+
} while (IS_NUM(p->input));
158+
159+
if (neg) {
160+
expr.u.num = 0 - expr.u.num;
161+
}
162+
163+
if (*p->input == ' ') p->input ++;
164+
165+
return expr;
166+
}
167+
168+
expr_t resolve(p_t *p, expr_t a) {
169+
sym_t *sym;
170+
171+
if (a.type == TYPE_ID) {
172+
sym = p->sym;
173+
while (a.u.id.len != sym->id.len ||
174+
strncmp(a.u.id.p, sym->id.p, sym->id.len)) sym = sym->list;
175+
a.type = TYPE_INT;
176+
a.u.num = sym->val;
177+
}
178+
179+
return a;
180+
}
181+
182+
expr_t parse(p_t *, int);
183+
184+
expr_t parse_let(p_t *p, int d) {
185+
char c;
186+
sym_t *sym;
187+
expr_t a, b;
188+
while (c = *p->input) {
189+
if (c == ' ') {
190+
p->input ++;
191+
continue;
192+
}
193+
a = parse(p, d);
194+
if (a.type == TYPE_INT) break;
195+
if (*p->input) {
196+
b = parse(p, d);
197+
b = resolve(p, b);
198+
sym = malloc(sizeof(*sym));
199+
//assert(sym);
200+
sym->id = a.u.id;
201+
sym->val = b.u.num;
202+
sym->scope = d;
203+
sym->list = p->sym;
204+
p->sym = sym;
205+
}
206+
}
207+
if (a.type == TYPE_ID) {
208+
a = resolve(p, a);
209+
}
210+
sym = p->sym;
211+
while (sym && sym->scope == d) {
212+
p->sym = sym->list;
213+
free(sym);
214+
sym = p->sym;
215+
}
216+
return a;
217+
}
218+
219+
expr_t parse(p_t *p, int d) {
220+
char c, *next;
221+
expr_t a, b, sym, expr;
222+
223+
if ((c = *p->input) == '(') {
224+
p->input ++;
225+
next = next_input(p->input);
226+
expr = parse(p, d + 1);
227+
p->input = next;
228+
} else if (IS_LET(p->input)) {
229+
p->input += 4;
230+
expr = parse_let(p, d);
231+
} else if (IS_ADD(p->input)) {
232+
p->input += 4;
233+
a = parse(p, d);
234+
b = parse(p, d);
235+
a = resolve(p, a);
236+
b = resolve(p, b);
237+
expr.type = TYPE_INT;
238+
expr.u.num = a.u.num + b.u.num;
239+
} else if (IS_MULT(p->input)) {
240+
p->input += 5;
241+
a = parse(p, d);
242+
b = parse(p, d);
243+
a = resolve(p, a);
244+
b = resolve(p, b);
245+
expr.type = TYPE_INT;
246+
expr.u.num = a.u.num * b.u.num;
247+
} else if (IS_NUM(p->input) ||
248+
*p->input == '-') {
249+
expr = parse_num(p);
250+
} else {
251+
expr.type = TYPE_ID;
252+
expr.u.id = parse_identifier(p);
253+
}
254+
255+
//assert(*p->input == 0);
256+
257+
return expr;
258+
}
259+
260+
int evaluate(char * expression) {
261+
expr_t result;
262+
p_t p = { 0 };
263+
p.input = expression;
264+
result = parse(&p, 0);
265+
return result.u.num;
266+
}
267+
268+
269+
/*
270+
Difficulty:Hard
271+
272+
273+
*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /