Browse Source

make org_lexer.l reentrant

- org_lexer.l has the %option reentrant
- bison is no longer used
- main.c was updated to work with a reentrant parser
- object.c was deleted
Andrew Young 8 years ago
parent
commit
b7bbb7917f
5 changed files with 57 additions and 72 deletions
  1. 5 0
      configure.ac
  2. 2 3
      src/Makefile.am
  3. 43 21
      src/main.c
  4. 7 11
      src/org_lexer.l
  5. 0 37
      src/org_parser.y

+ 5 - 0
configure.ac

@@ -11,6 +11,11 @@ AC_PROG_CC   # c compiler
 gl_EARLY     # gnu lib
 AM_PROG_CC_C_O
 AM_PROG_LEX  # flex
+if test "x$LEX" != xflex; then
+  AC_MSG_WARN([To generate the lexer, Flex is required.  Lexer will not be regenerated.])
+  AC_SUBST([LEX],[:])
+fi
+
 AC_PROG_YACC # bison
 
 # gnulib macros

+ 2 - 3
src/Makefile.am

@@ -1,11 +1,10 @@
 bin_PROGRAMS = org-merge-driver
 
 # Bison and Flex
-BUILT_SOURCES = org_parser.h org_lexer.h
-AM_YFLAGS = -d
+BUILT_SOURCES = org_lexer.h
 AM_LFLAGS = 
 
 org_merge_driver_SOURCES = org_parser.y org_lexer.l main.c elt_heading.c tree.c
 
 org_lexer.h: org_lexer.l
-	lex $^
+	flex $^

+ 43 - 21
src/main.c

@@ -1,32 +1,54 @@
 #include <stdio.h>
 #include <ctype.h>
-/*
-#include "Parser.h"
-#include "Lexer.h"
-*/
-#include "org_parser.h"
+#include <iconv.h>
 #include "org_lexer.h"
 
-char *progname;
-//double yylval;
-
-main( argc, argv )
-char *argv[];
+int
+main (int argc, char *argv[])
 {
-  progname = argv[0];
+  /* Process command line arguments */
+
+  char *progname = argv[0];
+
+
+  /* Initialize flex data structures */
+  yyscan_t scanner;
+  int err = 0;
+  err = yylex_init (&scanner);
+  if (err == ENOMEM) 
+    {
+      /* Memory allocation error.
+       * Abort execution. */
+    }
+  else if (err == EINVAL)
+    {
+      /* Invalid yylex_init argument 
+       * Abort execution. */
+    }
+
+  /* If a file path was passed, open that file and utilize it as the
+     flex source */
   if (argc > 1)
     {
-      yyin = fopen (argv[1], "r");
+      yyset_in (fopen (argv[1], "r"), scanner);
     }
-  if (yyin == 0)
-    yyin = stdin;
-  //yylex();
 
-  yyparse();
-}
+  /* If no file path is passed, use stdin as flex source */
+  if (yyget_in (scanner) == NULL)
+    {
+      yyset_in (stdin, scanner);
+    }
 
-yyerror( s )
-char *s;
-{
-  fprintf( stderr ,"%s: %s\n" , progname , s );
+  /* Establish source encoding */
+  /* Currently, assume source encoding is utf-8 */
+  iconv_t iconv_table = iconv_open ("UTF-8", "UTF-8");
+
+
+
+  yylex (&scanner);
+  yylex_destroy (&scanner);
+  iconv_close (&iconv_table);
+
+  /* Close yyin */
+  fclose (yyget_in(scanner));
 }

+ 7 - 11
src/org_lexer.l

@@ -1,30 +1,22 @@
 /* Org Mode file lexer -*-c-*- */
 
-
 %{
 /* Org Mode lexer */
 #include <stdio.h>
 #include <stdlib.h>
-#include "org_parser.h"
 
-int yywrap();
+  //int yywrap();
 
 /* extern int yylex (void); */
 extern void yyerror (char const  *);
-%}
 
+%}
 
 /* Flex Options */
 /* Generate a reentrant parser */
 %option reentrant
+/* %option extra-type="struct iconv_extra" */
 %option header-file="org_lexer.h"
-/* %option reentrant */
-/* %option bison-bridge */
-/* %option bison-locations */
-/* %option yylineno */
-/* %option warn nodefault */
-/* %option noyywrap */
-/* %option bison-bridge */
 
 
 /* Rule Start Conditions */
@@ -33,6 +25,7 @@ extern void yyerror (char const  *);
 
 
 %%
+
 [\n]*"\n\n\n" { /* New element detection */
    printf ( "element break; %d\n", yyleng);
    //return NEW_ELEMENT;
@@ -111,9 +104,12 @@ extern void yyerror (char const  *);
 }
 %%
 
+/*
 int
 yywrap (void)
 {
   printf ("End of File \n");
   return 1;
 }
+  
+*/

+ 0 - 37
src/org_parser.y

@@ -1,37 +0,0 @@
-/* Org Mode file parser -*-c-*- */
-%{
-#include <stdio.h>
-%}
-
-%defines
-
-%token HEADING TEXT NUMBERED_LIST LIST NEW_LINE NEW_ELEMENT ELEMENT_BREAK
-
-%%
-org_document: /* empty */
-| org_document heading
-| org_document text
-| org_document numbered_list
-| org_document list
-| org_document NEW_ELEMENT
-;
-
-heading: HEADING TEXT NEW_LINE {printf ("HEADING ");}
-| HEADING NEW_LINE {printf ("heading blank\n");}
-;
-
-numbered_list: NUMBERED_LIST TEXT NEW_LINE {printf ("NUMBERED_LIST ");}
-| NUMBERED_LIST NEW_LINE
-| numbered_list TEXT {printf ("nl_text ");}
-| numbered_list NUMBERED_LIST {printf (" element ");}
-| numbered_list NEW_LINE {printf ("nl_newline\n");}
-;
-
-list: LIST {printf ("new list\n");}
-| list TEXT {printf ("list text\n");}
-| list NEW_LINE
-
-text: TEXT {printf ("new text\n");}
-| text NEW_LINE
-
-%%