librdesc
Loading...
Searching...
No Matches
Functions
util.h File Reference

Development and debugging utilities. More...

#include <stdint.h>
#include <stdio.h>
Include dependency graph for util.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void rdesc_dump_cst (FILE *out, const struct rdesc *parser, void(*node_printer)(FILE *out, const struct rdesc_node *))
 Dumps the concrete syntax tree (CST) as a graphviz DOT graph.
 
void rdesc_dump_bnf (FILE *out, const struct rdesc_grammar *grammar, const char *const tk_names[], const char *const nt_names[])
 Dumps the grammar in BNF format.
 
void rdesc_flip_left (struct rdesc *parser, struct rdesc_node *parent, uint16_t child_index)
 Rotates a right-recursive concrete syntax tree into a left-recursive form.
 

Detailed Description

Development and debugging utilities.

This header provides tools for visualizing data structures in librdesc.

Function Documentation

◆ rdesc_dump_bnf()

void rdesc_dump_bnf ( FILE *  out,
const struct rdesc_grammar grammar,
const char *const  tk_names[],
const char *const  nt_names[] 
)

Dumps the grammar in BNF format.

Prints all production rules in human-readable BNF format. (e.g.A ::= B / C)

Parameters
outOutput file stream.
grammarUnderlying grammar struct.
tk_namesThe token name or literal representation (e.g., IDENT or +). Prefix with '@' to suppress automatic quote wrapping.
nt_namesThe raw name of the nonterminal (e.g., expr). Angle brackets <> are added automatically.

◆ rdesc_dump_cst()

void rdesc_dump_cst ( FILE *  out,
const struct rdesc parser,
void(*)(FILE *out, const struct rdesc_node *)  node_printer 
)

Dumps the concrete syntax tree (CST) as a graphviz DOT graph.

Parameters
outOutput file stream.
parserParser to dump its root.
node_printerCallback to print token and nonterminal names.

◆ rdesc_flip_left()

void rdesc_flip_left ( struct rdesc parser,
struct rdesc_node *  parent,
uint16_t  child_index 
)

Rotates a right-recursive concrete syntax tree into a left-recursive form.

Performs an in-place transformation of a subtree to convert right-associative nodes into left-associative ones. This is typically used as a post-processing step for grammars transformed by the rrr macro.

The function converts the structure represented by:

A → β A'
A' → α A' / ε

into the left-recursive equivalent:

A → A α / β

α and β may consist of multiple tokens or nonterminal nodes. CST connections are maintained to ensure valid destruction and traversal.

The node to be rotated cannot be the root of the entire parse tree, so parent cannot be NULL. This is because a match is considered complete if and only if all nonterminals complete their bodies; this condition cannot be met if the start symbol (the root) is a recursive nonterminal.

Parameters
parserPointer to the rdesc parser instance.
parentThe parent node of the subtree root being rotated.
child_indexThe index of the target node (A) within the parent's child list.