help-bison
[Top][All Lists]
Advanced

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

How to debug bison/flex program? usage of yyerror()


From: Peng Yu
Subject: How to debug bison/flex program? usage of yyerror()
Date: Tue, 29 Dec 2009 23:02:33 -0600

I have the source files listed at the end of the message. I basically
want to parse a file with only numbers (separated by spaces) and print
the numbers out. It is an overkill to use bison/flex. But I just want
to try how to use bison/flex.

I need to understand how to debug the program. Could somebody help me
with the following three questions?

1. I don't understand why the error message is printed. Shouldn't the
regexes [0-9]+ and [:space:] match all the strings in 'test.txt'.

2. I suspect that '.' is matched to EOF. I'm not sure if I'm correct.
But it seem that EOF can not be printed (is it why '%c' is printed
literally?).

3. Why yyerror() in the .l file has two arguments but it has one
argument in the .y file? Are they the same function or two different
functions?

$ make
bison -d yylval_string.y
flex yylval_string.l
cc -o yylval_string yylval_string.tab.c lex.yy.c -lfl
$./yylval_string< test.txt
NUMBER = 133
error: mystery character %c

NUMBER = 7
error: mystery character %c

NUMBER = 33
error: mystery character %c

NUMBER = 76

NUMBER = 35


--------------------------------source files listed
below---------------------------

$cat yylval_string.y %{
#  include <stdio.h>
%}

%token NUMBER

%%

numbers:
       NUMBER { printf("NUMBER = %d\n", $1); }
       | numbers NUMBER { printf("NUMBER = %d\n", $2); }
 ;

%%
main()
{
  yyparse();
}

yyerror(char *s)
{
  fprintf(stderr, "error: %s\n", s);
}


$cat yylval_string.l
%{
# include "yylval_string.tab.h"
%}

%%
[0-9]+ { yylval=atoi(yytext); return NUMBER; }
[:space:] { /*SPACE*/ }
.       { yyerror("mystery character %c\n", *yytext); }
%%

$cat Makefile
 .PHONY: all

all: yylval_string

yylval_string:  yylval_string.l yylval_string.y
        bison -d yylval_string.y
        flex yylval_string.l
        cc -o $@ yylval_string.tab.c lex.yy.c -lfl

$cat test.txt
133 7 33 76
35




reply via email to

[Prev in Thread] Current Thread [Next in Thread]