Decaf Compiler
Functions
p2-parser.c File Reference

Compiler phase 2: parser. More...

#include "p2-parser.h"

Functions

int get_next_token_line (TokenQueue *input)
 Look up the source line of the next token in the queue. More...
 
void match_and_discard_next_token (TokenQueue *input, TokenType type, const char *text)
 Check next token for a particular type and text and discard it. More...
 
void discard_next_token (TokenQueue *input)
 Remove next token from the queue. More...
 
bool check_next_token_type (TokenQueue *input, TokenType type)
 Look ahead at the type of the next token. More...
 
bool check_next_token (TokenQueue *input, TokenType type, const char *text)
 Look ahead at the type and text of the next token. More...
 
DecafType parse_type (TokenQueue *input)
 Parse and return a Decaf type. More...
 
void parse_id (TokenQueue *input, char *buffer)
 Parse and return a Decaf identifier. More...
 
ASTNodeparse (TokenQueue *input)
 Convert a queue of tokens into an abstract syntax tree (AST) More...
 

Detailed Description

Compiler phase 2: parser.

Function Documentation

◆ check_next_token()

bool check_next_token ( TokenQueue input,
TokenType  type,
const char *  text 
)

Look ahead at the type and text of the next token.

Parameters
inputToken queue to examine
typeExpected type of next token
textExpected text of next token
Returns
True if the next token is of the expected type and text, false if not

◆ check_next_token_type()

bool check_next_token_type ( TokenQueue input,
TokenType  type 
)

Look ahead at the type of the next token.

Parameters
inputToken queue to examine
typeExpected type of next token
Returns
True if the next token is of the expected type, false if not

◆ discard_next_token()

void discard_next_token ( TokenQueue input)

Remove next token from the queue.

Throws an error if there are no more tokens.

Parameters
inputToken queue to modify

◆ get_next_token_line()

int get_next_token_line ( TokenQueue input)

Look up the source line of the next token in the queue.

Parameters
inputToken queue to examine
Returns
Source line

◆ match_and_discard_next_token()

void match_and_discard_next_token ( TokenQueue input,
TokenType  type,
const char *  text 
)

Check next token for a particular type and text and discard it.

Throws an error if there are no more tokens or if the next token in the queue does not match the given type or text.

Parameters
inputToken queue to modify
typeExpected type of next token
textExpected text of next token

◆ parse()

ASTNode* parse ( TokenQueue input)

Convert a queue of tokens into an abstract syntax tree (AST)

Parameters
inputTokens to parse
Returns
Root of abstract syntax tree

◆ parse_id()

void parse_id ( TokenQueue input,
char *  buffer 
)

Parse and return a Decaf identifier.

Parameters
inputToken queue to modify
bufferString buffer for parsed identifier (should be at least MAX_TOKEN_LEN characters long)

◆ parse_type()

DecafType parse_type ( TokenQueue input)

Parse and return a Decaf type.

Parameters
inputToken queue to modify
Returns
Parsed type (it is also removed from the queue)