Author Topic: how to store tokens in array using lex and c  (Read 5548 times)

Offline yakirllc

  • Single posting newcomer
  • *
  • Posts: 2
how to store tokens in array using lex and c
« on: April 05, 2016, 01:21:37 pm »
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!

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
Re: how to store tokens in array using lex and c
« Reply #1 on: April 05, 2016, 03:28:24 pm »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline yakirllc

  • Single posting newcomer
  • *
  • Posts: 2
Re: how to store tokens in array using lex and c
« Reply #2 on: April 05, 2016, 03:43:32 pm »
Have I violated rule #1?
If so, I'm sorry. Didn't think my question was in it's area.
Plus, if I did violate rule #1, can you guys please tell me where can I ask that type of question?
Because I did search google and came up empty.

Thanks!

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: how to store tokens in array using lex and c
« Reply #3 on: April 09, 2016, 05:59:47 am »
Have I violated rule #1?
There are so many C/C++ programmers forums, I guess you can find out yourself. It depends on the area of programming you step into which you should choose. You could start also with with https://stackoverflow.com/.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