JP2011113148A - Macro development method and preprocessor - Google Patents

Macro development method and preprocessor Download PDF

Info

Publication number
JP2011113148A
JP2011113148A JP2009266740A JP2009266740A JP2011113148A JP 2011113148 A JP2011113148 A JP 2011113148A JP 2009266740 A JP2009266740 A JP 2009266740A JP 2009266740 A JP2009266740 A JP 2009266740A JP 2011113148 A JP2011113148 A JP 2011113148A
Authority
JP
Japan
Prior art keywords
macro
token
buffer
call
macro call
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Granted
Application number
JP2009266740A
Other languages
Japanese (ja)
Other versions
JP5385103B2 (en
Inventor
Isamu Hasegawa
勇 長谷川
Ayataro Kimura
綾太郎 木村
Shunsuke Yamashiro
俊介 山城
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Nomura Research Institute Ltd
Original Assignee
Nomura Research Institute Ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Nomura Research Institute Ltd filed Critical Nomura Research Institute Ltd
Priority to JP2009266740A priority Critical patent/JP5385103B2/en
Publication of JP2011113148A publication Critical patent/JP2011113148A/en
Application granted granted Critical
Publication of JP5385103B2 publication Critical patent/JP5385103B2/en
Expired - Fee Related legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Landscapes

  • Devices For Executing Special Programs (AREA)

Abstract

<P>PROBLEM TO BE SOLVED: To provide a macro development method allowing flexible and simple mounting of macro development processing including development of a multistage macro call in a preprocessor. <P>SOLUTION: The preprocessor executes: step S102 of cutting out a first token from a character string of a source code and generating token arrangement; step S104 of sequentially reading the first tokens into a buffer from the token arrangement; step S109 of performing output as a processing result when the first token inside the buffer is not a part corresponding to the macro call; step S111 of replacing the part corresponding to the macro call by contents of a corresponding macro definition and performing the development, when the first token is the part corresponding to the macro call and is completed as the macro call; step S112 of cutting out a second token from a character string obtained by developing the macro call; and step S113 of returning the second token to the head of the toke arrangement of input. <P>COPYRIGHT: (C)2011,JPO&INPIT

Description

本発明は、プログラミング言語などによって記述されたソースコードに対するマクロ展開の技術に関し、特に、多段のマクロ展開処理を含むマクロ展開方法およびプリプロセッサに適用して有効な技術に関するものである。   The present invention relates to a macro expansion technique for source code described in a programming language or the like, and more particularly to a technique effective when applied to a macro expansion method and a preprocessor including a multi-stage macro expansion process.

プログラミング言語などによって記述されたソースコードに対しては、一般的に、コンパイラによって字句解析や構文解析、意味解析などが行われ、その後、最適化などの処理を経てオブジェクトコードが生成される。ここでコンパイラにおいては一般的に、ソースコードに対して字句解析を行う前に、プリプロセッサによって、ソースコード内のマクロ呼び出しを、ソースコードやヘッダファイルなどに定義されているマクロ定義によって展開するマクロ展開処理を含む前処理が行われる。   In general, a compiler performs lexical analysis, syntax analysis, semantic analysis, and the like on a source code described in a programming language, and then generates an object code through processing such as optimization. Here, in general, in a compiler, before performing lexical analysis on the source code, the macro expansion in which the macro call in the source code is expanded by the macro definition defined in the source code or the header file by the preprocessor. Preprocessing including processing is performed.

ここで、マクロ呼び出しには、例えば、マクロ呼び出しにおける入力パラメータがさらにマクロ呼び出しによって記述されているなど、マクロ呼び出しが多段のネスト構造で記述されている場合がある。   Here, in the macro call, there are cases where the macro call is described in a multi-stage nested structure, for example, an input parameter in the macro call is further described by the macro call.

このとき、プリプロセッサでは、マクロ呼び出しを展開する際に、一般的には、例えば非特許文献1に記載されているように、マクロ呼び出しを展開した後、新たに展開されたテキストに対してさらにマクロ呼び出しがあるか否かのスキャンを行うことで、多段のネスト構造となっているマクロ呼び出しの展開を可能としている。なお、このような多段のマクロ呼び出しにおいて、ネストの内側から展開するか、外側から展開するかのいずれを優先させるかはコンパイラによって異なる場合がある。   At this time, when the preprocessor expands the macro call, generally, as described in Non-Patent Document 1, for example, after expanding the macro call, the macro is further expanded with respect to the newly expanded text. By scanning whether or not there is a call, it is possible to expand a macro call having a multi-level nested structure. In such a multistage macro call, it may differ depending on the compiler whether to give priority to expansion from the inside of the nest or from the outside.

“RAD Studioオンラインヘルプ C++リファレンス”、[online]、EMBARCADERO TECHNOLOGIES、[平成21年11月2日検索]、インターネット<URL:http://docwiki.embarcadero.com/RADStudio/ja/Define>“RAD Studio Online Help C ++ Reference”, [online], EMBARCADERO TECHNOLOGIES, [Search November 2, 2009], Internet <URL: http://docwiki.embarcadero.com/RADStudio/en/Define>

ここで、例えば、ソースコードの記述がプログラミングの規約・ルール等に沿っているか否かなどをチェックするコードチェックツールを考えた場合に、当該コードチェックツールは、一般的にコンパイラと同様のマクロ展開や、字句解析、構文解析などの処理を行い、その処理結果に対して種々の解析を行う機能を備えることになる。このとき、例えば、コードチェックツールのプリプロセッサは、当該ソースコードから実行可能プログラムを生成するために利用される実環境のコンパイラのプリプロセッサと同様の処理仕様を有していることが望ましい。   Here, for example, when considering a code check tool that checks whether or not the source code description conforms to programming rules and rules, the code check tool is generally expanded in the same way as a compiler. In addition, processing such as lexical analysis and syntax analysis is performed, and a function of performing various analyzes on the processing results is provided. At this time, for example, the preprocessor of the code check tool desirably has the same processing specifications as the preprocessor of the compiler in the real environment used for generating an executable program from the source code.

しかしながら従来のプリプロセッサでは、例えば、上述したような多段のマクロ呼び出しの展開の優先順について、これを変更可能とするような実装はされていない。従って、コードチェックツール等におけるプリプロセッサでは、多段のマクロ呼び出しの展開における優先順について、実環境のコンパイラの仕様に応じて適宜変更することが可能であるように柔軟に実装する必要がある。   However, in the conventional preprocessor, for example, the priority order of the expansion of the multi-stage macro call as described above is not implemented so that it can be changed. Therefore, a preprocessor in a code check tool or the like needs to be mounted flexibly so that the priority order in the development of multi-stage macro calls can be changed as appropriate according to the specifications of the compiler in the real environment.

またその際に、例えば、プリプロセッサに対する実行時の処理オプションの指定によって優先順に基づく処理の内容を切り替えられるようにするという手法などが考えられるが、切り替えによって実行される各処理は、プリプロセッサの開発・保守の生産性などを考慮すると、可能な限り簡素で、処理の重複も少なく効率的に実装できるのが望ましい。   At that time, for example, there is a method of switching the processing contents based on the priority order by specifying the processing option at the time of execution for the preprocessor. Considering the productivity of maintenance, it is desirable to be as simple as possible and to be efficiently implemented with less processing overlap.

一方、従来のプリプロセッサにおける多段のマクロ展開の処理方法自体についても、上述したように、マクロ呼び出しを展開した後、展開後のテキストに対してさらにマクロ呼び出しがあるか否か(マクロ展開が可能か否か)のスキャンを行うというのが一般的であり、プリプロセッサでの実装は効率性や柔軟性に乏しいものである。   On the other hand, as for the processing method itself of the multistage macro expansion in the conventional preprocessor, as described above, after expanding the macro call, whether or not there is a further macro call for the expanded text (whether macro expansion is possible). Scan) is generally performed, and pre-processor implementation is poor in efficiency and flexibility.

