Context-Free Grammar compiler design and implementation-Q & A with a student

Source: Internet
Author: User

Context-Free Grammar compiler design and implementation-Q & A with a student

This article is from a QQ chat Q & A record with a classmate I don't know. I hope I can help those who read the articles I wrote to make the compiler but cannot understand it.

The release of this article has been approved and supported by "I want to ask some questions". Thank you!

PS: the bold words are not me bold! What is wlw! Let's download the PDF version here. The format is better.

Some questions: 9:15:17

Hello ~

Nulshunde: 15: 21

Hi!

Nulshunde: 15: 26

What is it called?

Some questions: 9:15:51

Well, you can ask me some questions ~

Some questions: 9:16:07

I have been working on an experimental course on compilation principles recently ~ Then ~~ No ..

Some questions: 9:16:22

I would like to ask how to create a syntax tree from the ll1 analysis table ..

Nulshunde: 17: 14

This problem is too long.

Nulshunde: 17: 20

Have you read my blog?

Some questions: 9:17:20

Amount...

Some questions: 9:17:50

I saw it .. I don't know much about it ..

Nulshunde: 18: 28

Ll1Can I understand the structure of the analysis table?

Some questions: 9:25:35

Well, this can

Some questions: 9:28:12

But I don't quite understand the relationship between ll1 analysis table and syntax tree ..

Null9: 28: 27

Nulshunde: 28: 31

Read this section

Nulshunde: 29: 17

We recommend that you start by understanding the expression syntax tree.

Nulshunde: 31: 16

Can the Grammar be understood?

Some questions: 9:31:53

Yes ..

Some questions: 9:31:58

Looking at it ..

Nulshunde: 32: 12

Is that OK or not?

Some questions: 9:32:32

Yes...

Null9: 32: 38

Do you have an expression syntax?

Null9: 32: 39

That's it.

Null9: 32: 39

<Expression >::= <multiply> <plusopt>;
<Plusopt >:: = "+" <multiply> | "-" <multiply> | NULL;
<Multiply >::=< unit> <multiplyopt>;
<Multiplyopt >:: = "*" <unit> | "/" <unit> | NULL;
<Unit>: = Number | "(" <expression> ")";

Some questions: 9:33:58

This can be understood .. But it's a little different from the book.

Nulshunde: 34: 17

What's different?

Some questions: 9:34:43

It doesn't matter. It's a bit different in the way of narration...

Nulshunde: 34: 54

The structure must be the same.

Nulshunde: 35: 06

Grammar is the foundation of everything

Some questions: 9:48:16

.. In fact, I mean, there is now a syntax and a program, there are various first sets and follow sets of this syntax, ll1 analysis table, and then how to draw the syntax tree of the program...

Nulshunde: 49: 14

Draw the syntax tree of the program. It is enough to have this program and ll1 analysis table.

Nulshunde: 49: 49

4.1 ll1Analysis Process

Nulshunde: 49: 53

Can this part not be understood?

Some questions: 9:50:36

Let's take a look...

Nulshunde: 51: 22

The first step is to assume that the program has no syntax errors, then the entire program corresponds to the Start Node of the grammar.

Some questions: 9:51:36

Well

Nulshunde: 51: 49

The Start Node has one or more candidate types.

Nula9: 52: 03

The candidate formula is determined by the ll1 analysis table.

Some questions: 9:52:23

What is the starting node of the syntax tree?

Nulshunde: 52: 28

Pair

Some questions: 9:52:32

Nun

Nulshunde: 52: 44

The root node represents the entire program.

Some questions: 9:52:47

Well

Null9: 53: 00

Let me use expression as an example.

Some questions: 9:53:04

Well

Nulshunde: 54: 06

ExpressionAs the root node, the first word [(19 + 18) * (19-18)] is "("

Some questions: 9:54:18

Well

Null9: 54: 48

