Você está na página 1de 5

APPROACH DOCUMENT REQUIREMENTS:

Develop a preprocessor application. This application must provide the intermediate file as generated before the compilation . The application must implement the functionalities of #include, #define, #if , #elif , #else. The #include functionality should include the header file mentioned in the c program ,within the intermediate file. The file may be in same directory or in any other directory. The #include may be any where in the program. The #define functionality should replace the variable defined in the #define with the value mentioned. For Eg: #define x 10 int a=x; so x must be replaced by 10. (int a=10 ). The #define can be anywhere in the program. The replacement should happen any where in the program. The #define can also be in header file included and the replacement should happen accordingly in both the contents of the header file and also in main program. The symbol can also be present within the strings,but the functionality should identify difference between the strings and symbol in the code. The symbol can be in any combination in the code . If the symbol is defined more than once then the last occurrence of the symbol and definition should be replaced with the previous definition. The definition can also be again a symbol with definition,and it an continue. The symbol can also hold macros and strings. The symbol name should hold the rules of naming convention,hence the symbol name should not start with special character and it should not be integer.

The #if functionality should

behave as if.....else statements in C. It should

capture the constants, if the condition within #if statement is true then #if block will be executed or if the condition is false the statements in #else will be executed. The #endif is used to end the #if block. The statements after #endif must executed normally.

PROGRAM APPROACH #include The objective of #include is to include the header file in the intermediate file. The file is included by reading the corresponding header file that is found in #include statement. It follows the following approach: First it checks for the #include in the program Second it takes the corresponding header file name Third it reads the header file into the intermediate file The input file is read character by character. When #include is encountered the

corresponding file name is stored in an string array. This array contains only the file name. This file name is passed as an argument to a function which reads the header file in the intermediate file. The contents of the file are displayed on the screen along with rest of the input file. A loop is taken to store the name of the header file when #include is encountered. The file name is taken as character by character and stored is a string array. This string array is passed as a parameter to the function which reads the

contents of the header file and the contents of header file are displayed on the intermediate file instead of #include statement. #define The objective of #define is to replace the constant defined with its corresponding value in the code. The approach taken to solve this is as follows.

The first step is to identify the #define and collect all the constants and its corresponding value. The second step is to replace the constant by its value when it is encountered in the coding.

First the input file will be opened for reading . The file will be read as character by character in a loop and stored in a character array , until it reaches the end of file. As it was reading the each character will be checked if its a space or newline or a tab. If it is a space, character array will be compared with #define , if it return zero then the following constant and its value are stored .Each time the character array gets #define the memory will be created dynamically, so that the symbol and definition are stored. Before storing in the linked list it compares the symbol with all other symbol stored,if it matches then the new definition alone will be replaced in the link list ,by displaying a warning .Else if it is not matched then it should be checked if the definition is again a symbol, so the definition is compared with each symbol in the linked list,if it is matched then the corresponding symbol's definition will be taken instead . To replace value in the coding , the input file is read character by character in a loop until it reaches the end of file , and as when a space is encountered ,the word

is captured in character array. Then the search is performed in the dynamic blocks with the word to find if it matches the constants. If it matches the constant then the corresponding value will be printed instead of the word .If the search fails then the word will be printed.

#if and # elif In order to do the functionality of #if, #elif, #else and #endif. I need to include the file which should be the output file of #define functionality program approach. Now my program will work on the included file and it will search for the following statements : 1. #if statement 2. #elif statement 3. #else statement 4. #endif My program will now parse the file and will search for the above statements. After encountering the above statements it will check the conditions stated in the above different statements. The expression written against the #if and #elif now will be evaluated. If the result is true in #if statement then the block or the statement following the #if will be executed, or if the result is not true then the control will go to the #elif statement and the expression following it will be examined. If the result is true, the block or the statements following #elif will be executed. If neither of the expression of #if or #elif results true then the block or statements following the #else will be executed. The above conditional preprocessor directives are ended with #endif. After encountering #endif the rest of the statements are executed normally. After including the input file which has been already resolved for #define. The program will then read the contents of the file character by character in a loop until the end of the file is encountered. #if or #elif is normally followed by an expression. If it encounters a # character it will go one position back and captures the strings separated by spaces. The captured strings second and fourth will be then resolved and checked whether it results true or false. If it results true in #if statement then its block will be executed or if it is true in #elif its block will be executed. If neither of #if and #elif results true then surely block of #else is executed. #if is then ended with #endif statement. Rest of the statements are executed normally. INTEGRATION

The input file is opened in read mode. Until end of file(EOF) is encountered ,the file pointer reads character by character . when #define is encountered the constants and values are stored dynamically in linked list and the file is closed. The input file is again opened in read mode . Until EOF is encountered , the file pointer reads character by character. When #define is encountered operations are performed to replace the values as mentioned above. When #include is encountered the function for #include is called,and the header file is included into the intermediate file. When #if is encountered ,it checks for #define if #define is found then it replaces the values and continues with checking the condition and operations are performed as mentioned above for #if. Then the rest of the characters in the file are read and printed on the intermediate file . Finally the intermediate file is got as one output file with all functionalities

Você também pode gostar