Various types of token this lexer can recognize.
-
Data types:
- Int. e.g. 234,3445,2234
- Float. e.g. 234.34,345.5,1.00
- String. e.g. "sdf", "a", "Ads"
- Character. e.g. 's', 'A'
- It also can seperate Binary Operations like:
- '-'(Minus)
- '+'(Addition)
- '*'(Multiplication)
- '/'(Divide)
- '%'(MOD)
- ':='(Assignment)
- '==', '<', '<=', '>', '>=' (Binary Condition)
- We Skip space (' '), tab (\t), and new line (\n)
- End of statement recognized by semicolon (';').
- 'exit' token terminate the program.
In our parser we have tried to replicate the grammer below.
PROG → STMTS
STMTS → STMT STMTS | ɛ
STMT → DTYPE id IDLIST SEMI
STMT → id := EXPR SEMI
IDLIST → , id IDLIST | ɛ
EXPR → EXPR OPTR TERM | TERM
EXPR → ( EXPR ) | neg EXPR
TERM → id | CONST
DTYPE → int | float | char
CONST → ilit | rlit | clit | slit
OPTR → + | - | * | / | % | '==' | '<' | '<=' | '>' | '>='
SAY → id | id, IDLIST
flex gxx.l
bison -dyv gxx.y
gcc lex.yy.c y.tab.c -o gxx.exe
int a,b;
a := 2;
b := 3;
SAY: a,b;
a := a * a;
a := a + b;
SAY: a;
exit;
Output:
a: 2
b: 3
a: 7
Program End
1. Still, we can't store value and operate arithmetic operation. Arithmetic operations only allowed for integer.