Class Structure Flex – Bison (Salik Yar Khan)

FLEX:

%{

#include “TP.tab.h”

#include<stdlib.h>

%}

%%

“class”  {return CLASS;}

“public”                {return PUBLIC; }

“new”                   {return NEW; }

“printf”                 {return PRINT; }

“.”           {return DOT; }

“void”  {return VOID; }

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

“(”                          {return ORB; }

“)”                          {return CRB; }

“{”                          {return OCB; }

“}”                          {return CCB; }

“=”                         {return EQL;}

“;”                           {return SEMICOL;}

“\””                        {return ICOMA;}

[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 firstno , secondno;

%}

%token CLASS NAME PUBLIC ORB CRB OCB CCB VOID PRINT NEW

%token DATATYPE EQL NUMBER SEMICOL ICOMA DOT

%start program

%%

 

program: program statement ‘\n’

|

;

 

statement : expr

|

;

 

expr       : classdefine

| objectcalling

| printing

| method

;

 

classdefine: CLASS NAME

| PUBLIC NAME ORB DATATYPE NAME CRB

| OCB

| CCB

| DATATYPE NAME SEMICOL

| DATATYPE NAME EQL NUMBER SEMICOL

| NAME EQL NUMBER SEMICOL

| NAME EQL NAME SEMICOL

;

 

method  : PUBLIC VOID NAME ORB CRB

| PUBLIC VOID NAME ORB DATATYPE NAME CRB

;

 

objectcalling : NAME NAME EQL NEW NAME ORB NUMBER CRB SEMICOL { firstno = yylval; }

| NAME NAME EQL NEW NAME ORB CRB SEMICOL

| NAME DOT NAME ORB CRB     SEMICOL              {printf(“\nAnswer :%d\n” , firstno);}

| NAME DOT NAME ORB NUMBER CRB SEMICOL              { secondno = yylval;printf(“\nAnswer :%d\n” , secondno); }

;

printing : PRINT ORB NAME CRB SEMICOL

| PRINT ORB NUMBER CRB SEMICOL

| NAME ORB NUMBER CRB SEMICOL

;

%%

void yyerror(char *s)

{printf(“\nWRONG INPUT”);}

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