0

I'm currently working on a small c compiler and i'm trying to parse a test file but I'm having a error I cannot resolve. When i run the test file I get a syntax error 0, which i'm assuming is because of the b==0 part but I have yet to figure out a solution. I believe the problem lies within my flex file but I have yet to identify it.

Here is my flex code:

%{
#include <stdio.h>
#include <stdlib.h>

int count = 0;

%}

L     [a-zA-Z_]
D     [0-9]
H     [a-fA-F0-9]
E     ([Ee][+-]?{D}+)
P     ([Pp][+-]?{D}+)
FS    (f|F|l|L)
IS    ((u|U)|(u|U)?(l|L|ll|LL)|(l|L|ll|LL)(u|U))

%%

[ \t]   ;
[ \n] {count = count + 1;}
float       return FLOAT;
if      return IF;
int     return INT;
return      return RETURN;  
while       return WHILE;   
{D}+            return NUM;
cout        return COUT;
endl        return ENDL;
cin         return CIN;
"<<"        return STREAMIN;
">>"        return STREAMOUT;
else        return ELSE;
{L}({L}|{D})*   return ID;
"="         return ASSIGNOP;
"*"|"/"|"%"     return MULOP;
"+"|"-"     return ADDOP;
"+="|"-="   return INCOP;
"<"|">"|"<="|">="|"=="|"!=" return RELOP;
"!"         return NOT;
"||"        return OR;
"&&"        return AND;
0[xX]{H}+{IS}?  return INT_LITERAL;
0[0-7]*{IS}?    return INT_LITERAL;
[1-9]{D}*{IS}?  return INT_LITERAL;
0[xX]{H}+"."{H}*{P}?{FS}? return INT_LITERAL;
0[xX]{H}*"."{H}+{P}?{FS}? return INT_LITERAL;
0[xX]{H}+{P}{FS}? return INT_LITERAL;
{D}+{E}{FS}?    return FLT_LITERAL; 
{D}*"."{D}+{E}?{FS}? return FLT_LITERAL;    
{D}+"."{D}*{E}?{FS}? return FLT_LITERAL;
{L}?\"(\\.|[^\\"\n])*\" return STR_LITERAL;
"("     return '(';
")"     return ')';
"["     return '[';
"]"     return ']';
"{"     return '{';
"}"     return '}';
";"     return ';';
","     return ',';
^"#include ".+ ;    
\/\/.* ;
\/\*(.*\n)*.*\*\/ ;
"/*" ;
.       return yytext[0];

%%

and here is the test file:

#include <iostream>
int x,y; 

int gcd(int a,int b) {
if ( b == 0 ) return a;
else return gcd ( b, a % b );
}
int main() {
cout << "Enter two integers: ";
cin >> x >> y; cout << "The GCD is" << gcd(x,y) << endl;
}
rici
  • 234,347
  • 28
  • 237
  • 341
sippycup
  • 145
  • 3
  • 12
  • 1
    Bison won't print anything that looks like `Syntax error 0`, so that must be produced by your code. Since we can't see your code, it is hard to make any deduction about the `0` in that message. Perhaps you need to explain better why you think the problem has anything to do with your lexical analysis (which contains many errors, but none relevant to the sample input afaics.) You should probably try enabling debugging in your parser, and trying a simpler input. See https://www.gnu.org/software/bison/manual/bison.html#Debugging – rici Apr 25 '17 at 03:38
  • I look more into debugging my code then. Can you mention some mistakes in my lexical analysis? – sippycup Apr 25 '17 at 04:07
  • 1
    Your pattern for `/* ... */` comments is greedy so it will extend from the beginning of the first comment to the end of the last one. It also fails to count newlines. On the whole, it's better to let flex do the counting; see `%option yylineno`. One of your INT_LITERALs should be FLT_LITERAL and I have no idea why `NUM` is separate, since it matches a subset of `INT_LITERALs`. The other ones I saw are deviations from C, rather than incorrect regexes. Maybe you don't care about those. – rici Apr 25 '17 at 17:58
  • I corrected alot of the issues you mentioned but I couldn't find with INT_LITERALS you were talking about. could you bring it to my attention? – sippycup Apr 26 '17 at 05:25
  • 1
    `0[xX]{H}+{P}{FS}? return INT_LITERAL;` – rici Apr 26 '17 at 07:44

0 Answers0