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;
}