Skip to content

Calculator Example (AST Edition). Why not?

Notifications You must be signed in to change notification settings

HMRDevil/Calculator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

42 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Calculator

Why?

This repository is designed as an example of how you can implement a calculator based on abstract syntax tree generated by the LL(k) recurrence recursive descent parser.

As a grammar used the basic implementation with operators associativity, and not vulnerable to left recursion:

{
  tokens = [
    T_FLOAT = "regexp:\d+\.\d+"
    T_INT   = "regexp:\d+"
  ]
}

<expr>           ::= <addition> | <subtraction> | <term>

<term>           ::= <multiplication> | <division> | <factor>
<factor>         ::= "(" <expr> ")" | <value>

<subtraction>    ::= <term> "-" <expr>
<addition>       ::= <term> "+" <expr>
<multiplication> ::= <factor> "*" <term>
<division>       ::= <factor> ("/" | "÷") <term>

<value>          ::= T_FLOAT | T_INT

Example

Command Line Interface

$ php ./bin/cc run

image

image

Usage

Global:

$ composer global require serafim/calc
$ cc run

Local:

$ composer require serafim/calc
$ ./vendor/bin/cc run

About

Calculator Example (AST Edition). Why not?

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%