1
votes

I would like to make a simple compiler for my own simple programming language. Therefor I use Flex and Bison.

Flex perfectly matches the lexical rules and recognizes parenthesis, numbers, identifiers and not allowed characters. But Bison does nothing when encountering a syntax rule. So I do not understand why bison does not activate.

Here is my lexer file (lexicon.l):

%option noyywrap
%option nodefault

%{

#include <stdio.h>
#include "grammar.tab.h"
%}

digit   [0-9]
letter  [a-zA-Z]

%%

[ \t\n]     { ; }
^{letter}({letter}|{digit})*    { printf("IDENTIFIER\n"); return IDENTIFIER; }
{digit}+    { printf("NUMBER\n"); return NUMBER; }
\(      { printf("OPEN_PARENHESIS\n"); return OPEN_PARENTHESIS; }
\)      { printf("CLOSE_PARENTHESIS\n"); return CLOSE_PARENTHESIS; }
.       { printf("lexical error\n"); exit(0); }

%%

Here is my parser file (grammar.y):

%{
void yyerror(char *s);
#include <stdio.h>  
%}

%token NUMBER IDENTIFIER OPEN_PARENTHESIS CLOSE_PARENTHESIS
%start File

%%

File
:   %empty
|   Function File
;

Function
:   IDENTIFIER OPEN_PARENTHESIS CLOSE_PARENTHESIS   { printf("fn"); }
;

%%

void yyerror(char *s) {
    fprintf(stderr, "%s\n", s);
}

And this is my makefile:

iconv --from-code UTF-8 --to-code US-ASCII -c grammar > grammar.y

bison -d grammar.y

flex lexicon.l

cc -ll grammar.tab.c lex.yy.c

It does not activate when I enter "blabla ()" as a simple function.

1

1 Answers

1
votes

Your parser isn't being run at all.

You don't define a main function, but instead link the one from libl. That one only calls yylex() in a loop - it does not call yyparse().

Instead you should define your own main function, which can simply consist of a single call to yyparse().