Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Operator-precedence parser
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== Full parenthesization === Another approach is to first fully parenthesize the expression, inserting a number of parentheses around each operator, such that they lead to the correct precedence even when parsed with a linear, left-to-right parser. This algorithm was used in the early [[Fortran#FORTRAN|FORTRAN I]] compiler:<ref name="Padua2000">{{cite journal |last1=Padua |first1=David |title=The Fortran I Compiler |year=2000 |journal=Computing in Science & Engineering |volume=2 |issue=1 |pages=70β75 |url=http://polaris.cs.uiuc.edu/publications/c1070.pdf |doi=10.1109/5992.814661|bibcode=2000CSE.....2a..70P }}</ref> <blockquote> The Fortran I compiler would expand each operator with a sequence of parentheses. In a simplified form of the algorithm, it would * replace <code>+</code> and <code>β</code> with <code>))+((</code> and <code>))-((</code>, respectively; * replace <code>*</code> and <code>/</code> with <code>)*(</code> and <code>)/(</code>, respectively; * add <code>((</code> at the beginning of each expression and after each left parenthesis in the original expression; and * add <code>))</code> at the end of the expression and before each right parenthesis in the original expression. Although not obvious, the algorithm was correct, and, in the words of [[Donald Knuth|Knuth]], βThe resulting formula is properly parenthesized, believe it or not.β<ref name="Knuth1962">{{cite journal |last1=Knuth |first1=Donald E. |title=A HISTORY OF WRITING COMPILERS |year=1962 |publisher=Edmund C. Berkeley |journal=Computers and Automation |volume=11 |issue=12 |pages=8β14 |url=https://archive.org/details/bitsavers_computersA_13990695}}</ref> </blockquote> {{missing information|section|why parenthesization works|date=May 2023}} Example code of a simple C application that handles parenthesisation of basic math operators (<code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>^</code>, <code>(</code> and <code>)</code><!-- parentheses -->): <syntaxhighlight lang="c"> #include <stdio.h> #include <string.h> // The command-line argument boundary is our lexer. int main(int argc, char *argv[]) { int i; printf("(((("); for (i=1; i!=argc; i++) { // strlen(argv[i]) == 2 if (argv[i] && !argv[i][1]) { switch (*argv[i]) { case '(': printf("(((("); continue; case ')': printf("))))"); continue; case '^': printf(")^("); continue; case '*': printf("))*(("); continue; case '/': printf("))/(("); continue; case '+': // unary check: either first or had an operator expecting secondary argument if (i == 1 || strchr("(^*/+-", *argv[i-1])) printf("+"); else printf(")))+((("); continue; case '-': if (i == 1 || strchr("(^*/+-", *argv[i-1])) printf("-"); else printf(")))-((("); continue; } } printf("%s", argv[i]); } printf("))))\n"); return 0; } </syntaxhighlight> First, you need to compile your program. Assuming your program is written in C and the source code is in a file named program.c, you would use the following command: <pre>gcc program.c -o program</pre> The above command tells gcc to compile program.c and create an executable named program. Command to run the program with parameters, For example; a * b + c ^ d / e <pre>./program a '*' b + c '^' d / e</pre> it produces <pre>((((a))*((b)))+(((c)^(d))/((e))))</pre> as output on the console. A limitation to this strategy is that unary operators must all have higher precedence than infix operators. The "negative" operator in the above code has a higher precedence than exponentiation. Running the program with this input <pre>- a ^ 2</pre> produces this output <pre>((((-a)^(2))))</pre> which is probably not what is intended.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)