An open API service indexing awesome lists of open source software.

https://github.com/farhansadaf/chicken-language

A simple compiler constructed using Flex & Bison.
https://github.com/farhansadaf/chicken-language

bison-yacc compiler-design flex lex

Last synced: 7 months ago
JSON representation

A simple compiler constructed using Flex & Bison.

Awesome Lists containing this project

README

          

## *Chicken Programming Language*
A simple and light programming language for basic problem solving and calculation.


#### *Commands to create compiler `chick.exe`*
```
bison -d chicken.y
flex chicken.l
gcc -o chick lex.yy.c chicken.tab.c
```

#### *Run program*
1. ***Interactive mode*** command is `chick`.
2. ***To run a script***, command is `chick .txt`.
For example, `chick sample-programs/8_prime_numbers.txt`.


#### *Table of content*
| Topic | Description |
| :---: | --- |
| Data types | `Numbers`: Default data type. Similar to C double data type.
`String`: Similar to C character array. |
| Variables initialization | All variables are initialized globally.
***variablename := expression;***

`i := 0;`
`firstVariable := -21.5;` |
| Comment | Single line comment starts with `#` character.

`# This is a comment` |
| Print statement | Prints expression or string without newline.

***print expression;***
`print 10+20;`
`print firstVariable;`

***print string;***
`print "Hello world\n";` |
| Scan statement | Only works in interactive mode.
***scan variable;***

`scan firstVariable;` |
| Operators | Precedence:


  1. ***( expression )***
    `print (10+20)/3;` `Output: 10`


  2. ***^***: Exponent
    `print 8.3^2;` `Output: 68.89`


  3. ***not***: Logical NOT
    `print not 0;` `Output: 1`


  4. ***\**** : Multiplication
    `print 2*4;` `Output: 8`

    ***/*** : Division
    `print 4/2;` `Output: 2`

    ***%*** : Modulus
    `print 11%3;` `Output: 2`


  5. ***+*** : Plus
    `print 4+2;` `Output: 6`

    ***-*** : Minus
    `print 4-2;` `Output: 2`


  6. ***>=*** : Grater than or equal
    `print 11 >= 4.2;` `Output: 1`

    ***<=*** : Less than or equal
    `print 11 <= 11;` `Output: 1`

    ***=*** : Equal to
    `print 4 = 4;` `Output: 1`

    ***!=*** : Not equal to
    `print 4 != 4;` `Output: 0`

    ***>*** : Grater than
    `print 4 > 2;` `Output: 1`

    ***<*** : Less than
    `print 4 < 2;` `Output: 0`


  7. ***and*** : Logical AND
    `print 1 and 0;` `Output: 0`

    ***or*** : Logical OR
    `print 1 or 0;` `Output: 1`

|
| If Else |

  • ***if expression then statement***
    `if not 0 then print "Hello\n";`


  • ***if expression then statement***
    ***else statement***

    `if 0 then print "Zero\n";`
    `else print "Not zero";`


  • ***if expression then statement***
    ***else if expression then statement***
    ***else statement***

    `number1 := -10;`
    `number2 := -7;`
    `if number1 = number2 then print "number1 = number2";`
    `else if number1 > number2 then print "number1 > number2";`
    `else print "number1 < number2";`


|
| While loop | ***while expression then statement***

`i := 0;`
`while i < 10 then {`
    `print i;`
    `print "\n";`
    `i := i + 1;`
`}` |
| For loop | ***for variable : (start, end, step) then statement***
Where,
        `variable`: A pre-initialized variable.
        `start`: An expression defining where to start the loop.
        `end`: An expression defining when to end the loop.
        `step`: An expression defining the value variable will be incremented with.

Example 1:
`i := 0;`
`for i : (0, 5, 1) then print i;`
`Output: 0 1 2 3 4`

Example 2:
`i := 0;`
`for i : (5, 0, -1) then print i;`
`Output: 5 4 3 2 1`

Pseudocode for example 1:
`initialize i ← 0`
`set start as reference to i`
`set start ← 0, end ← 5, step ← 1`
`while (start < end) do`
    `execute statements`
    `start ← start + step`
`end while` |
| Exit program | Keyword `exit` terminates the program.
`exit;` |
| Random number | ***random(lower, upper)***
Returns a randomly generated number within lower to upper.
`print random(1.21, 3.15);` |
| Necessary maths |

  • ***abs(x)***
    Returns absolute value of given number.
    `print abs(-21.65);` `Output: 21.65`


  • ***sqrt(x)***
    Returns square root `√x` of given number.
    `print sqrt(81);` `Output: 9`


  • ***floor(x)***
    Returns the nearest integer less than given argument.
    `print floor(31.91);` `Output: 31`


  • ***ceil(x)***
    Returns the nearest integer grater than given argument.
    `print ceil(31.91);` `Output: 32`

|
| Logarithm | ***

  • log(x)***
    Returns natural logarithm of a number x with base e.
    `print log(10);` `Output: 2.302585`

    ***log(x, base)***
    Returns natural logarithm of a number x with base given as parameter.
    `print log(100, 2);` `Output: 6.643856`


  • ***exp(x)***
    Returns `e` (2.71828) raised to the power of the given argument.
    `print exp(10);` `Output: 22026.465795`

|
| Trigonometry |

  • ***PI***
    The keyword PI `π` returns the ratio of a circle’s circumference to its diameter.
    `print PI;` `Output: 3.141593`


  • ***sin(x)***
    Returns the sine of an argument (angle in radian).
    `print sin(PI/2);` `Output: 1`

    ***asin(x)***
    It takes a single argument (-1 ≤ x ≤ -1), and returns the arc sine (inverse of sin) in radian.
    `print asin(0.5);` `Output: 0.523599`


  • ***cos(x)***
    Returns the cosine of an argument (angle in radian).
    `print cos(PI/2);` `Output: 0`

    ***acos(x)***
    It takes a single argument (-1 ≤ x ≤ -1), and returns the arc cosine (inverse of cosine) in radian.
    `print acos(0.5);` `Output: 1.047198`


  • ***tan(x)***
    Returns the tangent of an argument (angle in radian).
    `print tan(PI/4);` `Output: 1`

    ***atan(x)***
    It takes a single argument, and returns the arc tangent (inverse of tangent) in radian.
    `print atan(0.5);` `Output: 0.463648`

|


*Licensed under [Apache License 2.0](LICENSE).*