1
1
In this chapter we will build a simple calculator using the top-down parsing
2
2
technique. This is the preparation before we start to implement the parser.
3
3
4
- I will introduce a small set of theories but not gurantee to be absolute
4
+ I will introduce a small set of theories but will not gurantee to be absolutely
5
5
correct, please consult your textbook if you have any confusion.
6
6
7
7
## Top-down parsing
8
8
9
- Traditionally, we have top-down parsing and bottom-up parsing. Top -down method
10
- will start with a non-terminator and recursively check the source code to
9
+ Traditionally, we have top-down parsing and bottom-up parsing. The top -down
10
+ method will start with a non-terminator and recursively check the source code to
11
11
replace the non-terminators with its alternatives until no non-terminator is
12
12
left.
13
13
14
14
You see I used the top-down method for explaining "top-down" because you'll
15
- have to know what a "non-terminator" is to understand the above graph . But I
16
- havn't tell you what that is. We will tell in the next section. For now,
17
- consider "top-down" is try to tear down a big object into small pieces.
15
+ have to know what a "non-terminator" is to understand the above paragraph . But I
16
+ havn't told you what that is. We will explain in the next section. For now,
17
+ consider "top-down" is trying to tear down a big object into small pieces.
18
18
19
19
On the other hand "bottom-up" parsing is trying to combine small objects into
20
20
a big one. It is often used in automation tools that generate parsers.
@@ -39,17 +39,17 @@ arithmetic calulater in BNF will be:
39
39
| Num
40
40
```
41
41
42
- The one enbraced by ` <> ` is called a ` Non-terminator ` . They got the name
43
- because we can replace them with the ones on the right hand of ` ::= ` .
42
+ The item enclosed by ` <> ` is called a ` Non-terminator ` . They got the name
43
+ because we can replace them with the items on the right hand of ` ::= ` .
44
44
` | ` means alternative that means you can replace ` <term> ` with any one of
45
45
` <term> * <factor> ` , ` <term> / <factor> ` or ` <factor> ` . Those do not appear on
46
46
the left side of ` ::= ` is called ` Terminator ` such as ` + ` , ` ( ` , ` Num ` , etc.
47
- They often corresponds to the tokens we got from lexer.
47
+ They often corresponds to the tokens we got from the lexer.
48
48
49
49
## Top-down Example for Simple Calculator
50
50
51
- Parse tree is the inner structure we got after the parser consumes all the
52
- tokens and finish all the parsing. Let's take ` 3 * (4 + 2) ` as an example to
51
+ The parse tree is the inner structure we get after the parser consumes all the
52
+ tokens and finishes all the parsing. Let's take ` 3 * (4 + 2) ` as an example to
53
53
show the connections between BNF grammer, parse tree and top-down parsing.
54
54
55
55
Top-down parsing starts from a starting non-terminator which is ` <term> ` in
@@ -71,8 +71,8 @@ non-terminator we encountered.
71
71
```
72
72
73
73
You can see that each step we replace a non-terminator using one of its
74
- alternatives(top-down) Until all of the sub-items are replaced to
75
- terminators(bottom). Some non-terminators are used recursively such as
74
+ alternatives (top-down) Until all of the sub-items are replaced by
75
+ terminators (bottom). Some non-terminators are used recursively such as
76
76
` <expr> ` .
77
77
78
78
## Advantages of Top-down Parsing
@@ -110,7 +110,7 @@ As you can see, function `expr` will never exit! In the grammar,
110
110
non-terminator ` <expr> ` is used recursively and appears immediately after
111
111
` ::= ` which causes left-recursion.
112
112
113
- Lucily, most left-recursive grammers(maybe all? I don't remember) can be
113
+ Lucily, most left-recursive grammers (maybe all? I don't remember) can be
114
114
properly transformed into non left-recursive equivalent ones. Our grammar for
115
115
calculator can be converted into:
116
116
@@ -254,7 +254,7 @@ int main(int argc, char *argv[])
254
254
255
255
You can play with your own calculator now. Or try to add some more functions
256
256
based on what we've learned in the previous chapter. Such as variable support
257
- so that user can define variable to store values.
257
+ so that a user can define variables to store values.
258
258
259
259
## Summary
260
260
0 commit comments