B-- is a simple toy language implemented using Lex and Yacc. It supports basic programming constructs and operations.
- Abhishek Jaiswal (2021CSB1061)
- Devanshu Dhawan (2021CSB1082)
The Lex file defines the lexical rules for tokenizing the input source code. It defines various tokens used in the B-- language, including keywords, operators, identifiers, and literals. The tokens recognized by Lex include:
- Whitespace: Skips whitespace characters.
- Newline: Represents a newline character.
- Tab: Represents a tab character.
- Float: Matches floating-point numbers.
- Int: Matches integer numbers.
- String: Matches string literals.
- IF, THEN, FOR, TO, STEP, DIM, DATA, GOTO, GOSUB, RETURN, DEF, LET, INPUT, PRINT, REM, END: Keywords in B--.
- NEQ, LE, GE, EQ, LT, GT: Comparison operators.
- PLUS, MINUS, MUL, DIV, POW: Arithmetic operators.
- COMMA, SEMICOLON, L_BRACE, R_BRACE: Miscellaneous symbols.
- EXCLAMATION: Represents the exclamation mark.
- FN: Matches function names.
- DOLLAR: Represents the dollar sign.
- MODULO: Represents the modulo operator.
- HASH: Represents the hash symbol.
- IDENTIFIER: Matches identifiers (variable names).
- Unexpected character: Prints an error message for unexpected characters.
The Yacc file defines the syntax rules for parsing the input source code based on the tokens generated by Lex. It specifies the grammar for the B-- language. The supported syntax includes statements, expressions, control structures, input/output operations, and more.
The B-- language supports the following constructs:
- Data statements: Used for declaring data values.
- Def statements: Used for defining functions.
- Dim statements: Used for declaring arrays.
- End statements: Marks the end of the program.
- For statements: Used for loop constructs.
- Gosub statements: Transfers control to a subroutine.
- Goto statements: Transfers control to a specific line number.
- If statements: Used for conditional branching.
- Let statements: Used for variable assignments.
- Input statements: Reads input from the user.
- Print statements: Prints output to the console.
- Rem statements: Comments or remarks.
- Return statements: Returns from a subroutine or function.
To run the B-- toy language, follow these steps:
- Install Lex and Yacc on your system.
- Clone the B-- repository:
git clone https://github.com/Jaiswal0786/b--
. - Navigate to the project directory:
cd b--
. - Build the Lex and Yacc files:
lex BMM_Scanner.l && yacc -d BMM_Pareser.y
. - Compile the generated C files:
gcc lex.yy.c y.tab.c -o b--
. - Run the B-- toy language:
./b--
. - You will get the desried output in
output.txt
.
Note: You can change the input instructions in Correct/IncorrectSample.bmm
.