Hi,
I'm using flex/lex and im totally new to this.
I want to store the tokens I'm getting in an array.
How can I do that? I get lots of syntax issues.
here is my current code(its not much):
%{
#include <stdio.h>
#include <string.h>
#define SIZE 10
#define ADD 1
#define MUL 2
#define LT 3
#define GT 4
#define LE 5
#define GE 6
#define EQ 7
#define NE 8
void showToken(char *);
%}
digit [0-9]
letter [a-zA-Z]
whitespace [\t\n ]
%%
{digit}+ showToken("number");
{letter}+ showToken("word");
{letter}+@{letter}+\.com showToken("email address");
\+ showToken("add");
{whitespace} ;
. printf("Unkown token(\"%s\")!\n", yytext);
%%
struct TokenStorage
{
struct TokenStorage *next;
struct TokenStorage *back;
char *arr[SIZE];
}TokenStorage;
int AddToArray(char *a[], char *token)
{
int i = 0;
while(a != NULL)
{
if(i < SIZE - 1)
{
i++;
}
else
{
return 0;
}
}
strcpy(*a, *token);
return 1;
}
void main
{
int addStatus;
TokenStorage *head = NULL, *tmp = NULL;
while(yylex)
{
if(*head == NULL)
{
head = (TokenStorage*)malloc(sizeof(TokenStorage));
head->next = head;
head->back = head;
addStatus = AddToArray();
}
}
}
void showToken(char * name){
printf("Line %d : Lex found a %s, the lexeme is %s and its length is %d\n", yylineno, name, yytext, yyleng);
}
Thanks for the help!