Skip to content

A Language Parser and Interpreter based on Flex and Bison

Notifications You must be signed in to change notification settings

Kimbbakar/BottleCap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BottleCap

Sample Program

Input:

Assign a to 10
Assign b to Add a a * with a a +
Print b
Assign b to 10
Print b
Print Divide a by b
Print 10 a b * * 2 +

Output:

120
10
1
1002
Program End

Compile Process

flex BottleCap.l
bison -dyv BottleCap.y
gcc lex.yy.c y.tab.c -o BottleCap.exe
BottleCap.exe < sample.txt

Token and Regular Expression

- [ \t]                       ; // ignore all whitespace
- [0-9][0-9]*                 {return T_INT;}
- "+"                         {return T_PLUS;}
- "-"                         {return T_MINUS;}
- "*"                         {return T_MULTIPLY;}
- "/"                         {return T_DIVIDE;}
- "Print"                     {return PRINT;}
- "%"                         {return T_MOD;}
- "Add"                       {return ADD;}
- "Assign"                    {return ASSIGN;}
- "Subtract"                  {return SUB;}
- "Multiply"                  {return MUL;}
- "Divide"                    {return DIV;}
- "by"                        {return BY;}
- "from"                      {return FROM;}
- "to"                        {return TO;}
- "with"                      {return WITH;}
- \n                          {return T_NEWLINE;}
- [a-zA-Z][a-zA-Z0-9]*        {return T_ID;}
- .                           {yyerror("Invalid Syntex!") ;}

Grammer

PROG:  STMTS	

STMTS:					   
     | STMT T_NEWLINE STMTS 
     | T_NEWLINE STMTS
     | STMT 					
;

STMT:  EXPR
     | PRINT EXPR			 
     | ASSIGN T_ID TO EXPR 	 
;

EXPR:  ADD EXPR WITH EXPR 	 
     | SUB EXPR FROM EXPR 	 
     | MUL EXPR BY EXPR 		 
     | DIV EXPR BY EXPR 		 	
     | RPN					 
     | TERM 					 
;

RPN:   TERM RPN				 
     | OP RPN				 
     | OP					 
;
 

TERM:  T_INT					 
     | T_ID 					 
;

OP:    T_PLUS				 
     | T_MINUS				 
     | T_MULTIPLY			 
     | T_DIVIDE				 
     | T_MOD					 
;

Limitation

  • It only takes Reverse Polish Notation expression.
  • Arithmetic operations only allowed for integer.

About

A Language Parser and Interpreter based on Flex and Bison

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published