Flex And Bison

Lexer

Various types of token this lexer can recognize.

  1. 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'
  2. It also can seperate Binary Operations like:
    • '-'(Minus)
    • '+'(Addition)
    • '*'(Multiplication)
    • '/'(Divide)
    • '%'(MOD)
    • ':='(Assignment)
    • '==', '<', '<=', '>', '>=' (Binary Condition)
  3. We Skip space (' '), tab (\t), and new line (\n)
  4. End of statement recognized by semicolon (';').
  5. 'exit' token terminate the program.

Parser

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

Compile Process

flex gxx.l
bison -dyv gxx.y
gcc lex.yy.c y.tab.c -o gxx.exe

Sample Program

Input:
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

Limitation

1. Still, we can't store value and operate arithmetic operation. Arithmetic operations only allowed for integer.