-
Notifications
You must be signed in to change notification settings - Fork 0
How to:
Lexi edited this page Sep 26, 2024
·
1 revision
In your <ypl>.cpp
- Add
<reserve word>
to the list of reserved words in the token table.
const TOKENTABLERECORD TOKENTABLE[] = { { COMMA,"COMMA", false } };
COMMA
: This is the token type, typically an enumeration value of type TOKENTYPE."COMMA"
: This is the description (a string) for the token, which provides a human-readable label for the token type.false
: This indicates whether the token is a reserved word in the language being parsed.The structure is defined as follows:
struct TOKENTABLERECORD { TOKENTYPE type; char description[12+1]; bool isReservedWord; };
- Create a parsing function
ParseReservedWordStatement()
for handlingReservedWord
statements.
Assuming
RESERVE_WORD
is a reserved word you're adding to your parser. A typical parsing function follows this structure:void ParseReserveWordStatement(TOKEN tokens[]) { // First, check if the current token matches the reserved word if (tokens[0].type == RESERVE_WORD) { // Consume the reserved word token GetNextToken(tokens); // Handle the specific syntax rules for this statement // For example, <RESERVE_WORDStatement> ::= RESERVE_WORD <expression> ; ParseExpression(tokens); } else { ProcessCompilerError(tokens[0].sourceLineNumber, tokens[0].sourceLineIndex, "Expecting RESERVE_WORD statement"); } }
- Add a case
ReservedWord:
to the switch statement inParseStatement()
that callsParseReserveWordStatement()
.
void ParseStatement(TOKEN tokens[]) { switch (tokens[0].type) { case RESERVE_WORD: ParseReserveWordtatement(tokens); break; } }
Note
If the statement involves more complex grammar (e.g., expressions or optional elements), you might need additional parsing functions like ParseExpression()
or use conditionals and loops to handle optional parts.