Why is a defined token not recognized during syntax analysis in C using Bison? -


i'm working on simple infix-to-postfix compiler given grammar. i'm @ stage of syntax analysis. have written lexical analyzer, using flex library, i'm stuck on seemingly simple problem. information below might seem lot process, presume problem rather basic experience in compiler construction.

here lexer:

%{      #include <stdlib.h>     #include "global.h"     int lineno = 1, tokenval = none;  %}   letter      [a-za-z] digit       [0-9] id      {letter}({letter}|{digit})*  %option     noinput %option     nounput  %%   [ \t]+      {} \n      {lineno++;} {digit}+    {tokenval = atoi(yytext);         printf("digit\n");          return num;} {id}        {int p;          p = lookup(yytext);          if(p==0){             p = insert(yytext, id);             }          tokenval = p;          return symtable[p].token;         } <<eof>>     {return done;} .       {tokenval = none;          return yytext[0];} 

nothing special here, defining tokens , handling them.

and parser.y file:

%{      #include "global.h" %}   %token digit   %%  start:  line {printf("success!\n");};  line:   expr ';' line | expr ;  expr:   digit;   %%   void yyerror(char const *s) {     printf("error\n"); };   int main() {     yyparse();     return 0; } 

the problem on line:

expr:   digit; 

the compiler has evidently problem digit token, since if put instead constant other digit, works fine, , expressions -; or +; accepted. have no idea why happening, i'm pretty sure lexical analyzer works fine.

the global.h file linkage other files, contains necessary function prototypes , links necessary variables:

#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <string.h>  #define bsize 128 #define none -1 #define eos '\0' #define num 256 #define div 257 #define mod 258 #define id  259 #define done 260 extern int tokenval; extern int lineno; struct entry {   char *lexptr;   int token; }; extern struct entry symtable[]; int insert (char s[], int tok); void error (char *m) ; int lookup (char s[]) ; void init () ; void parse () ; int yylex (void) ; void expr () ; void term () ; void factor () ; void match (int t) ; void emit (int t, int tval) ; void yyerror(char const *s); 

your scanner returns num when has found sequence of digits, not digit. identifier digit used internally in flex specification.

then have another digit defined token in bison grammar, not connected in way flex one.

to fix this, use num, both in bison grammar , return value lexer. don't declare #define, let bison create declarations, %token definitions. can use -d flag bison output header file. run bison before flex, , #include bison's output header file, num in it, in flex code.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -