Basic predefined lex variables and their meaning in lex

Image

Advertisements

Array Syntax Acceptance Flex Bison

Flex File

%{
#include <stdio.h>
#include <stdlib.h>
#include “cal.tab.h”
%}
%%

“int” return INT;
“char” return CHAR;

[,;()\n] return *yytext;
[a-zA-Z]+ {yylval=*yytext;
return VARIABLE;}
[0-9]+ {yylval = atoi(yytext);
return NUM;}
“{” { return ‘{‘; }
“}” { return ‘}’; }
“[” { return ‘[‘; }
“]” { return ‘]’; }
“=” { return ‘=’; }
%%

int yywrap(void) {
return 1;
}

Bison

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

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

 

%}

%token INT
%token VARIABLE
%token CHAR NUM
%%
program : program statement ‘\n’
|
;

statement : INT VARIABLE ‘[‘NUM’]’ ‘;’ {printf(“ACCEPT\n”);}
| CHAR VARIABLE ‘[‘NUM’]’ ‘;’ {printf(“ACCEPT\n”);}
| INT VARIABLE ‘[‘NUM’]’ ‘=’ ‘{‘ exp ‘}’ ‘;’ {printf(“ACCEPT\n”);}
| CHAR VARIABLE ‘[‘NUM’]’ ‘=’ ‘{‘ expr ‘}’ ‘;’ {printf(“ACCEPT\n”);}
;

 

exp :NUM
|exp’,’ NUM
;

expr :VARIABLE
|expr ‘,’VARIABLE
;
%%
int main()
{
yyparse();
return 0;
}

 

 

Checking of repetition of inputs and basic items FLEX

Flex File 

%{

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char *s;
int i=0 ;
int f=0;
char arr[8][8];

%}

Operator [+|*|/|>|<|=]

Parenthsis [(|)]

Quoted [“][a-zA-Z][a-zA-Z_0-9_?_, ]*[“]

Integers [0-9]+

Identifier [a-zA-Z][a-zA-Z_0-9]*

%%

[ \t\n] ;

int|float|decimal|double|array|list printf( “The sentence contains the special DataType = %s\n”, yytext );

if|then|else|endif|print|newline printf( “The sentence contains the special keyword = %s\n”, yytext );

“;” printf( “The sentence contains the special Semicolon = %s\n”, yytext );

“-” printf( “The sentence contains the special operator = %s\n”, yytext );
“=>” printf( “The sentence contains the special operator = %s\n”, yytext );
“>=” printf( “The sentence contains the special operator = %s\n”, yytext );
“<=” printf( “The sentence contains the special operator = %s\n”, yytext );
“=<” printf( “The sentence contains the special operator = %s\n”, yytext );
“==” printf( “The sentence contains the special operator = %s\n”, yytext );
“!=” printf( “The sentence contains the special operator = %s\n”, yytext );

{Parenthsis} printf( “The sentence contains the special Parenthesis = %s\n”, yytext );

{Quoted} s=yytext; s++; s[strlen(s)-1] =”; printf( “The sentence contains the special quote = %s \n”,s);

{Identifier} i=0; for(i;i<8;i++) {if(strcmp(arr[i], yytext)==0){printf(” Identifier Already Exsist = %s \n “,yytext);i=1;break;}} if(i>1){printf(“The sentence contains the special Identifier = %s \n”,yytext);strcpy(arr[f],yytext);++f;};

{Integers} printf(“The sentence contains the special integer = %d \n”,atoi(yytext));

 

{Operator} printf(“The sentence contains the special operator = %s\n”,yytext);

 

 

 

%%

int main(int argc,char* argv[])
{
yyin=fopen(argv[1],”r”);
yylex();

 

 

}

 

 

Try Catch (Fariz)

%{

#include “ccproject.tab.h”
#include<stdlib.h>

%}

%%

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

“try” { return TRY;}

“int” {return KEYWORD;}

“string” {return KEYWORD;}

“float” {return KEYWORD;}

“catch” { return CATCH;}

“exception” { return KEYWORD;}

“printf” { return KEYWORD;}

