
Explore implementing a c++ calculator using context-free grammar, detailing expression grammar, precedence and associativity, and how numbers, parentheses, and operators form the parsing rules.
Learn how a context-free grammar parses arithmetic expressions, defining expression, term, primary, and number rules with examples such as 2 and 2 plus 3 for a c++ calculator.
Understand token abstraction to enable parsing, turning numbers and operators into tokens like plus, minus, multiplication, and parentheses, and apply production rules using a token class with kind and value.
Implement expression, term, and primary in a C++ calculator using a context-free grammar, defining a token class and get_token to handle numbers, plus, minus, and parentheses.
Implement get_token() in a calculator using c++ by returning token types for operators and numbers, handling multi-digit input with putback, and testing by building and running.
debug with gdb to diagnose the segmentation fault caused by left recursion in the expression grammar; remove left recursion by turning expression and term into iterative loops with get_token.
Debug a C++ calculator parser by tracing expression and term parsing, fixing the missing left operand after encountering '+', using -g, gdb, and introducing a token stream to recover tokens.
Implement a token stream class with a buffer and put back capability to get and return tokens, supporting operator handling, parentheses, and expression parsing.
Explore how a calculator program uses context free grammar to parse expressions in C++, including multiplication, division, modulo, and divide-by-zero checks, with exception handling and iterative input.
In this course we will do live coding of:
1. Calculator implementation in C++ using Expression Grammar (context free grammar)
I have referred to Chapter 6 of the book "Programming: Principles and Practice using C++" by Bjarne Stroustrup.
I have tried to share my learning experience while converting Expression Grammar into the Calculator program using C++. Hope, it will help people like me to understand the concepts in a faster way and help to resolve doubts.
While coding the program, I am also sharing the thought process which goes on in developers mind. We will face compile-time, run-time errors and then we will resolve them using gdb debugger.
Implementing Expression Grammar in C++ also gives us a bit of understanding on how the parsing works.
In this course we will do hands-on programming:
Theory of arithmetic expression Calculator:
Theory of Expression Grammar (CFG)
Expression Parsing
left recursion, removal of left recursion
Token & Token Stream class abstraction
Coding:
expression(), term(), primary(), function implementations
TokenStream get() and putback() implementations
debugging with gdb
Conclusion
Different C++ & programming concepts used during software development:
classes, public & private variables, functions, constructors, operator precedence, operator associativity, left recursion, removal of left recursion, segmentation fault, debugging with gdb.