そこで本発明の目的は、プリプロセッサにおいて多段のマクロ呼び出しの展開を含むマクロ展開処理を柔軟かつ簡素に実装することができるマクロ展開方法および当該マクロ展開方法を実装したプリプロセッサを提供することにある。本発明の前記ならびにその他の目的と新規な特徴は、本明細書の記述および添付図面から明らかになるであろう。   SUMMARY OF THE INVENTION An object of the present invention is to provide a macro expansion method capable of flexibly and simply mounting macro expansion processing including expansion of multi-stage macro calls in a preprocessor, and a preprocessor equipped with the macro expansion method. The above and other objects and novel features of the present invention will be apparent from the description of this specification and the accompanying drawings.

本願において開示される発明のうち、代表的なものの概要を簡単に説明すれば、以下のとおりである。   Of the inventions disclosed in this application, the outline of typical ones will be briefly described as follows.

本発明の代表的な実施の形態によるマクロ展開方法は、コンピュータプログラムによりソースコード内のマクロ呼び出しをマクロ定義によって展開するマクロ展開方法であって、以下のステップを実行することを特徴とするものである。   A macro expansion method according to a representative embodiment of the present invention is a macro expansion method for expanding a macro call in a source code by a macro definition by a computer program, and is characterized by executing the following steps. is there.

すなわち、前記コンピュータプログラムは、前記ソースコードの文字列から字句解析により第1のトークンを切り出してトークン配列を生成するステップと、前記トークン配列を入力として、前記トークン配列から順に前記第1のトークンをバッファに読み込むステップと、前記バッファ内の前記各第1のトークンについて前記マクロ呼び出しに該当する部分であるか否かを判定し、前記マクロ呼び出しに該当する部分ではない場合に、前記バッファ内の前記各第1のトークンを処理結果として出力して前記バッファをクリアするステップとを実行する。   That is, the computer program generates a token array by cutting out a first token from a character string of the source code by lexical analysis, and inputs the token array in order from the token array with the token array as an input. A step of reading into the buffer, and determining whether each of the first tokens in the buffer is a part corresponding to the macro call, and if not, the part in the buffer Outputting each first token as a processing result and clearing the buffer.

さらに、前記バッファ内の前記各第1のトークンが前記マクロ呼び出しに該当する部分である場合に、前記バッファ内の前記各第1のトークンの全部または一部が前記マクロ呼び出しとして完結しているか否かを判定し、前記マクロ呼び出しとして完結している場合に、前記マクロ呼び出しとして完結している部分を対応する前記マクロ定義の内容によって置換して展開するステップと、前記マクロ呼び出しを展開した文字列から字句解析により第2のトークンを切り出すステップと、切り出した前記各第2のトークンを、入力の前記トークン配列の先頭に戻し、前記バッファにおいて前記マクロ呼び出しを展開した部分をクリアするステップとを実行することを特徴とするものである。   Further, if each of the first tokens in the buffer corresponds to the macro call, whether or not all or part of the first tokens in the buffer are completed as the macro call. And when the macro call is completed, a step of replacing the portion completed as the macro call with the content of the corresponding macro definition and expanding, and a character string obtained by expanding the macro call A step of cutting out a second token by lexical analysis from the above, and a step of returning each cut out second token to the beginning of the token array of the input and clearing a portion where the macro call is expanded in the buffer It is characterized by doing.

本願において開示される発明のうち、代表的なものによって得られる効果を簡単に説明すれば以下のとおりである。   Among the inventions disclosed in the present application, effects obtained by typical ones will be briefly described as follows.

本発明の代表的な実施の形態によれば、マクロ展開によって生成された文字列を入力文字列に戻すことで、プリプロセッサにおけるマクロ展開の処理アルゴリズムを簡素化し、マクロ展開処理の柔軟性やプリプロセッサの開発・保守の生産性などを向上させることが可能となる。   According to the representative embodiment of the present invention, the character string generated by the macro expansion is returned to the input character string, thereby simplifying the macro expansion processing algorithm in the preprocessor, the flexibility of the macro expansion processing, the preprocessor Development and maintenance productivity can be improved.

本発明の一実施の形態であるプリプロセッサにおけるマクロ展開処理の例を示したフローチャートである。It is the flowchart which showed the example of the macro expansion | deployment process in the preprocessor which is one embodiment of this invention. 本発明の一実施の形態における多段のマクロ展開の処理例を示した図である。It is the figure which showed the example of a process of the multistage macro expansion | deployment in one embodiment of this invention. 本発明の一実施の形態における多段のマクロ展開の別の処理例を示した図である。It is the figure which showed another processing example of the multistage macro expansion | deployment in one embodiment of this invention.

