Yacc/Bison parser states

So, I was trying to teach myself Lex & Yacc. Understanding how it works, is not trivial. The important thing to note is how the look ahead logic in bison works. The look-ahead changes the state of the parser, after looking ahead one token and then deciding whether to change the state or not. This link describes how the look ahead works. For example, if I had a grammar like this:
%{
/*prologue copied as is in the generated file */
%}
%token NAME NUMBER
%%
statement : NAME '=' expression
| expression {printf("=%d\n",$1);}
;
expression: expression '+' mulexp { $$ = $1 + $3; }
| expression '-' mulexp { $$ = $1 - $3; }
| mulexp { $$ = $1; }
;

mulexp: mulexp '*' primary { $$ = $1 * $3; }
| mulexp '/' primary { $$ = $1 / $3; }
| primary { $$ = $1; }
;

primary: '(' expression ')' { $$ = $2; }
| '-' primary { $$ = -$2; }
| NUMBER { $$ = $1; }
;
%%
int main()
{
return yyparse();
}

The lexer code for the parser above is:

%{
#include "cal.tab.h"
extern int yylval;
%}

%%
[0-9]+ {yylval = atoi(yytext); printf("num=%d\n",yylval);return NUMBER;}
[ \t] ; /*ignore whitespaces */
\n {return 0; /* logical EOF */ }
. return yytext[0];
%%

And the makefile for these looks like this:

all:cal.exe
clean:
rm -f cal.exe cal.tab.c cal.tab.h lex.yy.c cal.output
rm -f *.*~
rm Makefile~

cal.exe : cal.tab.c lex.yy.c
gcc -g -o cal.exe cal.tab.c lex.yy.c -lfl -ly
cal.tab.c: cal.y
bison -v --report=state -d cal.y
lex.yy.c: cal.l
flex cal.l

Note that the -v and –report flags are used to print the states of the bison parser.
Continue reading “Yacc/Bison parser states”

You got mail – reversed

This thought occured to me when I was doing something else, at the end of the day, when I was really tired. What would have happened in You got mail, if roles were reversed. Instead of Joe Fox realizing that shopgirl was Kathleen Kelly, if it was the other way round. If Joe Fox was waiting in the restauarant, and Kathleen Kelly, screamed on the road – I told you he was going to be handsome. How would have the story turned to ? Will Kathleen Kelly go to the extent of befriending Joe Fox, even after she knew who he was. Why is it that in the movie, the lady was shown to be a tough exterior, but a mushy inside lady. I wonder how the story will turn out to be ? Is it possible that a lady will go and tell the man that she indeed liked him – and it meant a lot to talk to him. Just a random thought 🙂