Four quiz compiler theory work - sort of knowledge of grammar and language

1. carding second chapter, understand and write a summary.

The second chapter, we learn the grammar and language

1) First, we have to first look at the concept of grammar. Similar to the Chinese usually say, there are also the grammar. The simple phrase "I am a university student," contains the subject, predicate, and object, expressed in EBNF if this is as follows

  <Sentence> :: = <subject> | <predicate>

  <Subject> :: = <pronouns> | <noun>

  <Pronouns> :: = me | you | he | it

  <Noun> :: = Teachers | Students | .....

  <Predicate> :: = <verb> | <direct object>

  <Verb> :: = a | learn .....

  <Direct object> :: = <pronouns> | <noun>

There are many components of a sentence, the sentence in line with these rules in order to become a legal sentence structure. Such rules and description of the language we call grammar

2) Rules

Rules also called rewrite rules, production or formula. For chestnut, there is such a rule A -> a, which is called an A on the production. In this we have to mention grammar quad

Define a grammar G is a four-tuple grammar (V N , V T , P, S), wherein

V N non-terminal symbol set; V T is the set of terminal symbols; P is a set of rules or productions; S or an identifier called the start symbol. V N , V T , takes a non-finite set P!

for example

Given a grammar = G (V N , V T , P, S), V N = {S}, V T = {0,1}, P = {S -> 0S1, S -> 01}, S is the start symbol

S may then generate the next generation can 0S1,0S1 00S11, may then go on to generate a non-S is terminated,

When S -> 01 expression when it ended, so called 0,1 terminator, the above formula is generated equation, S is the start symbol would mean needless to explain, in literally

3) Derivation

To a grammar (as above) can be continuously expanded to give the corresponding language, such as described above is a 0n1n grammar language, this process is also called derivation

Using the example above G (S): S -> 01 | 0S1

0S1 -> 0011 in one step, replace the left nonterminal become the ultimate symbol form on the right, we called a direct derivation, also known as derivation step

Leftmost derivation: if every priority to replace the leftmost nonterminal in derivation, it is the most left

Rightmost derivation: is the most left turn, always first for the rightmost nonterminal.

For example, there grammar: find i + i

       G (E):

  E=> E + T | T

  T=>T * F | F

  F=>(E)| i

最左:E --> E+T--->T+T--->F+T--->i+T -->i+F -->i+i

最右:E -->E+T -->E+F -->E+i -->T+i -->F+i -->i+i

4) analysis of tree

Parse tree, also known as the parse tree, is an illustration of the derivation method is to have an ordered tree, and therefore has a property tree.

Or the example above, to find i * i + i

最左推导:E=>E+T=>E+F=>E+i=>T+i=>T*F+i=>T*i+i=>F*i+i=>i*i+i

最右推导:E=>E+T=>T+T=>T*F+T=>F*F+T=>i*F+T=>i*i+T=>i*i+F=>i*i+i

Derivation Tree:

             

As above, each leaf node in sequence, can constitute a sentence. Wherein each child node down to constitute a phrase, and the like F -> i Such a direct phrase, the phrase called direct leftmost handle

And can be seen from the above example, the leftmost and rightmost derivation deduced derivation tree is the same!

5) the type of grammar

 

 

2. Try to write grammar PL / 0 language. (Or do you think the better language rules)

Integer n n -> 0 | 1 | 2 | 3 | ...... | 8 | 9

Identifier i i-> a | b | c | ...... | y | z | A | B | C | ...... | Y | Z (<letter> {<letters> | <number>})

Expression e :: = [+ | -] <item> {<subtraction operator> <term>}

Conditional statement :: = if <condition> then <statement>

Assignment :: = <id>: = <expression>

Compound statement :: = begin <statement> {; <statement>} end

= Function :: = <main function>

Program :: = <Block> :: = [<Constant Description section>] [<Variable Description section>] [<Process Description section>] <statement>

 

Guess you like

Origin www.cnblogs.com/xiaoAP/p/11597703.html