以下、本発明の実施の形態を図面に基づいて詳細に説明する。なお、実施の形態を説明するための全図において、同一部には原則として同一の符号を付し、その繰り返しの説明は省略する。   Hereinafter, embodiments of the present invention will be described in detail with reference to the drawings. Note that components having the same function are denoted by the same reference symbols throughout the drawings for describing the embodiment, and the repetitive description thereof will be omitted.

本発明の一実施の形態であるプリプロセッサは、一般的なプリプロセッサと同様に、ソースコード内のマクロ呼び出しを、ソースコードやヘッダファイルなどに定義されているマクロ定義によって展開するマクロ展開処理を含む前処理を行い、処理結果として前処理後のソースコードを出力するソフトウェアプログラムである。   A preprocessor according to an embodiment of the present invention includes a macro expansion process for expanding a macro call in a source code by a macro definition defined in the source code, a header file, or the like, as in a general preprocessor. It is a software program that performs processing and outputs a preprocessed source code as a processing result.

[マクロ展開処理フロー]
図1は、本実施の形態であるプリプロセッサにおけるマクロ展開処理の例を示したフローチャートである。プリプロセッサは、まず、ソースコードから文字を切り出して文字配列を生成する(S101)。次に、ソースコード(文字配列)に対して字句解析を行ってトークンを切り出してトークン配列を生成する(S102)。なお、ソースコードから文字を切り出し字句解析によってトークン化する手法については一般的なプリプロセッサのものと同様であるため、ここでの説明は省略する。
[Macro expansion processing flow]
FIG. 1 is a flowchart showing an example of macro expansion processing in the preprocessor according to the present embodiment. First, the preprocessor generates a character array by cutting out characters from the source code (S101). Next, lexical analysis is performed on the source code (character array) to cut out tokens to generate a token array (S102). Note that the method of cutting out characters from the source code and tokenizing them by lexical analysis is the same as that of a general preprocessor, and thus description thereof is omitted here.

次に、ステップS101で切り出したトークン配列110内の各トークンを順に入力として処理を繰り返すループ処理を開始する(S103)。ループ処理では、まず、対象のトークンをマクロ展開処理用のバッファに読み込む(S104)。このとき、バッファ内に既にトークンが存在する場合は後端に連結する形で読み込む。なお、当該バッファは、プリプロセッサがマクロ展開処理時にプリプロセッサが動作するコンピュータシステムのメモリ上に設けるデータ領域である。   Next, a loop process is started in which the tokens in the token array 110 cut out in step S101 are sequentially input and the process is repeated (S103). In the loop processing, first, the target token is read into the macro expansion processing buffer (S104). At this time, if a token already exists in the buffer, it is read in a form linked to the rear end. The buffer is a data area provided on the memory of a computer system in which the preprocessor operates during macro expansion processing.

次に、プリプロセッサは、バッファ内のトークンがマクロ定義に該当する部分であるか否かを判定する(S105)。マクロ定義に該当する部分である場合は、バッファ内のトークンがマクロ定義として完結しているか否かを判定する(S106)。マクロ定義として完結している場合には、バッファ内のトークンの内容(マクロ定義)をマクロテーブルに出力してバッファをクリアする(S107)。なお、マクロテーブルに出力したトークンの内容(マクロ定義)について、後の解析において利用するために、後述するステップS109の処理と同様に拡張文字として出力してもよい。   Next, the preprocessor determines whether or not the token in the buffer corresponds to the macro definition (S105). If the portion corresponds to the macro definition, it is determined whether or not the token in the buffer is completed as the macro definition (S106). If the macro definition is completed, the contents of the token in the buffer (macro definition) are output to the macro table and the buffer is cleared (S107). Note that the token content (macro definition) output to the macro table may be output as an extended character in the same manner as in step S109 described later for use in later analysis.

ステップS106においてバッファ内のトークンがマクロ定義として完結していない場合は、次のトークンの処理に移る(S113、S103)。なお、ソースコードにおいてマクロ定義はマクロ呼び出しよりも前に記述されている必要があるため、通常は、以降のマクロ展開の処理が実行される前に、上記の処理によってマクロテーブルにマクロ定義が保持されることになる。   If the token in the buffer is not completed as a macro definition in step S106, the processing proceeds to the next token (S113, S103). Since the macro definition needs to be described before the macro call in the source code, the macro definition is usually held in the macro table by the above process before the subsequent macro expansion process is executed. Will be.

ステップS105においてバッファ内のトークンがマクロ定義に該当しない場合は、次に、バッファ内のトークンがマクロ呼び出しに該当する部分であるか否か、すなわち、マクロ定義に対する参照(呼び出し)を含んでいるか否かを判定する(S108)。マクロ呼び出しに該当しない場合は、バッファ内のトークンをマクロ展開の処理結果として出力してバッファをクリアし(S109)、次のトークンの処理に移る(S113、S103)。なお、バッファ内のトークンを処理結果として出力する際には、すでに出力されている結果の後端に連結する形で出力する。   If the token in the buffer does not correspond to the macro definition in step S105, next, whether or not the token in the buffer is a part corresponding to the macro call, that is, whether or not a reference (call) to the macro definition is included. Is determined (S108). If it does not correspond to the macro call, the token in the buffer is output as the result of the macro expansion process, the buffer is cleared (S109), and the process moves to the next token (S113, S103). When a token in the buffer is output as a processing result, the token is output in a form linked to the trailing end of the already output result.

ステップS108においてバッファ内のトークンがマクロ呼び出しに該当する部分である場合には、次に、バッファ内のトークンの全部または一部がマクロ呼び出しとして完結し、マクロ定義による展開が可能であるか否かを判定する(S110)。   If the token in the buffer corresponds to the macro call in step S108, then whether or not all or part of the token in the buffer is completed as a macro call and can be expanded by the macro definition. Is determined (S110).

