Related articles |
---|
To the users of PCCTS parrt@ecn.purdue.edu (1993-02-12) |
Newsgroups: | comp.compilers |
From: | parrt@ecn.purdue.edu (Terence J Parr) |
Keywords: | tools, question, parse |
Organization: | Compilers Central |
Date: | Fri, 12 Feb 1993 02:35:06 GMT |
In order to help us improve PCCTS, we would like to obtain working sample
grammars from as many users as possible. We are not interested in seeing
your proprietary PCCTS grammars, but just the structural properties of
real PCCTS grammars. We have included a simple C program that will
extract this information from your PCCTS grammars.
For each grammar, please send email to pccts@ecn.purdue.edu with the
Subject line:
Subject: sample
The email should contain:
[1] A phrase describing what the grammar is used for, e.g.,
"Database query language", "Fortran cross referencer", etc.
[2] Your name, as you would like it to appear in acknowledgements
in papers using statistics partly derived from you grammar.
If you would prefer not to be listed as a contributor (i.e.,
to avoid any implication that you endorse our research),
please indicate this by saying "No acknowledgement".
[3] Your stripped grammar. The stripped grammar is produced by
first using "antlr -p" to generate a version of your grammar
without actions. That output is then filtered by the
following C program, which will convert all terminal and
nonterminal names into generic names. Although we would
accept your original grammar, these two simple transformations
allow you to send us the information we want without
compromising the privacy of your work.
---- C program to rename grammar terminals and nonterminals ----
/* Input: a grammar processed by "antlr -p"
* Output: grammar with all symbols renamed generically
*/
#include <stdio.h>
#include <ctype.h>
#define MAX_ATOM_SIZE 1025
#define MAX_NEWID_SIZE 16
#define TOKEN 1
#define RULE 2
#define Eof 3
#define RETURN(_t) {token = _t; return;}
typedef struct _map {
char *id;
char newid[MAX_NEWID_SIZE];
struct _map *next;
} map;
static map *mapping = NULL;
static int c;
static int token;
static char lexbuf[MAX_ATOM_SIZE];
/* say malloc is 'char *' as some C compilers don't do 'void *' */
extern char *malloc();
static char *mapid();
static map *new_mapping_entry();
static char *newid();
static void lex();
main(argc, argv)
int argc;
char **argv;
{
if (argc != 1) {
fprintf(stderr,
"Usage: %s <action_free_grammar >renamed_grammar\n",
argv[0]);
exit(1);
}
c = getchar();
lex();
while ( token != Eof )
{
printf(" %s\n", mapid(&(lexbuf[0])));
lex();
}
putchar('\n');
return(0);
}
static void
lex()
{
char *p;
try_again:
while ( isspace(c) ) { c = getchar(); }
lexbuf[0] = '\0';
if ( c == EOF ) RETURN(Eof);
if ( c=='"' )
{
p = &(lexbuf[0]);
*p++ = c;
c = getchar();
while ( p<&lexbuf[MAX_ATOM_SIZE-2] )
{
if ( c=='"' ) {*p++ = '"'; c = getchar(); break;}
if ( c=='\\' )
{
*p++ = c;
c = getchar();
}
*p++ = c;
c = getchar();
}
*p = '\0';
RETURN(TOKEN);
}
else if ( islower(c) || isupper(c) || c=='_' )
{
for (p=&(lexbuf[0]); !isspace(c); c=getchar())
{
*p++ = c;
}
*p = '\0';
if ( islower(lexbuf[0]) ) {RETURN(RULE);}
else RETURN(TOKEN);
}
else
{
printf(" %c", c);
c = getchar();
goto try_again;
}
}
/* if id is not already defined, create a new id for it and store in
* 'mapping' return newid field of (possibly new) entry for id
*/
static char *
mapid(id)
char *id;
{
map *p;
char *prefix;
/* is 'id' in 'mapping' already? */
for (p=mapping; p!=NULL && strcmp(p->id, id)!=0; p=p->next)
{
;
}
if ( p == NULL ) /* new mapping? */
{
p = new_mapping_entry(id);
p->next = mapping;
mapping = p;
if ( islower(id[0]) ) prefix = "n";
else prefix = "T";
strcpy(&(p->newid[0]), newid(prefix));
}
return &(p->newid[0]);
}
static char *
newid(prefix)
char *prefix;
{
static char buf[MAX_NEWID_SIZE];
static int i = 1;
sprintf(&(buf[0]), "%s%d", prefix, i++);
return &(buf[0]);
}
static map *
new_mapping_entry(id)
char *id;
{
map *p = (map *) malloc(sizeof(map));
if ( p == NULL )
{
fprintf(stderr, "fatal: out of memory\n");
exit(1);
}
p->id = malloc(strlen(id)+1);
if ( p->id == NULL )
{
fprintf(stderr, "fatal: out of memory\n");
exit(1);
}
strcpy(p->id, id);
return p;
}
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.