[Redbutton-devel] SF.net SVN: redbutton: [362] redbutton-author/trunk
Brought to you by:
skilvington
|
From: <ski...@us...> - 2007-08-31 07:22:12
|
Revision: 362
http://redbutton.svn.sourceforge.net/redbutton/?rev=362&view=rev
Author: skilvington
Date: 2007-08-31 00:21:40 -0700 (Fri, 31 Aug 2007)
Log Message:
-----------
parse everything except enum values
Modified Paths:
--------------
redbutton-author/trunk/Makefile
redbutton-author/trunk/ccc.y
redbutton-author/trunk/mhegc.c
redbutton-author/trunk/parser.c.header
redbutton-author/trunk/parser.h.footer
redbutton-author/trunk/parser.h.header
redbutton-author/trunk/parser.l.footer
redbutton-author/trunk/parser.l.header
redbutton-author/trunk/tokens.h.header
Modified: redbutton-author/trunk/Makefile
===================================================================
--- redbutton-author/trunk/Makefile 2007-08-30 16:29:28 UTC (rev 361)
+++ redbutton-author/trunk/Makefile 2007-08-31 07:21:40 UTC (rev 362)
@@ -1,6 +1,9 @@
CC=gcc
CFLAGS=-Wall -O2
+# for vasprintf
+DEFS=-D_GNU_SOURCE
+
LEX=flex
YACC=bison
#LEX=lex
@@ -16,21 +19,21 @@
TARDIR=`basename ${PWD}`
mhegc: parser.h ${OBJS}
- ${CC} ${CFLAGS} -o mhegc ${OBJS} ${LIBS}
+ ${CC} ${CFLAGS} ${DEFS} -o mhegc ${OBJS} ${LIBS}
ccc: ccc.y ccc.l asn1type.o
${LEX} -i -t ccc.l > lex.ccc.c
${YACC} -b ccc -d ccc.y
- ${CC} ${CFLAGS} -o ccc lex.ccc.c ccc.tab.c asn1type.o
+ ${CC} ${CFLAGS} ${DEFS} -o ccc lex.ccc.c ccc.tab.c asn1type.o
lex.parser.o parser.o parser.h: parser.l.* parser.c.* parser.h.* tokens.h.* grammar ccc
cat grammar | ./ccc -l parser.l -p parser.c -h parser.h -t tokens.h
${LEX} -i -t parser.l > lex.parser.c
- ${CC} ${CFLAGS} -c lex.parser.c
- ${CC} ${CFLAGS} -c parser.c
+ ${CC} ${CFLAGS} ${DEFS} -c lex.parser.c
+ ${CC} ${CFLAGS} ${DEFS} -c parser.c
.c.o:
- ${CC} ${CFLAGS} -c $<
+ ${CC} ${CFLAGS} ${DEFS} -c $<
install: mhegc
install -m 755 mhegc ${DESTDIR}/bin
Modified: redbutton-author/trunk/ccc.y
===================================================================
--- redbutton-author/trunk/ccc.y 2007-08-30 16:29:28 UTC (rev 361)
+++ redbutton-author/trunk/ccc.y 2007-08-31 07:21:40 UTC (rev 362)
@@ -1,5 +1,4 @@
%{
-#define _GNU_SOURCE /* for vasprintf */
#include <unistd.h>
#include <stdio.h>
#include <string.h>
@@ -72,7 +71,6 @@
void output_def(char *);
-void print_tokens(struct str_list *);
char *add_token(struct str_list **, char *);
char *unquote(char *);
@@ -377,6 +375,7 @@
/* C code for the parse_Xxx functions */
buf_append(&state.parse_hdr, "void parse_%s(struct state *);\n", name);
buf_append(&state.parse_fns, "void parse_%s(struct state *state)\n{\n", name);
+buf_append(&state.parse_fns, "printf(\"<%s>\\n\");\n", name);
/* count how many items make it up */
nitems = 0;
/* skip literals at the start */
@@ -391,13 +390,17 @@
}
if(nitems == 1)
{
+ buf_append(&state.parse_fns, "\ttoken_t next;\n\n");
item = state.items;
while(item && item->type == IT_LITERAL)
{
-buf_append(&state.parse_fns, "// TODO: eat %s\n\n", item->name);
+ char *tok_name = unquote(item->name);
+ buf_append(&state.parse_fns, "\t/* %s */\n", item->name);
+ buf_append(&state.parse_fns, "\texpect_token(%s, %s);\n\n", tok_name, item->name);
+ free(tok_name);
item = item->next;
}
- buf_append(&state.parse_fns, "\ttoken_t next = next_token();\n\n");
+ buf_append(&state.parse_fns, "\tnext = peek_token();\n\n");
if(item->type == IT_IDENTIFIER)
{
buf_append(&state.parse_fns, "\t/* %s */\n", item->name);
@@ -418,9 +421,8 @@
buf_append(&state.parse_fns, "\twhile(is_%s(next))\n", item->name);
buf_append(&state.parse_fns, "\t{\n");
buf_append(&state.parse_fns, "\t\tparse_%s(state);\n", item->name);
- buf_append(&state.parse_fns, "\t\tnext = next_token();\n");
+ buf_append(&state.parse_fns, "\t\tnext = peek_token();\n");
buf_append(&state.parse_fns, "\t}\n");
- buf_append(&state.parse_fns, "\n\tunget_token(next);\n");
}
else
{
@@ -429,7 +431,10 @@
item = item->next;
while(item)
{
-buf_append(&state.parse_fns, "\n// TODO: eat %s\n", item->name);
+ char *tok_name = unquote(item->name);
+ buf_append(&state.parse_fns, "\t/* %s */\n", item->name);
+ buf_append(&state.parse_fns, "\texpect_token(%s, %s);\n\n", tok_name, item->name);
+ free(tok_name);
item = item->next;
}
}
@@ -442,7 +447,7 @@
/* assert */
if(state.and_items)
fatal("CHOICE or ENUMERATED type, but and_items set");
- buf_append(&state.parse_fns, "\ttoken_t next = next_token();\n\n");
+ buf_append(&state.parse_fns, "\ttoken_t next = peek_token();\n\n");
buf_append(&state.parse_fns, "\t/* CHOICE or ENUMERATED */\n");
item = state.items;
for(item=state.items; item; item=item->next)
@@ -481,12 +486,15 @@
item = state.items;
while(item && item->type == IT_LITERAL)
{
-buf_append(&state.parse_fns, "// TODO: eat %s\n\n", item->name);
+ char *tok_name = unquote(item->name);
+ buf_append(&state.parse_fns, "\t/* %s */\n", item->name);
+ buf_append(&state.parse_fns, "\texpect_token(%s, %s);\n\n", tok_name, item->name);
+ free(tok_name);
item = item->next;
}
buf_append(&state.parse_fns, "\t/* SET */\n");
buf_append(&state.parse_fns, "\twhile(true)\n\t{\n");
- buf_append(&state.parse_fns, "\t\tnext = next_token();\n");
+ buf_append(&state.parse_fns, "\t\tnext = peek_token();\n");
while(item && item->type != IT_LITERAL)
{
if(item->type != IT_IDENTIFIER && item->type != IT_OPTIONAL)
@@ -504,7 +512,10 @@
/* eat any trailing literals */
while(item && item->type == IT_LITERAL)
{
-buf_append(&state.parse_fns, "\n// TODO: eat %s\n", item->name);
+ char *tok_name = unquote(item->name);
+ buf_append(&state.parse_fns, "\t/* %s */\n", item->name);
+ buf_append(&state.parse_fns, "\texpect_token(%s, %s);\n\n", tok_name, item->name);
+ free(tok_name);
item = item->next;
}
break;
@@ -523,11 +534,13 @@
buf_append(&state.parse_fns, "\n\t/* %s */\n", item->name);
if(item->type == IT_LITERAL)
{
-buf_append(&state.parse_fns, "// TODO: eat %s\n", item->name);
+ char *tok_name = unquote(item->name);
+ buf_append(&state.parse_fns, "\texpect_token(%s, %s);\n\n", tok_name, item->name);
+ free(tok_name);
}
else
{
- buf_append(&state.parse_fns, "\tnext = next_token();\n");
+ buf_append(&state.parse_fns, "\tnext = peek_token();\n");
buf_append(&state.parse_fns, "\tif(is_%s(next))\n", item->name);
buf_append(&state.parse_fns, "\t\tparse_%s(state);\n", item->name);
if(item->type != IT_OPTIONAL)
@@ -544,6 +557,7 @@
break;
}
}
+buf_append(&state.parse_fns, "printf(\"</%s>\\n\");\n", name);
buf_append(&state.parse_fns, "}\n\n");
/* C code for the is_Xxx functions */
@@ -630,18 +644,6 @@
return;
}
-void
-print_tokens(struct str_list *t)
-{
- while(t)
- {
- printf("%%token %s\n", t->name);
- t = t->next;
- }
-
- return;
-}
-
char *
add_token(struct str_list **head, char *quoted)
{
Modified: redbutton-author/trunk/mhegc.c
===================================================================
--- redbutton-author/trunk/mhegc.c 2007-08-30 16:29:28 UTC (rev 361)
+++ redbutton-author/trunk/mhegc.c 2007-08-31 07:21:40 UTC (rev 362)
@@ -32,6 +32,9 @@
parse_InterchangedObject(&state);
+ if(next_token())
+ parse_error("Unexpected text after InterchangedObject");
+
return EXIT_SUCCESS;
}
Modified: redbutton-author/trunk/parser.c.header
===================================================================
--- redbutton-author/trunk/parser.c.header 2007-08-30 16:29:28 UTC (rev 361)
+++ redbutton-author/trunk/parser.c.header 2007-08-31 07:21:40 UTC (rev 362)
@@ -5,8 +5,6 @@
int yylineno = 1;
-int yylex(void);
-
void
yyerror(const char *str)
{
@@ -25,29 +23,65 @@
void parse_INTEGER(struct state *state)
{
+ if(next_token() != INTEGER)
+ yyerror("Expecting INTEGER");
+
+printf("<INTEGER value=%s/>\n", token_text());
+
+ return;
}
void parse_BOOLEAN(struct state *state)
{
+ if(next_token() != BOOLEAN)
+ yyerror("Expecting BOOLEAN");
+
+printf("<BOOLEAN value=%s/>\n", token_text());
+
+ return;
}
void parse_STRING(struct state *state)
{
+ if(next_token() != STRING)
+ yyerror("Expecting STRING");
+
+printf("<STRING value=%s/>\n", token_text());
+
+ return;
}
void parse_QPRINTABLE(struct state *state)
{
+ if(next_token() != QPRINTABLE)
+ yyerror("Expecting QPRINTABLE");
+
+printf("<QPRINTABLE value=%s/>\n", token_text());
+
+ return;
}
void parse_BASE64(struct state *state)
{
+ if(next_token() != BASE64)
+ yyerror("Expecting BASE64");
+
+printf("<BASE64 value=%s/>\n", token_text());
+
+ return;
}
void parse_Null(struct state *state)
{
+ if(next_token() != Null)
+ yyerror("Expecting Null");
+
+printf("<Null/>\n");
+
+ return;
}
Modified: redbutton-author/trunk/parser.h.footer
===================================================================
--- redbutton-author/trunk/parser.h.footer 2007-08-30 16:29:28 UTC (rev 361)
+++ redbutton-author/trunk/parser.h.footer 2007-08-31 07:21:40 UTC (rev 362)
@@ -6,5 +6,4 @@
#define is_BASE64(TOK) (TOK == BASE64)
#define is_Null(TOK) (TOK == Null)
-#endif /* __NEW_PARSER_H__ */
-
+#endif /* __PARSER_H__ */
Modified: redbutton-author/trunk/parser.h.header
===================================================================
--- redbutton-author/trunk/parser.h.header 2007-08-30 16:29:28 UTC (rev 361)
+++ redbutton-author/trunk/parser.h.header 2007-08-31 07:21:40 UTC (rev 362)
@@ -1,8 +1,16 @@
-#ifndef __NEW_PARSER_H__
-#define __NEW_PARSER_H__
+#ifndef __PARSER_H__
+#define __PARSER_H__
+#include <stdio.h>
#include <stdbool.h>
struct state { int dummy; };
typedef int token_t;
+token_t peek_token(void);
+void expect_token(token_t, char *);
+token_t next_token(void);
+char *token_text(void);
+
+void parse_error(const char *, ...);
+
Modified: redbutton-author/trunk/parser.l.footer
===================================================================
--- redbutton-author/trunk/parser.l.footer 2007-08-30 16:29:28 UTC (rev 361)
+++ redbutton-author/trunk/parser.l.footer 2007-08-31 07:21:40 UTC (rev 362)
@@ -3,19 +3,57 @@
/* don't use #define in case they are macros too */
token_t
+peek_token(void)
+{
+ token_t tok = yylex();
+
+ /* return it to the input stream */
+ yyless(0);
+
+ return tok;
+}
+
+void
+expect_token(token_t tok, char *name)
+{
+ if(next_token() != tok)
+ parse_error("Unexpected token; expecting '%s'", name);
+
+ return;
+}
+
+token_t
next_token(void)
{
return yylex();
}
-void
-unget_token(token_t tok)
+char *
+token_text(void)
{
- unput(tok);
+ return yytext;
}
+void yyerror(const char *);
+
void
-parse_error(char *err)
+parse_error(const char *fmt, ...)
{
+ va_list ap;
+ char *err;
+
+ va_start(ap, fmt);
+ if(vasprintf(&err, fmt, ap) < 0)
+ {
+ fprintf(stderr, "Out of memory or illegal format string");
+ exit(EXIT_FAILURE);
+ }
+ va_end(ap);
+
yyerror(err);
+
+ free(err);
+
+ return;
}
+
Modified: redbutton-author/trunk/parser.l.header
===================================================================
--- redbutton-author/trunk/parser.l.header 2007-08-30 16:29:28 UTC (rev 361)
+++ redbutton-author/trunk/parser.l.header 2007-08-31 07:21:40 UTC (rev 362)
@@ -1,4 +1,6 @@
%{
+#include <stdio.h>
+#include <stdarg.h>
#include "parser.h"
#include "tokens.h"
extern int yylineno;
Modified: redbutton-author/trunk/tokens.h.header
===================================================================
--- redbutton-author/trunk/tokens.h.header 2007-08-30 16:29:28 UTC (rev 361)
+++ redbutton-author/trunk/tokens.h.header 2007-08-31 07:21:40 UTC (rev 362)
@@ -1,3 +1,4 @@
+#define END_OF_SRC 0 /* must be 0 */
#define INVALID 1
#define INTEGER 2
#define BOOLEAN 3
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|