Application Center - Maplesoft

App Preview:

A Simple Expression Parser

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

Simple Expression Parser 

Bill Bauldry
Dept of Mathematical Sciences
Appalachian State University
BauldryWC@appstate.edu
 

Description 

This worksheet implements a simple parser for Maple expressions. The output is a nested list that describes how Maple interprets the expression. The syntax is simple: Parse(expression). The Parse function can be used to learn about expression evaluation and Maple's internal expression data structures. 

Functions 

The Op function replaces an item with a form: [operation, operand list] 

Op := proc (x) if 1 < nops(x) and not type(x, function) then [whattype(x), op(x)] elif type(x, function) then [op(0, x), op(x)] else x end if end proc; -1
Op := proc (x) if 1 < nops(x) and not type(x, function) then [whattype(x), op(x)] elif type(x, function) then [op(0, x), op(x)] else x end if end proc; -1
Op := proc (x) if 1 < nops(x) and not type(x, function) then [whattype(x), op(x)] elif type(x, function) then [op(0, x), op(x)] else x end if end proc; -1
Op := proc (x) if 1 < nops(x) and not type(x, function) then [whattype(x), op(x)] elif type(x, function) then [op(0, x), op(x)] else x end if end proc; -1
Op := proc (x) if 1 < nops(x) and not type(x, function) then [whattype(x), op(x)] elif type(x, function) then [op(0, x), op(x)] else x end if end proc; -1
Op := proc (x) if 1 < nops(x) and not type(x, function) then [whattype(x), op(x)] elif type(x, function) then [op(0, x), op(x)] else x end if end proc; -1
Op := proc (x) if 1 < nops(x) and not type(x, function) then [whattype(x), op(x)] elif type(x, function) then [op(0, x), op(x)] else x end if end proc; -1
Op := proc (x) if 1 < nops(x) and not type(x, function) then [whattype(x), op(x)] elif type(x, function) then [op(0, x), op(x)] else x end if end proc; -1
Op := proc (x) if 1 < nops(x) and not type(x, function) then [whattype(x), op(x)] elif type(x, function) then [op(0, x), op(x)] else x end if end proc; -1
Op := proc (x) if 1 < nops(x) and not type(x, function) then [whattype(x), op(x)] elif type(x, function) then [op(0, x), op(x)] else x end if end proc; -1
 

The Parse function recursively traverses the expression generating a nested list of [operation, operands] pairs in essentially prefix notation. 

Parse := proc (expr) local tmp, i; tmp := Op(expr); for i from 2 to nops(tmp) do if 1 < nops(tmp[i]) or type(tmp[i], function) then tmp := subsop(i = Parse(tmp[i]), tmp) end if end do; RETURN(tmp) end...
Parse := proc (expr) local tmp, i; tmp := Op(expr); for i from 2 to nops(tmp) do if 1 < nops(tmp[i]) or type(tmp[i], function) then tmp := subsop(i = Parse(tmp[i]), tmp) end if end do; RETURN(tmp) end...
Parse := proc (expr) local tmp, i; tmp := Op(expr); for i from 2 to nops(tmp) do if 1 < nops(tmp[i]) or type(tmp[i], function) then tmp := subsop(i = Parse(tmp[i]), tmp) end if end do; RETURN(tmp) end...
Parse := proc (expr) local tmp, i; tmp := Op(expr); for i from 2 to nops(tmp) do if 1 < nops(tmp[i]) or type(tmp[i], function) then tmp := subsop(i = Parse(tmp[i]), tmp) end if end do; RETURN(tmp) end...
Parse := proc (expr) local tmp, i; tmp := Op(expr); for i from 2 to nops(tmp) do if 1 < nops(tmp[i]) or type(tmp[i], function) then tmp := subsop(i = Parse(tmp[i]), tmp) end if end do; RETURN(tmp) end...
Parse := proc (expr) local tmp, i; tmp := Op(expr); for i from 2 to nops(tmp) do if 1 < nops(tmp[i]) or type(tmp[i], function) then tmp := subsop(i = Parse(tmp[i]), tmp) end if end do; RETURN(tmp) end...
Parse := proc (expr) local tmp, i; tmp := Op(expr); for i from 2 to nops(tmp) do if 1 < nops(tmp[i]) or type(tmp[i], function) then tmp := subsop(i = Parse(tmp[i]), tmp) end if end do; RETURN(tmp) end...
Parse := proc (expr) local tmp, i; tmp := Op(expr); for i from 2 to nops(tmp) do if 1 < nops(tmp[i]) or type(tmp[i], function) then tmp := subsop(i = Parse(tmp[i]), tmp) end if end do; RETURN(tmp) end...
Parse := proc (expr) local tmp, i; tmp := Op(expr); for i from 2 to nops(tmp) do if 1 < nops(tmp[i]) or type(tmp[i], function) then tmp := subsop(i = Parse(tmp[i]), tmp) end if end do; RETURN(tmp) end...
Parse := proc (expr) local tmp, i; tmp := Op(expr); for i from 2 to nops(tmp) do if 1 < nops(tmp[i]) or type(tmp[i], function) then tmp := subsop(i = Parse(tmp[i]), tmp) end if end do; RETURN(tmp) end...
Parse := proc (expr) local tmp, i; tmp := Op(expr); for i from 2 to nops(tmp) do if 1 < nops(tmp[i]) or type(tmp[i], function) then tmp := subsop(i = Parse(tmp[i]), tmp) end if end do; RETURN(tmp) end...
 

 

Examples 

A polynomial 

p := 2+x^2*sin(x+ln(y))+x/(y+1) 

2+x^2*sin(x+ln(y))+x/(y+1) 

q := Parse(p) 

[`+`, 2, [`*`, [`^`, x, 2], [sin, [`+`, x, [ln, y]]]], [`*`, x, [`^`, [`+`, y, 1], -1]]] 

 

A rational function 

r := randpoly([x, y], terms = 4, expons = rand(-3 .. 3))/randpoly([x, y], terms = 5, expons = rand(-3 .. 3)) 

(87-7*x^3*y^3+22*x*y^2-62*x/y)/(-4*y/x^3-10/(x*y^2)-82/y-17/(x^3*y^2)-7*y) 

s := Parse(r) 

[`*`, [`+`, 87, [`*`, -7, [`^`, x, 3], [`^`, y, 3]], [`*`, 22, x, [`^`, y, 2]], [`*`, -62, x, [`^`, y, -1]]], [`^`, [`+`, [`*`, -4, [`^`, x, -3], y], [`*`, -10, [`^`, x, -1], [`^`, y, -2]], [`*`, -82,...
[`*`, [`+`, 87, [`*`, -7, [`^`, x, 3], [`^`, y, 3]], [`*`, 22, x, [`^`, y, 2]], [`*`, -62, x, [`^`, y, -1]]], [`^`, [`+`, [`*`, -4, [`^`, x, -3], y], [`*`, -10, [`^`, x, -1], [`^`, y, -2]], [`*`, -82,...
[`*`, [`+`, 87, [`*`, -7, [`^`, x, 3], [`^`, y, 3]], [`*`, 22, x, [`^`, y, 2]], [`*`, -62, x, [`^`, y, -1]]], [`^`, [`+`, [`*`, -4, [`^`, x, -3], y], [`*`, -10, [`^`, x, -1], [`^`, y, -2]], [`*`, -82,...