当前位置:网站首页>Fundamentals of programming language (2)

Fundamentals of programming language (2)

2022-04-23 19:43:00 flysh05

3. Basic principles of compiler

The function of the compiler is to translate the source program written in a high-level language into an equivalent target program ( Assembly language Speech or machine language ). The working process of compiler is divided into 6 Stages , As shown in the figure below :

Lexical analysis : Is the first stage of the compilation process paragraph . The task of this stage is from left to right Read character by character into the source program , That is to scan the character stream constituting the source program Then recognize the word according to the word formation rules ( Also known as single A word or symbol ).

Syntax analysis : Is a logical phase of the compilation process . The task of grammar analysis is to combine word sequences into various types on the basis of lexical analysis Grammatical phrases , Such as “ Program ”,“ sentence ”, “ expression ” wait . The parser judges the source Whether the program is structurally correct

Semantic analysis : Is a logical phase of the compilation process . The task of semantic analysis is to analyze the structurally correct source program Conduct a review of a context sensitive nature , Conduct type review . Such as type matching 、 The divisor of division is not 0 etc. . It is also divided into static semantic errors ( It can be found in the compilation stage ) And dynamic semantic errors ( Can only be issued at run time present ).

Intermediate code and object code generation : Intermediate code is generated according to semantic analysis , Need optimized Links , Finally generate executable object code . The purpose of introducing intermediate code is to optimize the code independent of the machine . The commonly used intermediate code has suffix ( Reverse Polish )、 Ternary form ( Three address codes )、 Quaternions and trees . Three questions need to be considered ( One is how to generate shorter object code ; The second is how to make full use of Computing Registers in the machine , Reduce the number of accesses to the storage unit ; The third is how to make full use of computer instructions Characteristics of the system , To improve the quality of object code ).

To master the above three expressions , In fact, it is three kinds of traversal of trees , Generally, the normal expression is middle order traversal Infix expression , Construct a tree based on it , Then according to the requirements of the topic, find the prefix or suffix .

Simple solution : The suffix expression starts from left to right , First put the expression in parentheses , Then add the operators to the brackets of this level in turn .

3. Grammar defines

A formal grammar is an ordered quadruple G=(V,T,S,P), among :

1)V: non-terminal . Not part of language , Not the end result , Can be understood as placeholder .

2)T: Terminator . Is an integral part of language , Is the end result .V⌒T=θ

3)S: Start character . Is the beginning symbol of language .

4)P: Generative formula . The rule of replacing a nonterminal with a Terminator . Form like α->β

Terminator : final result , Other elements cannot be derived .

non-terminal : Can deduce other elements .

Generative formula : That is, the formula of the terminator derived from the non Terminator .

Closure , The concept is as follows , Generally, the closure can be 0 The case of is substituted into the operation :

Regular closure :A+=A1UA2UA3U…UAn U…( That is, the combination of all powers ).

Closure :A*=A0UA+( On the basis of regular closure , add A0={ε}).

for example a={a,aa,aaa,…,ε}, and (ab)*={ab,abab,ababab,.,ε}

4. Grammar type

5. Normal form

For the alphabet ∑, The normal expression on it and the normal set it represents can be recursively defined as follows .

(1) ε It's a normal form , It represents a collection L( ε)={ ε}.

(2) if a yes ∑ Characters on , be a It's a normal form , The normal set it represents is {a}.

(3) If normal r and s Represent normal set respectively L and L(s), be :

①r|s It's normal , Represents a collection L U L(s).

②r·s It's normal , Represents a collection LL(s).

③r’ It's normal , Represents a collection (L(r)).

④ It's normal , Represents a collection L.

6. Finite automaton

Deterministic finite automata and uncertain finite automata : Enter a character , Look yes Whether we can get the only successor , If you can , Is certain , Otherwise, it is uncertain if multiple successors are obtained

7. Grammar analysis method

Top down parsing : Leftmost inference , From left to right . A given grammar G And source program string r. from G The start sign of S set out , By way of anti Use production to replace the non terminator in the sentence pattern ( deduction ), Gradually deduce r.

Recursive descent thought : The principle is to use recursive calls between functions to simulate the top-down construction process of syntax tree , It's a top-down Grammar analysis method .

Bottom up parsing : Rightmost derivation , right to left . Start with the given input string , Keep looking for substrings and grammar G A certain property in Life style P Match the candidates of , And use P Replace the left part of ( reduction ) And , Gradually reduce to the starting symbol S.

Move into conventional thinking : A stack setting , Move the input symbols into the stack one by one , When the top of the stack forms the right part of a production , Just use the left to Instead of , It's called reduction . Obviously , The idea is to deduce the left from the right , Therefore, it is the core idea of bottom-up grammar analysis .

版权声明
本文为[flysh05]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231941311726.html