473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

this is a hand written lexer..its showing segmentation fault.can any1 help?

Expand|Select|Wrap|Line Numbers
  1. extern void *malloc();
  2. //structure of a token
  3. struct token
  4. {
  5.     int tval;
  6.     char *lexeme;
  7.     struct token *link;
  8. };
  9.  struct token *tfirst=NULL;
  10. char *seentext;
  11.  
  12.  int state=0;int nc=0;
  13.  
  14. //adds token at the beginning in a symbol table
  15. struct token * tokenlist(int val,char *word,struct token *tfirst)
  16.         struct token *tp;
  17.  
  18.     tp=(struct token *)malloc(sizeof(struct token));
  19.     tp->tval=val;
  20.     tp->lexeme=(char *) malloc(strlen(word) + 1);
  21.     strcpy(tp->lexeme,word);
  22.     tp->link=tfirst;
  23.     return tp->link;
  24. }
  25.  
  26.  
  27.  
  28. int addtoken(int val,char * word)
  29. {       int x;
  30.      if ((x=lookup(word))==0)
  31.     {
  32.      struct token * newtoken;
  33.     newtoken=(struct token *)malloc(sizeof(struct token));
  34.     newtoken->tval=val;
  35.     newtoken->lexeme=(char *)malloc(strlen(word)+1);
  36.     strcpy(newtoken->lexeme,word);
  37.     newtoken->link=tfirst;
  38.     tfirst=newtoken;
  39.     free(seentext);
  40.     return val;
  41.     }
  42.  
  43.     return x;
  44.  
  45.  
  46. int lookup(char *word)
  47. {
  48.     struct token *mark;
  49.     mark=tfirst;
  50.     while(mark!=NULL)
  51.         {
  52.         if(strcmp(mark->lexeme,word)==0)
  53.         return mark->tval;
  54.         mark=mark->link;
  55.         }
  56.     return 0;
  57. }
  58. void delete(struct token *tfirst)
  59. {
  60.   struct token *m;
  61.   if(tfirst==NULL)
  62.   return;
  63.   m=tfirst;
  64.   while(m!=NULL)
  65.   { 
  66.     tfirst=m;
  67.     m=m->link;
  68.     free(tfirst);
  69.   }
  70. }
  71.  
  72. int lex()
  73. {
  74.     char c[2];c[1]='\0';
  75.  
  76.     int BR=0;
  77.  
  78.     while(1)
  79.     {
  80.         switch(state)
  81.         {
  82.             case 0:{
  83.                 c[0]=getchar();
  84.                 seentext=(char * )malloc(50);
  85.                 if(c[0]==EOF) return 0;
  86.                 if(c[0]=='-')
  87.                 addtoken(MINUS,"-");
  88.  
  89.                 else if(c[0]=='+')
  90.                 addtoken(PLUS,"+");
  91.  
  92.                 else if(c[0]=='*')
  93.                 addtoken(MUL,"*");
  94.  
  95.                 else if(c[0]=='/')
  96.                 addtoken(DIV,"/");
  97.  
  98.                 else if(c[0]=='%'){state=0;addtoken(REM,"%");}
  99.                 else if(c[0]=='$'){state = 3;break;}
  100.                 else if(c[0]=='<'){state=4;break;}
  101.                 else if(c[0]=='>'){state=5;break;}
  102.                 else if(isalpha(c)){state=1;break;}
  103.                 else if(isdigit(c)){state=2;break;}
  104.                 if(c[0]==' '||c[0]=='\t'||c[0]=='\n') {state=13;}
  105.                 //else error();
  106.                 }
  107.             case 1:{
  108.                 seentext=(char *)malloc(50);
  109.                 strcpy(seentext,c);
  110.                 while(1)
  111.                     {
  112.                         c[0]=getchar();
  113.                         if((c[0]==EOF) && !isalpha(c))
  114.                             {
  115.                                 ungetc(c[0],stdin);
  116.                                 state=0;
  117.                                 addtoken(ID,seentext);
  118.  
  119.                             }
  120.                         else
  121.                         strcat(seentext,c);
  122.                     }    
  123.                 }
  124.             case 2:{
  125.  
  126.                 strcpy(seentext,c);
  127.                 while(1)
  128.                     {
  129.                         c[0]=getchar();
  130.                         if ((c[0]==EOF) || !isdigit(c))
  131.                             {
  132.                                 ungetc(c[0],stdin);
  133.                                 state=0;addtoken(NUM,seentext);
  134.                             }
  135.                         else 
  136.                             strcat(seentext,c);
  137.                     }
  138.                 }
  139.  
  140.             case 3:{
  141.                 c[0]=getchar();
  142.                 if(c[0]=='$')
  143.                 {
  144.                 BR++;
  145.                 state=0;
  146.                 addtoken(OPBR,"$$");
  147.                 }
  148.                  if(c[0]==' '||c[0]=='\n'||c[0]=='\t')
  149.                 {
  150.                 BR--;
  151.                 state=0;
  152.                 addtoken(CLBR,"$");}
  153.                 //else error();
  154.                 }
  155.             case 4:{
  156.                 //seentext=(char *)malloc(50);
  157.                 //strcpy(seentext,c);
  158.                 c[0]=getchar();
  159.                 if(c[0]=='=')
  160.                 {
  161.                 //strcpy(seentext,c);
  162.                 state=0;
  163.                 addtoken(ROP,"<=");
  164.                 }
  165.                  if(c[0]=='>')
  166.                 {
  167.                 //strcpy(seentext,c);
  168.                 state=0;
  169.                 addtoken(ROP,"<>");
  170.                 }
  171.                  if(c[0]==' ')
  172.                 {
  173.                 state=0;ungetc(c[0],stdin);    
  174.                 addtoken(ROP,"<");
  175.                 }
  176.                 //else
  177.                 //error();
  178.                             }
  179.             case 5:
  180.                 {
  181.                 //seentext=(char *)malloc(50);
  182.                 //strcpy(seentext,c);
  183.                 c[0]=getchar();
  184.                 if(c[0]=='=')
  185.                 {
  186.                 //strcat(seentext,c);
  187.                 state=0;            
  188.                 addtoken(ROP,">=");
  189.                 }
  190.                  if(c[0]==' ')
  191.                 {            
  192.                 state=0;
  193.                 ungetc(c[0],stdin);            
  194.                 addtoken(ROP,">");
  195.                 }
  196.                 //else error();
  197.  
  198.                  }
  199.             case 6:{
  200.                 while((c[0]=getchar())!=EOF && (c[0]=='\n'||c[0]=='\t'||c[0]==' '))
  201.                             if(c[0]=='\n') nc++;
  202.  
  203.                             if(c[0]==EOF) return 0;
  204.                             ungetc(c[0],stdin);state=0;
  205.                             break;
  206.                 }
  207.         }    
  208.     }   
  209. }
  210.  
  211. int main()
  212.     { 
  213.         int val;
  214.  
  215.         tfirst=tokenlist(MAIN,"main()",tfirst);
  216.         tfirst=tokenlist(PLUS,"+",tfirst);
  217.         tfirst=tokenlist(MINUS,"-",tfirst);
  218.         tfirst=tokenlist(MUL,"*",tfirst);
  219.         tfirst=tokenlist(DIV,"/",tfirst);
  220.         tfirst=tokenlist(REM,"%",tfirst);
  221.         tfirst=tokenlist(iff,"iff",tfirst);
  222.         tfirst=tokenlist(den,"den",tfirst);
  223.         tfirst=tokenlist(orelse,"orelse",tfirst);
  224.         tfirst=tokenlist(till,"till",tfirst);
  225.         tfirst=tokenlist(ASS,"=",tfirst);
  226.         tfirst=tokenlist(ROP,"<=",tfirst);
  227.         tfirst=tokenlist(ROP,"<>",tfirst);
  228.         tfirst=tokenlist(ROP,"<",tfirst);
  229.         tfirst=tokenlist(ROP,">=",tfirst);
  230.         tfirst=tokenlist(ROP,">",tfirst);
  231.  
  232.             while (val=lex()){
  233.                 printf("%d \n",val);}
  234.         return 0;
  235.     }
  236.  
May 9 '10 #1
1 2019
jkmyoung
2,057 Expert 2GB
Can you give us more information on what it's supposed to do, so we can better understand it?
May 10 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: cpptutor2000 | last post by:
I am using an STL list to store data packets in a network simulator. The data packets are really C structs, which have other C structs inside them. These structs contain unsigned char arrays of...
4
by: Marcia Hon | last post by:
Hi, I am trying to run a program except I get the following segmentation fault. I don't know how to solve it. Please if you know could you please help. Thanks, Marcia Program received...
5
by: silverburgh.meryl | last post by:
Hi, I have a segmentation fault in line 66 of GroupResult.h and I can't figure out why that causes any problem, I appreciate if anyone can help. line 66 of Result.h: 66 size_t size()...
14
by: Vlad Dogaru | last post by:
Hello, I am trying to learn C, especially pointers. The following code attempts to count the appearences of each word in a text file, but fails invariably with Segmentation Fault. Please help me...
1
by: ahab | last post by:
Hi, when I execute this code I receive a segmentation fault. I know that somethig's happening when I try to erase an element from the string list but I don't know a solution The line which cause...
8
by: Andrea | last post by:
I wrote this code: void * xmalloc (size_t size){ register void *value = OPENSSL_malloc(size); if (value == 0) printf("virtual memory exhausted"); return value; } int _chooseTSK(char*...
1
by: Sharad Maloo | last post by:
Hi, I am running the image file of my project, it is giving following segmentation fault: ERROR----- > Program received signal SIGSEGV, Segmentation fault. 0x004240cb in strlen () from...
4
by: yinglcs | last post by:
Hi, i get this segmentation fault in my c++ program: My question is 'it shows inMethodStr is not null, how come this can have *inMethodStr.Ptr segmentation fault? Program received signal...
10
by: H.S. | last post by:
Hello, I have class in which I am allocating space for a double array in the constructor. I use the double array twice in one of the methods and then delete that array in the class's destructor....
7
by: kathyayini | last post by:
i am trying to implement the algorithm given in the dragon book but i am facing a lot of difficulties please help...... especially the regular expression to DFA using C
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.