Monday, January 22, 2024

 Having Peg Grammar Troubles?

Try this trick for Expression Engines in the C Languages. Instead of the hassle of having to use Peg Grammar routines and countless methods for precedence orders, instead simply do it with Function Naming. I wrote this small Test in C++ without a loop and several look-a-head token variables.


Try this trick for Expression Engines in the C Languages. Instead of the hassle of having to use Peg Grammar routines and countless methods for precedence orders, instead simply do it with Function Naming. I wrote this small Test in C++ without a loop and several look-a-head token variables.

#include <iostream>

#include <string.h>


double add_two_mult_one(double x, double y, double z){

    double t = x+y*z;

    return t;

};

double prec_add_two_mult_one(double x, double y, double z){

    double t = (x+y)*z;

    return t;

};


double add_two_add_prec_mult_two(double x, double y, double a, double b){

    double t = x+y+(a*b);

    return t;

};

int main()

{

    /* if(tok[i]=='LPAREN' && tok[i+1]==VALUE && tok[i+2]=='PLUS'....etc.) {

           dbl v = PRECENDENCE_add_two_mult_one(tok[i+1], tok[i+3].....etc.);     

    }else etc. */

    double x = prec_add_two_mult_one(2, 2, 5);

    

    double y = add_two_mult_one(2, 2, 5);

    

    double z = add_two_add_prec_mult_two(2, 2, 5, 5);

    

    std::cout << x << "\n" << y << "\n" << z <<  "\n";


I call this "Alphanumeric Function Grammar" which does not require any node traversing from right to left or back again. Why? Because the Pre named Functions for their Precedence Orders are all fully  Recursive and if you Pre Set them all then your done.

Node Traversing is a Pain and developers simply cannot easily get them to work correctly traversing back from Left To right without having extra Structs to store their Precedence Orders.

Its all about Naming and Conditionals and a few Sate Switch Variables to achieve what a Node Traversal Engine is causing to be extremely complicated.