diff options
author | JSDurand <mmemmew@gmail.com> | 2021-11-08 16:37:57 +0800 |
---|---|---|
committer | JSDurand <mmemmew@gmail.com> | 2021-11-08 16:37:57 +0800 |
commit | 9594210f02572681ed581c5197ace4c207db0917 (patch) | |
tree | 08bf1bf079d111c64cf3128dd68323abdce78228 /src/grammar.h |
initial commit
Now the rough framework is established and the grammar class is sort
of ready.
It remains to write a general input reading mechanism.
Diffstat (limited to 'src/grammar.h')
-rw-r--r-- | src/grammar.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/grammar.h b/src/grammar.h new file mode 100644 index 0000000..2baec68 --- /dev/null +++ b/src/grammar.h @@ -0,0 +1,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 |