Functions bison flex (Salik-yar-khan)

FLEX:

%{

#include “TP.tab.h”

#include<stdlib.h>

%}

%%

“public”                {return PUBLIC; }

“printf”                 {return PRINTF; }

“void”  {return VOID; }

“string”|”int”     {return DATATYPES; }

“(”                          {return ORB; }

“,”                           {return COMA; }

“)”                          {return CRB; }

“{”                          {return OCB; }

“}”                          {return CCB; }

“=”                         {return EQL;}

“;”                           {return COL;}

[a-zA-Z]+             {return NAME; }

[0-9]+    {yylval = atoi(yytext);

return NUMBER; }

[\n]                        {return *yytext; }

[ \t]                        ;               /*SKIP SPACES*/

.               yyerror(“\nInvalid Character\n”);

%%

int yywrap(void)

{

return 1;

}

BISON:

%{

#include <stdio.h>

void yyerror(char *s);

int printing;

%}

%token PUBLIC VOID  ORB DATATYPES COMA CRB OCB CCB NAME

%token PRINTF EQL NUMBER COL

%start program

%%

program: program statement ‘\n’

|

;

statement : expr

|

;

expr       : functionLine

| declaration

| callingfunc

;

functionLine: PUBLIC VOID NAME ORB CRB

| PUBLIC VOID NAME ORB CRB OCB

| PUBLIC VOID NAME ORB DATATYPES NAME CRB

| PUBLIC VOID NAME ORB DATATYPES NAME COMA DATATYPES NAME CRB

| PUBLIC VOID NAME ORB DATATYPES NAME COMA DATATYPES NAME CRB OCB

| OCB

| CCB

;

declaration : DATATYPES NAME EQL NUMBER COL

| DATATYPES NAME COL

| NAME EQL NUMBER COL

| PRINTF ORB NAME CRB COL

;

callingfunc : NAME ORB CRB COL

| NAME ORB NAME CRB COL

| NAME ORB NUMBER CRB COL {int s = yylval; printf(“\nAnswer :%d\n”, s);}

| NAME ORB NAME COMA NAME CRB COL

| NAME ORB NUMBER COMA NUMBER CRB COL

| NAME ORB NAME COMA NUMBER CRB COL

| NAME ORB NUMBER COMA NAME CRB COL

;

%%

void yyerror(char *s)

{

{printf(“\n while ghalat”);}

}

int main(void)

{

yyparse();

return 0;

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s