Skip to content

Commit 1176c39

Browse files
authored
Merge pull request #22 from krupan/grammar-fixes
Grammar fixes
2 parents 4a93869 + 71af032 commit 1176c39

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

tutorial/en/4-Top-down-Parsing.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
In this chapter we will build a simple calculator using the top-down parsing
22
technique. This is the preparation before we start to implement the parser.
33

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
55
correct, please consult your textbook if you have any confusion.
66

77
## Top-down parsing
88

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
1111
replace the non-terminators with its alternatives until no non-terminator is
1212
left.
1313

1414
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.
1818

1919
On the other hand "bottom-up" parsing is trying to combine small objects into
2020
a big one. It is often used in automation tools that generate parsers.
@@ -39,17 +39,17 @@ arithmetic calulater in BNF will be:
3939
| Num
4040
```
4141

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 `::=`.
4444
`|` means alternative that means you can replace `<term>` with any one of
4545
`<term> * <factor>`, `<term> / <factor>` or `<factor>`. Those do not appear on
4646
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.
4848

4949
## Top-down Example for Simple Calculator
5050

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
5353
show the connections between BNF grammer, parse tree and top-down parsing.
5454

5555
Top-down parsing starts from a starting non-terminator which is `<term>` in
@@ -71,8 +71,8 @@ non-terminator we encountered.
7171
```
7272

7373
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
7676
`<expr>`.
7777

7878
## Advantages of Top-down Parsing
@@ -110,7 +110,7 @@ As you can see, function `expr` will never exit! In the grammar,
110110
non-terminator `<expr>` is used recursively and appears immediately after
111111
`::=` which causes left-recursion.
112112

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
114114
properly transformed into non left-recursive equivalent ones. Our grammar for
115115
calculator can be converted into:
116116

@@ -254,7 +254,7 @@ int main(int argc, char *argv[])
254254
255255
You can play with your own calculator now. Or try to add some more functions
256256
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.
258258
259259
## Summary
260260

tutorial/en/5-Variables.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
In this chapter we are going to use EBNF to describe the grammer of our C
2-
interpreter, and add the support of variable.
2+
interpreter, and add the support of variables.
33

4-
Parser is more complicated than lexer, thus we will split it into 3 parts:
4+
The parser is more complicated than the lexer, thus we will split it into 3 parts:
55
variables, functions and expressions.
66

77
## EBNF grammar
@@ -11,7 +11,7 @@ We've talked about BNF in the previous chapter,
1111
Extended-BNF. If you are familiar with regular expression, you should feel
1212
right at home. Personally I think it is more powerful and straightforward than
1313
BNF. Here is the EBNF grammar of our C interpreter, feel free to skip it if
14-
you feel too hard to understand.
14+
you feel it's too hard to understand.
1515

1616
```
1717
program ::= {global_declaration}+
@@ -61,15 +61,15 @@ void program() {
6161
```
6262

6363
I know that we havn't defined `global_declaration`, sometimes we need wishful
64-
thinking that maybe someone(say Bob) will implement that for you. So you can
64+
thinking that maybe someone (say Bob) will implement that for you. So you can
6565
focus on the big picture at first instead of drill down into all the details.
6666
That's the essence of top-down thinking.
6767

6868
## global_declaration()
6969

70-
Now it is our duty(not Bob's) to implement `global_declaration`. It will try
71-
to parse variable definition, type definition(only enum is supported) and
72-
function definition:
70+
Now it is our duty (not Bob's) to implement `global_declaration`. It will try
71+
to parse variable definitions, type definitions (only enum is supported) and
72+
function definitions:
7373

7474
```c
7575
int basetype; // the type of a declaration, make it global for convenience

tutorial/en/6-Functions.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ if (token == '(') {
4343
...
4444
```
4545
46-
The type for current identifier(i.e. function name) had already been set
47-
correctly. The above chunk of code set the type(i.e. `Fun`) and the
46+
The type for current identifier (i.e. function name) had already been set
47+
correctly. The above chunk of code set the type (i.e. `Fun`) and the
4848
address in `text segment` for the function. Here comes `parameter_decl` and
4949
`body_decl`.
5050
5151
## Parameters and Assembly Output
5252
53-
Before we get our hand dirty, we have to understand the assembly code that
53+
Before we get our hands dirty, we have to understand the assembly code that
5454
will be output for a function. Consider the following:
5555
5656
```c
@@ -62,8 +62,8 @@ int demo(int param_a, int *param_b) {
6262
}
6363
```
6464

65-
When `demo` is called, its calling frame(states of stack) will look like the
66-
following(please refer the the VM of chapter 2):
65+
When `demo` is called, its calling frame (states of stack) will look like the
66+
following (please refer to the VM of chapter 2):
6767

6868
```
6969
| .... | high address
@@ -83,7 +83,7 @@ following(please refer the the VM of chapter 2):
8383
| .... | low address
8484
```
8585

86-
The key point here is no matter it is parameter(e.g. `param_a`) or local
86+
The key point here is no matter if it is a parameter (e.g. `param_a`) or local
8787
variable (e.g. `local_1`), they are all store on **stack**. Thus they are
8888
referred to by pointer `new_bp` and relative shfitment, while global variables
8989
which are stored in `text segment` are refered to by direct address. So we

0 commit comments

Comments
 (0)