Subject: yacc bug? or me?
To: None <current-users@netbsd.org>
From: Patrick Welche <prlw1@newn.cam.ac.uk>
List: current-users
Date: 03/05/2001 17:35:11
When trying to parse the simple input file
dog cat
one two
I want to be able to read (dog,cat) (one,two) and thought that I could do
that in the line: rule of my grammar. However for some reason $1 is
concatentated with $2:
scan: |dog|
scan: |cat|
gram: |dog cat| !cat!
scan: |one|
scan: |two|
gram: |one two| !two!
This probably has something to do with the fact that my yylval's are char *
not int, but I thought I had to actively $$=$1+' '+$2 for the behaviour I'm
seeing. Is this a bug, or my misunderstanding?
Cheers,
Patrick
To try it at home:
Makefile:
PROG=lexprob
SRCS=lexprob.c gram.y scan.l
NOMAN=noman
CFLAGS+=-g
LDADD+=-ll
YHEADER=yes
.include <bsd.prog.mk>
scan.l:
%{
#include "gram.h"
char *yylval;
%}
%%
[a-zA-Z]{3} {
yylval=yytext;
printf("scan: |%s|\n",yylval);
return TLA;
}
[ \t\n\r] /* eat up whitespace */
%%
void yyerror(const char *errmsg)
{
errx(1,"parser: %s at or near \"%s\"",errmsg,yytext);
}
gram.y:
%{
#include <stdio.h>
#define YYSTYPE char *
void yyerror(const char *);
%}
%token TLA
%%
input: /* empty */
| input line
;
line: TLA TLA {printf("gram: |%s| !%s!\n",$1,$2);}
;
lexprob.c:
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
FILE *yyin;
int yyparse(void);
int main()
{
if(yyin=fopen("lex.in","r"),yyin==(FILE *)NULL)
errx(1,"Cannot open file \"lex.in\" for reading\n");
yyparse();
return 0;
}
lex.in:
dog cat
one two