Skip to content

A simple C program that uses a CFG to evaluate simple arithmetic expressions.

License

Notifications You must be signed in to change notification settings

Ryan-h1/Context-Free-Grammar-Based-Calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Arithmetic Expression Evaluator

This program uses a CFG to evaluate simple arithmetic expressions.

Context-Free Grammar (CFG) Rules

  • sim_exp → md_exp
  • sim_exp → sim_exp l_op md_exp
  • md_exp → num
  • md_exp → md_exp h_op num
  • l_op → + | -
  • h_op → * | /
  • num → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

The code defines four functions to evaluate arithmetic expressions recursively: get_op(), get_num(), sim_sub_exp(), and md_sub_exp(). The main function is sim_exp(), which evaluates a simple arithmetic expression by calling md_exp() to evaluate the first multiplicative/divisive expression, and then calling sim_sub_exp() to evaluate the rest of the expression recursively.

The main function also includes a loop that prompts the user to enter a simple arithmetic expression, evaluates it, prints the result, and then asks if the user wants to continue. If the user enters 'Y', the loop continues; if the user enters 'N' or any other character, the program exits.

Compilation and Execution

A CMake file is provided to compile and run the program. Feel free to use this or your preferred method to run the program.

For example:

make main
./main

Or

gcc -o expression_evaluator main.c
./expression_evaluator

And so on.

License

MIT

About

A simple C program that uses a CFG to evaluate simple arithmetic expressions.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published