Search code examples
cyacclex

String library usage in Yacc file


I am trying to write a compiler using lax and Yacc. I started by defining the tokens and syntax tree with adding any associated actions, but when I compiled it, I got some errors.

Lexer:

%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "y.tab.h"
%}

%%

" " {};
"is" {};
[A-Z]+ { return LAYER;}
[0-9]+\.*[0*9]* {return NUMBER;}
"minimum width" {return MIN_WIDTH;}
"maximum width" {return MAX_WIDTH;}
"minimum length" {return MIN_LENGTH;}
"maximum length" {return MAX_LENGTH;}
"minimum space to" {return MIN_SPACE_TO;}
"minimum space" {return MIN_SPACE;}
"with minimum width" {return MIN_W_FILTER;}
"with minimum span" {return MIN_SPAN_FILTER;}

%%

Parser:

%{
#include <stdio.h>
#include <string.h>

extern FILE * file_in;
%}

%start rule

%token <strval> LAYER NUMBER MIN_WIDTH MAX_WIDTH MIN_LENGTH MAX_LENGTH MIN_SPACE MIN_SPACE_TO MIN_W_FILTER MIN_SPAN_FILTER
%%

rule:
    LAYER operation NUMBER {}
    |
    derived_layer operation NUMBER {}
    ;


derived_layer:
    '(' LAYER filter NUMBER ')' {}
    |
    '(' LAYER operation LAYER ')' {}
    ;

operation:
    MIN_WIDTH {}
    |
    MAX_WIDTH {}
    |
    MIN_LENGTH {}
    |
    MAX_LENGTH {}
    |
    MIN_SPACE_TO {}
    ;

filter:
    MIN_W_FILTER {}
    |
    MIN_SPAN_FILTER {}
    ;

%%

int parsefile (const char * FileName)
{

    FILE * fileIn;
    string strFileName = string(FileName);
    fileIn = fopen(strFileName.c_str(), "r");
    if(!fileIn)
    {
        Std::cout << “error while opening file\n”;
    }
    else
    {
        file_in = fileIn;
        flag = cdfparse();

        if(fileIn)
        {
            fclose(fileIn);
        }
    }
}

I get the following errors:

drc.y: In function ‘parsefile’:
drc.y:53: error: ‘string’ undeclared (first use in this function)
drc.y:53: error: (Each undeclared identifier is reported only once
drc.y:53: error: for each function it appears in.)
drc.y:53: error: expected ‘;’ before ‘strFileName’
drc.y:54: error: ‘strFileName’ undeclared (first use in this function)

drc.y:57: error: expected expression before ‘:’ token
drc.y:57: error: stray ‘\342’ in program
drc.y:57: error: stray ‘\200’ in program
drc.y:57: error: stray ‘\234’ in program
drc.y:57: error: stray ‘\’ in program

drc.y:57: error: stray ‘\342’ in program
drc.y:57: error: stray ‘\200’ in program
drc.y:57: error: stray ‘\235’ in program

drc.y:62: error: ‘flag’ undeclared (first use in this function

I think the error means that the string library is not defined, but I've already included string.h in the header. Isn't that enough?


Solution

  • You need to include:

    #include <string>
    #include <iostream>
    

    parsefile should return something or it should be void.

    This statement:

    string strFileName = string(FileName);
    

    should be:

    std::string strFileName = FileName;
    

    this statement:

    Std::cout << “error while opening file\n”;
    

    should be (notice the S in std and the quotes):

    std::cout << "error while opening file\n";
    

    There is no definition or declaration of flag = cdfparse();, check it.