Fundamentals of programming languages and language processing programs

Chapter 7, Basics of Programming Languages ​​and Language Processing Programs

1. Compilation and interpretation

The compiler converts the entire high-level language program into the machine code of the target machine at one time, and the result of compilation is aexecutable binary

The interpreter interprets the high-level language program line by line, and dynamically generates machine code during the interpretation process. The result of the interpretation isdirect program execution

Please add a picture description

Lexical Analysis:

Definition: Lexical analysis is the first stage of the compiler, and its task is to decompose the source code into a series of lexical units (Token).
Function: The lexical analyzer scans the source code character stream, recognizes the string of characters as a lexical unit with independent meaning, and generates the corresponding token (Token).
Scan the characters in the source program one by one from left to right, and recognize such asKeywords (or reserved words), identifiers, constants, operators, and delimiters (punctuation and parentheses)wait.
For the source code int x = 10 + y; in C language, the lexical analyzer can decompose it into the following morpheme sequence:

Morpheme 1: Keyword "int"
Morpheme 2: Identifier "x"
Morpheme 3: Operator "="
Morpheme 4: Constant "10"
Morpheme 5: Operator "+"
Morpheme 6: Identifier "y"
Morpheme 7: Semicolon ";"
Syntax Analysis:

Definition: Parsing is the second stage of the compiler, and its task is to verify whether the structure of the sequence of lexical units conforms to the grammatical specification according to the grammatical rules.
Function: The syntax analyzer constructs a syntax tree (Syntax Tree) or an abstract syntax tree (Abstract Syntax Tree) according to the given grammar rules to represent the structure of the program.

Decompose word symbols into various grammatical units according to grammatical rules, and analyzeWhether the source program has grammatical errors. Including: errors in language structure,if...end if does not matchmissing semicolonbracket mismatch, expressions missing operands, etc. This question belongs to the role of the grammatical analysis stage.
Semantic Analysis:

Definition: Semantic analysis is the third stage of the compiler. Its task is to check and deduce the semantic meaning of lexical units and syntax trees, perform type checking and semantic rule verification.
Function: The semantic analyzer performs semantic checks on the program, includingType matching, variable declaration and usage checks, function call checksetc. to ensure that the program is semantically correct.

carry out typeanalysis and inspection, mainly to detect whether the source program existsstatic semantic error. include:The operator does not match the operation type, such as using a floating-point number when taking the remainder.
Example:
For the statement int x = 10 / "5";, the semantic analyzer will detect a type mismatch error because integers cannot be divided by strings Object Code Generation
(Code Generation):

Definition: Object code generation is the last stage of the compiler, whose task is to convert the semantically analyzed intermediate representation into the machine code or assembly code of the target machine.
Function: The target code generator applies optimization techniques based on intermediate representations (such as syntax trees, three-address codes, etc.) to generate executable codes related to the target machine.

2, Grammar

A formal grammar is an ordered quadruple G=(V, T, S, P), where:
1) V: non-terminal. Not part of the language, not the end result, it can be understood as a placeholder.
2) T: terminator. It is an integral part of language, and it is the end result. V∩T=∅
3) S: Start character. is the start symbol of the language.
4) P: production formula. Rules for substituting terminals for non-terminals. Shape like α→β

insert image description here
A syntax tree should have the following characteristics:
1. Each node has a label, which is a symbol of V
2. The label of the root is S
3. If a node n has at least one descendant except itself, And there is a mark A, then A must be in Vn:
4. If the direct descendants of node n, the order from left to right is node n1, n2, ... nk, and their marks are: A1, A2, ..., Ak, then A->A1, A2...Ak must be a production in P.
insert image description here

3. Formal

insert image description here

4. Finite Automata

insert image description here
insert image description here
Example: The figure below shows a finite automaton (where A is the initial state and C is the final state), which can recognize (1).

insert image description here

5. Expression

insert image description here
Prefix expression (+ab)
Infix expression (a+b)
Postfix expression (ab+)

Example: the suffix of the expression (ab)*(c+5) is

A.a b c 5 + * -
B.a b - c + 5*
C.a b c - * 5 +
D.a b - c 5 + *

insert image description here

6. Passing by value and address

insert image description here
insert image description here
pass by value

void swap(int x,int y)
{
    
    
	int t;
	t=x;
	x=y;
	y=t;
	printf("%d%d",x,y);
}
void main()
{
    
    
	int a=3,b=4;
	swap(a,b);
	printf("%d%d",a,b);
}
4 3,3 4

address passing

void swaps(int *x,int *y)
{
    
    
	int t;
	t=*x;
	*x=*y;
	*y=t;
	printf("%d %d",*x,*y);
}
void main()
{
    
    
	int a=3,b=4;
	swaps(&a,&b);
	printf("%d %d",a,b);
}
4 3,4 3

1.Fortran language (scientific computing, high execution efficiency)
2.Pascal language (developed for teaching, strong expressive ability, Delphi)
3.C language (strong pointer operation ability, high efficiency)
4.Lisp language (functional programming language , symbol processing, artificial intelligence)
5. C++ language (object-oriented, efficient)
6. Java language (object-oriented, intermediate code, cross-platform)
7. C# language (object-oriented, intermediate code, .Net)
8. Prolog language ( logical reasoning, conciseness, expressiveness, databases and expert systems)

7. Features of multiple programming languages

Guess you like

Origin blog.csdn.net/qq_52108058/article/details/130623363