0

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yassin
  • 289
  • 1
  • 5
  • 14
  • 1
    there is no `string()` function in `string.h`. what are you trying to do? are you confusing C with C++? – Andreas Grapentin Jan 25 '14 at 08:00
  • You figured out the problem, I did confuse c & c++ here! When used c++ compiler, it worked fine. – yassin Jan 25 '14 at 08:09
  • Re *"lax"*: Do you mean *"[Lex](https://en.wikipedia.org/wiki/Lex_(software))"*? – Peter Mortensen Apr 28 '23 at 15:26
  • OK, the OP has left the building; *"Last seen more than 8 years ago"*. Perhaps somebody can chime in? Does "Lax" exist? – Peter Mortensen Apr 28 '23 at 15:27
  • The "stray" error (though attention should always be on the first reported compiler error; this is 4 lines later than the first error): This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 28 '23 at 15:38
  • For that particular error: 342 200 234 (octal) → 0xE2 0x80 0x9C (hexadecimal) → UTF-8 sequence for Unicode code point U+201C ([LEFT DOUBLE QUOTATION MARK](https://www.charset.org/utf-8/9)). And 342 200 235 (octal) → 0xE2 0x80 0x9D (hexadecimal) → UTF-8 sequence for Unicode code point U+201D ([RIGHT DOUBLE QUOTATION MARK](https://www.charset.org/utf-8/9)). They can be searched for (and replaced) by the regular expression `\x{201C}|\x{201D}` in any modern text editor (the notation is different in Visual Studio Code (and probably others): `\u201C|\u201D`). – Peter Mortensen Apr 28 '23 at 15:59

2 Answers2

1

string.h contains string functions for char arrays. The string type is contained in the string file. Use #include <string>

Jerry Jeremiah
  • 9,045
  • 2
  • 23
  • 32
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131