[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Very basic question about Flex
From: |
address@hidden |
Subject: |
Re: Very basic question about Flex |
Date: |
Sat, 23 Feb 2019 05:40:31 +0100 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.1 |
Thanks for you help. Now i've typed in one of the first examples and it
throws me an strange error:
> flex01.l:99: premature EOF
The code looks like this:
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum {
LOOKUP = 0,
BOOL,
INT,
FLOAT,
STRING,
VEC2,
VEC3,
VEC4
};
int state;
int add_word(int type, char *word);
int lookup_word(char *word);
⅝}
%%
\n { state = LOOKUP; }
^bool { state = BOOL; }
^int { state = INT; }
^float { state = FLOAT; }
^string { state = STRING; }
^vec2 { state = VEC2; }
^vec3 { state = VEC3; }
^vec4 { state = VEC4; }
[a-zA-z] {
if(state == LOOKUP){
add_word(state, yytext);
}else{
switch(lookup_word(yytext)){
case BOOL: printf("%s is a bool variable.\n"); break;
case INT: printf("%s is a int variable.\n"); break;
case FLOAT: printf("%s is a float variable.\n"); break;
case STRING: printf("%s is a string variable.\n");
break;
case VEC2: printf("%s is a vec2 variable.\n"); break;
case VEC3: printf("%s is a vec3 variable.\n"); break;
case VEC4: printf("%s is a vec4 variable.\n"); break;
default: { printf("%s not recognized!\n", yytext);
break; }
}
}
}
%%
struct word {
char *word_name;
int word_type;
struct word *next;
};
struct word *wordlist;
int add_word(int type, char *word)
{
struct word *wp;
if(lookup_word(word) != LOOKUP){
printf("Word %s already in the symbol table!\n", word);
return 0;
}
wp = (struct word *) malloc(sizeof(struct word));
wp->next = word_list;
wp->word_name = (char*) malloc((sizeof(char)*strlen(word))+1);
strcpy(wp->word_name, word);
wp->word_type = type;
word_list = wp;
return 1;
}
int lookup_word(char *word)
{
struct word *wp = word_list;
for(; wp; wp=wp->next){
if(strcmp(wp->word_name, word) == 0){
return wp->word_type;
}
}
return LOOKUP;
}
int main(int argc, char **argv)
{
yylex();
return 0;
}
Why is the there an premature EOF ??
best regards!
On 22.02.19 18:29, Jannick wrote:
On Fri, 22 Feb 2019 17:02:57 +0100, address@hidden wrote:
$gcc lex.yy.c -o test -Wall
Comparing to your earlier compile statement, you dropped '-lfl' which caused
the linking error. To remedy the situation add to the top of the flex file
%option noyywrap
%option noinput nounput
Then the errors should go away. The first option makes linking against the flex
library obsolete.
Enjoy flex.
HTH
J.
_______________________________________________
address@hidden https://lists.gnu.org/mailman/listinfo/help-bison