[brlcad-commits] SF.net SVN: brlcad:[47537] brlcad/trunk/src/other/perplex
Open Source Solid Modeling CAD
Brought to you by:
brlcad
From: <n_...@us...> - 2011-11-17 22:50:30
|
Revision: 47537 http://brlcad.svn.sourceforge.net/brlcad/?rev=47537&view=rev Author: n_reed Date: 2011-11-17 22:50:24 +0000 (Thu, 17 Nov 2011) Log Message: ----------- adding lemon parser; moving towards useful conversion of input Modified Paths: -------------- brlcad/trunk/src/other/perplex/Makefile.local brlcad/trunk/src/other/perplex/perplex.c brlcad/trunk/src/other/perplex/perplex.h brlcad/trunk/src/other/perplex/scanner.re Added Paths: ----------- brlcad/trunk/src/other/perplex/parser.y brlcad/trunk/src/other/perplex/token_type.h Modified: brlcad/trunk/src/other/perplex/Makefile.local =================================================================== --- brlcad/trunk/src/other/perplex/Makefile.local 2011-11-17 22:39:52 UTC (rev 47536) +++ brlcad/trunk/src/other/perplex/Makefile.local 2011-11-17 22:50:24 UTC (rev 47537) @@ -1,14 +1,17 @@ CFLAGS = -g -ansi -pedantic -perplex : perplex.o scanner.o +perplex : perplex.o scanner.o parser.o $(CC) -o $@ $^ -lm -scanner.c perplex.o : perplex.h +scanner.c perplex.o : perplex.h parser.h scanner.c : scanner.re re2c -c -o $@ $< +parser.c parser.h : parser.y + lemon -q $< + .PHONY : clean clean : - -rm -f perplex *.o scanner.c + -rm -f perplex *.o scanner.c parser.c parser.h Added: brlcad/trunk/src/other/perplex/parser.y =================================================================== --- brlcad/trunk/src/other/perplex/parser.y (rev 0) +++ brlcad/trunk/src/other/perplex/parser.y 2011-11-17 22:50:24 UTC (rev 47537) @@ -0,0 +1,32 @@ +%include { +#include <assert.h> +#include "token_type.h" +} + +%token_type {YYSTYPE} + +file ::= definitions TOKEN_DEFINITIONS rules TOKEN_RULES code. + +definitions ::= /* empty */. +definitions ::= definitions TOKEN_ANY(s). +{ + printf("%s", s.string); +} + +rules ::= /* empty */. +rules ::= rules rule. + +rule ::= TOKEN_PATTERN(pattern) TOKEN_ACTION(action). +{ + printf("%s", pattern.string); + printf("\n{\n"); + printf("%s", action.string); + printf("\nCONTINUE;\n"); + printf("\n}\n"); +} + +code ::= /* empty */. +code ::= code TOKEN_ANY(s). +{ + printf("%s", s.string); +} Property changes on: brlcad/trunk/src/other/perplex/parser.y ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + native Modified: brlcad/trunk/src/other/perplex/perplex.c =================================================================== --- brlcad/trunk/src/other/perplex/perplex.c 2011-11-17 22:39:52 UTC (rev 47536) +++ brlcad/trunk/src/other/perplex/perplex.c 2011-11-17 22:50:24 UTC (rev 47537) @@ -73,6 +73,7 @@ FILE *inFile, *templateFile; char c; int tokenID; + void *parser; if (argc != 2) { printf("Usage: perplex input.l\n"); @@ -90,7 +91,10 @@ } scanner = perplexFileScanner(inFile); + parser = ParseAlloc(malloc); + scanner->appData = malloc(sizeof(YYSTYPE)); + while ((tokenID = yylex(scanner)) != YYEOF) { switch(tokenID) { case TOKEN_DEFINITIONS: @@ -103,8 +107,12 @@ case TOKEN_RULES: printf("*/\n}\n"); } + Parse(parser, tokenID, *(YYSTYPE*)scanner->appData); } + Parse(parser, 0, *(YYSTYPE*)scanner->appData); + free(scanner->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-17 22:39:52 UTC (rev 47536) +++ brlcad/trunk/src/other/perplex/perplex.h 2011-11-17 22:50:24 UTC (rev 47537) @@ -42,17 +42,14 @@ #define PERPLEX_H #include <stdio.h> +#include "parser.h" +#include "token_type.h" /* tokens are typically ints defined starting from 0, * thus -1 is least likely to conflict with another token */ #define YYEOF -1 -enum { - TOKEN_DEFINITIONS, - TOKEN_RULES -}; - /* support for start conditions */ typedef enum YYCONDTYPE { DEFINITIONS, @@ -79,12 +76,18 @@ char *tokenStart; struct Buf *buffer; condition_t condition; + void *appData; } *perplex_t; perplex_t perplexFileScanner(FILE *input); perplex_t perplexStringScanner(char *input); void perplexFree(perplex_t scanner); +void *ParseAlloc(void *(*mallocProc)(size_t)); +void ParseFree(void *parser, void (*freeProc)(void*)); +void Parse(void *parser, int tokenID, YYSTYPE data); + + #endif /* Modified: brlcad/trunk/src/other/perplex/scanner.re =================================================================== --- brlcad/trunk/src/other/perplex/scanner.re 2011-11-17 22:39:52 UTC (rev 47536) +++ brlcad/trunk/src/other/perplex/scanner.re 2011-11-17 22:50:24 UTC (rev 47537) @@ -59,7 +59,9 @@ */ #include <stdio.h> #include <stdlib.h> +#include <string.h> #include "perplex.h" +#include "token_type.h" /* start-condition support */ @@ -75,11 +77,18 @@ return scanner->condition; } -#include <math.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> +static void* +copyString(const char *str) +{ + void *copy; + size_t numChars = strlen(str) + 1; + copy = malloc(numChars * sizeof(char)); + strncpy(copy, str, numChars); + + return copy; +} + #include <math.h> #include <stdio.h> #include <stdlib.h> @@ -491,7 +500,10 @@ EOL = '\n'; EOF = '\000'; ANY = [^\000]; +WHITE = [\n\t ]; LINE_ANY = [^\000\n]; +LINE_WHITE = [\t ]; +LINE_NOT_WHITE = [^\000\n\t ]; SEPARATOR = "%%"EOL; FAUX_SEPARATOR = LINE_ANY"%%"EOL; @@ -513,6 +525,18 @@ setCondition(scanner, CODE); RETURN(TOKEN_RULES); } +<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; + RETURN(TOKEN_PATTERN); +} <*>ANY { /* just text - echo it */ Added: brlcad/trunk/src/other/perplex/token_type.h =================================================================== --- brlcad/trunk/src/other/perplex/token_type.h (rev 0) +++ brlcad/trunk/src/other/perplex/token_type.h 2011-11-17 22:50:24 UTC (rev 47537) @@ -0,0 +1,8 @@ +#ifndef TOKEN_TYPE_H +#define TOKEN_TYPE_H + +typedef union YYSTYPE { + char *string; +} YYSTYPE; + +#endif Property changes on: brlcad/trunk/src/other/perplex/token_type.h ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |