I'm getting an error from gcc on my lex code: "#endif without #if" - lex

This is my first attempt at writing a compiler using flex and bison. I wrote what look to me like legal lex and yacc code, but when I run it through the compiler I get an error message. This seems to come from gcc, so it's something wrong with the code generated by flex.
ghlex.l:20:2: error: #endif without #if
.map return DOTMAP;
Can anybody tell me what's wrong with the .map pattern and/or action?
(In case it's not obvious or I miscoded it, that's supposed to match the token ".map")
Here's my lex/flex source code:
%{
#include <stdio.h>
#include <string.h>
#include "gh.tab.h"
%}
DIGIT [0-9]
STARTCHAR [_a-zA-Z]
WORDCHAR {DIGIT}|{STARTCHAR}
FILECHAR {WORDCHAR}|[-.+##$%()]
FILECHAR1 {FILECHAR}|[/" ]
FILECHAR2 {FILECHAR}|[/' ]
/* special "start states" for matching
%s DESC FNAME1 FNAME2
%%
{DIGIT}+ yylval.number = atoi(yytext); return INT;
{STARTCHAR}{WORDCHAR}* yylval.string = strdup(yytext); return WORD;
FILECHAR$ yylval.string = strdup(yytext); return FILEPART;
'FILECHAR1+' yylval.string = strdup(yytext); return QUOTE;
"FILECHAR2+" yylval.string = strdup(yytext); return QUOTE;
\.map return DOTMAP;
\.m return DOTM;
\.r return DOTR;
\.c return DOTC;
\.d return DOTD;
\.t return DOTT;
\.o return DOTO;
\.u return DOTU;
\.v return DOTV;
, return COMMA;
\+ return PLUS;
- return MINUS;
\/ return SLASH;
; return SEMI;
\[ return LBRACKET;
\] return RBRACKET;
\%\$ return PCTDOL;
\%\/ return PCTSLASH;
\%t return PCTT;
\%# return PCTAT;
\n /* ignore newlines */
[ \t] /* ignore whitespace */
\/\/ /* ignore c++-style comments */
<DESC>.* yylval.string = strdup(yytext); return STRING;
%%

The problem is your unterminated comment on line 12:
/* special "start states" for matching
since this line begins with whitespace, it is copied verbatim into the lex.yy.c file, where it screws things up, commenting out several following lines generated by flex, including an #ifdef and the #line directives that would make the compiler output better line number information.
If you compile with -Wall (which you ALWAYS should), you'll get a warning: "/*" within comment before the error, which at least hints that the problem is related to comments (though this message too has an incorrect line number.)

Related

Why scanf in bison grammatical rule not working?

I have this gramatical rule in my .y file
...
listaIdentificadores :listaIdentificadores ',' ID {leer($3);}
|ID {leer($1);}
;
...
and this is definition of leer()
void leer(char* identificador) {
int valor;
scanf("%d",&valor);
cargarTS(identificador, valor);
}
but when scanf() should be executed it does not ask for the keyboard input and it simply continues with the execution of the following code line.

How can I get the corresponding error string from an FT_Error code?

Given an FT_Error, is there a macro/function to return the corresponding error message string?
The answer is No, as far as I know.
You should create your own macro or function,
using macros defined in fterrors.h.
For example, the following code will work:
#include <ft2build.h>
#include FT_FREETYPE_H
const char* getErrorMessage(FT_Error err)
{
#undef FTERRORS_H_
#define FT_ERRORDEF( e, v, s ) case e: return s;
#define FT_ERROR_START_LIST switch (err) {
#define FT_ERROR_END_LIST }
#include FT_ERRORS_H
return "(Unknown error)";
}
Read fterrors.h (found in /usr/local/include/freetype2/ in my environment) for more detail and another example.
In case anyone else stumbles upon this question, FT_Error_String was introduced in FreeType2 version 2.10.1 to retrieve the error string of an FT_Error code.
FT_Error error = ...
if (error != 0)
{
puts(FT_Error_String(error), stderr);
}
Important: You must define FT_CONFIG_OPTION_ERROR_STRINGS when compiling FreeType otherwise the function will return NULL.

Counting lines of a file in Scala

I am studying Scala nowadays and this is my code snippet to count the number of lines in a text file.
//returns line number of a file
def getLineNumber(fileName: String): Integer = {
val src = io.Source.fromFile(fileName)
try {
src.getLines.size
} catch {
case error: FileNotFoundException => -1
case error: Exception => -1
}
finally {
src.close()
}
}
I am using Source.fromFile method as explained in Programming in Scala book. Here is the problem: If my text file is like this:
baris
ayse
deneme
I get the correct result 6. If I press enter after word deneme I still get number 6, however I exptect 7 in this case. If I press space after pressing enter I get 7 which is correct again. Is this a bug in Scala standard library or more possibly am I missing something?
Finally, my basic main method here If it helps:
def main(args: Array[String]): Unit = {
println(getLineNumber("C:\\Users\\baris\\Desktop\\bar.txt"))
}
It uses java.io.BufferedReader to readLine. Here is the source of that method:
/**
* Reads a line of text. A line is considered to be terminated by any one
* of a line feed ('\n'), a carriage return ('\r'), or a carriage return
* followed immediately by a linefeed.
*
* #return A String containing the contents of the line, not including
* any line-termination characters, or null if the end of the
* stream has been reached
*
* #exception IOException If an I/O error occurs
*
* #see java.nio.file.Files#readAllLines
*/
public String readLine() throws IOException {
return readLine(false);
}
Which calls this:
...
* #param ignoreLF If true, the next '\n' will be skipped
...
String readLine(boolean ignoreLF) ...
...
/* Skip a leftover '\n', if necessary */
if (omitLF && (cb[nextChar] == '\n'))
nextChar++;
skipLF = false;
omitLF = false;
So basically that's how it's implemented. I guess it depends what a line means to you. Are you counting lines that contain something or new line characters? - different things obviously.
If you press enter after word deneme simply you add an end-of-line sequence (CR+LF, in your case) to the 6th line. You see the cursor goes to new line, but you did not create a new line: You simply specify that the sixth line is over. To create a new line you have to put a character after the end-of-line sequence, as you make when you press space.

Lex program rules not working

%{
#include <stdio.h>
int sline=0,mline=0;
%}
%%
"/*"[a-zA-Z0-9 \t\n]*"*/" { mline++; }
"//".* { sline++; }
.|\n { fprintf(yyout,"%s",yytext); }
%%
int main(int argc,char *argv[])
{
if(argc!=3)
{
printf("Invalid number of arguments!\n");
return 1;
}
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
yylex();
printf("Single line comments = %d\nMultiline comments=%d\nTotal comments = %d\n",sline,mline,sline+mline);
return 0;
}
I am trying to make a Lex program which would count the number of comment lines (single-line comments and multi-line comments separately).
Using this code, I gave a .c file and a blank text file as input and output arguments.
When I have any special characters in multi-line comments, its not working for that multi-line and mline is not incremented for the comment line.
How do I fix this problem?
Below is a nudge in the right direction. The main differences between what you did and what I have done is that I made only two regex - one for whitespace and one for ident (identifiers). What I mean by identifiers is anything that you want to comment out. This regex can obviously be expanded out to include other characters and symbols. I also just defined the three patterns that begin and end comments and associated them with tokens that we could pass to the syntax analyzer (but that's a whole new topic).
I also changed the way that you feed input to the program. I find it cleaner to redirect input to a program from a file and redirect output to another file - if you need this.
Here is an example of how you might use this program:
flex filename.l
g++ lex.yy.c -o lexer
./lexer < input.txt
You can redirect the output to another file if you need to by using:
./lexer < input.txt > output.txt
Instead of the last command above.
Note: the '.'(dot) character at the end of the pattern matching is used as a catch-all for characters, sequences of characters, symbols, etc. that do not have a match.
There are many nuances to pattern matching using regex to match comment lines. For example, this would still match even if the comment line was part of a string.
Ex. " //This is a comment in a string! "
You will need to do a little more work to get past these nuances - like I said, this is a nudge in the right direction.
You can do something similar to this to accomplish your goal:
%{
#include <stdio.h>
int sline = 0;
int mline = 0;
#define T_SLINE 0001
#define T_BEGIN_MLINE 0002
#define T_END_MLINE 0003
#define T_UNKNOWN 0004
%}
WSPACE [ \t\r]+
IDENT [a-zA-Z0-9]
%%
"//" {
printf("TOKEN: T_SLINE LEXEME: %s\n", yytext);
sline++;
return T_SLINE;
}
"/*" {
printf("TOKEN: T_BEGIN_MLINE LEXEME: %s\n", yytext);
return T_BEGIN_MLINE;
}
"*/" {
printf("TOKEN: T_END_MLINE LEXEME: %s\n", yytext);
mline++;
return T_END_MLINE;
}
{IDENT} {/*Do nothing*/}
{WSPACE} { /*Do Nothing*/}
. {
printf("TOKEN: UNKNOWN LEXEME: %s\n", yytext);
return T_UNKNOWN;
}
%%
int yywrap(void) { return 1; }
int main(void) {
while ( yylex() );
printf("Single-line comments = %d\n Multi-line comments = %d\n Total comments = %d\n", sline, mline, (sline + mline));
return 0;
}
The problem is your regex for multiline comments:
"/*"[a-zA-Z0-9 \t\n]*"*/"
This only matches multiline comments that ONLY contain letters, digits, spaces, tabs, and newlines. If the comment contains anything else it won't match. You want something like:
/"*"([^*]|"*"+[^*/])*"*"+/
This will match anything except a */ between the /* and */.
Below is the full lex code to count the number of comment line and executable line.
%{
int cc=0,cl=0,el=0,flag=0;
%}
%x cmnt
%%
^[ \t]*"//".*\n {cc++;cl++;}
.+"//".*\n {cc++;cl++;el++;}
^[ \t]*"/*" {BEGIN cmnt;}
<cmnt>\n {cl++;}
<cmnt>.\n {cl++;}
<cmnt>"*/"\n {cl++;cc++;BEGIN 0;}
<cmnt>"*/" {cl++;cc++;BEGIN 0;}
.*"/*".*"*/".+\n {cc++;cl++;}
.+"/*".*"*/".*\n {cc++;cl++;el++;}
.+"/*" {BEGIN cmnt;}
.\n {el++;}
%%
main()
{
yyin=fopen("abc.cpp","r");
yyout=fopen("abc.txt","w");
yylex();
fprintf(yyout,"Comment Count: %d \nCommented Lines: %d \nExecutable Lines: %d",cc,cl,el);
}
int yywrap()
{
return 1;
}
The program takes the input as a c++ program that is abc.cpp and appends the output in the file abc.txt

ncurses and stdin blocking

I have stdin in a select() set and I want to take a string from stdin whenever the user types it and hits Enter.
But select is triggering stdin as ready to read before Enter is hit, and, in rare cases, before anything is typed at all. This hangs my program on getstr() until I hit Enter.
I tried setting nocbreak() and it's perfect really except that nothing gets echoed to the screen so I can't see what I'm typing. And setting echo() doesn't change that.
I also tried using timeout(0), but the results of that was even crazier and didn't work.
What you need to do is tho check if a character is available with the getch() function. If you use it in no-delay mode the method will not block. Then you need to eat up the characters until you encounter a '\n', appending each char to the resulting string as you go.
Alternatively - and the method I use - is to use the GNU readline library. It has support for non-blocking behavior, but documentation about that section is not so excellent.
Included here is a small example that you can use. It has a select loop, and uses the GNU readline library:
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <stdlib.h>
#include <stdbool.h>
int quit = false;
void rl_cb(char* line)
{
if (NULL==line) {
quit = true;
return;
}
if(strlen(line) > 0) add_history(line);
printf("You typed:\n%s\n", line);
free(line);
}
int main()
{
struct timeval to;
const char *prompt = "# ";
rl_callback_handler_install(prompt, (rl_vcpfunc_t*) &rl_cb);
to.tv_sec = 0;
to.tv_usec = 10000;
while(1){
if (quit) break;
select(1, NULL, NULL, NULL, &to);
rl_callback_read_char();
};
rl_callback_handler_remove();
return 0;
}
Compile with:
gcc -Wall rl.c -lreadline