“Console.Write” {return KEYWORD;}

“.ToString()” {return CONVERT;}
[a-zA-Z]+ { yylval = *yytext;
return ID;
}
“{” { return OPENBRACE; }

“}” { return CLOSEBRACE;}

” ” { return SPACE;}

[()=;\n] return *yytext;

[\t]

. yyerror (“INVALID INPUT”);

%%
int yywrap(void)
{

return 1;

}

Bison

%{

#include<stdio.h>
void yyerror(char*s);

%}

%token NUM TRY ID CATCH KEYWORD OPENBRACE CLOSEBRACE OPENPAREN CLOSEPAREN SPACE CONVERT

%%

program:
program Statement’\n’
|
;

Statement:project {printf(“SYNTAX ACCEPTED\n”);}
project : TRY ‘\n’ OPENBRACE ‘\n’ E1′;’ ‘\n’ E1′;’ ‘\n’ CLOSEBRACE’\n’ catch_statement ‘\n’ OPENBRACE ‘\n’ E3′(‘ ID CONVERT ‘)”;’ ‘\n’ CLOSEBRACE

E1 : KEYWORD SPACE ID’=’NUM
| KEYWORD SPACE ID’=’ID
| ID
| KEYWORD'(‘ID CONVERT’)’
| NUM
;

E3 :KEYWORD
;
catch_statement : CATCH ‘(‘ type_expression’)’
;
type_expression :KEYWORD SPACE ID
;

%%

void yyerror(char*s) {

printf(“%s\n”,s);

}

int main(void) {

yyparse();
return 1;

}

 

Nested If flex bison

Flex File

%{
#include “if.tab.h”
#include<stdlib.h>

%}

%%

if {return IF;}

“else if” {return ELSEIF;}

else {return ELSE;}

“=” {return EQUAL;}

“(” {return BRO; }

“)” {return BRC; }

“{” {return CBO; }

“}” {return CBC; }

“;” {return SEMI; }

“>=”|”<=”|”==”|”!=”|”<“|”>”|”++”|”–” {
yylval = *yytext;
return multiop;
}
[0-9]+ {yylval=*yytext;return NUM;}

[a-zA-Z]+ {yylval=*yytext;return ID;}

[ \t] ; /* skip whitespace */
%%

int yywrap(void)

{

return 1;

}

Bison File

%{

#include <stdio.h>
void yyerror(char *s);

%}

%token IF BRO ELSE BRC CBO CBC SEMI EQUAL NUM ID ELSEIF multiop

%%

program: program State ‘\n’
|
;
State: IF BRO ID EQUAL ID BRC CBO E1 CBC ELSE CBO ID SEMI CBC {printf(“Accept \n”);}
;

E1:A
| A E1
;

A:IF BRO ID EQUAL ID BRC CBO ID SEMI CBC
| A B
;

B: ELSE CBO ID SEMI CBC
;

%%
void yyerror(char *s)
{

{printf(“\n Syntax is Wrong”);}

}

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

 

 

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;}

 

 

sentence structure bison flex(Salik-Yar-Khan)

TP.FLEX:

%{

#include “TP.tab.h”

#include <stdio.h>

#include <ctype.h>

#include <stdlib.h>

%}

 

 

%%

 

“i”|”they”|we|you                         { return SUBJECT; }

 

“went”|”ate”|”took”|”killed”    { return VERB; }

 

 

[a-z]+                                                    { return OBJECT; }

 

 

[\n]                        { return *yytext; } ;

 

[ \t]                        ;

 

.              yyerror(“invalid character”);

 

 

%%

 

int yywrap(void)

{

return 1;

}TP.Y

%{

#include <stdio.h>

#include <string.h>

void yyerror(const char *s);

%}

 

%token SUBJECT VERB OBJECT

 

%start prog

 

%%

prog : prog S ‘\n’

|

;

 

S              : expr

|

;

 

expr       : SUBJECT VERB OBJECT  {printf(“Accepted!\n”);}

;

 

%%

 

 

void yyerror(const char *s)

{

printf(“%s\n”, s);

}

 

int main(void)

{

yyparse();

return 0;

}