circuitus20250729002.sh

#!/bin/bash

# (C) David Vajda
# 2025-07-29
# Circuit Excersize, US ...

# bakus naur - normalform - grammar rules (easiest level, linksrekursiv)
# expr ::= expr + term | term
# term ::= term * fact | fact
# fact ::= const | (expr)

# beseitgte Linksrekursion
# expr ::= term + expr2
# expr2 ::= expr + term | term
# term ::= fact * term2
# fact2 ::= term *  fact | fact
# ...

# alternative beseitgte Linksrekursion
# expr ::= term + expr2
# expr2 ::= expr | term
# term ::= fact * term2
# fact2 ::= term *  fact | fact
# ...


# rechtzeitiges abbruchkriterium erzeugen sonst wird ausdruck etwas zu lange

date=$(date)
yes=1
no=0
op1="AND"
op2="OR"
op3="NOT"
MAXVAR=4

if [[ "$1" == "expr" || -z "$1" ]]
then
    /bin/bash "$0" "term"
    echo -n " $op2 "
    /bin/bash "$0" "expr2"
elif [ "$1" == "expr2" ]
then
    decision=$(($RANDOM%2))
    if [ $decision -eq "$yes" ]
    then
        /bin/bash "$0" "expr"
        /bin/bash "$0" "term"
    else
        /bin/bash "$0" "term"
    fi
elif [ "$1" == "term" ]
then
    /bin/bash "$0" "fact"
    echo -n " $op2 "
    /bin/bash "$0" "term2"
elif [ "$1" == "term2" ]
then
    decision=$(($RANDOM%2))
    if [ $decision -eq "$yes" ]
    then
        /bin/bash "$0" "term"
        /bin/bash "$0" "fact"
    else
        /bin/bash "$0" "fact"
    fi
elif [ "$1" == "fact" ]
then
    decision=$(($RANDOM%2))
    if [ $decision -eq "$yes" ]
    then
        if [ $decision -eq "$yes" ]
        then
            echo -n " $op3 "
        fi
        echo -n "x$(($RANDOM%$MAXVAR))"
        decision=$(($RANDOM%2))
    else
        decision=$(($RANDOM%4))
        if [ "$decision" == "1" ]
        then
            echo -n "("
            /bin/bash "$0" "expr"
            echo -n ")"
        fi
    fi
fi