help-bison
[Top][All Lists]
Advanced

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

Beginners Questions #1


From: Douglas Pollock
Subject: Beginners Questions #1
Date: Wed, 2 Jan 2002 21:24:07 -0500

W32 Port Flex Version 2.5.4a
W32 Port Bison Version 1.25
Windows NT 4 Operating System
Visual C++ Version 6 Compiler

I am a beginner with Bison and Flex. I am trying to start out by simply
getting Bison and Flex to talk together and run some test scenarios to see
if Flex and Bison can scan for some character patterns and then output the
result to stdout so that I can determine if they are correct. I've started
by trying to recognize characters and then I figure I would move up to
longer patterns checking for validity as I get accustom.

I have a several issues:
1. I'm not sure how to cause Bison to complete after it has recognized one
pattern. When Bison has gotten to the point where it has found a pattern or
character it does not seem to perform the action of sending output to
stdout.
2. When Bison completes I get a parse error and I do not get my "Parsing
completed" message.
3. Is there a character or method to cause the parser to reduce the pattern
at that point and perform the action. In other words if you want the parser
to perform an action with each pattern rather then stacking up patterns
before reducing. I noticed that it would recognize one pattern and never
perform the action but would then start shifting in the next pattern. I have
tryed to add in "PgmEndStatement:  k_end;" to cause a completion.

I would appreciate any help. Thanks.
Doug

----------------------------------------------------------
Here are the results:

Starting parse
Entering state 0
Reading a token: --(end of buffer or a NUL)
--accepting rule at line 50 ("*")
Next token is 258 (c_star)
Shifting token 258 (c_star), Entering state 1
Reading a token: --accepting rule at line 65 (" ")
--accepting rule at line 24 ("end")
Next token is 260 (k_end)
parse error
Error: state stack now 0

---------------------------------------------------------

Here is the data:
* end
,

---------------------------------------------------------
Here is the Flex file.
%{
 /* Prototypes\Proto01\Proto01.l */
 /* Used to test basic Bison Parser scenarios */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "proto01.tab.h"
 unsigned int comments, code, whitespace;

%}
/* -------------------------------------------------------------------------
----------------------------------------- */
%s COMMENT
alphachar [a-zA-Z]
numerchar [0-9]
specialchar [_]
blank [\ ]
character  {alphachar}|{numerchar}|{specialchar}
identifier {alphachar}({character})*
/* -------------------------------------------------------------------------
----------------------------------------- */
%%

"end"   { return(k_end);  }

({numerchar})*\.({numerchar})+ {
                 yylval.rval = atof(yytext);
                 return(v_realnumber);
                }

{numerchar}+    {
                 yylval.ival = atoi(yytext);
                 return(v_integer);
                }

{identifier}    {
                 yylval.string = malloc(strlen(yytext)+1);
                 strcpy(yylval.string,yytext);
                 return(v_identifier);
                }

\"({character}|{blank})*\"    {
                 yylval.string = malloc(strlen(yytext)+1);
                 strcpy(yylval.string,yytext);
                 return v_string;
                }

\+  { return(c_plus);        }
\-  { return(c_minus);       }
\*  { yylval.cval = yytext; return(c_star);     }
\/  { return(c_fwdslash);    }
\\  { return(c_bckslash);    }
\,  { return(c_comma);       }
\?  { return(c_question);    }
\:  { return(c_colon);       }
\;  { return(c_semicolon);   }
\[  { return(c_ltbracket);   }
\]  { return(c_rtbracket);   }
\"  { return(c_quote);       }
\.  { return(c_period);      }
\(  { return(c_ltparen);     }
\)  { return(c_rtparen);     }
\=  { return(c_equal);       }

[\n\t\ ]+ {;}

.  {
   puts("this is an error");
   puts(yytext);
  }

%%
/* -------------------------------------------------------------------------
----------------------------------------- */

char **targv;  /* remembers arguments  */
char **arglim; /* end of arguments   */

int yywrap(void)
{
 return 1;
}

-------------------------------------------------------------
Here is the Bison file.

 /* Prototypes\Proto01\Proto01.y */
 /* Used to test basic Bison Parser scenarios */
%{
#include <stdio.h>
#include <string.h>
#define YYDEBUG 1
void yyerror(char *s);
extern int yylex(void);
int yydebug = 1;
char szTmp01[100];
%}

%type <string> PgmStatements PgmStatement PgmTokens PgmToken
%type <cval>  c_star c_comma

/* k_ = keyword  */
/* c_ = character */
/* v_ = lex returned variable */


%token  k_end

%token c_eol
%token c_plus
%token c_minus
%token c_equal
%token c_star
%token c_comma
%token c_question
%token c_colon
%token c_semicolon
%token c_quote
%token c_period
%token c_fwdslash
%token c_bckslash
%token c_ltbracket
%token c_rtbracket
%token c_ltbrace
%token c_rtbrace
%token c_ltparen
%token c_rtparen
%token <string>     v_identifier
%token  <rval>      v_realnumber
%token  <ival>      v_integer
%token  <string>    v_string


%union
{
 char   *string;
 double  rval;
 int   ival;
 char    cval;
}


%left  '-' '+'
%left  '*' '/'
%nonassoc UMINUS

/* -------------------------------------------------------------------------
----------------------------------------- */
/* -------------------------------------------------------------------------
----------------------------------------- */
%%

PgmOrganization:    PgmStatements PgmEndStatement
          {
           puts("Parsing completed\n");
          };
PgmStatements:   PgmStatement;
PgmStatement:    PgmTokens;
PgmTokens:       PgmToken;
PgmToken:        c_star ' ' PgmEndStatement   { sprintf(szTmp01,"Token =>
%c\n", yylval.cval);
                                                puts(szTmp01);
                                                $$ = szTmp01;
                                              };

PgmEndStatement:  k_end;

%%
/* -------------------------------------------------------------------------
----------------------------------------- */

int
main(void)
{
  return yyparse();
}

void
yyerror(char *s)
{
 fprintf(stderr,"%s\n", s);
}







reply via email to

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