Back to blog
Dec 05, 2023
3 min read

Programming Language in Lex and Yacc

Explore the intricacies of my programming language, its unique syntax, functionalities, and how it compares with existing languages through practical code examples.

Bilkent CS315 Project

The project aimed to design and implement a programming language using Lex and Yacc. The language was designed to be a simple, yet powerful, language with a unique syntax and functionalities. The project involved creating a lexer and parser for the language, as well as implementing a code generator to execute the code. It features a familiar, readable syntax that eases the learning curve for new users while providing seasoned programmers with the tools they need to write efficient, error-free code.

Features

  • Zero Tolerance for Null Inputs: The language introduces a novel approach to input validation, ensuring that programs are robust against null or invalid inputs right from the get-go.
x := input "Enter value for x: ";
while (x == 0) begin
  display "Please enter a non-zero value for x";
  x := input "Enter value for x again: ";
end
  • Functionality and Flow Control: Clear, concise function definitions and flow control mechanisms, making code more readable and maintainable.
foo(p, q) : largest begin
  display "Function foo with parameters", p, "and", q;
  if (p > q) then largest := p else largest := q;
  return largest;
end
  • Iterative Processing and Data Handling: The language supports sophisticated data structures and iteration patterns, enabling developers to handle complex data manipulation tasks efficiently.
a := [12, 13, 14, 15, 16, 17];
sum := 0;
for (i := 0; i < length(a); i := i + 1) begin
  sum := sum + a[i];
end
display "Sum of array:", sum;

Example Programs

Program 1: Input Validation and Error Handling

x := input "enter value for x: ";
while (x == 0) begin
  display "please enter a non-zero value for x";
  x := input "enter value for x again: ";
end else begin
  display "value of x was correct in inital input";
end

y := input "enter value for y: ";
while (y == 0) begin
  display "please enter a non-zero value for y";
  y := input "enter value for y again: ";
end else begin
  display "value of y was correct in inital input";
end

z := input "enter value for z: ";
while (z == 0) begin
  display "please enter a non-zero value for z";
  z := input "enter value for z again: ";
end else begin
  display "value of z was correct in inital input";
end

display x, " times ", y, " times ", z;

Program 2: Function Definition and Invocation

foo(p,q) : largest begin
  display "my name is foo and my parameters are\n";
  display "p = ", p, ", q =", q, "\n";
  if (p>q) begin
    largest := p;
  end else begin
    largest := q;
  end
  return largest;
end

a := [5,0,3,-7];
b := [9,-2,-1];
for (i := 0, i < a.length(), i := i + 1) begin
  for (j := 0, j < b.length(), j := j + 1) begin
    c := foo(a[i],b[j]);
    display "c = ", c, ", a = ", a[i], ", b = ", b[j];
  end
end

Program 3: Iterative Processing and Data Handling

a := [12,13,14,15,16,17];
sum := 0;
for (i := 0, i < a.length(), i := i + 1) begin
  sum := sum + a[i];
end

display "sum of array is: ", sum;

Program 4: Sorting an Array

get_max(a,b):largest begin
  if (a > b) begin
    largest := a;
  end else begin
    largest := b;
  end
  return largest;
end

arr := [25,6,36,2,425,26,13];
for (i := 0, i < arr.length(), i := i + 1) begin
  for (j := i + 1, j < arr.length(), j := j + 1) begin
    if ( max(arr[i], arr[j]) != arr[j] ) begin
      temp := arr[i];
      arr[i] := arr[j];
      arr[j] := temp;
    end
  end
end

display "[ ";
for (i := 0, i < arr.length(), i := i + 1) begin
  display arr[i], ", ";
end
display " ]\n";

Program 5: Inline Functions and Expressions

get(a):d begin

  d := input "calculating d";  /* inline*/
  display "value of d: ", d; 
  return d; /* inline */
end

b := a + 2 + get(5) * 10; /* inline */
a := input "enter a value for a: ";
arr := [12,13,14];
b := arr.size();
c := arr[2];
while (a > 12) begin
  a := a - 1;
end else begin
  display "a was greater than 12";
end
for (i := 1, i < 10, i := i + 1) begin
  display "test";
end

The source code for the project can be found on GitHub