Skip to content

How to:

Lexi edited this page Sep 26, 2024 · 1 revision

Add a token

In your <ypl>.cpp

  1. 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;
};
  1. Create a parsing function ParseReservedWordStatement() for handling ReservedWord 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");
   }
}
  1. Add a case ReservedWord: to the switch statement in ParseStatement() that calls ParseReserveWordStatement().
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.

Clone this wiki locally