Commit 338aa647 authored by Michaël Cadilhac's avatar Michaël Cadilhac
Browse files

2005-06-04 Michael Cadilhac <michael.cadilhac@lrde.epita.fr>

	* parser.y: Add  a  `%namespace' directive   which makes  monoburg
	output  functions in  a  namespace. This  directive could  produce
	nested namespaces. Syntax is:
	%namespace namespace1_name
	%namespace namespace2_name
	without double quote or anything.
	
	This directive  can only be  used in the  `rule' part of  the burg
	file.

	The user is warned that  the code produced will only be compilable
	with a C++ compiler.
	
	* parser.y (yylex):  Remove  a warning  when comparing  a `sizeof'
	with an int.
	
	* monoburg.c,
	* monoburg.h: Add `%namespace' handling.
parent 698759c9
2005-06-04 Michael Cadilhac <michael.cadilhac@lrde.epita.fr>
* parser.y: Add a `%namespace' directive which makes monoburg
output functions in a namespace. This directive could produce
nested namespaces. Syntax is:
%namespace namespace1_name
%namespace namespace2_name
without double quote or anything.
This directive can only be used in the `rule' part of the burg
file.
The user is warned that the code produced will only be compilable
with a C++ compiler.
* parser.y (yylex): Remove a warning when comparing a `sizeof'
with an int.
* monoburg.c,
* monoburg.h: Add `%namespace' handling.
2005-06-04 Michael Cadilhac <michael.cadilhac@lrde.epita.fr>
* monoburg.c: Add a `--with-references' option which makes
......
......@@ -34,6 +34,9 @@ static int dag_mode = 0;
static int predefined_terms = 0;
static int default_cost = 0;
char *namespaces[MAX_NAMESPACES];
int n_namespace = 0;
static void output (char *fmt, ...)
{
va_list ap;
......@@ -1187,7 +1190,7 @@ main (int argc, char *argv [])
} else if (strcmp (argv [i] + 2, "without-exported-symbols") == 0) {
with_exported_symbols = FALSE;
} else if (strcmp (argv [i] + 2, "with-references") == 0) {
warn_cpp ("`--with-references' option");
warn_cpp ("`--with-references' option");
with_references = TRUE;
} else {
usage ();
......@@ -1241,9 +1244,13 @@ main (int argc, char *argv [])
g_error ("no start symbol found");
emit_header ();
for (i = 0; i < n_namespace; ++i)
output ("namespace %s {\n", namespaces[i]);
emit_nonterm ();
emit_state ();
emit_prototypes ();
for (i = 0; i < n_namespace; ++i)
output ("}\n");
if (deffd) {
output ("#endif /* _MONO_BURG_DEFS_ */\n");
......@@ -1264,6 +1271,8 @@ main (int argc, char *argv [])
output ("#include \"%s\"\n\n", deffile);
}
for (i = 0; i < n_namespace; ++i)
output ("namespace %s {\n", namespaces[i]);
emit_vardefs ();
emit_cost_func ();
emit_emitter_func ();
......@@ -1273,6 +1282,8 @@ main (int argc, char *argv [])
emit_label_func ();
emit_kids ();
for (i = 0; i < n_namespace; ++i)
output ("}\n");
if (infiles) {
GList *l = infiles;
......
......@@ -3,12 +3,16 @@
#include <glib.h>
#define MAX_NAMESPACES 10
void yyerror (char *fmt, ...);
int yylex (void);
extern FILE *inputfd;
extern FILE *outputfd;
extern GHashTable *definedvars;
extern char *namespaces[MAX_NAMESPACES];
extern int n_namespace;
typedef struct _Rule Rule;
......@@ -62,9 +66,11 @@ Rule *make_rule (char *id, Tree *tree);
void rule_add (Rule *rule, char *code, char *cost, char *cfunc);
void create_rule (char *id, Tree *tree, char *code, char *cost,
void create_rule (char *id, Tree *tree, char *code, char *cost,
char *cfunc);
void warn_cpp (const gchar *use_of);
void yyparsetail (void);
void reset_parser (void);
......
......@@ -17,7 +17,7 @@
#include <stdarg.h>
#include "monoburg.h"
static int yylineno = 0;
static int yylinepos = 0;
......@@ -38,6 +38,7 @@ static int yylinepos = 0;
%token COST
%token TERM
%token TERMPREFIX
%token NAMESPACE
%token <ivalue> INTEGER
%type <tree> tree
......@@ -49,10 +50,17 @@ static int yylinepos = 0;
%%
decls : /* empty */
decls : /* empty */
| START IDENT { start_nonterm ($2); } decls
| TERM tlist decls
| TERMPREFIX plist decls
| NAMESPACE IDENT {
warn_cpp ("`%namespace' directive");
if (n_namespace == MAX_NAMESPACES)
yyerror ("maximum namespace depth reached.");
else
namespaces[n_namespace++] = g_strdup($2);
} decls
| rule_list optcost optcode optcfunc {
GList *tmp;
for (tmp = $1; tmp; tmp = tmp->next) {
......@@ -101,7 +109,7 @@ optcfunc : /*empty */ { $$ = NULL; }
static char input[2048];
static char *next = input;
void
void
yyerror (char *fmt, ...)
{
va_list ap;
......@@ -232,7 +240,7 @@ nextchar ()
case 0:
if (ll) {
next_state = 1;
} else
} else
fputs (input, outputfd);
break;
case 1:
......@@ -248,7 +256,7 @@ nextchar ()
state = next_state;
yylineno++;
} while (next_state == 2 || ll);
}
}
return *next++;
}
......@@ -262,8 +270,8 @@ yyparsetail (void)
input[0] = '\0';
}
int
yylex (void)
int
yylex (void)
{
char c;
......@@ -292,6 +300,12 @@ yylex (void)
next += 4;
return TERM;
}
if (!strncmp (next, "namespace", 9) && isspace (next[9])) {
next += 9;
return NAMESPACE;
}
return c;
}
......@@ -315,7 +329,7 @@ yylex (void)
return COST;
}
while (isalpha (*n) || isdigit (*n) || *n == '_')
while (isalpha (*n) || isdigit (*n) || *n == '_')
n++;
l = n - next + 1;
......@@ -323,14 +337,14 @@ yylex (void)
next = n;
return IDENT;
}
if (c == '"') {
int i = 0;
static char buf [100000];
while ((c = *next++) != '"' && c)
buf [i++] = c;
buf [i] = '\0';
yylval.text = g_strdup (buf);
......@@ -338,9 +352,9 @@ yylex (void)
}
if (c == '{') {
int i = 0, d = 1;
unsigned i = 0, d = 1;
static char buf [100000];
while (d && (c = nextchar ())) {
buf [i++] = c;
assert (i < sizeof (buf));
......@@ -357,9 +371,9 @@ yylex (void)
return CODE;
}
return c;
} while (1);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment