Syntax and parse tree:
Let's take a look at the syntax parsing process. Sentence: "I see Andy Lau singing." In the computer, how to use the program to parse it. From the grammatical perspective, the composition of the sentence consists of subject, verb, and predicate clauses, the subject is "I", the verb is "see", the predicate clause is "Andy Lau sings". Thus a sentence can be decomposed into a subject + verb + predicate clause:
Sentence--subject + verb + predicate clause
The subject is a noun, so there are:
noun, subject
The noun in the sentence has: "I", "Andy Lau", so there are analytic rules:
Noun-"Me" | "Andy Lau".
The verb in the sentence is "see", "sing", thus have the analytic rule:
Verb--"see" | "Sing"
Then look at the predicate clause, the predicate clause consists of the object and the predicate verb, the object is "Andy Lau", the predicate verb is "singing", the analytic rule of the predicate clause is:
predicate clause, object + predicate verb
The predicate verb belongs to the verb, and then there are:
Verbs, verb, verb, predicate
Verb-"See" | Singing
Thus, the parsing rules for the whole sentence are:
1. Sentence--subject + verb + predicate clause
2. Predicate clause, object + predicate verb
3. Noun, subject
4 Verb-verb
5. Verb-"See" | "Sing"
6. "I" | "Andy Lau".
The above-mentioned set of rules is used to parse sentences in the computer algorithm, next we have a series of substitution, from this set of rules to restore back to the sentence, first from the first rule, with the right side of the formula to replace the left symbol,
1. Sentence through rule: sentence-to-subject + verb + predicate clause substitution to get:
2. Subject + verb + predicate clause, which is replaced by the noun by the rule subject:
3. Noun + verb + predicate clause, by rule noun-"I" | "Andy Lau" was replaced by
4. i + verb + predicate clause, through the regular verb--"see" Replaced by:
5. I see the + predicate clause, which is replaced by the object + predicate verb by the rule predicate clause:
6. I see the object + predicate verb, which is replaced by a noun by the rule object:
7. I see noun + predicate verb, through the rule noun-"I" | "Andy Lau" was replaced by:
8. I see Andy Lau + predicate verb, which is replaced by a verb, a regular predicate verb:
9. I see Andy Lau verbs. Replaced by a regular verb, "singing"
10 I saw Andy Lau singing
At this point, we have no place to replace, so the parsing is done. Thus, syntax parsing is the process of establishing a set of rules and then judging whether the input text conforms to a given rule. We see that some of the rules at the bottom are this:
Noun-"Me" | "Andy Lau", verb--"see" | "Sing"
These rules, in fact, the previous (http://blog.csdn.net/tyler_download/article/details/50668983) said lexical analysis,-> left is the label, the right is the lexical analysis of the string. Throughout the parsing process, a tree structure is formed, called the syntax parsing tree:
Imagine, the text composed of words, its form is infinite, the rule of grammar parsing is to be infinite text, select the combination form conforms to the grammatical rules of the text, for example, for the above syntax, sentence: "I see Jacky sang" Can not pass the grammar rules, according to the above substitution process, we found that the 7th step to the object , the object can not be replaced with a noun after the word "Jacky", so "I see Jacky sang" for the above grammatical rules, is illegal input.
Of course, the grammar rules are limited to the text input is not unique, sentence: "Andy Lau saw me singing" also conforms to the above grammar rules, we can imitate the above replacement process to verify.
If you want to recognize the grammar "I see Jacky Cheung sing", then just change the rules: noun-"I" | "Andy Lau" | "Jacky" can be.
Let's take a look at how the above substitution process turns into a computer pseudo code:
Assuming "I see Andy Lau singing" in the sentence that exists in buffer buffers, the code is stated as follows:
Sentence (buffer) {
Replace the sentence with the subject + verb + predicate clause
Subject (buffer);
verb (buffer);
predicate clause (buffer);
}
Subject (buffer) {
Noun Replacement subject
noun (buffer);
}
noun (buffer) {
"I" | "Andy Lau" replace the noun
if (buffer[0] = = "I") {
Buffer = buffer.substring (1);
Return
}
if (buffer[0,1,2] = = "Andy Lau") {
Buffer = buffer.substring (3);
Return
}
throw new Exception ("the statement does not conform to the syntax");
}
Verb (buffer) {
"See" | "Singing" Replacement verb
if (buffer[0,1]== "See" | | buffer[0,1] = = "Sing") {
Buffer = buffer.substring (2);
Return
}
throw new Exception ("the statement does not conform to the syntax");
}
predicate clause (buffer) {
The object predicate verb replaces the predicate clause
object (buffer);
predicate verb (buffer);
}
Object (buffer) {
Noun Substitution Object
noun (buffer);
}
predicate verb (buffer) {
Verb to replace a predicate verb
verb (buffer);
}
In the next article, we'll look at how to make a set of grammar rules and corresponding syntax substitution codes for arithmetic expressions with good and multiplication sign.
Using Java to implement a simple compiler-syntax parsing