Syntax error in lex yacc -
Syntax error in lex yacc -
here lex yacc code parse xml file , print contents between , tags.
lex
%{ %} %% "<xml>" {return xmlstart;} "</xml>" {return xmlend;} [a-z]+ {yylval=strdup(yytext); homecoming text;} "<" {yylval=strdup(yytext);return yytext[0];} ">" {yylval=strdup(yytext);return yytext[0];} "\n" {yylval=strdup(yytext);return yytext[0];} . {} %%
yacc
%{ #include<stdio.h> #include<stdlib.h> #include<string.h> #define yystype char * %} %token xmlstart %token xmlend %token text %% programme : xmlstart '\n' '\n' xmlend {printf("%s",$3); } : '<' text '>' '\n' '\n' '<' text '>' { $$ = strcat($1,strcat($2,strcat($3,strcat($4,strcat($5,strcat($6,strcat($7,strcat($8,$9))))))));} | text %% #include"lex.yy.c"
i'm getting syntax error, tried using echos @ places didn't find error. input file i'm using is:
<xml> <hello> hi <hello> </xml>
please help me figure out error. have relatively less experience using lex , yacc
that grammar parse file has xmlend
@ end. however, text files end newline.
although presumably prepare adding newline @ end of start rule, it's bad thought seek parse whitespace. in general, except line-oriented languages -- xml not -- best ignore whitespace.
your utilize of strcat
incorrect. quoting man strcat
gnu/linux system:
the strcat()
function appends src string dest string, overwriting terminating null byte ('\0') @ end of dest, , adds terminating null byte. strings may not overlap, , the dest string must have plenty space result. if dest not big enough, programme behavior unpredictable; buffer overruns favorite avenue attacking secure programs.
you might want utilize asprintf
if exists in standard library.
also, never free()
strings produced strdup
, of them leak memory. in general, it's improve not set strdup
tokens string representation known -- particularly single-character tokens -- of import thing maintain track of tokens string value has been freshly allocated. apply semantic values produced asprintf
if above suggestion taken.
yacc lex
Comments
Post a Comment