parse - Maple Programming Help

This page is an exact match for your search term. Click here to search for all help pages containing the term, "parse"

parse

parse a string as a Maple statement

 Calling Sequence parse(string, option)

Parameters

 string - Maple string containing a Maple expression, or a Maple statement if the 'statement' option is specified option - zero or other options

Description

 • The parse command takes a Maple string and parses the string into a Maple expression as if it had been entered or read from a file.
 • The string must consist of exactly one Maple expression. The expression is parsed, and returned unevaluated.
 • If the option 'statement' is specified, the string must consist of exactly one Maple statement (which includes expressions). The statement is parsed and evaluated, and the result is returned.
 • Partial statements or expressions cannot be parsed.
 • The 'lastread' and 'offset' options can be used to parse multiple statements or expressions.  The calling sequence parse("expr1; expr2;",'lastread' = 'n'), parses the given string, and assigns n the offset of the next unparsed character. Thus parse("expr1; expr2;",'offset' = n) can be used to parse the next statement in the string.  The variable indicated by lastread should have single uneval quotes around it to prevent evaluation if it is already assigned a value.  The lastread variable is not updated when parsing generates a syntax error.
 • If the string is syntactically incorrect, parse returns an error of the form "incorrect syntax in parse: ... (near n'th character of parsed string)". The "n'th" part of the message indicates the offset, in characters, counted from the beginning of the string, at which the syntax error was detected.  The error can be trapped by using a try-catch construct, like any other Maple run-time error.
 • If the string does not end with a semicolon (or colon) and a newline character, a semicolon and newline character is supplied automatically. This behavior can be suppressed by specifying the option 'nosemicolon'.
 • If the string ends with a semicolon (or colon) but no newline character, the newline character is supplied automatically.
 • Whether the expression is displayed when parse is called depends on whether the call to parse ends in a semicolon or a colon.
 • When using the statement option you can prevent evaluation by also using the noeval option.  In this case NULL is always returned. This can be useful in combination with lastread to determine statement boundaries.
 • If parse is called with no arguments, it returns either true or false depending on whether  the last string parsed by parse ended with a colon.
 • To convert a Maple statement to a string use convert/string.

 • The parse command is thread-safe as of Maple 15.

Examples

 > $\mathrm{parse}\left("a+b"\right)$
 ${a}{+}{b}$ (1)
 > $\mathrm{parse}\left("a+b:"\right)$
 ${a}{+}{b}$ (2)
 > $\mathrm{parse}\left("a+b;"\right)$
 ${a}{+}{b}$ (3)
 > $\mathrm{parse}\left("a+b:"\right):$
 > $\mathrm{parse}\left(\right)$
 ${\mathrm{true}}$ (4)
 > $\mathrm{parse}\left("a+b;"\right):$
 > $\mathrm{parse}\left(\right)$
 ${\mathrm{false}}$ (5)

Parse generates errors on incorrect input.

 > $\mathrm{parse}\left("a^^b"\right)$
 > $\mathrm{parse}\left("sin\left(3.0\right)"\right)$
 ${\mathrm{sin}}{}\left({3.0}\right)$ (6)
 > $\mathrm{parse}\left("sin\left(3.0\right)",\mathrm{statement}\right)$
 ${0.1411200081}$ (7)

Parse multiple statements.

 > $n≔0$
 ${n}{≔}{0}$ (8)
 > $\mathrm{input}≔"a := 4; a^2; a^3;"$
 ${\mathrm{input}}{≔}{"a := 4; a^2; a^3;"}$ (9)
 > $\mathrm{parse}\left(\mathrm{input},'\mathrm{lastread}'='n','\mathrm{offset}'=n,\mathrm{statement}\right)$
 ${4}$ (10)
 > $\mathrm{parse}\left(\mathrm{input},'\mathrm{lastread}'='n','\mathrm{offset}'=n,\mathrm{statement}\right)$
 ${16}$ (11)
 > $\mathrm{parse}\left(\mathrm{input},'\mathrm{lastread}'='n','\mathrm{offset}'=n,\mathrm{statement}\right)$
 ${64}$ (12)