[GoLUG] I just made my first Flex lexical analyzer Hello World
Steve Litt
slitt at troubleshooters.com
Sat Nov 11 18:40:34 EST 2023
Hi all,
I just made my first Flex lexical analyzer Hello World, as follows:
====================================
%{ // START DEFINITIONS SECTION
#include <stdio.h>
%}
%% // TRANSITION DEFS TO RULES SECTION
[\n] printf(": Hello World!\n");
%% // TRANSITION RULES TO CODE SECTION
int yywrap(){}
int main() {
yylex(); //Call rules section
}
====================================
You compile the preceding hello.l file with flex to create C file
lex.yy.c . Then you compile lex.yy.c to create a.out. Finally, you
perform the following command:
cat | ./a.out
Then type a few words separated by newlines (hit the Enter key), and
then press Ctrl+D to send end of file. The result is output in which
your original lines are typed, and for each line, a colon, a space and
the phrase "Hello Word!" are appended.
Like I say, this useless program is simply a hello world.
My hope is that, in about 5 years, I'll be able to use flex and its
semi-companion bison to create a parser for an incredibly easy keyboard
friendly book authoring system superior to LyX, and much more flexible
than Markdown or AsciiDoc.
SteveT
SteveT
Steve Litt
Autumn 2023 featured book: Rapid Learning for the 21st Century
http://www.troubleshooters.com/rl21
More information about the GoLUG
mailing list