summaryrefslogtreecommitdiff
path: root/src/grammar.h
blob: 2baec687d63fe10be3c2c0dabf8d36fc76f31cae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef GRAMMAR_H
#define GRAMMAR_H

#include <stdarg.h>
#include <stdio.h>
#include "list.h"

/* The class file for grammars */

/* A grammar is a list of rules.  A rule replaces a non-terminal with
   a finite string of terminals and non-terminals.

   For us, a terminal is a positive integer, and a non-terminal is a
   negative integer.  Since the signs of these two types is fixed, we
   represent them as unsigned integers.  Since we want to deal with
   unicode points, the terminal is stored as unsigned long, so that we
   can include all unicode poitns. */

typedef unsigned long T;
typedef unsigned NT;

typedef struct TNT_s TNT;
typedef struct Rule_s Rule;

/* A grammar is a list of rules. */
typedef List *Grammar;

/* This accepts one and only one optional argument, which is of type
   either T or NT, depending on TYPE being 0 or not. */
TNT *new_tnt(int type, ...);

/* For debugging purposes, we need to print out rules to examine their
   values. */

void print_tnt(void *element);

void print_rule(void *r);

/* This will generate errors if G is not a list of rules. */
void print_grammar(Grammar g);

/* constructors */

/* FORMAT specifies the types of TNTs.  It is a string of t and n's.
   FORMAT_LEN is the length of FORMAT, excluding the terminating null
   byte.

   Upon failure NULL is returned. */
List *new_tnt_string(char *format, int format_len, ...);

/* RIGHT should be created by new_tnt_string or something alike.

   If RIGHT is NULL, NULL is returned. */
Rule *new_rule(NT left, List *right);

/* destructors */
void destroy_rule(Rule *rule);

#endif