Computer algebra systems are configured to receive user inputs of mathematical expressions, understand the inputs, and perform various tasks according to the inputs. Existing computer algebra systems apply strict syntactical rules to the mathematical expressions input by a user. This approach eliminates ambiguity in the interpretation of the inputs processed by the system, but can be difficult for users to learn due to the complexity of the syntactical rules. In addition, the mathematical notations used in mathematical textbooks and papers sometimes contain ambiguities. To remove these ambiguities, the syntactical rules of existing computer algebra systems often differ from the mathematical notations appearing in these textbooks and papers. This can be particularly frustrating to beginning users who are attempting to solve the problems appearing in these textbooks and papers using an existing computer algebra system.
Prior parsing techniques for user input of mathematical expressions involve tokenizing an entire input string, and then passing the tokens to a grammar analyzer to build up an expression tree. Thus, the user input is tokenized before the grammar analysis begins, and only lexical rules are used in the tokenization procedure. While these prior parsing techniques may be suitable for computer algebra systems that apply strict syntactical rules, these techniques suffer from the drawback that they cannot handle ambiguity in syntax, and thus cannot be used to process user input in a user-friendly manner.
Systems and methods for progressively parsing user input of a mathematical expression are provided. One disclosed method includes looping through characters in an input string, and on each loop, extracting a next token from the input string and determining a current grammar context based on the token or tokens extracted thus far. If it is determined that the current grammar context matches a predetermined condition, then the method may include modifying the tokens extracted from the input string in a predetermined manner associated with the predetermined condition. A parsing result may be obtained based on the modified tokens. The parsing result may be converted to a modified input string.
This Summary is provided to introduce a selection of concepts in a simplified form that are further described below in the Detailed Description. This Summary is not intended to identify key features or essential features of the claimed subject matter, nor is it intended to be used to limit the scope of the claimed subject matter. Furthermore, the claimed subject matter is not limited to implementations that solve any or all disadvantages noted in any part of this disclosure.
The tokenizer 32 is configured to receive the input string via the graphical user interface of a computer program, and loop through the input string and extract a token from the input string during each loop. A token can be a number, a constant or variable name, an operator, a function name, or a whitespace, for example. The grammar analyzer 34 is configured to, during each loop, determine a current grammar context based on the token or tokens extracted thus far, determine whether the current grammar context matches a predetermined condition, and if the current grammar context matches the predetermined condition, modify the tokens extracted from the input string in a predetermined manner associated with the predetermined condition, to produce a modified input string, and update a current grammar context to prepare the next token for extraction.
The progressive parser 30 is configured to obtain a parsing result 36 based on the modifications made to the tokens extracted from the input string during each loop. The parsing result 36 of the progressive parser 30 is configured to be output from the progressive parser 30 to a serializer 37, which converts the parsing result 36 into a modified input string 39. The modified input string 39 may be displayed on the GUI 16, and also may be sent to the expression evaluator 38 for evaluation. The expression evaluator 38 is configured to evaluate a mathematical expression contained in the modified input string 39, and display the resulting output 40 on the GUI 16, as further illustrated in
As shown in
In the example illustrated in
During parsing, the grammar analyzer 34 is configured to build a result expression tree 50 based on the contents of the operator stack 44, operand stack 46, and implicit function stack 48, and may also build temporary expression trees, such as 50A illustrated in
As illustrated in
At 110, the method includes determining a current grammar context based on the token or tokens extracted thus far. Various token types that may be identified are further illustrated in
At 118, the method may include, determining whether the input string is fully processed, and if not, looping back to step 104. If it is determined that the user input string is fully processed, the method may include, at 120, building up an final expression tree based on all of the extracted tokens, as modified by the techniques discussed above. As at 115, the expression tree is built up based on the contents of the operator stack, operand stack and implicit function stack, as discussed above.
At 121, the method includes obtaining a parsing result based on the modified tokens. The parsing result may be obtained by compiling the expressions of the expression tree to obtain the parsing result. At 122, the method includes converting the parsing result to a modified input string. This may be performed, for example, by a serializer 37 that converts a parsing result into a text string, as described above. At 123, the method includes displaying the parsing result on the GUI 16. At 123A, the method may include displaying an alternative modified input string on the GUI, which is based on an alternative parsing result. At 124, the method may include evaluating a mathematical expression contained in the modified input string, and at 126, displaying a resulting output on the graphical user interface. At 124A, the alternative modified input string may be evaluated, and at 126A the resulting output be displayed. It will be appreciated that a link may alternatively be displayed to the alternative output and/or the alternative modified input string, as discussed above.
Turning now to
As shown at 146, identifying the token type may include identifying the token type as an operator. Once so identified, as shown at 148, determining whether the grammar context matches a predetermined condition may include determining that a precedence of the operator of the extracted token is smaller than the precedence of a top operator in the operator stack. If it is determined that the precedence of the operator of the extracted token is smaller than the precedence of the top operator, then as shown at 150, modifying the tokens extracted from the input string in a predetermined manner may include using the elements in the operator and operand stack to build sub-expressions, until a top of the operator stack has a higher precedence than the current operator, and pushing the result into the operand stack.
As shown at 152, identifying the token type may include identifying the token type as a comma. Once so identified, as illustrated at 154, determining whether the grammar context matches a predetermined condition may include determining that the comma is a delimiting comma in a list or an equation array of function parameters. If such a determination is made, as shown at 156, modifying the tokens extracted from the input string in a predetermined manner may include examining a top of the operand stack to determine whether a list or equation array should be started, scanning the operator and operand stack to determine a location to start the list or equation array, and as shown at 158, inserting a list start or equation array start in the determined location.
As shown at 160, identifying the token type may include identifying the token type as an open parenthesis. Once so identified, as shown at 162, determining whether the grammar context matches a predetermined condition may include determining whether a top of the operator stack is a function that requires parameters. As shown at 164, upon determining that the top of the operator stack is a function that requires parameters, modifying the tokens extracted from the input string in a predetermined manner may include replacing the current token with a parameter start. An example of such a modification is illustrated in
At 168, the method may further include, prior to obtaining the parsing result, placing the next token in a stack selected from the group consisting of an operator stack, operand stack and implicit function stack, as appropriate under the rules described above.
Steps 115-121 are similar to those described above and will not be redescribed in detail. It will be appreciated that during the step of building the subexpression trees at 120, the method may further include, as shown at 170, encountering an outstanding start operator without a corresponding closing operator, and as shown at 172, inserting a corresponding closing operator at the top of the operator stack. After the parsing result is obtained at 121, it will be appreciated that the method may continue to step 122 in
It will be appreciated that the current grammar context may be determined to match the predetermined conditions described above by referencing a function start flag that is attached to each token that is a function operator designated to accept at least one parameter. For example, the flag may include the function name, an indication of whether the function accepts comma-delimited lists or arrays, and an indication of whether the lists or arrays accepted by the function are of a fixed size.
The above described systems and method may be used to enable more user friendly input processing, by progressively parsing user input mathematical expressions and modifying the input string as appropriate according to an updated grammar context of the input string, to thereby accommodate variations in syntax such as appear in mathematical textbooks and papers.
The systems and method described above may be used to parse a variety of mathematical expressions. The following chart illustrates various user input strings, and associated modified input string, output, and decimal form output, that may be calculated according to the systems and method described above.
The following list further illustrates the manner in which the systems and methods described above can be used to enable flexible parsing of various user inputs.
1. Users can use parentheses both for a grouping purpose and as a function parameter boundary. For example, both “3·(x+π)” and “max(3, 2, 2.5)” are allowed.
2. Users can omit the parentheses when used as function parameter boundary, and the expression will be parsesd in a sensible way. For example, “sin 30x” may be interpreted as “sin(30˜x)”, “sin 30+x” may be interpreted as “(sin(30))+x”, and “max 3, 2, 2.5” may be interpreted as “max(2, 3, 2.5)”.
3. Users don't have to use whitespaces to separate keywords. For example, “xln sin x” should be interpreted as “x·ln(sin(x))”.
4. Users can omit a closing parentheses and the system will automatically add a closing parenthesis in a sensible way. For example, “3·(x+π” is accepted and interpreted as “3·(x+π)”.
5. While curly braces may be used to denote lists (e.g. “{1,2,3}”), users can omit the curly braces and type in only the comma delimited list “1,2,3”, and the system will interpret the input string as “{1,2,3}”.
6. Users can input in comma-delimited equations to represent an equation system. For example, “x+y=3, 3x−2y=5” may be interpreted as
It will be appreciated that the computing devices described herein may be any suitable computing device configured to execute the programs and display the graphical user interfaces described herein. For example, the computing devices may be a personal computer, laptop computer, portable data assistant (PDA), computer-enabled wireless telephone, networked computing device, or other suitable computing device, and may be connected to each other via computer networks, such as the Internet. These computing devices typically include a processor and associated volatile and non-volatile memory, and are configured to execute programs stored in non-volatile memory using portions of volatile memory and the processor. As used herein, the term “program” refers to software or firmware components that may be executed by, or utilized by, one or more computing devices described herein, and is meant to encompass individual or groups of executable files, data files, libraries, drivers, scripts, database records, etc. It will be appreciated that computer-readable media may be provided having program instructions stored thereon, which upon execution by a computing device, cause the computing device to execute the methods described above and cause operation of the systems described above.
It should be understood that the embodiments herein are illustrative and not restrictive, since the scope of the invention is defined by the appended claims rather than by the description preceding them, and all changes that fall within metes and bounds of the claims, or equivalence of such metes and bounds thereof are therefore intended to be embraced by the claims.