سؤال

I am trying to parse a code, and for that i have written LEX and YACC file which will given below. first line it is reading correctly but after that it is giving syntax error, it is not reading next line,should i modify input and unput function,i am reading from file and writing my output in a file.....i have just started using LEX YACC, need some of the idea.

input file

b_7 = _6 + b_3;
a_8 = b_7 - c_5;

lex file

    %{
        /*
            parser for ssa;
        */

    #include<stdio.h>
    #include<stdlib.h>
    #include"y.tab.h"


    %}
    %%
    [\t]+   ;
    \n  ;



    [if]+       printf("first input\n");
    [else]+     return(op);
    [=]+        return(equal);
    [+]+        return(op);
    [*]+        return(op);
    [-]+        return(op);

    [\<][b][b][ ]+[1-9][\>] {return(bblock);}

    ([[_][a-z]])|([a-z][_][0-9]+)|([0-9]+)  {return(var);}

    .   ;




    %%

yacc file

%{
/* lexer for ssa gramer to use for recognizing operations*/
#include<stdio.h>
char add_graph(char,char,...);

%}

%token  opif opelse equal op bblock var

%%

sentence: var equal var op var  { add_graph($1,$2,$3,$4,$5);} 

    ;


%%
extern FILE *yyin;
main(argc,argv)
int argc;
char **argv;
{
    if(argc > 1) {
        FILE *file;
        file=fopen(argv[1],"r");
        if(file==NULL) {
            fprintf(stderr,"couldnot open%s\n",argv[0]);
            exit(1);
        }
        yyin=file;
    }
      do
    {
    yyparse();
    }while (!feof(yyin));
fclose(yyin);
}
char add_graph(something)
{
.....
.....
}

yyerror(s)
char *s;
{
    fprintf(stderr,"%s there is error\n",s);
}
yywrap()
{
printf("the output");
}
هل كانت مفيدة؟

المحلول 2

Your grammar only permits one sentence. So if there is any input after the first sentence, an error will be raised. You want to permit one or more sentences. Try this in your .y file:

%%
sentences : sentences sentence
          | sentence
          ;
sentence  : var equal var op var  { add_graph($1,$2,$3,$4,$5);}
          ;
%%

نصائح أخرى

Lots of problems here:

  • your grammar is expecting the token op, but your lexer will never produce it, instead producing opadd opmul etc
  • your example has ; at the end of lines, but neither your lexer nor parser deal with them. The default lexer action of copying to stdout is almost never what you want.
  • your yacc file tries to use \\ as some sort of comment marker, but yacc doesn't understand that. Some versions of yacc understand C++-style // as a comment, but not all
  • your grammar only allows for one sentence in the input
  • your sentence has a spurious op at the end (on the next line), which is not a separate sentence rule -- you need | to separate rules.
  • you attempt to loop if you haven't reached the eof when yyparse returns, but if there's an error, its likely that the input will still have some cruft that will cause an immediate error, resulting in an error storm -- probably not what you want.

DAVID IS SAYING CORRECT BUT ONE MORE MODIFICATION NEED TO BE MADE

ADD

";"     ;

SEE IF THIS CAN HELP.acknowledge me if i am wrong.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top