I am trying to create a basic compiler and I am facing some problems.
My flex code:
%{
#include <stdio.h>
#include "SymbolTable.h"
#include <y.tab.h>
//extern "C"
//{
// extern int yylex(void);
// extern int yyparse(void);
//}
int line_count = 1;
SymbolTable table;
static int yywrap(void);
void insert(const char* token, char* yytext) {
////printf ("%s: %s ", token, yytext);
//printf ("%s ", token);
SymbolInfo symbolInfo(yytext, token);
table.insert(symbolInfo, line_count);
}
%}
newLine \n
delim [ \t]
digit [0-9]
unsigned {digit}+
exponent [eE][+-]?{unsigned}
number [-+]?({unsigned}|{unsigned}\.{unsigned}?|{unsigned}?\.{unsigned}){exponent}?
letter [A-Za-z]
keyword program|if|not|end|begin|else|then|do|while|function|Procedure|integer|real|var|oh|array|write|include
id (_|{letter})({letter}|{digit}|_)*
header {id}\.h
sin_comment \/\/.*
mul_comment "/*"(([^*]|(("*"+)[^*/]))*)("*"+)"/"
string \"(\\.|[^"])*\"
char \'(\\.|[^'])*\'
addop [\+-]|or
mulop [\*\/]|div|mod|and
orop ||
leop <=
geop >=
eqop ==
neop !=
assignop :=
dotdot \.\.
brace [\[\]\(\)]
other [,;:]
hash #
err_dotdot [0-9]+\.([0-9]+\.){1,}
err_id {digit}+{id}
leftcurl {
rightcurl }
%%
{newLine} {
line_count++;
}
{string} {
//printf ("str: %s\n", yytext);
//printf ("line no: %d\n",line_count);
//fprintf(tokenout,"\n<STRING, %s>\n",yytext);
//fprintf(logout,"\nLine no %d: STRING <%s> found\n",line_count,yytext);
}
{char} {
//printf ("str: %s\n", yytext);
//printf ("line no: %d\n",line_count);
//fprintf(tokenout,"\n<CHAR, %s>\n",yytext);
//fprintf(logout,"\nLine no %d: CHAR <%s> found\n",line_count,yytext);
}
{keyword} {
////printf ("keyword");
//fprintf(tokenout,"\n<KEYWORD, %s>\n",yytext);
//fprintf(logout,"\nLine no %d: KEYWORD <%s> found\n",line_count,yytext);
}
{header} {
insert("header",yytext);
//fprintf(tokenout,"\n<HEADER %s>\n",yytext);
//fprintf(logout,"\nLine no %d: HEADER <%s> found\n",line_count,yytext);
}
{id} {
SymbolInfo symbolInfo(yytext, "ID");
table.insert(symbolInfo, line_count);
//printf ("id: %s\n", yytext);
//fprintf(tokenout,"<ID, %s>\n",yytext);
//fprintf(logout,"\nLine no %d: ID <%s> found\n",line_count,yytext);
}
{sin_comment} {
//printf ("\nSingleline comment found: %s\n",yytext);
//fprintf(logout,"\nLine no %d: SINGLELINECOMMENTP <%s> found\n",line_count,yytext);
}
{mul_comment} {
//printf ("\nMultiline comment found: %s\n",yytext);
//fprintf(logout,"\nLine no %d: MULTILINECOMMENT <%s> found\n",line_count,yytext);
}
{number} {
//printf ("number:%s\n",yytext);
//printf ("line no: %d\n",line_count);
SymbolInfo symbolInfo(yytext, "NUMBER");
table.insert(symbolInfo, line_count);
//fprintf(tokenout,"<NUMBER, %s>\n",yytext);
//fprintf(logout,"\nLine no %d: NUMBER <%s> found\n",line_count,yytext);
return NUMBER;
}
{addop} {
insert("ADDOP", yytext);
//printf ("line no: %d\n",line_count);
//fprintf(tokenout,"\n<ADDOP, %s>\n",yytext);
//fprintf(logout,"\nLine no %d: ADDOP <%s> found\n",line_count,yytext);
return ADDOP;
}
{mulop} {
insert("MULOP", yytext);
//printf ("line no: %d\n",line_count);
//fprintf(tokenout,"\n<MULOP, %s>\n",yytext);
//fprintf(logout,"\nLine no %d: MULOP <%s> found\n",line_count,yytext);
}
{assignop} {
insert("ASSIGNOP", yytext);
//printf ("line no: %d\n",line_count);
//fprintf(tokenout,"\n<ASSIGNOP, %s>\n",yytext);
//fprintf(logout,"\nLine no %d: ASSIGNOP <%s> found\n",line_count,yytext);
}
{dotdot} {
insert("DOTDOT", yytext);
//printf ("line no: %d\n",line_count);
}
{hash} {
insert("#",yytext);
}
{delim}+ {}
{err_dotdot} {
//printf ("Illegal usage of decimal\n");
//fprintf(logout,"\nLine no %d: Illegal usage of decimal <%s> found\n",line_count,yytext);
}
{err_id} {
//printf ("Illegal id\n");
//fprintf(logout,"\nLine no %d: Illegal usage of id <%s> found\n",line_count,yytext);
}
%%
I can happily create lex.yy.c by
lex -o lex.yy.c lex.l
.
But when I try to create lex object file using g++ -c -w -o lexer.o lex.yy.c
it says
lex.l:4:19: fatal error: y.tab.h: No such file or directory
I have already parsed the parser using bison -d -y grammar.y
Change to "y.tab.h" in your code. In addition you might include a parameter to gcc to include the path to the file