I found it easy to learn about Lex/Flex however learning Yacc/Bison seemed a lot more confusing due to the lack of simple example programs. From my observations, the first example parser introduced to students tends to be a calculator, which is not too beginner-friendly. It was quite hard for me to comprehend how Yacc works simply by looking at complex source code, thus I decided to write my own very simple program which incorporates Lex and Yacc.
test.l:
%{
#include "y.tab.h"
%}
%%
"PRINT" { printf("Returning PRINT\n"); return PRINT; }
"EXIT" { printf("Returning EXIT\n"); return EXIT; }
. ;
[ \t\n] ;
%%
int yywrap(void) { return 1; }
test.y:
%{
int yylex();
#include <stdio.h>
#include <stdlib.h>
//#include "y.tab.h"
%}
%start line
%token PRINT
%token EXIT
%%
line: PRINT {printf("Caught PRINT\n");}
| EXIT {printf("Caught EXIT\n");}
| ;
%%
int main() { yyparse(); }
Compilation:
yacc -d test.y
lex test.l
gcc lex.yy.c y.tab.h -ll
I understand that this program is very simple and there are tons of examples for Yacc, I would like to sincerely assure you that I tried for a couple hours before asking out for some help.
Although I think I got the basics done, I'm unable to figure out why it won't work, please let me know how I could fix it. I suspect that I might be compiling incorrectly. I could also display y.tab.h if necessary. Thank you for your time.
Edit: The goal is simply for the lexer to return the appropriate return value to the yacc parser, and for the yacc parser to "catch" it and print "Caught PRINT" or "Caught EXIT".
Edit 2: My compilation process was indeed incorrect. I would like to thank the person who was helped me understand how to fix the issue.
| ;
means empty (the;
terminates the yacc rule) so the parser accepts an empty input. Maybe that is not wat you want. – Paul Ogilvie