Jump to content

Compiler/Simple file inclusion pre processor: Difference between revisions

Specified the minimum functionality required for the pre-processor.
(Specified the minimum functionality required for the pre-processor.)
Line 3:
;Task:
<br>
<b>Introduction</b>
<br><br>
Many programming languages allow file inclusion, so that for example, standard declarations or code-snippets can be stored in a separate source file and be included into the programs that require them at compile-time, without the need for cut-and-paste.
<br>
Line 13 ⟶ 15:
Other languages, on the other hand do have file inclusion, e.g.: C, COBOL, PL/1.
<br>
<br>
<br>The distinction between compiled and interpreted languages should be irrelevent - this is a specialised text processing excersiseexercise - reading a source file and producing a modified source file that aldo contains the contents of one or more other files.
<br>
<br>
Line 19 ⟶ 22:
<br>
<br>
<b>The pre-processor</b>
So...
<br><br>
Create a simple pre-processor that implements file-inclusion for your language.
<br>
The pre-processor need not implement macros, conditional compilation, etc. (E.g. for COBOL, the REPLACING option need not be implemented ).
<br>
<br>The pre-processor need not check the validity of the resultant source. The pre-processor's job is purely to insert te specified files into the source at the required points. Whether the result is syntacticly valid or not is a matter for the compiler/interpreter when it is presented with the source.
<br>
<br>
The syntax accepted for your pre-processor should be as per the standard for your language, should your language have such a facility.
E.g. for C the pre-processor must recognise and handle "#include" directives. For PL/1, %include statements would be processed and for COBOL, COPY statements, etcand so on.
<br>
<br>
If your language does not have a standard facility for file-inclusion, implement that used by a popular compiler/interpreter for the language.
<br>
If there is no such feature, either use the C style #include directive or choose something of your own invention, e.g., #include would be problematic for languages where # introduces a comment.
<br>
<br>
Line 40 ⟶ 42:
<br>
<br>
<b>Minimum requirements</b>
<br>
<br><br>
If possible, implement your pre-processor as a filter, i.e. read the main source file from standard input and write the pre-processed source to standard output.
As a minimum, your pre-procdessor must be able to process a source file (read from a file or standard input, as you prefer) and generate another source file (written to a file or standard output, as you prefer). The file-inclusion directives in the source should be replaced by the contents of the specified files. Implementing nested file inclusion directives (i.e., if an included file contains another file-inclusion directive) is optional.
<br>
<br>
Pre-processors for some languages offer additional facilities, such as macro expansion and conditional compilation. Your pre-processor need not implement such things.
NOTE to task implementors: The Task is about implementing a pre-processor for your language, not just describing it's features.
<br>Just as the task [https://www.rosettacode.org/wiki/Calculating_the_value_of_e Calculating the value of e] is not about using your language's in-built exp function but showing how e could be calculated, this is about showing how file inclusion could be implemented - even if the compiler/interpreter you are using has such a facility.
<br>
<br>
<b>Notes</b>
NOTE to anyone who uses the pre-processors on this page: They are supplied as-is, with no warranty - use at your own peril : )
<br>* implementors: The Task is about implementing a pre-processor for your language, not just describing it's features. Just as the task [https://www.rosettacode.org/wiki/Calculating_the_value_of_e Calculating the value of e] is not about using your language's in-built exp function but showing how e could be calculated, this is about showing how file inclusion could be implemented - even if the compiler/interpreter you are using already has such a facility.
NOTE to anyone who uses* the pre-processors on this page: They are supplied as-is, with no warranty - use at your own peril : )
 
 
3,044

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.