我目前正在开发一个小型编译器,并且我试图解析一个测试文件,但我遇到了一个无法解决的错误。当我运行测试文件时,我得到语法错误0,我假设是因为b == 0部分,但我还没有找到解决方案。我相信问题出在我的flex文件中,但我还没有确定它。
这是我的灵活代码:
%{
#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];
%%
这是测试文件:
#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;
}