Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Operator no-precedence parsing


1. Operator no-precedence parsing

2. Shunting algorithm
Here we use a simplified shunting algorithm for operator precedence parsing, originally invented by Edsger Dijkstra. It is simplified because fully parenthesized expressions are required. This will later be relaxed by adding operator precedences.

The goal is to parse a sequence of characters into a postfix form of a logical expression (tree).

A simplified shunting algorithm is used to show the essential ideas of a compiler.

3. Compiler
A compiler typically includes the following parts.

4. Source handler
A source handler separates the source of the characters in a program from the scanner which separates the characters into tokes.

Here, for logical expressions, we consider only single character tokens with the extension to multiple character tokes being left as an exercise.

5. Logical expression
Expression tree for ( X & Y ) | (( ! X ) & (! Y ))
( X & Y ) | ( ( ! X ) & ( ! Y ) )


6. Truth table using manual method
Here is the extended truth table for this logical expression.
X Y | ( X & Y ) | ( ( ! X ) & ( ! Y ) ) --------------------------------------- 0 0 | ( 0 0 0 ) 1 ( ( 1 0 ) 1 ( 1 0 ) ) 0 1 | ( 0 0 1 ) 0 ( ( 1 0 ) 0 ( 0 1 ) ) 1 0 | ( 1 0 0 ) 0 ( ( 0 1 ) 0 ( 1 0 ) ) 1 1 | ( 1 1 1 ) 1 ( ( 0 1 ) 0 ( 0 1 ) )

For more on manually creating this table, see Truth tables: manual method .

7. Expression tree
Rather than a file or other source, the text of the infix logical expression is hard-coded to a string variable. Here is a way to iterate through that string.

Here is the JavaScript code.

Here is the output of the JavaScript code.

Note that this code is not particularly useful as a way is needed to request the next character in sequence.

8. Next character
A source handler will be initialized (with a source), provide characters, be able to detect the end of the source (e.g., file) and whether the end of the source has been reached. Here is some simple code to accomplish this. Here is the source handler.

Here is the JavaScript code.


9. Output

10. Object and class remark
As with examples to date, a module or global scope is used. When objects and classes are covered, some of the examples used previously will be updated to see how they would be expressed using objects and classes.

In the above code, the prefix source is used. These parts might become part of a module or part of a class, depending on what is being done.

11. Source handler comments
The sourceDone1 routine is typically used to clean up after all the input is read, raise an error if not all the input was read, etc.

The above code will be assumed in the following examples, but not shown in order to concentrate on the essential parts being discussed.

12. Output
The output is the same as the previous output except that it is convenient to have the sourcePos1 be one more than the value of i1 in the previous example.

Here is the output of the JavaScript code.


13. State machines
The source handler is a simple example of a state machine, remembering the state of the previous character so it can return the next character.

An enhanced source handler would allow input form a file and the inclusion of a file from within the current file. This requires a stack mechanism to push the next file being read, pop the stack when that file is finished, until all of the input is read.

14. Scanners
A scanner, or lexer, takes as input a sequence of characters and returns a sequence of tokens, one at a time. There is a special token to indicate the end of the input.

In systems that provide source error location, the location of the token is part of the token structure returned. This is omitted for example purposes.

15. Scanner comments
Note that the scanner removes the white space and returns only the symbols (or tokens).

Left as exercises:

16. Scanner
Here is the JavaScript code.


17. Output
Here is the output of the JavaScript code.


18. Parser
Here is the JavaScript code.


19. Output
Here is the output of the JavaScript code.


20. End of page