Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22248

Bison parser always prints "syntax error"

$
0
0

I am trying to build a 3 address code generator which would produce:

input:x=a+3*(b/7)output: t1=b/7t2=3*t1t3=a+t2x=t3

NO matter whatever i give as input the output is "syntax error".

I'm using Windows 10.

Yacc code:

%{#include <stdlib.h>#include <stdio.h>#include <string.h>int yylex(void);int t_count = 1;void yyerror(char *s){    fprintf(stderr,"%s\n",s);    return;}char * generateToken(int i){    char* ch=(char*)malloc(sizeof(char)*5);    sprintf(ch,"t%d",i++);    return ch;}%}%union { double dval; char  ivar[50]; }%token <ivar> DOUBLE%token <ivar> NAME%token <ivar> DIGITS%type <ivar> expr%type <ivar> term%left "+""-"%left "*""/"%left "("")"%%program:    line program            {                            }    | line                  {                            }    ;line:    expr    "\n"            {                                  t_count =1;                            }    | NAME "=" expr "\n"    {                                  printf("%s = %s", $3,$1);                                t_count=1;                            }    ;expr:    expr "+" expr           {                                char * x=generateToken(t_count);                                printf("%s = %s + %s",x,$1,$3);                                strcpy($$,x);                            }    | expr "-" expr         {                                strcpy($$,generateToken(t_count));                                printf("%s = %s - %s",$$,$1,$3);                            }    | expr "*" expr         {                                strcpy($$,generateToken(t_count));                                printf("%s = %s * %s",$$,$1,$3);                            }    | expr "/" expr         {                                strcpy($$,generateToken(t_count));                                printf("%s = %s / %s",$$,$1,$3);                            }    | term                  {                                              strcpy($$, $1);                            }    | "(" expr ")"          {                                strcpy($$,generateToken(t_count));                                printf("%s =( %s )" ,$$,$2);                            }    ;term:           NAME                    {                                strcpy($$, $1);                            }    | DIGITS                {                                  strcpy($$, $1);                            }    ;%%int main(void){    yyparse();    return 0;}

Lex code:

#include <stdio.h>#include <string.h>#include "threeAdd.tab.h"void yyerror(char*);extern YYSTYPE yylval;%}NAME    [a-zA-Z]DIGITS  [0-9]DOUBLE  {DIGITS}(\.{DIGITS})?%%[ \t]+   { }{DIGITS}+ {//sscanf(yytext,"%s", &yylval.ivar);            strcpy(yylval.ivar, yytext);         }"+"      {            return *yytext;         }"-"      {            return *yytext;         }"*"      {            return *yytext;         }"/"      {            return *yytext;         }"="      {            return *yytext;         }       "("      {            return *yytext;         }")"      {            return *yytext;         }{NAME}   {            //sscanf(yytext,"%s", &yylval.ivar);            strcpy(yylval.ivar,yytext);        }"\n"         {            return *yytext;         }exit     {            return 0;         }.        {            char msg[25];            sprintf(msg,"<%s>","invalid character",yytext);            yyerror(msg);         }%%

Sample build & run:

C:\Users\USER\OneDrive\Desktop\Compiler\ICG>flex file.l  C:\Users\USER\OneDrive\Desktop\Compiler\ICG>bison -d file.y  C:\Users\USER\OneDrive\Desktop\Compiler\ICG>gcc lex.yy.c file.tab.c -o ICG.exeC:\Users\USER\OneDrive\Desktop\Compiler\ICG>ICG.exe 3+9syntax error

Viewing all articles
Browse latest Browse all 22248

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>