help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: token value return in custom yylex()


From: Tomas Crhak
Subject: Re: token value return in custom yylex()
Date: Wed, 4 Dec 2002 13:34:51 +0100

On Wed, 04 Dec 2002 09:52:19 +0100
Nils Hasler <address@hidden> wrote:

> hi bernd.
> 
> you need some check for end-of-string in your for()-loop. the easiest 
> would be somthing like:
> 
>    for (i = 0; clone[0] && (strchr(delimiters, clone[0])==NULL); i++) {
>       if (clone[0] == '\\') {   // escape next character
>         clone++;
>       }
>       yytext[i] = clone[0];
>       clone++;
>    }
> 
> the (i < length) construct doesn't work, because you increase the 
> base-pointer (clone) without reducing the length. the other solution to 
> your problem would be to --length whereever you ++clone.

True, but the parser keeps producing
==========================
word: >word1<
word: >word2<
object: (token >word2<)
term: (single >word2< object >word2<)
object: (list >word2 word2 <)
Segmentation fault
===========EOT=============

Try this patch (the above is not included) - you'll get
===========================
word: >word1<
word: >word2<
object: (token >word2<)
term: (single >word1< object >word2<)
object: (list >word1 word2<)
===========EOT=============

The patch:
===========================
--- /tmp/t      2002-12-04 10:47:49.000000000 +0100
+++ /tmp/ttt/t.y        2002-12-04 10:44:10.000000000 +0100
@@ -1,11 +1,18 @@
 %{
 #include <stdio.h>
 #include <string.h>
-#include "parser.tab.h"
+
 
 #define TESTSTRING "word1 word2" // testpattern
 
 char *data, buffer[1024];
+
+void
+yyerror (char *s)
+{
+  fprintf (stderr, "%s\n", s);
+}
+
 %}
 
 %union {
@@ -14,7 +21,7 @@
 }
 
 %type <str> object token list
-%token <id> WORD
+%token <str> WORD
 
 %start object
 %%
@@ -22,12 +29,12 @@
 object: token
        {
           printf( "object: (token >%s<)\n", $1);
-          strcpy($$, $1);
+          $$ = strdup($1);
        }
     |  list
        {
           printf ("object: (list >%s<)\n", $1);
-          strcpy($$, $1);
+          $$ = strdup($1);
        }
     ;
 
@@ -35,7 +42,7 @@
 token:  WORD
        {
           printf( "word: >%s<\n", $1);
-          strcpy($$, (char *)$1);
+          $$ = strdup($1);
        }
     ;
 
@@ -43,6 +50,7 @@
        {
           char buf[1024];
           printf( "term: (single >%s< object >%s<)\n", $1, $2);
+         $$ = (char *)malloc(strlen($1)+strlen($2)+2);
           sprintf($$, "%s %s", $1, $2);
        }
     ;
@@ -59,6 +67,10 @@
 {
   int i;
 
+#if YYDEBUG == 1
+  yydebug = 1;
+#endif
+
   /* allocate memory for string lexbuffers */
   length = strlen(TESTSTRING) + 1;
   lexbuffer = (char *)malloc( length );
@@ -77,7 +89,7 @@
 {
    int i;
 
-   if (clone >= lexbuffer + length) return 0;           // end of string
+   if (clone >= lexbuffer + length - 1) return 0;           // end of string
 
    while (clone[0] == ' ' || clone[0] == '\t') clone++; // skip white space

=============EOT=============

Tomas




reply via email to

[Prev in Thread] Current Thread [Next in Thread]