help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

My baby text to HTML paragraph converter


From: Steve Litt
Subject: My baby text to HTML paragraph converter
Date: Thu, 14 Dec 2023 02:20:17 -0500

Hi all,

After over a week of trying and asking voluminous questions on this
mailing list (and getting voluminous help, thank you), I finally made a
text to HTML converter that took blank line separated paragraphs and
installed <p> and </p> to surround them. All the relevant files are in
this message body...


==================== Preprocessor ==================================
#!/usr/bin/python
# preproc.py
import sys
lines = sys.stdin.readlines()
toptrash = True
for line in lines:
    line = line.strip()
    if toptrash:
        if line == "":
            continue
        else:
            toptrash = False
    print(line);
print("\n\n")
====================================================================


======================= Compilation shellscript =================
#!/bin/ksh

rm paragraphs.tab.c
rm paragraphs.tab.h
rm paragraphs.exb
rm paragraphs.lex.c
rm a.out

bison --html -d paragraphs.y
flex -o paragraphs.lex.c paragraphs.l
gcc -Wall -o paragraphs.exb paragraphs.tab.c paragraphs.lex.c -lfl


# Following line runs the converter
cat dataparagraphs.txt | ./preproc.py | ./paragraphs.exb
====================================================================


=================== Input File ===================================
Steve was here,
and now is gone,
but left his name
to carry on.

Flex and Bison:
Use as one.
When you need to parse,
they get it done.
====================================================================


===================== Output =======================================
<p>Steve was here,
and now is gone,
but left his name
to carry on.</p>

<p>Flex and Bison:
Use as one.
When you need to parse,
they get it done.</p>
====================================================================


========================= paragraphs.l =============================
%option noinput nounput
%{
#include "paragraphs.tab.h"
%}
  
%%
(\n){2,} {return SEP;}
. {strcpy (yylval.y_char, yytext); return CHARACTER;  };

%%


int yywrap(void)
{
     return 1;
}

int yyerror(char *errormsg)
{
      fprintf(stderr, "%s\n", errormsg);
      exit(1);
}
====================================================================


===================== paragraphs.y =================================
%{

#include <stdio.h>
#include <stdlib.h>
int yylex(void);
int yyerror (char *errmsg);
#define EOF_ 0

int prevtok = 0;
%}

%union {
        char    y_char [10000];
}

%token <y_char>   SEP
%token <y_char>   CHARACTER

%%

wholefile : thing
          | wholefile thing
;

thing : character | sep;

character : CHARACTER {
        if(prevtok == SEP){
                printf("\n\n<p>");
        }
        printf($1);
        prevtok = CHARACTER;
}

sep: SEP {
        printf("</p>");
        prevtok = SEP;
}

%%

int main(int argc, char *argv[]){
        printf("<p>");
        yyparse();
        if(prevtok == CHARACTER)
                printf("</p>");
        printf("\n");
}
====================================================================

Thanks,

SteveT

Steve Litt 

Autumn 2023 featured book: Rapid Learning for the 21st Century
http://www.troubleshooters.com/rl21



reply via email to

[Prev in Thread] Current Thread [Next in Thread]