ここで、マクロ展開が可能であるか否かを判定する際に、プリプロセッサに対する指示内容に応じて、多段のマクロ呼び出しにおいてネストの内側から展開するか外側から展開するかのいずれを優先させるかを切り替えることが可能であることが望ましい。すなわち、外側から優先的に展開する場合は、バッファ内のトークンを先頭から判断して、各トークンの連結がマクロ呼び出しとして完結している場合(すなわち、図1の処理フローではバッファ内の先頭から後端までの全てのトークンがマクロ呼び出しとして完結している場合)に、マクロ展開可能と判定する。   Here, when deciding whether or not macro expansion is possible, whether to expand from the inside of the nest or from the outside in the multistage macro call according to the instruction content to the preprocessor It is desirable to be able to switch. That is, when preferentially expanding from the outside, the tokens in the buffer are determined from the top, and when the connection of each token is completed as a macro call (that is, from the top in the buffer in the processing flow of FIG. 1). When all tokens up to the rear end are completed as macro calls), it is determined that macro expansion is possible.

一方、内側から優先的に展開する場合は、バッファ内のトークンを逆に後端から判断して、トークン(バッファ内の一部のトークンである場合がある)の連結がマクロ呼び出しとして完結している場合に、当該トークンの部分についてマクロ展開可能と判定する。なお、プリプロセッサに対する、マクロ展開の際の内側からの展開と外側からの展開のいずれを優先させるかの指示は、プリプロセッサの実行時のオプションとして指示したり、設定ファイル等に予め指定しておいたりなど、種々の方法によって行うことができる。   On the other hand, when preferentially expanding from the inside, the tokens in the buffer are judged from the back end, and the concatenation of tokens (which may be some tokens in the buffer) is completed as a macro call. If it is, it is determined that macro expansion is possible for the token portion. The preprocessor is instructed as an option at the time of execution of the preprocessor, or specified in advance in a setting file, etc. It can carry out by various methods.

ステップS110においてバッファ内のトークンの全部または一部がマクロ呼び出しとして完結しておらずマクロ展開できない場合は、次のトークンの処理に移る(S113、S103)。一方、マクロ展開が可能である場合には、バッファ内のマクロ呼び出しとして完結している部分のトークンをマクロ定義に対応するトークンにより置換して展開する(S111)。さらに、マクロ展開した部分のトークンを入力のトークン配列の先頭に戻して、マクロ展開した部分のバッファをクリアし(S112)、次のトークンの処理に移る(S113、S103)。   If all or some of the tokens in the buffer are not completed as a macro call and cannot be expanded in step S110, the process proceeds to the next token (S113, S103). On the other hand, if the macro expansion is possible, the token of the part completed as the macro call in the buffer is replaced with the token corresponding to the macro definition and expanded (S111). Further, the macro expanded portion token is returned to the head of the input token array, the macro expanded portion buffer is cleared (S112), and the processing of the next token is started (S113, S103).

上記の処理を入力のトークン配列(ステップS112において戻されたトークンを含む)が空になるまで繰り返してマクロ展開の処理を終了する。これにより、ソースコード(トークン配列110の全てのトークン)に対してマクロ展開を行った結果の出力を得ることができる。   The above process is repeated until the input token array (including the token returned in step S112) becomes empty, and the macro expansion process ends. As a result, it is possible to obtain an output as a result of performing macro expansion on the source code (all tokens in the token array 110).

上述したように、本実施の形態では、バッファ内でマクロ展開した結果の文字列に対して、そのまま出力せずに、多段のマクロ展開を考慮して入力に戻す。これにより、バッファ内でマクロ展開した文字列に対してさらに再スキャンしてマクロ呼び出しの判定を行うというような処理を要さずにマクロ展開処理を実装することができる。また、多段のマクロ呼び出しにおいてネストの内側から展開するか外側から展開するかのいずれを優先させるかによって異なる処理を必要最小限に抑えることができるため、簡素かつ柔軟にマクロ展開処理を実装することができる。   As described above, in this embodiment, a character string obtained as a result of macro expansion in the buffer is not output as it is, but is returned to the input in consideration of multi-stage macro expansion. As a result, the macro expansion process can be implemented without requiring a process of re-scanning the character string that has been macro expanded in the buffer and determining the macro call. In addition, since it is possible to minimize different processing depending on whether priority is given to expansion from the inside of the nest or from the outside in multistage macro calls, the macro expansion processing must be implemented simply and flexibly. Can do.

[マクロ展開の具体例]
図2は、本実施の形態における多段のマクロ展開の処理例を示した図である。図2の例では、多段のマクロ呼び出しにおいてネストの外側から優先的に展開する場合の例を示している。すなわち、バッファ内に読み込んだトークンを先頭から判断して、各トークンの連結がマクロ呼び出しとして完結している場合(すなわち、本実施の形態ではバッファ内の先頭から後端までの全てのトークンがマクロ呼び出しとして完結している場合)に、マクロ展開可能と判定する。
[Specific examples of macro expansion]
FIG. 2 is a diagram showing an example of multi-stage macro expansion processing according to the present embodiment. In the example of FIG. 2, an example is shown in which multistage macro call is preferentially expanded from the outside of the nest. That is, when the token read into the buffer is determined from the top and the connection of each token is completed as a macro call (that is, all tokens from the top to the back end in the buffer are macros in this embodiment). When it is completed as a call), it is determined that macro expansion is possible.

図中で、ソースコード100は、C言語で記述された多段のマクロ展開を有するソースコードである。なお、入力となるソースコード100は、C言語などのプログラミング言語やXML(eXtensible Markup Language)などのタグ言語によって記述されたものに限らず、マクロ定義およびマクロ呼び出しを有するものであれば適用可能である。また、ソースコード100は複数のファイルから構成されていてもよく、例えば、マクロ定義を有するヘッダファイルなどを含んでいてもよい。   In the figure, a source code 100 is a source code having a multi-stage macro expansion described in C language. Note that the input source code 100 is not limited to that written in a programming language such as C language or a tag language such as XML (eXtensible Markup Language), but can be applied to any source code having macro definition and macro call. is there. The source code 100 may be composed of a plurality of files, and may include, for example, a header file having a macro definition.

