Converting BNF rules for arithmetic expression into yacc form and generating trees

 st.lex

******************

%{

        #include <bits/stdc++.h>

        #include "st.tab.h"

%}

%option noyywrap

%%

[a-zA-Z_][a-zA-Z_0-9]*  yylval.str=strdup(yytext);return ID ;

([0-9]*\.?[0-9]*)|(\".*?\")|(\'.\')     yylval.str=strdup(yytext);return VAL ;

";"             yylval.str=strdup(yytext);return SC ;

"+"             yylval.str=strdup(yytext);return PL ;

"-"             yylval.str=strdup(yytext);return MI ;

"*"             yylval.str=strdup(yytext);return MUL ;

"/"             yylval.str=strdup(yytext);return DIV ;

"="             yylval.str=strdup(yytext);return EQ ;

"("             yylval.str=strdup(yytext);return OP ;

")"             yylval.str=strdup(yytext);return CL ;

"^"             yylval.str=strdup(yytext);return POW ;

[\n \t]+        ;

.               yylval.str=strdup(yytext);return UNR;

%%

st.y
******************

%{

        #include<bits/stdc++.h>

        using namespace std;

        void yyerror(char *s);

        int yylex();

        typedef struct node

        {

                struct node *left,*right;

                string val;

                int label;

        }NODE;

        NODE* makeNode(string val, NODE* left , NODE* right)

        {

                NODE *temp=new NODE();

                temp->val=val;

                temp->left=left;

                temp->right=right;

                temp->label=0;

                return temp;

        }

        NODE* synTree;

%}

%token PL MI MUL DIV OP CL EQ ID VAL SC UNR POW

%union{

        typedef struct node NODE;

        char *str;

        NODE *node;

}

%type <node> s e t f g

%type <str> PL MI MUL DIV OP CL EQ ID VAL SC UNR POW

%%

s : e {$$=$1;synTree=$$;}

e : e PL t {$$=makeNode($2,$1,$3);} |

         e MI t {$$=makeNode($2,$1,$3);} |

          t {$$=$1;};

t : t MUL f {$$=makeNode($2,$1,$3);}|

         t DIV f {$$=makeNode($2,$1,$3);}|

          f {$$=$1;};

f : g POW f {$$=makeNode($2,$1,$3);} |

         g {$$=$1;};

g : OP e CL {$$=$2;} |

         ID{$$=makeNode($1,NULL,NULL);} |

          VAL{$$=makeNode($1,NULL,NULL);} /*|

           MI g{$$=makeNode($1,NULL,$2);} */;

%%

void inOrder(NODE* root)

{

        if(root)

        {

                inOrder(root->left);

                cout<<root->val<<" ";

                inOrder(root->right);

        }

}

void postOrder(NODE* root)

{

        if(root)

        {

                postOrder(root->left);

                postOrder(root->right);

                cout<<root->val<<" ";

        }

}


void preOrder(NODE* root)

{

        if(root)

        {

                cout<<root->val<<" ";

                preOrder(root->left);

                preOrder(root->right);

        }

}

void yyerror(char *s)

{

printf("%s\n",s);

}

int main(void)

{

        yyparse();

        cout<<"In Order:"<<endl;

        inOrder(synTree);

        cout<<endl<<"Pre Order:"<<endl;

        preOrder(synTree);

        cout<<endl<<"Post Order:"<<endl;

        postOrder(synTree);

        cout<<endl;

}

Execution
**************************
$ bison -d st.y
$ lex st.lex
$ g++ lex.yy.c st.tab.c
$./a.out
a+b*c -d
In Order:
a + b * c - d
Pre Order:
- + a * b c d
Post Order:
a b c * + d -



Comments

Popular posts from this blog

KTU Compiler Lab CSL411 - Dr Binu V P

lexical analyzer for a c program

13.Precedence and conflict resolution in yacc