[brlcad-commits] SF.net SVN: brlcad:[47555] brlcad/trunk/src/other/perplex
Open Source Solid Modeling CAD
Brought to you by:
brlcad
From: <n_...@us...> - 2011-11-18 20:53:41
|
Revision: 47555 http://brlcad.svn.sourceforge.net/brlcad/?rev=47555&view=rev Author: n_reed Date: 2011-11-18 20:53:34 +0000 (Fri, 18 Nov 2011) Log Message: ----------- pass app data to lemon parser; first try at embellishing input Modified Paths: -------------- brlcad/trunk/src/other/perplex/parser.y brlcad/trunk/src/other/perplex/perplex.c brlcad/trunk/src/other/perplex/perplex.h brlcad/trunk/src/other/perplex/scanner.re brlcad/trunk/src/other/perplex/template.c Modified: brlcad/trunk/src/other/perplex/parser.y =================================================================== --- brlcad/trunk/src/other/perplex/parser.y 2011-11-18 18:32:28 UTC (rev 47554) +++ brlcad/trunk/src/other/perplex/parser.y 2011-11-18 20:53:34 UTC (rev 47555) @@ -1,32 +1,69 @@ %include { #include <assert.h> +#include "perplex.h" #include "token_type.h" } %token_type {YYSTYPE} +%extra_argument {appData_t *appData} -file ::= definitions TOKEN_DEFINITIONS rules TOKEN_RULES code. +file ::= definitions_section rules_section code. +/* definitions section */ + +definitions_section ::= definitions TOKEN_SEPARATOR. +{ + FILE *templateFile = appData->template; + char c; + + while ((c = fgetc(templateFile)) != EOF) { + printf("%c", c); + } + printf("\n"); + fclose(templateFile); +} + definitions ::= /* empty */. -definitions ::= definitions TOKEN_ANY(s). +definitions ::= definitions TOKEN_DEFINITIONS(text). { - printf("%s", s.string); + printf("%s", text.string); + free(text); } -rules ::= /* empty */. +/* rules section */ + +rules_section ::= rules TOKEN_SEPARATOR. +{ + /* close re2c comment and scanner routine */ + printf("*/\n}\n"); +} + +rules ::= rule. rules ::= rules rule. -rule ::= TOKEN_PATTERN(pattern) TOKEN_ACTION(action). +rule ::= pattern TOKEN_LBRACE action TOKEN_RBRACE. { - printf("%s", pattern.string); - printf("\n{\n"); - printf("%s", action.string); - printf("\nCONTINUE;\n"); - printf("\n}\n"); + /* prevent fall-through to next rule */ + printf("\n CONTINUE;\n}\n"); } +pattern ::= TOKEN_PATTERN(text). +{ + printf("%s {", text.string); + free(text); +} + +action ::= TOKEN_ACTION(text). +{ + printf("%s", text.string); + free(text); +} + +/* code section */ + code ::= /* empty */. -code ::= code TOKEN_ANY(s). +code ::= code TOKEN_CODE(text). { - printf("%s", s.string); + printf("%s", text.string); + free(text); } Modified: brlcad/trunk/src/other/perplex/perplex.c =================================================================== --- brlcad/trunk/src/other/perplex/perplex.c 2011-11-18 18:32:28 UTC (rev 47554) +++ brlcad/trunk/src/other/perplex/perplex.c 2011-11-18 20:53:34 UTC (rev 47555) @@ -74,6 +74,7 @@ char c; int tokenID; void *parser; + appData_t *appData; if (argc != 2) { printf("Usage: perplex input.l\n"); @@ -93,25 +94,18 @@ scanner = perplexFileScanner(inFile); parser = ParseAlloc(malloc); - scanner->appData = malloc(sizeof(YYSTYPE)); + scanner->appData = malloc(sizeof(appData_t)); + appData = scanner->appData; + appData->in = inFile; + appData->template = templateFile; + while ((tokenID = yylex(scanner)) != YYEOF) { - switch(tokenID) { - case TOKEN_DEFINITIONS: - while ((c = fgetc(templateFile)) != EOF) { - printf("%c", c); - } - printf("\n"); - fclose(templateFile); - break; - case TOKEN_RULES: - printf("*/\n}\n"); - } - Parse(parser, tokenID, *(YYSTYPE*)scanner->appData); + Parse(parser, tokenID, appData->tokenData, appData); } - Parse(parser, 0, *(YYSTYPE*)scanner->appData); + Parse(parser, 0, appData->tokenData, appData); - free(scanner->appData); + free(appData); ParseFree(parser, free); perplexFree(scanner); fclose(inFile); Modified: brlcad/trunk/src/other/perplex/perplex.h =================================================================== --- brlcad/trunk/src/other/perplex/perplex.h 2011-11-18 18:32:28 UTC (rev 47554) +++ brlcad/trunk/src/other/perplex/perplex.h 2011-11-18 20:53:34 UTC (rev 47555) @@ -54,6 +54,7 @@ typedef enum YYCONDTYPE { DEFINITIONS, RULES, + ACTION, CODE } condition_t; @@ -64,8 +65,13 @@ int nmax; /* max capacity of elements. */ }; +typedef struct appData_t { + FILE *in, *template; + YYSTYPE tokenData; +} appData_t; + /* scanner data */ -typedef struct perplex_data_t { +typedef struct perplex_t { union { FILE *file; char *string; @@ -76,7 +82,7 @@ char *tokenStart; struct Buf *buffer; condition_t condition; - void *appData; + appData_t *appData; } *perplex_t; perplex_t perplexFileScanner(FILE *input); @@ -85,9 +91,9 @@ void *ParseAlloc(void *(*mallocProc)(size_t)); void ParseFree(void *parser, void (*freeProc)(void*)); -void Parse(void *parser, int tokenID, YYSTYPE data); +void Parse(void *parser, int tokenID, YYSTYPE tokenData, appData_t *appData); +void ParseTrace(FILE *fp, char *s); - #endif /* Modified: brlcad/trunk/src/other/perplex/scanner.re =================================================================== --- brlcad/trunk/src/other/perplex/scanner.re 2011-11-18 18:32:28 UTC (rev 47554) +++ brlcad/trunk/src/other/perplex/scanner.re 2011-11-18 20:53:34 UTC (rev 47555) @@ -88,7 +88,6 @@ return copy; } - #include <math.h> #include <stdio.h> #include <stdlib.h> @@ -420,7 +419,7 @@ newScanner() { perplex_t scanner; - scanner = (perplex_t)calloc(1, sizeof(struct perplex_data_t)); + scanner = (perplex_t)calloc(1, sizeof(struct perplex_t)); setCondition(scanner, DEFINITIONS); scanner->buffer = NULL; @@ -472,7 +471,10 @@ #define UPDATE_START scanner->tokenStart = scanner->cursor; #define yytext getTokenText(scanner, _perplex_token_string) -#define RETURN(id) buf_destroy(_perplex_token_string); return id; +#define RETURN(id) \ + buf_destroy(_perplex_token_string); \ + free(_perplex_token_string); \ + return id; #define CONTINUE UPDATE_START; continue; int yylex(perplex_t scanner) { @@ -486,15 +488,14 @@ while (1) { /*!re2c -re2c:condenumprefix = ""; -re2c:define:YYCONDTYPE = condition_t; -re2c:define:YYGETCONDITION:naked = 1; - re2c:yych:emit = 0; re2c:define:YYCTYPE = char; re2c:define:YYCURSOR = scanner->cursor; re2c:define:YYLIMIT = scanner->null; re2c:define:YYMARKER = scanner->marker; +re2c:condenumprefix = ""; +re2c:define:YYCONDTYPE = condition_t; +re2c:define:YYGETCONDITION:naked = 1; NAME = [a-zA-Z_][a-zA-Z0-9_-]*; EOL = '\n'; @@ -510,39 +511,51 @@ <*>EOF { RETURN(YYEOF); } -<*>FAUX_SEPARATOR { - /* just text - echo it */ - printf("%s\n", yytext); - CONTINUE; +<DEFINITIONS>FAUX_SEPARATOR { + scanner->appData->tokenData.string = copyString(yytext); + RETURN(TOKEN_DEFINITIONS); } - <DEFINITIONS>SEPARATOR { setCondition(scanner, RULES); + RETURN(TOKEN_SEPARATOR); +} +<DEFINITIONS>ANY { + scanner->appData->tokenData.string = copyString(yytext); RETURN(TOKEN_DEFINITIONS); } +<RULES>FAUX_SEPARATOR { + CONTINUE; +} <RULES>SEPARATOR { setCondition(scanner, CODE); - RETURN(TOKEN_RULES); + RETURN(TOKEN_SEPARATOR); } -<RULES>'{'[^}\000]*'}' { - char *text = yytext; - char *copy = copyString(text); - ((YYSTYPE*)scanner->appData)->string = copy; - RETURN(TOKEN_ACTION); -} -<RULES>LINE_NOT_WHITE+ { - char *text = yytext; - char *copy = copyString(text); - ((YYSTYPE*)scanner->appData)->string = copy; +<RULES>[^\000\n\t {]+ { + scanner->appData->tokenData.string = copyString(yytext); RETURN(TOKEN_PATTERN); } - -<*>ANY { - /* just text - echo it */ - printf("%s", yytext); +<RULES>'{' { + setCondition(scanner, ACTION); + RETURN(TOKEN_LBRACE); +} +<RULES>ANY { CONTINUE; } + +<ACTION>'}' { + setCondition(scanner, RULES); + RETURN(TOKEN_RBRACE); +} +<ACTION>[^}\000]+ { + scanner->appData->tokenData.string = copyString(yytext); + RETURN(TOKEN_ACTION); +} + +<CODE>ANY { + scanner->appData->tokenData.string = copyString(yytext); + RETURN(TOKEN_CODE); +} */ } } Modified: brlcad/trunk/src/other/perplex/template.c =================================================================== --- brlcad/trunk/src/other/perplex/template.c 2011-11-18 18:32:28 UTC (rev 47554) +++ brlcad/trunk/src/other/perplex/template.c 2011-11-18 20:53:34 UTC (rev 47555) @@ -400,7 +400,7 @@ newScanner() { perplex_t scanner; - scanner = (perplex_t)calloc(1, sizeof(struct perplex_data_t)); + scanner = (perplex_t)calloc(1, sizeof(struct perplex_t)); setCondition(scanner, DEFINITIONS); scanner->buffer = NULL; @@ -452,12 +452,24 @@ #define UPDATE_START scanner->tokenStart = scanner->cursor; #define yytext getTokenText(scanner, _perplex_token_string) -#define RETURN(id) buf_destroy(_perplex_token_string); return id; +#define RETURN(id) \ + buf_destroy(_perplex_token_string); \ + free(_perplex_token_string); \ + return id; #define CONTINUE UPDATE_START; continue; -int yylex(perplext_t scanner) -{ +int yylex(perplex_t scanner) { + char yych; + struct Buf *_perplex_token_string; + + UPDATE_START; + + _perplex_token_string = (struct Buf*)malloc(sizeof(struct Buf)); + buf_init(_perplex_token_string, sizeof(char)); + + while (1) { /*!re2c +re2c:yych:emit = 0; re2c:define:YYCTYPE = char; re2c:define:YYCURSOR = scanner->cursor; re2c:define:YYLIMIT = scanner->null; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |