help-bison
[Top][All Lists]
Advanced

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

List compiling parser not working as expected (help please)


From: Dustin Robert Kick
Subject: List compiling parser not working as expected (help please)
Date: Fri, 16 Mar 2007 00:42:02 -0500

I want this code to simply put some strings into a list structure, but in my test output, the early strings contain all of the strings that are supposed to be separate nodes, as well as characters that the lexer was supposed to only return as tokens, or ignore. Will someone point out what I'm doing wrong with this?

<SAMPLE_OUTPUT>
__list parser__12:22:14__brunoise-tigre__./list-parser-y.exe
a b c : 1 2 3 ;
STRING
list_base->current:a:
list_end->current:a:
STRING
yylval.string_item:b:
list_base->current:a b:
list_end->current:b:
STRING
yylval.string_item:c:
list_base->current:a b c:
list_end->current:c:
COLON
STRING
list_base->current:1:
list_end->current:1:
STRING
yylval.string_item:2:
list_base->current:1 2:
list_end->current:2:
STRING
yylval.string_item:3:
list_base->current:1 2 3:
list_end->current:3:
SEMI-COLON
FINITO
list_end->current:(null):
list_base->rest->current:2 3 ;:
list_base->current:1 2 3 ;:
yylval->string_item:3 ;:
current_types->current:a b c : 1 2 3 ;:
1 2 3 ;:
2 3 ;:
3 ;:
</SAMPLE_OUTPUT>

<LEXER>
%{
#include "y.tab.h"
%}

end "@end"
colon \:
semi-colon \;
string [^ \t\n]+
whitespace [ \t\n]

%%

{end} {printf("END\n");return END;}
{whitespace} ;
{colon} {printf("COLON\n");return ':';}
{semi-colon} {printf("SEMI-COLON\n");return ';';}
{string} {printf("STRING\n");yylval.string_item = yytext;return STRING;}

%%
</LEXER>

<GRAMMAR>
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

  struct string_list{
    char * current;
    struct string_list * rest;};
  struct typed_list{
    struct string_list * types;
    struct string_list * names;};

  struct string_list *current_types;
  struct string_list *current_names;
  struct string_list *list_base;
  struct string_list *list_end;
  struct string_list *temp_list1;
  struct typed_list *parsed_type_list;

  %}

%union{
  struct typed_list * parsed_type_list_item;
  struct string_list * string_list_item;
  char * string_item;}

%token END
%token<string_item> STRING

%%

list : type_list string_list ';' {printf("FINITO\n");return;}
;

string_list : STRING {
list_base = (struct string_list *) malloc(sizeof(struct string_list));
  list_end = (struct string_list *) malloc(sizeof(struct string_list));

  char * temp = (char *) malloc(sizeof(char));
  temp = $1;
  list_end->current = temp;
temp_list1 = (struct string_list *) malloc(sizeof(struct string_list));

  list_end->rest = temp_list1;

  list_base = list_end;

  printf("list_base->current:%s:\n", list_base->current);
  printf("list_end->current:%s:\n", list_end->current);

  list_end = list_end->rest;
}
| string_list STRING {
  printf("yylval.string_item:%s:\n", yylval.string_item);

  char * temp = (char *) malloc(sizeof(char));
  temp = $2;
  list_end->current = temp;

temp_list1 = (struct string_list *) malloc(sizeof(struct string_list));

  list_end->rest = temp_list1;

  printf("list_base->current:%s:\n", list_base->current);
  printf("list_end->current:%s:\n", list_end->current);

  list_end = list_end->rest;
}
;

type_list : string_list ':' {
current_types = (struct string_list *) malloc(sizeof(struct string_list));
  current_types = list_base;}
;

%%

extern FILE * yyin;

main(){
  yyparse();
  printf("list_end->current:%s:\n", list_end->current);
  printf("list_base->rest->current:%s:\n", list_base->rest->current);
  printf("list_base->current:%s:\n", list_base->current);
  printf("yylval->string_item:%s:\n", yylval.string_item);
  printf("current_types->current:%s:\n", current_types->current);
  while(list_base->rest!=NULL)
    {
      printf("%s:\n", list_base->current);
      list_base=list_base->rest;
    }
}

yyerror(s)
char * s;
{
  fprintf(stderr, "%s\n", s);
}
</GRAMMAR>

Dustin Kick




reply via email to

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