ExpressionThis syntax tree node type corresponds to "(" this word type corresponds to the candidate formula in the ll1 analysis table is <multiply> <plusopt>

Nulshunde: 55: 06

Therefore, the expression root node generates the multiply and plusopt leaf nodes.

Nulshunde: 55: 11

No, it's a subnode.

Some questions

9:55:23

Nun

Nulshunde: 55: 43

Then we continue to analyze multiply.

Nulshunde: 56: 16

MultiplyThis syntax tree node corresponds to "(" the candidate formula in the ll1 analysis table is <unit> <multiplyopt>

Some questions: 9:56:24

Well

Nulshunde: 56: 29

Therefore, multiply generates the unit and multiplyopt subnodes.

Null9: 57: 08

Then, let's look at the unit and (", in the ll1 analysis table corresponding to the candidate formula [" ("<expression> ") "].

Null

9:57:34

Therefore, unit generates the "(", <expression>, ")" subnodes.

Some questions: 9:57:43

Well

Nulshunde: 58: 18

Then, let's look at "(" this syntax tree node is exactly "(" this word type, so "(" It's done, and then we will process the word "19" later

Nulshunde: 58: 45

And so on

Nulshunde: 58: 48

You will understand.

Some questions: 9:58:58

So "(" is a leaf node?

Nulshunde: 59: 04

Pair

Nulshunde: 59: 34

Some questions: 10:01:43

That is to say, every leaf node that can't be processed will be stopped?

Null10: 01: 53

Pair

Some questions: 10:02:58

The child nodes of the root node are multiply and plusopt. What about plusopt?

Null10: 03: 01

"("This syntax tree node is a Terminator, so no child nodes are generated. The previous expressions, multiply, and so on are non-Terminators.

Some questions: 10:03:14

Well, I understand this.

Null10: 03: 30

I just processed "(", the expression below is

Null10: 03: 35

The newly generated expression

Null10: 04: 28

Then you will find that after the expression is processed, you should process ")".

Null10: 04: 46

")"The Unit Terminator corresponds

Null10: 05: 55

Now that the unit node is processed, let's look at the syntax tree. The multiply node is processed, so we need to process the multiplyopt node.

Null10: 09: 27

MultiplyoptWhich candidate formula is corresponding to the "*" Word type in the ll1 analysis table?

Some questions: 10:11:55

It should be * <unit>

Null10: 12: 05

You learned

Null10: 12: 28

Therefore, multiplyopt generates two subnodes: "*" and unit.

Some questions: 10:13:29

But why search for multiplyopt and "*" instead of anything else ..

Null10: 14: 14

Because the preceding code has been processed (19 + 18) * (19-18) (19 + 18 ).

Some questions: 10:14:35

Well, I'll try again ~

Null10: 14: 43

Some questions: 10:15:25

Oh, that is to say, (19 + 18) These leaf nodes have already appeared, right?

Null10: 15: 29

Pair

Some questions: 10:15:57

Well, I understand ~ Do some exercises now ..

Null10: 16: 02

Fuel

Null10: 17: 47

Can I post the chat content to my blog? Easy for others to understand

Some questions: 10:18:20

Nhang ~ But can I just remove the nickname ..

Null10: 18: 28

No problem

Null10: 21: 31

Which blog did you read to ask me?

Some questions: 10:23:41

Http://www.cnblogs.com/bitzhuwei/archive/2013/06/05/CompilerDesignAndImp4Context-freeGrammar.html

Some questions: 10:23:45

This article ..

Null10: 24: 05

It seems that I wrote too poorly,

Some questions: 10:24:40

Should 4 in the ll1 analysis table (3/3) be included in plusopt and?

Null10: 25: 49

Oh, it seems like ah

Null10: 26: 29

Thank you for choosing the wrong one.

Some questions: 10:26:39

No ~ Thank you ~

If you have any questions or want code, you can leave a message on my blog http://bitzhuwei.cnblogs.com with my other contact information on it. Thank you for your harassment ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.