help-bison
[Top][All Lists]
Advanced

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

Flex/Bison converter doesn't stop on EOF


From: Steve Litt
Subject: Flex/Bison converter doesn't stop on EOF
Date: Tue, 5 Dec 2023 14:05:22 -0500

Hi all,

I've written a Flex source file and Bison source file, linked together
with the Flex derived scanner acting as a library for the Bison derived
parser. The resulting executable does exactly what I want it to do:
Replace every instance of the string "ball" in stdin with the string
"Hello world\n" in stdout. My program's one and only purpose is to
provide a proof of concept scanner/parser program where the scanner
reads, passing tokens and content to the parser, which modifies the
tokens and content and sends the mods to stdout. Armed with this proof
of concept, I and others who learn from me can incrementally keep
learning and thus incrementally keep making our compilers and
converters more competent. 

In other words, the purpose of this program is to produce the simplest
possible 

Although my program correctly replaces every "ball" with "Hello
World\n", it has the problem that it keeps running after the input
file's end of file. I'm hoping one of you can give me some direction in
finding the reason it keeps trying to process after input eof, whether
supplied by a cat command or echo command (Ctrl+d does nothing). With
diagnostic printfs I've determined that the yylex() I use, which is the
default one, doesn't return 0 upon encountering the input file's EOF.

If it matters, I'm running this on an up to date 64 bit GCC compiled
Void Linux OS.

Do you have any advice to help me continue to narrow down this "never
stops" problem? I'd love to hear anything you have to say. In case it
helps you give me advice, the following are the files comprising my
proof of concept:


============== Compilation and run script ===============
#!/bin/ksh

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

# Following line runs the converter
cat datahellopair.txt | ./hellopair.exb
=========================================================


============== Input file (datahellopair.txt) ===========
Ballroom dancing is cool,
beachballs are cooler.
I had a ball at the party last night.
=========================================================

=============== Output via stdout to screen =============
Starting...
Hello World
room dancing is cool,
beachHello World
s are cooler.
I had a Hello World
 at the party last night.

=========================================================

About the preceding ouput, as I mentioned, it never returns to the
command prompt.

====================== hellopair.l ======================
%option noinput nounput
%{
#include "hellopair.tab.h"
int yyerror(char *errormsg);
%}
  
%%

[Bb]all       { return TARGET;  }

%%


int yywrap(void)
{
     return 0;
}

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

==================== hellopair.y ========================
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
int yyerror(const char *s);

%}

%token TARGET
%token OTHER

%%

document: chunk | document chunk;
chunk: 
OTHER
| TARGET;

%%

int main(int argc, char *argv[]){
        int tok;
        printf("\nStarting...\n");
        while((tok = yylex()) > 0){
                if (tok == TARGET){
                        printf("Hello World\n");
                }
        }
        printf("Done!\n\n");
        yyparse();
}
=========================================================

If you have any thoughts on how I can narrow down the root cause scope
of my converter program not returning to the command prompt upon the
end of the input file, I'd appreciate the any guidance you may have.

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]