help-bison
[Top][All Lists]
Advanced

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

Re: Parse Tree using Bison


From: UJWAL POTLURI
Subject: Re: Parse Tree using Bison
Date: Wed, 7 Mar 2012 01:33:38 -0500

I tried to implement the parse but a couple of errors are showing up and I
am unable to get rid of them. Can anyone please look into this.Its very
urgent for me to get the parser running. Thanks in advance. Here is the y
file:

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

void yyerror(const char *str)
{
        fprintf(stderr,"error FAIL: %s\n",str);
}

int yywrap()
{
        return 1;
}

enum treetype {operator_node, variable_node};
 typedef struct tree {
   enum treetype nodetype;
   union {
     struct {struct tree *left, *right; char operator;} an_operator;
      char *a_variable;
   } body;
 } tree;
static tree *make_op (tree *l, char o, tree *r) {
   tree *result= (tree*) malloc (sizeof(tree));
   result->nodetype= operator_node;
   result->body.an_operator.left= l;
   result->body.an_operator.operator= o;
   result->body.an_operator.right= r;
   return result;
 }
static void printtree (tree *t, int level) {
 #define step 4
   if (t)
     switch (t->nodetype)
     {
       case operator_node:
        printtree (t->body.an_operator.right, level+step);
        printf ("%*c%c\n", level, ' ', t->body.an_operator.operator);
        printtree (t->body.an_operator.left, level+step);
        break;
       case variable_node:
        printf ("%*c%c\n", level, ' ', t->body.a_variable);
     }
 }

%}

%union {
   char* a_variable;
   tree* a_tree;
 }

%start file
%token <a_variable> TOKDIGIT TOKFLOAT TOKID TOKSEMICOLON TOLCOLON TOKCOMMA
TOKUNRECOG TOKCOMMENT TOKDOT TOKMINUS TOKCOLON
%type <a_tree> field object file ID
%right TOKMINUS

%%

file :
| object file { printtree($1, 1); }
;
object :
field object {$$ = make_op($1, '', $2);}
|
field {$$ = $1 ; }
;
field  :
|
ID TOKCOLON field {$$ = make_op ($1, ':', $3); }
|
ID TOKCOMMA field {$$ = make_op ($1, ',', $3); }
|
ID TOKSEMICOLON field {$$ = make_op ($1, ';', $3); }
;
ID     :   TOKID { $$ = $1; }
           ;
%%


On Sat, Jan 14, 2012 at 8:50 AM, Hans Aberg <address@hidden> wrote:

> On 14 Jan 2012, at 11:28, Luca wrote:
>
> >>> The bison manual has some good basic examples if i recall.
> >> ...
> >>> Read on here
> >>>
> http://www.gnu.org/software/bison/manual/html_node/Infix-Calc.html#Infix-Calc
> >> The C++ example is good, too. It shows how to generate error messages.
> >>
> http://www.gnu.org/software/bison/manual/html_node/A-Complete-C_002b_002b-Example.html
> >>
> > Also read on here:
> > http://epaperpress.com/lexandyacc/download/LexAndYaccTutorial.pdf
> > it is about yacc, but the tutorial is very good and also valid using
> Bison.
>
> This calculator example is good, because it shows how to do control flow
> ('while' and 'if then else'), lacking in the Bison manual examples, but
> from time to time asked here. Also, it computes the AST (abstract syntax
> tree) rather than the parse tree, which is what one normally would be
> interested in in applications.
>
> Hans
>
>
>


-- 
Ujwal Potluri,
Masters student,
Computer Science,
University of Connecticut,
Connecticut, USA.


reply via email to

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