[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Question about Lexx & Yacc
From: |
Nattapong |
Subject: |
Question about Lexx & Yacc |
Date: |
Thu, 11 Jan 2001 15:20:21 -0700 |
User-agent: |
Mozilla/5.0 (X11; U; Linux 2.2.15-4mdk i686; en-US; m18) Gecko/20001107 Netscape6/6.0 |
Hello there!,
I hope I send my question to the right place. If not, I apologize for that.
The question is about my simple program on Lex & Yac ... (well actually
flex and yacc).
please take a look at my "ch1-05.y". I compiled the code without any
problem. But
the error hit me when I tried to run it. "Segmentation Fault"
Listing === ch1-05.y
------------------------------------------------------------------
%{
#include <stdio.h>
%}
%token NOUN PRON VERB ADV ADJ PREP CONJ
%%
sentence: subject VERB object { printf ("Sentence is valid.\n"); }
;
subject: NOUN
| PRON
;
object: NOUN
;
%%
extern FILE *yyin;
main ()
{
while (!feof(yyin)) {
yyparse ();
}
}
yyerror (s)
char *s;
{
fprintf (stderr, "%s\n", s);
}
------------------------------------------------------------------
The program terminated when it hit "feof ()".
This problem only occur when running on linux but not solaris.
I guess that is because the lex and yacc program on the two systems are
different.
Below is my lex file "ch1-05.l"
------------------------------------------------------------------
%{
/*
* Word recognizer with a symbol table
*/
#include "y.tab.h"
#define LOOKUP 0
int state;
int add_word (int type, char *word);
int lookup_word (char *word);
%}
%%
\n { state = LOOKUP; }
\.\n { state = LOOKUP;
return 0;
}
^verb { state = VERB; }
^adj { state = ADJ; }
^adv { state = ADV; }
^noun { state = NOUN; }
^prep { state = PREP; }
^pron { state = PRON; }
^conj { state = CONJ; }
[a-zA-Z]+ {
if (state != LOOKUP) {
add_word (state, yytext);
}
else {
switch (lookup_word (yytext)) {
case VERB:
return (VERB);
case ADJ:
return (ADJ);
case ADV:
return (ADV);
case NOUN:
return (NOUN);
case PREP:
return (PREP);
case PRON:
return (PRON);
case CONJ:
return (CONJ);
default:
printf ("%s: don't recognize\n", yytext);
}
}
}
. ;
%%
struct word {
char *word_name;
int word_type;
};
struct word *word_list; /* first element in word list */
extern void *malloc();
int add_word (int type, char *word)
{
struct word *wp;
if (lookup_word (word) != LOOKUP) {
printf ("!!! warning: word %s already defined \n", word);
return 0;
}
/* word not there, allocate a new entry and link it on the list */
wp = (struct word *) malloc (sizeof (struct word));
wp->next = word_list;
/* have to copy the word itself as well */
wp->word_name = (char *) malloc (strlen (word) + 1);
strcpy (wp->word_name, word);
wp->word_type = type;
word_list = wp;
return 1; /* it worked! */
}
int lookup_word (char *word)
{
struct word *next;
struct word *wp = word_list;
for (;wp;wp = wp->next) {
if (strcmp (word, wp->word_name) == 0) {
return wp->word_type;
}
}
return LOOKUP; /* word not found */
}
--------------------------------------------------------------------
- Question about Lexx & Yacc,
Nattapong <=