parser module

class src.parser.Parser(tokens)

Bases: object

A parser for the Ulto programming language.

The Parser class is responsible for converting a list of tokens, generated by the lexical analyzer, into an abstract syntax tree (AST) representing the structure of the Ulto program. It supports various statements such as assignments, loops, conditional branches, and more, and ensures that the syntax of the input program is valid according to the language’s grammar.

tokens

The list of tokens to be parsed.

Type:

list

current_token

The current token being processed.

Type:

tuple or None

pos

The current position in the token list.

Type:

int

advance()

Advances to the next token.

consume(token_type)

Consumes the current token if it matches the expected type.

Args: token_type (str): The expected token type.

Returns: str: The value of the consumed token.

consume_value()

Consumes a value token (number, identifier, or string).

Returns: The consumed value.

error()

Raises a syntax error with the current token.

Raises: Exception: Indicates a syntax error.

parse()

Parses the tokens into an abstract syntax tree (AST).

Returns: list: The parsed AST.

parse_assignment()

Parses an assignment statement.

Returns: tuple: The parsed assignment node.

parse_block()

Parses a block of statements.

Returns: list: The list of parsed statements.

parse_break()

Parses a break statement.

Returns: tuple: The parsed break node.

parse_expression()

Parses an expression.

Returns: tuple: The parsed expression node.

parse_for()

Parses a for loop statement.

Returns: tuple: The parsed for loop node.

parse_if()

Parses an if statement.

Returns: tuple: The parsed if node.

parse_primary_expression()

Parses a primary expression (number, identifier, or parenthesized expression). Returns: The parsed expression.

parse_print()

Parses a print statement.

Returns: tuple: The parsed print node.

parse_reverse()

Parses a reverse statement.

Returns: tuple: The parsed reverse node.

parse_revtrace()

Parses a revtrace statement.

Returns: tuple: The parsed revtrace node.

parse_statement()

Parses a single statement.

Returns: tuple: The parsed statement node.

parse_while()

Parses a while statement.

Returns: tuple: The parsed while node.

peek_next_token()

Peeks at the next token without consuming it.

Returns: tuple: The next token.