Thread: Lex help - Segmentation Fault

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    1

    Lex help - Segmentation Fault

    I'm using Lex to do lexical analysis, in the below code when it gets to returning 1 (on the EOF rule), it quits with a segmentation fault.

    Any idea whats causing this?

    I don't actually need the 1 value, i just need the program generated to exit the subroute, keeping the two arrays in memory so they can be accessed by my parsing program.

    any ideas?

    thanks.

    Code:
    %{
    #include <math.h>
    #include <stdio.h>
    
    /* codes for the different tokens */
    #define MYEOF 0
    #define IDENTIFIER 1
    #define NUMBER 2
    #define INTEGER 3
    #define CHAR 4
    
    
    int input[64]; /* buffer for holding the input */
    
    char *input_value[64];
    
    int count = 0; /*int to point to the next array location to be writen to*/
    
    void Set_Arrays(int token, char *Value);
    
    %}
    
    %%
    
    "int"			{Set_Arrays(INTEGER, yytext);}
    "char"			{Set_Arrays(CHAR, yytext);}
    
    "EOF"                   {Set_Arrays(MYEOF, yytext); return 1;}
    
    [0-9]+			{Set_Arrays(NUMBER, yytext);}
    [a-zA-Z][0-9a-zA-Z]*	{Set_Arrays(IDENTIFIER, yytext);}
    "("			{Set_Arrays('(', yytext);}
    ")"			{Set_Arrays(')', yytext);}
    
    "+"			{Set_Arrays('+', yytext);}
    "-"			{Set_Arrays('-', yytext);}
    "*"			{Set_Arrays('*', yytext);}
    "/"			{Set_Arrays('/', yytext);}
    "="			{Set_Arrays('=', yytext);}
    ";"			{Set_Arrays(';', yytext);}
    "{"			{Set_Arrays('{', yytext);}
    "}"			{Set_Arrays('}', yytext);}
    
    [ \t\n]+		{/* eat up white space */}
    
    .			{printf("Unrecognized character: %s\n", yytext);}
    
    %%
    
    void Set_Arrays(int token, char *value){
         input[count] = token;
         input_value[count] = value;
         printf("Tokeen = %d \n", input[count]);
         printf("Value = %s \n", input_value[count]);
         count++;
         
    }
    
    
    yywrap() {} /* For Linux compatibility */

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    is that C code? I don't think it will compile, maybe there's a problem with the formatting.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    it quits with a segmentation fault.
    I think it compiles. Or do you mean the compiler quits with a seg fault?

    I think the file gets pre-parsed by Lex, whatever that is, which turns it into valid C code.

    Or are you using digraphs? No, even then I don't think that would compile.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM