Adapted and combined from the original CPPGM preprocessing assignments. See NOTICE for attribution.
Build a preprocessor using the tokenizer, post-token converter, and preprocessing-expression evaluator from PA1–PA3. It performs translation phases 1 through 6 and the tokenization part of phase 7 under the course rules below. Its reusable token pipeline becomes the parser's input.
Implement one program, preproc. Macro replacement and directive processing
are groups within this assignment, with the same file interface throughout.
preproc -o <outfile> <source1> [<source2> ...]Read each UTF-8 source relative to the working directory. At least one source is required. Reset macro definitions, conditional state, and pragma-once state between primary sources. Included files share the state of their including translation unit.
Create or replace the output file with:
preproc <number-of-primary-sources>
sof <source1>
<PA2 posttoken records>
eof
sof <source2>
<PA2 posttoken records>
eof
The sof spelling is the command-line source path. There is one eof for
each primary source; included files do not introduce sof or eof records.
PA2 defines token spelling, literal data, and string-literal concatenation.
This text is a test observation. Share structured tokens with later stages;
you do not need to turn the dump into an internal transport format.
For example, if first.cpp contains #define F(x) x x followed by F(3),
and second.cpp contains F, then:
preproc -o /tmp/tokens first.cpp second.cppproduces:
preproc 2
sof first.cpp
literal 3 int 03000000
literal 3 int 03000000
eof
sof second.cpp
identifier F
eof
Return EXIT_SUCCESS on success. Return EXIT_FAILURE on a preprocessing
error, an active #error, an erroneous controlling expression, a missing
include, or an invalid phase-7 token. Output-file contents after failure
are unspecified. Standard output and standard error are ignored. As in
PA1, unsupported inputs outside the documented course contract do not create
additional requirements. Arguments beginning with - other than the initial
-o are outside this interface.
- Implement macro replacement: definitions, arguments, stringizing, token pasting, rescanning, and recursion suppression.
- Implement conditional inclusion using the PA3 evaluator, including nested inactive groups and macro-expanded controlling expressions.
- Add file inclusion and directives: include search, source
locations, line control, predefined macros, pragmas, and
_Pragma. - Check integration: macro definitions from headers, locations through expansions, pragma-once file identity, and translation-unit state reset.
The macro group needs only ordinary source files containing macro directives
and text. You can run it before supporting includes or conditionals. Preserve
the course-defined recursion rules in macros.md; a host preprocessor is not
an exact oracle for those rules. The directives handout specifies the default
include search and predefined values. Hosted-system-header compatibility is
later work: PA30 adds the GNU and Clang extensions that the host's headers
use, such as __COUNTER__, the __has_* probes and the GNU comma paste
, ## __VA_ARGS__.
Continue in the shared dev/ tree. The student export installs
dev/preproc.cpp from the preprocessor scaffold, including a host file-identity
helper for #pragma once. Reuse your earlier token and expression machinery.
There is no additional starter-kit checkout.
Run these commands from this assignment directory:
make check TEST='tests/macros/*.t'
make check TEST='tests/directives/200-*.t'
make check TEST='tests/directives/400-*.t tests/directives/800-*.t'
make test
make run INPUT='tests/directives/400-multiple-source-files.t*'make check runs selected cases and compares their outputs. make test runs
both groups. The wrappers arrange the working directory needed for relative
includes. For a case named name.t, any numbered companions such as name.t2
are additional primary source files for that invocation, in lexical order.
References compare the output file and exit status exactly; no fixture
prints the values of __DATE__, __TIME__ or __CPPGM_AUTHOR__, and the
comparator does not normalize them. Diagnostic text is not graded.
Use ../dev/preproc-ref to investigate a test and the root testing guide for
reference policy. You may run the tool directly with temporary files while
developing; no separate stdin implementation or macro-only executable is
required. Finish with the cumulative through-PA4 report from the root.
The macro and directive guides describe the clearest way to get every rule right, and it is a fine way to finish PA4. Later assignments preprocess the host's standard library headers for every translation unit, and PA35 compiles the compiler itself: hundreds of thousands of tokens per compile, most of them from macros, include guards and groups that are skipped. At that scale the representation decides the cost, not the rules. One representation that scales:
- Find a macro through the identifier's interned entry (a field that says whether, and as what, the name is defined), not by probing a table keyed by spelling on every rescan.
- Keep each token's set of unavailable macro names as a small interned id, so that a produced token copies one number instead of a set of names.
- Expand by pulling tokens from a stack of active expansions, with arguments kept as ranges of the invocation's tokens. Memory then follows the nesting depth, not the length of the output, and nothing rewrites a long token sequence in place.
- A chain of forwarding macros can reuse an expanded argument's interior when its tokens have no active macro to invoke. Keep the usual rescan at boundaries that can bring a function-like name and an opening parenthesis together. Preserve token origins and whitespace when the tokens are read for stringizing or pasting.
- Record leading whitespace and the start of a line as flags on the next token instead of keeping whitespace and new-line tokens.
- Give each file one range in a single location space, so a token's position
is one number; work out a file and line only when a diagnostic,
__LINE__or a debug location asks. - Remember a header whose whole text sits inside one
#ifndefguard, and skip it on a later inclusion while the guard macro stays defined. Lex an inactive group only far enough to find its directives, without interning its identifiers or looking up macros.
None of this changes PA4's output, so it can come later, but it is easier to leave room for it now. Any design that meets the output here and the speed gates later is acceptable.
Keep token kind, source spelling, decoded literal data, and source location available to the parser. Keep preprocessing state local to a translation unit and expose a reusable preprocessing entry point. The next lesson builds a structured AST over that token stream; preprocessing remains a completed stage and its tests remain in the cumulative report.
The detailed macro and directive explanations retain material from the CPPGM Foundation's 2013 handouts.
Accept -std=c++11, -std=c++14, -std=c++17 or -std=c++20 before -o.
Set __cplusplus to 201103L, 201402L, 201703L or 202002L for the
selected C++11, C++14, C++17 or bounded C++20 profile. In C++20 mode, evaluate
UTF-8 character literals as unsigned byte values and provide
__cpp_char8_t=202207L, __cpp_consteval=201811L,
__cpp_constexpr_dynamic_alloc=201907L, __cpp_constinit=201907L and
__cpp_impl_coroutine=201902L.
Define these macros only in C++20 mode. In C++17, evaluate UTF-8 character
literals using the target representation of char. Token pasting must use
the same UTF-8 character-prefix rules as the source lexer.
Keep the selected token and literal rules through macro expansion, token
pasting, stringizing and controlling expressions. Stringizing retains the
separator spelling; evaluating an integer uses its value. This extends
PA1–PA3's numeric contracts without requiring hosted include discovery.
In C++20 mode, <=> is one preprocessing punctuator, including when
formed by token pasting. Preserve that token through macro expansion.
Earlier modes retain their existing punctuation boundaries.