https://rentry.org/PPP2_p189

// Code derived from Stroustrup's PPP2 book
// § 6.4 Grammars
//  -and beginning on p 189

//------------------------------------------------------------------------------
// via pp 189, 190
/*
                                                   Question: Is 2 an expression?
 A simple expression grammar                           Parsing the number 2
                                                      (read from bottom up):

Expression:                                      (which is an) Expression
    Term                                                           ^
    Expression "+" Term   // addition                              |
    Expression "–" Term   // subtraction                           |
                                                                   |
Term:                                               (which is a) Term
    Primary                                                        ^
    Term "*" Primary      // multiplication                        |
    Term "/" Primary      // division                              |
    Term "%" Primary      // remainder (modulo)                    |
                                                                   |
Primary:                                           (which is a) Primary
    Number                                                         ^
    "(" Expression ")"    // grouping                              |
                                                                   |
Number:                                             (which is a) Number
    floating-point-literal                                         ^
                                                                   |
                                                   (is a) floating-point-literal
                                                                   ^
                                                                   |
                                                                   2

                                                     So yes, 2 is an expression.
*/
//------------------------------------------------------------------------------

#include <iostream>

using std::cout;

// a simple user-defined type
class Token {
 public:
  Token(char ch) : kind{ch} {}

  Token(char ch, double val) : kind{ch}, value{val} {}

  char   kind  = '0';
  double value = 0.0;
};

//------------------------------------------------------------------------------

double expression(double d);  // addition, subtraction
double term(double d);        // multiplication, division, remainder (modulo)
double primary(double d);     // grouping
double number(double d);      // floating-point literals

//------------------------------------------------------------------------------

// Note: this is a greatly-simplified (and contrived) example, using rudimentary
// function stubs just to demonstrate the grammar call-chain involved in parsing
// the number 2.

int main()
{
  cout << expression(2) << '\n';  // this will call all the grammar functions
}

double expression(double d)
{
  double left = term(d);  // read and evaluate a Term

  return left;
}

double term(double d)
{
  double left = primary(d);  // read and evaluate a Primary

  return left;
}

double primary(double d)
{
  double left = number(d);  // read and evaluate a Number

  return left;
}

double number(double d)
{
  Token t = Token{'8', d};  // just a stub for the get_token() function f. p 188

  return t.value;
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_06/main_p189.cpp && ./a.out

PrevUpNext

Edit
Pub: 14 Mar 2023 23:40 UTC
Edit: 02 May 2023 20:45 UTC
Views: 715