Professional Documents
Culture Documents
1 ABOULFATEH AL-DHABEI
Numerous applications:
– Parsing code:
• Matching parenthesis
• XML (e.g., XHTML)
– Tracking function calls
– Dealing with undo/redo operations
– Reverse-Polish calculators
– Assembly language
2 ABOULFATEH AL-DHABEI
Most parsing uses stacks
Examples includes:
– Matching tags in XHTML
– In C++, matching
• parentheses ( ... )
• brackets, and [ ... ]
• braces { ... }
3 ABOULFATEH AL-DHABEI
Parsing XHTML
The first example will demonstrate parsing
XHTML
5 ABOULFATEH AL-DHABEI
XHTML is made of nested
– opening tags, e.g., <some_identifier>, and
– matching closing tags, e.g.,
</some_identifier>
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
6 ABOULFATEH AL-DHABEI
Nesting indicates that any closing tag must
match the most recent opening tag
7 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html>
8 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html> <head>
9 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
10 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
11 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html> <head>
12 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html> <body>
13 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
14 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
15 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
16 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
17 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html> <body>
18 ABOULFATEH AL-DHABEI
<html>
<head><title>Hello</title></head>
<body><p>This appears in the
<i>browser</i>.</p></body>
</html>
<html>
19 ABOULFATEH AL-DHABEI
We are finished parsing, and the stack is
empty
Possible errors:
– a closing tag which does not match the
opening tag on top of the stack
– a closing tag when the stack is empty
– the stack is not empty at the end of the
document
20 ABOULFATEH AL-DHABEI
HTML
Old HTML required neither closing tags nor
nesting
<html>
<head><title>Hello</title></head>
<body><p>This is a list of topics:
<ol> <!-- para ends with start of list -->
<li><i>veni <!-- implied </li> -->
<li>vidi <!-- italics continues -->
<li>vici</i>
</ol> <!-- end-of-file implies </body></html> -->
21 ABOULFATEH AL-DHABEI
XML
XHTML is an implementation of XML
22 ABOULFATEH AL-DHABEI
Reverse-Polish Notation
Normally, mathematics is written using what
we call in-fix notation:
(3 + 4) × 5 – 6
The operator is placed between to operands
23 ABOULFATEH AL-DHABEI
Alternatively, we can place the operands first, followed
by the operator:
(3 + 4) × 5 – 6
3 4 + 5 × 6 –
24 ABOULFATEH AL-DHABEI
Other examples:
3 4 5 × + 6 –
3 20 + 6 –
23 6 –
17
3 4 5 6 – × +
3 4 –1 × +
3 –4 +
–1
25 ABOULFATEH AL-DHABEI
Benefits:
– No ambiguity and no brackets are required
– It is the same process used by a computer to
perform computations:
• operands must be loaded into registers before
operations can be performed on them
– Reverse-Polish can be processed using
stacks
26 ABOULFATEH AL-DHABEI
The easiest way to parse reverse-Polish
notation is to use an operand stack:
– operands are processed by pushing them
onto the stack
– when processing an operator:
• pop the last two items off the operand stack,
• perform the operation, and
• push the result back onto the stack
27 ABOULFATEH AL-DHABEI
Evaluate the following reverse-Polish expression using a stack:
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
28 ABOULFATEH AL-DHABEI
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
1
29 ABOULFATEH AL-DHABEI
Push 2 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
2
1
30 ABOULFATEH AL-DHABEI
Push 3 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
3
2
1
31 ABOULFATEH AL-DHABEI
Pop 3 and 2 and push 2 + 3 = 5
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
5
1
32 ABOULFATEH AL-DHABEI
Push 4 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
4
5
1
33 ABOULFATEH AL-DHABEI
Push 5 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
5
4
5
1
34 ABOULFATEH AL-DHABEI
Push 6 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
6
5
4
5
1
35 ABOULFATEH AL-DHABEI
Pop 6 and 5 and push 5 × 6 = 30
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
30
4
5
1
36 ABOULFATEH AL-DHABEI
Pop 30 and 4 and push 4 – 30 = –26
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–26
5
1
37 ABOULFATEH AL-DHABEI
Push 7 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
7
–26
5
1
38 ABOULFATEH AL-DHABEI
Pop 7 and –26 and push –26 × 7 = –182
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–182
5
1
39 ABOULFATEH AL-DHABEI
Pop –182 and 5 and push –182 + 5 = –177
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
–177
1
40 ABOULFATEH AL-DHABEI
Pop –177 and 1 and push 1 – (–177) = 178
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
178
41 ABOULFATEH AL-DHABEI
Push 8 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
8
178
42 ABOULFATEH AL-DHABEI
Push 1 onto the stack
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
9
8
178
43 ABOULFATEH AL-DHABEI
Pop 9 and 8 and push 8 × 9 = 72
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
72
178
44 ABOULFATEH AL-DHABEI
Pop 72 and 178 and push 178 + 72 = 250
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
250
45 ABOULFATEH AL-DHABEI
Thus
1 2 3 + 4 5 6 × – 7 × + – 8 9 × +
evaluates to the value on the top: 250
The equivalent in-fix notation is
((1 – ((2 + 3) + ((4 – (5 × 6)) × 7))) + (8 × 9))
46 ABOULFATEH AL-DHABEI