Make

xiaoxiao2021-03-06  76

Define some

This section includes c code (2 lines to 53 rows) that directly enter the output file, and information to establish a marking, data structure, and grammar rules for the analysis program (56 lines to 66 lines).

16 - 23 The global variables here will be assigned to the parsing, these values ​​are used to generate the LPC syntax when executing Edit_Source -Build_efuns, and automatically generates some of the headers needed to compile MUDOS.

37 - 51 LPC support for various data types.

56 - 59 By default, the return value of the Actions and the lexical analysis is an integer. In order to allow the parser to support multiple data types, define a Union, in the file Make_func.tab.c after the Bison program process, this Union is defined:

Typedef union {

Int number;

CHAR * STRING;

} yystype;

62 Define the EBNF end symbol.

64 Define non-end symbols. It returns the value related data type is INT. The ID appears in the end symbol declaration and is still a terminal.

66 non-end symbols. Its return value related data type is char *. NUM appears in the end symbol declaration and is still a terminal.

If the 61 line to 65 lines are declared in this way, the file make_func.tab.c processed by Bison is exactly the same:

% token ID

% Token Num

% Token Default Operator

% Type Type Arg_list Basic Typel Arg_Type Typel2

% Type optional_id optional_default

lexical analysis

This section defines how the resolution is analyzed from the source text. This work can be handed over to Flex or Lex, but since this resolution program is relatively simple, custom lexical analysis code (328 lines to 414 lines).

330 lexical analysis function must be declared as int yylex ()

334 yyn declares in edit_source.c, streaming for FUNC_SPEC.CPP.

338 - 340 Recording the line number of the source file.

341 - 347 When a source of the source file is "!", It means that the resolution file failed. This situation may appear in the case of linked two or three memory allocation function libraries, see Macros.h93 line to 95 lines:

#if (defined (Sysmalloc) Defined (Smalloc) Defined (BSDmalloc)> 1

! Only One Malloc Package Should Be Defined

#ENDIF

Execute the edit_source -build_func_spec "gcc -e" instruction When you get the source code list of FUNC_SPEC.C, you may include "! Only One Mall Package Should Be defined" line in FUNC_SPEC.CPP.

348 - 363 When the source file is started with "#", the file name and the line number will be saved to current_file and current_line, which is similar to the precompilation instruction #LINE. By default, when the error is found, the report is FUNC_SPEC. CPP file name and line number, if the resolution text is resolved to the line starting with "#", such as "# 1" op_spec.c "

1"

Keep the resolution error, the report is the first line of "op_spec.c", which will guarantee the correctness of the error position.

364 - 366 encountered file end sign, parsing, returning -1.

368 - 364 Western to the numbers and return Num termination symbols.

385 - 387 parsing one of the characters, returns ID, default, and operator.

397 - 403 Error handling of the indicator.

406 - 407 If you match the string "default", return the value of default, this value appears in the file Make_FUCN.TAB.C after Bison processing: "# Define default

259 "

408 - 409 If you match the string "Operator", return the value of Operator, this value appears in the file Make_Fucn.tab.c after Bison processing: "# Define Operator

260 "

411 - 413 Other strings will be treated as ID, their own values, such as "to_int" is pressed into Yylval.String, this value is $ N in the Action code, see 81 rows and 194 lines . In the defined section, the ID-related data type is char *.

Rule section

This section includes EBNF grammar rules, as well as the Action code portion (69 lines to 231 rows) that is performed when identifying the relevant grammat rules.

Sentence type example of to be parsed:

Operator Add, Subtract, Multiply, Divide, Mod, And, OR, XOR, LSH, RSH;

INT CALL_OUT (String | Function, Int, ...);

Remove the relevant Action code, the rest of the EBNF grammar rules are as follows:

SPECS à / * Empty * /

| SPECS SPEC;

Spec à Operator

| func;

Operator à operator op_list ';';

Op_list à op

| op_list ',' OP;

Op à id;

Func à type id optional_id '(' arg_list optional_default ')'; ';

OPTIONAL_ID à id

| / * Empty * /;

OPTIONAL_DEFAULT à / * EMPTY * /

| Default ':' Num

| Default ':' id;

Type à Basic

| Basic '*';

Basic à id;

Arg_list à / * Empty * /

| Typel2

| arg_list ',' Typel2;

Typel2 à Typel;

Arg_type à type;

Typel à arg_type

| TYPEL '|' Arg_Type

| '.' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '

73 - 91 Looking for a sentence: "Operator ID (, ID) * /;" The value of the ID will be saved in Oper_codes while logging the number of OPERATORs OP_CODE.

93 - 231 Search: "(ID | ID / *) ID [ID] {0,1} / (..." (I don't know how to write L, road is long)

93 When EMPTY, the value of optional_id is "", otherwise the value of the ID is returned directly.

95 When EMPTY, the value of Optional_Default is "default_none".

96 - 101 Matching sentence type "Default: Num", convert NUM's value to string, so that the value of Optional_Default is this String. 102 - 107 Matching sentence type "default: id", if the value of the ID is not "f_this_object", resolving the failure, otherwise, the value of Optinal_Default is "default_this_object"

187 - 231 describes the rules of Arg_List, which is more complicated, sorted into the table below, the first column in the table represents various sentence patterns of Arg_List, the first behavior program has a global variable and the value of arg_list itself, other The value of the ranks represents the value of these global variables in the case of arg_list matches different sentence patterns, and the reason is summarized because it is used in the ACTION function in the Func:

arg_list min_arg limited_max curr_arg_type_size / * Empty * / 0 -1 0 0 ID (ID! = void) 1 -1 0 1 ID (ID == VOID) 1 0 0 0 ID | ID 1-1 0 2 ID | ID (some ID == VOID) 1 0 0 1

Id

,

Id

2 -1 0 3

Id

,

Id

(Id == VOID) 1 0 0 2 ... 1 -1 1 0 ID, ... 2 1 1 1 ... (omit)

Now analyze the meaning of each global variable.

Arg_list: The number of function parameters.

Min_arg: The minimum number of parameters in the function, this quantity is determined by "VOID" or "..." in the parameter.

LIMIT_MAX: Indicates whether the number of parameters is limited, is not limited to 1, there is a maximum parameter limit when 0, which is limited to the number of arg_list.

CURR_ARG_TYPE_SIZE: This parameter is related to arg_list, if it is a comma-separated parameter list, then there must be a 0 as a separator between the two parameters, so if it is ID, ID, the value of curr_arg_type_size is 3; if an ID is void, It will not be calculated.

114 - 116 If the value of min_arg has been maintained in -1 in the lexical analysis of Arg_List, then the value of the am_arg is 4, exceeds the error, the program exits.

117 - 139 If optional_id is null, the f_name and efun1_code or EFUN_CODE assign values, the number of the parameters is the number of parameters, if the single parameter is assigned to EFUN1_CODE, others will assign a value to efun_code.

139 - 149 If optional_ID is not null, for example: Object this_user this_player (int default: 0); this means that this function is an alias, f_name = "f_this_player | f_alias_flag"

150 - 167 Assigning the parameter list curr_arg_types to arg_type, but if the consecutive element in curr_arg_type is exists, it will not be assigned. This point, guarantee, parameter list uniqueness, see curr_arg_types and arg_types Note: Store the Types of the current efun. The Type BE Copied Into the arg_types list if it were not already the the aere (to save memory) .167 - 170 Eligible source files are only: unknown call_other (Object | String | Object *, string | mixed *, ...); if you encounter such a sentence type, turn the value of TYPE, or $ 1 is changed to Mixed.

171 - 174 Assignment to BUFF, its assignment statement can be interpreted as: ID, f_name, 0, 0, min_arg, limit_max? -1: arg_list, type! = Void? Ctype (type): "type_novalue", ETYPE (0), ETYPE (1), Eytpe (2), ETYPE (3), I, OPTIONAL_DEFAULT

178 - 180 Additional BUFF to the BUF array.

235 - 261 A return value data type for EFUNS is obtained.

264 - 326 Number of data types. Since the maximum value of EFUNS is 4, only 4 ETYPE functions are required to assign a value on the 171 - 174 line, if the number of parameters is less than 4, then the ETYPE returns to T_Any.

Reference:

[1] Kenneth C. Louden. Feng Boqin, Feng Wei (translation). "Compilation Principle and Practice". Mechanical Industry Press

[2] Stephen C. Johnson. Yacc: yet another compiler-compiler

[3] YACC and Lex Quick Start:

http://www-900.ibm.com/developerWorks/cn/linux/SDK/LEX/index.shtml

[4] Bison Manual:

http://tinf2.vub.ac.be/~dvermeir/courses/compilers/bison-1.25/bison_toc.html

转载请注明原文地址:https://www.9cbs.com/read-121061.html

New Post(0)