ソースコード100の3行目の右辺は、マクロ定義“MUL(x,y)”を呼び出しているが、その際に入力パラメータ“x”としてマクロ定義“NN”を呼び出すという多段のマクロ呼び出しの構造を有している。この式は、図1のステップS101、S102の処理により、例えば図示するようなトークンに分割され、トークン配列110に保持される。プリプロセッサは、このトークン配列110を入力としてマクロ展開を行う。なお、この時点で、ソースコード100の1行目および2行目のマクロ定義(“MUL(x,y)”および“NN”)は、図1のステップS105、S106およびS107の処理によりマクロテーブルに登録されているものとする。   The right side of the third line of the source code 100 calls the macro definition “MUL (x, y)”. At this time, the macro definition “NN” is called as the input parameter “x”. have. This expression is divided into tokens as shown in the figure, for example, by the processing in steps S101 and S102 of FIG. The preprocessor performs macro expansion using the token array 110 as an input. At this time, the macro definitions (“MUL (x, y)” and “NN”) on the first and second lines of the source code 100 are stored in the macro table by the processes in steps S105, S106, and S107 in FIG. Shall be registered.

まず、入力のトークン配列110の先頭のトークン“MUL”をマクロ展開処理用のバッファであるマクロ用バッファ10に読み込む(図中の(1))。このとき、マクロ用バッファ10内のトークン“MUL”は、マクロ呼び出しに該当する部分(すなわち、マクロテーブルに登録されているマクロ定義“MUL(x,y)”に該当する部分)であり、かつマクロ呼び出しとして完結していない(マクロ展開できない)ため、マクロ用バッファ10の内容を出力せずに次のトークンの処理に移る(図中の(2))。   First, the first token “MUL” of the input token array 110 is read into the macro buffer 10 which is a buffer for macro expansion processing ((1) in the figure). At this time, the token “MUL” in the macro buffer 10 is a portion corresponding to the macro call (that is, a portion corresponding to the macro definition “MUL (x, y)” registered in the macro table), and Since the macro call is not completed (the macro cannot be expanded), the processing of the next token is started without outputting the contents of the macro buffer 10 ((2) in the figure).

以降の処理では、入力のトークン配列110から順次トークンを読み込んでマクロ用バッファ10の後端に連結するが、マクロ用バッファ10内のトークンの連結がマクロ呼び出しとして完結しないため、読み込みを継続する。その後、“)”のトークンを読み込んだ時点で、マクロ用バッファ10内の先頭からのトークンの連結“MUL(NN,3)”がマクロ呼び出しとして完結し、マクロ展開可能となる(図中の(3))。   In the subsequent processing, tokens are sequentially read from the input token array 110 and connected to the rear end of the macro buffer 10, but reading is continued because the connection of tokens in the macro buffer 10 is not completed as a macro call. After that, when the token “)” is read, the token concatenation “MUL (NN, 3)” from the beginning in the macro buffer 10 is completed as a macro call, and macro expansion is possible (( 3)).

従って、プリプロセッサは、トークンの連結“MUL(NN,3)”をマクロ定義“MUL(x,y)”の内容“x*y”によって展開し、文字列“NN*3”を得る。その後、文字列“NN*3”を、図1のステップS112の処理によって“NN”、“*”、“3”の各トークンに分割して切り出す。切り出した各トークンは出力せず、図1のステップS113の処理により多段のマクロ展開を考慮して入力のトークン配列110の先頭に戻す。   Therefore, the preprocessor expands the token concatenation “MUL (NN, 3)” with the content “x * y” of the macro definition “MUL (x, y)” to obtain the character string “NN * 3”. Thereafter, the character string “NN * 3” is divided into tokens “NN”, “*”, and “3” by the process of step S112 in FIG. The cut out tokens are not output, and are returned to the top of the input token array 110 in consideration of multi-stage macro expansion by the processing of step S113 in FIG.

次のトークンの処理として、プリプロセッサは、入力のトークン配列110の先頭のトークン“NN”をマクロ用バッファ10に読み込む(図中の(4))。このとき、マクロ用バッファ10内のトークン“NN”は、マクロ呼び出しに該当する部分であり、かつマクロ呼び出しとして完結し、マクロ展開可能である(図中の(5))。従って、プリプロセッサは、トークン“NN”をマクロ定義によって展開して文字列“10”を得て、これをトークンとして切り出す。切り出したトークンは出力せず、多段のマクロ展開を考慮して入力のトークン配列110の先頭に戻す。   As processing for the next token, the preprocessor reads the first token “NN” of the input token array 110 into the macro buffer 10 ((4) in the figure). At this time, the token “NN” in the macro buffer 10 is a portion corresponding to the macro call, and is completed as the macro call and can be expanded ((5) in the figure). Therefore, the preprocessor expands the token “NN” by the macro definition to obtain the character string “10”, and cuts it out as a token. The cut out token is not output, and is returned to the top of the input token array 110 in consideration of multi-stage macro expansion.

次のトークンの処理として、プリプロセッサは、入力のトークン配列110の先頭のトークン“10”をマクロ用バッファ10に読み込む(図中の(6))。このとき、マクロ用バッファ10内のトークン“10”は、マクロ呼び出しに該当しないため、プリプロセッサは、マクロ用バッファ10の内容(トークン“10”)をマクロ展開結果として出力し、次のトークンをマクロ用バッファ10に読み込む(図中の(7)、(8))。以降のトークンは全てマクロ呼び出しに該当しないためそのまま出力され、トークン配列110の全てのトークンを処理した後に、マクロ展開処理の結果として“10*3;”の文字列を得ることができる。   As processing for the next token, the preprocessor reads the first token “10” of the input token array 110 into the macro buffer 10 ((6) in the figure). At this time, since the token “10” in the macro buffer 10 does not correspond to the macro call, the preprocessor outputs the contents of the macro buffer 10 (token “10”) as a macro expansion result, and the next token is the macro. Read into the buffer 10 ((7), (8) in the figure). Since all subsequent tokens do not correspond to macro calls, they are output as they are, and after processing all tokens in the token array 110, a character string “10 * 3;” can be obtained as a result of macro expansion processing.

一方、図3は、本実施の形態における多段のマクロ展開の別の処理例を示した図である。図3の例では、多段のマクロ呼び出しにおいてネストの内側から優先的に展開する場合の例を示している。すなわち、マクロ用バッファ10内のトークンを逆に後端から判断して、トークンの連結がマクロ呼び出しとして完結している場合に、当該トークンの部分についてマクロ展開可能と判定する。   On the other hand, FIG. 3 is a diagram illustrating another example of multi-stage macro expansion processing according to the present embodiment. The example of FIG. 3 shows an example in the case of preferential expansion from the inside of the nest in a multi-stage macro call. That is, the token in the macro buffer 10 is determined from the rear end, and when the token concatenation is completed as a macro call, it is determined that macro expansion is possible for the token portion.

図中の(1)および(2)までの処理は、上述の図2の(1)および(2)で示したものと同様である。その後、入力のトークン配列110から順次トークンを読み込んでマクロ用バッファ10の後端に連結するが、“NN”のトークンを読み込んだ時点で、マクロ用バッファ10内の後端のトークン“NN”がマクロ呼び出しとして完結し、マクロ展開可能となる(図中の(3))。従って、プリプロセッサは、トークン“NN”をマクロ定義によって展開して文字列“10”を得て、図2の例と同様に、図1のステップS112の処理によって文字列“10”をトークンとして切り出す。切り出したトークンは出力せず、図1のステップS113によって、多段のマクロ展開を考慮して入力のトークン配列110の先頭に戻す。   The processes up to (1) and (2) in the figure are the same as those shown in (1) and (2) of FIG. Thereafter, the tokens are sequentially read from the input token array 110 and connected to the rear end of the macro buffer 10. When the “NN” token is read, the rear end token “NN” in the macro buffer 10 is It is completed as a macro call and can be expanded ((3) in the figure). Therefore, the preprocessor expands the token “NN” by the macro definition to obtain the character string “10”, and cuts out the character string “10” as a token by the process of step S112 in FIG. 1 as in the example of FIG. . The cut out token is not output, and is returned to the top of the input token array 110 in consideration of multi-stage macro expansion in step S113 of FIG.

次のトークンの処理として、プリプロセッサは、入力のトークン配列110の先頭のトークン“10”をマクロ用バッファ10に読み込む(図中の(4))。このとき、マクロ用バッファ10内のトークン“MUL(10”は、マクロ呼び出しに該当する部分であり、かつマクロ呼び出しとして完結していないため、マクロ用バッファ10の内容を出力せずに次のトークンの処理に移る(図中の(5))。   As processing of the next token, the preprocessor reads the first token “10” of the input token array 110 into the macro buffer 10 ((4) in the figure). At this time, the token “MUL (10” in the macro buffer 10 is a portion corresponding to the macro call and is not completed as the macro call, so the next token is not output without outputting the contents of the macro buffer 10. (5 in the figure).

以降の処理では、入力のトークン配列110から順次トークンを読み込んでマクロ用バッファ10の後端に連結するが、マクロ用バッファ10内のトークンの連結がマクロ呼び出しとして完結しないため、読み込みを継続する。その後、“)”のトークンを読み込んだ時点で、マクロ用バッファ10内の後端からのトークンの連結“MUL(10,3)”がマクロ呼び出しとして完結し、マクロ展開可能となる(図中の(6))。   In the subsequent processing, tokens are sequentially read from the input token array 110 and connected to the rear end of the macro buffer 10, but reading is continued because the connection of tokens in the macro buffer 10 is not completed as a macro call. Thereafter, when the token ")" is read, the token concatenation "MUL (10, 3)" from the rear end in the macro buffer 10 is completed as a macro call, and macro expansion is possible (in the figure). (6)).

従って、プリプロセッサは、トークンの連結“MUL(10,3)”をマクロ定義“MUL(x,y)”の内容“x*y”によって展開し、文字列“10*3”を得る。その後、文字列“10*3”を“10”、“*”、“3”の各トークンに分割して切り出す。切り出した各トークンは出力せず、多段のマクロ展開を考慮して入力のトークン配列110の先頭に戻す。以降の(7)〜(10)の処理は、上述の図2の(6)〜(9)で示したものと同様である。   Therefore, the preprocessor expands the token concatenation “MUL (10, 3)” with the content “x * y” of the macro definition “MUL (x, y)” to obtain the character string “10 * 3”. Thereafter, the character string “10 * 3” is divided into tokens “10”, “*”, and “3” and cut out. The extracted tokens are not output, but are returned to the top of the input token array 110 in consideration of multi-stage macro expansion. The subsequent processes (7) to (10) are the same as those shown in (6) to (9) of FIG.

以上に示したように、本発明の一実施の形態であるプリプロセッサのマクロ展開方法によれば、マクロ用バッファ10内でマクロ展開した文字列に対して、そのまま出力せずに、多段のマクロ展開を考慮して入力に戻すことで、バッファ内でマクロ展開した文字列に対してさらに再スキャンしてマクロ呼び出しの判定を行うというような処理を実装することを要しない。   As described above, according to the macro expansion method of the preprocessor according to the embodiment of the present invention, a multi-stage macro expansion is performed without outputting the character string expanded in the macro buffer 10 as it is. Therefore, it is not necessary to implement a process of performing re-scanning on the character string expanded in the macro in the buffer and determining the macro call.

また、多段のマクロ呼び出しにおいてネストの内側から展開するのか外側から展開するのかのいずれを優先させるかによって異なる処理を、マクロ用バッファ10内のトークンに対するマクロ展開の可否(マクロ呼び出しが完結しているか否か)をマクロ用バッファ10の先頭から判断するか後端から判断するかという必要最小限の処理の相違に抑えることができる。これらにより、プリプロセッサにおいて簡素かつ柔軟にマクロ展開処理を実装することができる。   In addition, whether or not macro expansion is possible for a token in the macro buffer 10 (depending on whether the macro call is completed) depends on whether priority is given to expansion from the inside or the outside of the nest in multi-stage macro calls. Whether or not) is determined from the beginning or the rear end of the macro buffer 10 can be suppressed to the minimum necessary processing difference. As a result, the macro expansion processing can be implemented simply and flexibly in the preprocessor.

以上、本発明者によってなされた発明を実施の形態に基づき具体的に説明したが、本発明は前記実施の形態に限定されるものではなく、その要旨を逸脱しない範囲で種々変更可能であることはいうまでもない。   As mentioned above, the invention made by the present inventor has been specifically described based on the embodiment. However, the present invention is not limited to the embodiment, and various modifications can be made without departing from the scope of the invention. Needless to say.

