help-bison
[Top][All Lists]
Advanced

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

Updated Flex/Bison Hello World


From: Steve Litt
Subject: Updated Flex/Bison Hello World
Date: Sun, 10 Dec 2023 10:27:22 -0500

Hi all,

A few days ago I posted a Flex/Bison Hello World. Offlist, somebody
guided me in understanding that the scanner (lexer) should not print
anything, but instead should pass all strings (semantics I believe
they're called in this use case) to the parser, which can do with them
as the parser sees fit. He then guided me in making changes in order to
accomplish this, including replacing my yylex() loop with a single call
to yyparse(). His suggestions work perfectly and will form the basis
for my further work (thank you offline person!).

All code concerning the new, Parser-prints-everything Hello World is
posted following this sentence.


============== 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.

Input file ended!
Done!
=========================================================

====================== hellopair.l ======================
%option noinput nounput
%{
#include "hellopair.tab.h"
int yylex(void);
%}
  
%%

[Bb]all  { strcpy (yylval.y_char, yytext); return TARGET; }
\n       { strcpy (yylval.y_char, yytext); return NL; }
.        { strcpy (yylval.y_char, yytext); return OTHER; }

%%

/* yywrap() definition not necessary in this situation,
   but if present, it must return 1 in order to
   quit processing on input file EOF. */

/*int yywrap(void) {
     return 1;
} */

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

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

%union {
        char    y_char [10000];
}
%token <y_char>   TARGET
%token <y_char>   OTHER
%token <y_char>   NL
%token <y_char>   NOTHING

%%

document: chunk_list {printf("\nInput file ended!\n");}
chunk_list: /* empty */ | chunk_list chunk;
chunk: 
TARGET      { printf("Hello World\n"); }
| NL        { printf("%s", $1); }
| OTHER     { printf("%s", $1); }
;

%%

int main(int argc, char *argv[]){
        printf("\nStarting...\n");
        yyparse();
        printf("Done!\n\n");
}
=========================================================

This Hello World isn't obvious to a newbie, or at least to this newbie,
so I'll be using this Hello World, in all my future BNF/Flex/Bison
presentations.

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]