1
votes

I am trying to implement a simple flex/bison code which will check a C like a program as follows:

main(){
x = 3;
print x;
}

But when I provide this input to the program, it is not able to match with the rules I mentioned in the flex file. Below is my code for both flex and bison files:

calc.l

%{
#include <stdio.h>
#include <string.h>
#include "calc.tab.h"
int lineno = 1;
%}

digit   [0-9]+
id      [a-z][a-zA-Z0-9]*

%%
{digit}+    { yylval.num = atoi(yytext); return TOK_NUMBER; }
"main"      { return TOK_MAIN; }
"("         { return TOK_ORBRACKET; }
")"         { return TOK_CRBRACKET; }
"{"         { return TOK_OCBRACKET; }
"}"         { return TOK_CCBRACKET; }
"print"     { return TOK_PRINT; }
{id}        { sscanf(yytext, "%s", (yylval.index)); return TOK_VARIABLE; }
";"         { return TOK_SEMICOLON; }
"+"         { return TOK_ADD; }
"*"         { return TOK_MUL; }
"(-{digit}+)" { return TOK_NEGNUM; } 
"="         { return TOK_EQUAL; }
[ \t]+      { }
[ \n]+      { lineno++; }
.           { printf("Lexical error:'%c'\n", yytext[0]); }

%%

calc.y

%{
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "calc.tab.h"

extern int lineno;
int variable_counter = 0;

/* Flex functions */
int yylex(void);
void yyerror(char *s);
void set_variable(char* var_name, int var_value);
int get_variable_value(char* var_name);
extern FILE* yyin;
%}

%union {
    char index[100];
    int num;
}

%token TOK_NUMBER TOK_MUL TOK_ADD TOK_NEGNUM TOK_EQUAL TOK_MAIN TOK_ORBRACKET TOK_CRBRACKET TOK_OCBRACKET TOK_CCBRACKET TOK_SEMICOLON TOK_PRINT TOK_VARIABLE

%code requires {
    struct symtable
    {
        char var_name[100];
        int var_value;
    };
}

%code {
    struct symtable symboltable[100];
    int pos = 0;
}

%type <num> expr TOK_NUMBER TOK_NEGNUM
%type <index> TOK_VARIABLE

%left TOK_ADD
%left TOK_MUL

%%

prog:
    TOK_MAIN TOK_ORBRACKET TOK_CRBRACKET TOK_OCBRACKET stmts TOK_CCBRACKET
;

stmts:
    | stmt TOK_SEMICOLON stmts
;

stmt:
    expr TOK_SEMICOLON
    | TOK_PRINT expr TOK_SEMICOLON      {   fprintf(stdout, "%d\n", $2);    }
    | assignment
;

expr:
    TOK_NUMBER                                  { $$ = $1; }
    | TOK_VARIABLE                              { $$ = get_variable_value($1); }
    | expr TOK_MUL expr                         { $$ = $1 * $3; }
    | expr TOK_ADD expr                         { $$ = $1 + $3; }
    | TOK_NEGNUM                                { $$ = -$1; }
    | TOK_ORBRACKET expr TOK_CRBRACKET          { $$ = $2; }
;

assignment:
    TOK_VARIABLE TOK_EQUAL expr { set_variable($1, $3); }
;

%%

void set_variable(char* var_name, int var_value) {
    int counter;
    bool found = false;
    for (counter = 0; counter<=variable_counter; counter++) {
        if (strcmp(var_name, symboltable[counter].var_name) == 0) {
            found = true;
            break;
        }
    }

    if(!found) {
        strcpy(symboltable[counter].var_name, var_name);
        symboltable[counter].var_value = var_value;
        variable_counter++;
    }
}

int get_variable_value(char* var_name) {
    int counter;
    for (counter = 0; counter<=variable_counter; counter++) {
        if (strcmp(var_name, symboltable[counter].var_name) == 0) {
            return symboltable[counter].var_value;
        }
    }
}

void yyerror(char *s)
{
    fprintf(stderr, "Parsing error: line %d and %s\n", lineno, s);
}

int main(int argc,char* argv[])
{
    if(argc==1) {
        printf("\nPlease provide an input file name. Exiting...\n");
        return 0;
    }

    yyin = fopen(argv[1], "r");
    if (!yyin) {
        printf("ERROR: Couldn't open file %s\n", argv[1]);
        return -1;
    } 

    yyparse();
    return 0;
}

It is showing output as:

'exical error:'
'exical error:'
3
'exical error:'
Parsing error: line 4 and syntax error

Thanks in advance for your help.

1

1 Answers

2
votes

Two issues:

The messages 'exical error:' come from DOS-style \r\n line endings in your parsed file. You should either convert these to \n endings or eat the \r characters in your parser, for example by extending the rule

[ \t]+      { }

to

[ \t\r]+      { }

The message Parsing error: line 4 and syntax error comes from a logic flaw in your parser. Here

stmts:
    | stmt TOK_SEMICOLON stmts
;

you specify, that each stmt is terminated by a semicolon. But here

stmt:
    expr TOK_SEMICOLON
    | TOK_PRINT expr TOK_SEMICOLON      {   fprintf(stdout, "%d\n", $2);    }
    | assignment
;

you also require a semicolon after expr and TOK_PRINT expr. So you needed two semicolons after this line

print x;;

But you probably want to remove the redundant TOK_SEMICOLON like this:

stmt:
    expr
    | TOK_PRINT expr      {   fprintf(stdout, "%d\n", $2);    }
    | assignment
;

Then your file should parse as you expect.