例えば、本実施の形態におけるマクロ展開方法は、上述したプリプロセッサに限らずマクロ呼び出しをマクロ定義によって展開するマクロ展開処理を行う種々のソフトウェアプログラムに適用可能である。   For example, the macro expansion method according to the present embodiment is not limited to the preprocessor described above, but can be applied to various software programs that perform macro expansion processing for expanding macro calls by macro definition.

本発明は、プログラミング言語などによって記述されたソースコードに対する多段のマクロ展開処理を含むマクロ展開方法およびプリプロセッサに利用可能である。   The present invention can be applied to a macro expansion method and a preprocessor including a multi-stage macro expansion process for a source code described in a programming language or the like.

10…マクロ用バッファ、100…ソースコード、110…トークン配列。   10 ... Macro buffer, 100 ... Source code, 110 ... Token array.

Claims (5)

コンピュータプログラムによりソースコード内のマクロ呼び出しをマクロ定義によって展開するマクロ展開方法であって、
前記コンピュータプログラムは、
前記ソースコードの文字列から字句解析により第1のトークンを切り出してトークン配列を生成するステップと、
前記トークン配列を入力として、前記トークン配列から順に前記第1のトークンをバッファに読み込むステップと、
前記バッファ内の前記各第1のトークンについて前記マクロ呼び出しに該当する部分であるか否かを判定し、前記マクロ呼び出しに該当する部分ではない場合に、前記バッファ内の前記各第1のトークンを処理結果として出力して前記バッファをクリアするステップと、
前記バッファ内の前記各第1のトークンが前記マクロ呼び出しに該当する部分である場合に、前記バッファ内の前記各第1のトークンの全部または一部が前記マクロ呼び出しとして完結しているか否かを判定し、前記マクロ呼び出しとして完結している場合に、前記マクロ呼び出しとして完結している部分を対応する前記マクロ定義の内容によって置換して展開するステップと、
前記マクロ呼び出しを展開した文字列から字句解析により第2のトークンを切り出すステップと、
切り出した前記各第2のトークンを、入力の前記トークン配列の先頭に戻し、前記バッファにおいて前記マクロ呼び出しを展開した部分をクリアするステップとを実行することを特徴とするマクロ展開方法。
A macro expansion method for expanding a macro call in a source code by a macro definition by a computer program,
The computer program is
Cutting out a first token from the character string of the source code by lexical analysis to generate a token array;
Reading the first token into the buffer in order from the token array, using the token array as an input;
It is determined whether each of the first tokens in the buffer is a part corresponding to the macro call, and if it is not a part corresponding to the macro call, the first tokens in the buffer are Outputting the processing result and clearing the buffer;
Whether or not all or a part of each first token in the buffer is completed as the macro call when each first token in the buffer is a portion corresponding to the macro call. Determining, if completed as the macro call, replacing the portion completed as the macro call with the content of the corresponding macro definition, and expanding,
Cutting out a second token from a string obtained by expanding the macro call by lexical analysis;
And a step of returning each cut-out second token to the beginning of the input token array and clearing a portion of the buffer where the macro call is expanded.
請求項1に記載のマクロ展開方法において、
前記コンピュータプログラムは、
前記バッファ内の前記各第1のトークンの全部または一部が前記マクロ呼び出しとして完結しているか否かを判定する際に、前記バッファ内の先頭からの前記各第1のトークンの全部または一部によって判定することを特徴とするマクロ展開方法。
The macro expansion method according to claim 1,
The computer program is
When determining whether all or a part of each first token in the buffer is completed as the macro call, all or a part of each first token from the top in the buffer The macro expansion method characterized by determining by.
請求項1に記載のマクロ展開方法において、
前記コンピュータプログラムは、
前記バッファ内の前記各第1のトークンの全部または一部が前記マクロ呼び出しとして完結しているか否かを判定する際に、前記バッファ内の後端からの前記各第1のトークンの全部または一部によって判定することを特徴とするマクロ展開方法。
The macro expansion method according to claim 1,
The computer program is
When determining whether all or part of each first token in the buffer is completed as the macro call, all or one of the first tokens from the rear end in the buffer. A macro expansion method characterized in that determination is performed by a section.
請求項1に記載のマクロ展開方法において、
前記コンピュータプログラムは、
前記バッファ内の前記各第1のトークンの全部または一部が前記マクロ呼び出しとして完結しているか否かを判定する際に、前記コンピュータプログラムに対する指示に基づいて、前記バッファ内の先頭からの前記各第1のトークンの全部または一部によって判定するか、前記バッファ内の後端からの前記各第1のトークンの全部または一部によって判定するかを切り替えることを特徴とするマクロ展開方法。
The macro expansion method according to claim 1,
The computer program is
When determining whether or not all or part of each of the first tokens in the buffer is completed as the macro call, each of the first tokens in the buffer is determined based on an instruction to the computer program. A macro expansion method characterized by switching between determination by all or part of a first token and determination by all or part of each first token from the rear end in the buffer.
ソースコード内のマクロ呼び出しをマクロ定義によって展開するマクロ展開処理を実行するプリプロセッサであって、
前記ソースコードの文字列から字句解析により第1のトークンを切り出してトークン配列を生成するステップと、
前記トークン配列を入力として、前記トークン配列から順に前記第1のトークンをバッファに読み込むステップと、
前記バッファ内の前記各第1のトークンについて前記マクロ呼び出しに該当する部分であるか否かを判定し、前記マクロ呼び出しに該当する部分ではない場合に、前記バッファ内の前記各第1のトークンを処理結果として出力して前記バッファをクリアするステップと、
前記バッファ内の前記各第1のトークンが前記マクロ呼び出しに該当する部分である場合に、前記バッファ内の前記各第1のトークンの全部または一部が前記マクロ呼び出しとして完結しているか否かを判定し、前記マクロ呼び出しとして完結している場合に、前記マクロ呼び出しとして完結している部分を対応する前記マクロ定義の内容によって置換して展開するステップと、
前記マクロ呼び出しを展開した文字列から字句解析により第2のトークンを切り出すステップと、
切り出した前記各第2のトークンを、入力の前記トークン配列の先頭に戻し、前記バッファにおいて前記マクロ呼び出しを展開した部分をクリアするステップとを実行することを特徴とするプリプロセッサ。
A preprocessor for executing a macro expansion process for expanding a macro call in a source code by a macro definition;
Cutting out a first token from the character string of the source code by lexical analysis to generate a token array;
Reading the first token into the buffer in order from the token array, using the token array as an input;
It is determined whether each of the first tokens in the buffer is a part corresponding to the macro call, and if it is not a part corresponding to the macro call, the first tokens in the buffer are Outputting the processing result and clearing the buffer;
Whether or not all or a part of each first token in the buffer is completed as the macro call when each first token in the buffer is a portion corresponding to the macro call. Determining, if completed as the macro call, replacing the portion completed as the macro call with the content of the corresponding macro definition, and expanding,
Cutting out a second token from a string obtained by expanding the macro call by lexical analysis;
And a step of returning each cut-out second token to the head of the input token array and clearing a portion of the buffer where the macro call is expanded.
JP2009266740A 2009-11-24 2009-11-24 Macro expansion method and preprocessor Expired - Fee Related JP5385103B2 (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
JP2009266740A JP5385103B2 (en) 2009-11-24 2009-11-24 Macro expansion method and preprocessor

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
JP2009266740A JP5385103B2 (en) 2009-11-24 2009-11-24 Macro expansion method and preprocessor

Publications (2)

Publication Number Publication Date
JP2011113148A true JP2011113148A (en) 2011-06-09
JP5385103B2 JP5385103B2 (en) 2014-01-08

Family

ID=44235458

Family Applications (1)

Application Number Title Priority Date Filing Date
JP2009266740A Expired - Fee Related JP5385103B2 (en) 2009-11-24 2009-11-24 Macro expansion method and preprocessor

Country Status (1)

Country Link
JP (1) JP5385103B2 (en)

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116756103A (en) * 2023-08-23 2023-09-15 北京云枢创新软件技术有限公司 Macro definition text expansion method based on use line, electronic device and medium
CN117112730A (en) * 2023-08-23 2023-11-24 北京云枢创新软件技术有限公司 Target text searching method based on macro expansion text, electronic equipment and medium

Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JPH08314728A (en) * 1995-03-27 1996-11-29 Sun Microsyst Inc Method and apparatus for conversion of source program into object program
JPH10301790A (en) * 1997-04-28 1998-11-13 Nec Ic Microcomput Syst Ltd Assemble processing system

Patent Citations (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
JPH08314728A (en) * 1995-03-27 1996-11-29 Sun Microsyst Inc Method and apparatus for conversion of source program into object program
JPH10301790A (en) * 1997-04-28 1998-11-13 Nec Ic Microcomput Syst Ltd Assemble processing system

Cited By (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN116756103A (en) * 2023-08-23 2023-09-15 北京云枢创新软件技术有限公司 Macro definition text expansion method based on use line, electronic device and medium
CN116756103B (en) * 2023-08-23 2023-10-27 北京云枢创新软件技术有限公司 Macro definition text expansion method based on use line, electronic device and medium
CN117112730A (en) * 2023-08-23 2023-11-24 北京云枢创新软件技术有限公司 Target text searching method based on macro expansion text, electronic equipment and medium
CN117112730B (en) * 2023-08-23 2024-03-22 北京云枢创新软件技术有限公司 Target text searching method based on macro expansion text, electronic equipment and medium

Also Published As

Publication number Publication date
JP5385103B2 (en) 2014-01-08

Similar Documents

Publication Publication Date Title
KR20090006147A (en) Parallel program generation method
JP2006243838A (en) Program development device
JP2007094731A (en) Compiler apparatus
JP5385103B2 (en) Macro expansion method and preprocessor
JP5385102B2 (en) Source analysis program, preprocessor, lexer, and syntax tree analysis program
JP2008276735A (en) Program code converter and program code conversion method
JP2007122187A (en) Program code generation device
JP6175306B2 (en) Control program dividing apparatus, control program dividing method and recording medium therefor
JP2001290655A (en) Compiler, computer system, optimization method, optimizing program, storage medium and program transmitter
JPH0756745A (en) Compiler processing system for language processing program
JP2009064207A (en) Compiler
JP2956591B2 (en) Method and apparatus for parallelizing a loop having a conditional jump out of the loop
JP5181788B2 (en) Source program language conversion apparatus, method and program
JP2008015665A (en) Program analysis method and program analyzer
JP6447358B2 (en) Source code generation program, source code generation method, and computer
US20090254879A1 (en) Method and system for assuring data integrity in data-driven software
JP2004246924A (en) Application generator development support device and application generator development support method
JP5197061B2 (en) Program dividing apparatus and method
US7478372B2 (en) Method for operating a computer system
JPH08328841A (en) Software generating device
JP2008123249A (en) Compiling device and loop speed-up method
JP2001075831A (en) Program processor, ic test system, program processing method and storage medium
JP2001005655A (en) Device and method for aiding development of application generator
JPH08272622A (en) Program conversion device
JP6402639B2 (en) Language converter

Legal Events

Date Code Title Description
A621 Written request for application examination

Free format text: JAPANESE INTERMEDIATE CODE: A621

Effective date: 20120913

A977 Report on retrieval

Free format text: JAPANESE INTERMEDIATE CODE: A971007

Effective date: 20130625

A131 Notification of reasons for refusal

Free format text: JAPANESE INTERMEDIATE CODE: A131

Effective date: 20130730

A521 Written amendment

Free format text: JAPANESE INTERMEDIATE CODE: A523

Effective date: 20130912

TRDD Decision of grant or rejection written
A01 Written decision to grant a patent or to grant a registration (utility model)

Free format text: JAPANESE INTERMEDIATE CODE: A01

Effective date: 20131001

A61 First payment of annual fees (during grant procedure)

Free format text: JAPANESE INTERMEDIATE CODE: A61

Effective date: 20131003

R150 Certificate of patent or registration of utility model

Free format text: JAPANESE INTERMEDIATE CODE: R150

R250 Receipt of annual fees

Free format text: JAPANESE INTERMEDIATE CODE: R250

R250 Receipt of annual fees

Free format text: JAPANESE INTERMEDIATE CODE: R250

LAPS Cancellation because of no payment of annual fees