Skip to content

Commit

Permalink
Several Significant Upgrades:
Browse files Browse the repository at this point in the history
- Changed the parser implementation to use lex and yacc
- Enhanced the parsed grammar to include if-then-else structure
- Tested that the parser works both on tax calculation rules and national insurance rules
  • Loading branch information
vchaudhri committed Aug 31, 2018
1 parent a68d854 commit 4a6df2d
Show file tree
Hide file tree
Showing 8 changed files with 9,300 additions and 411 deletions.
50 changes: 32 additions & 18 deletions RuleSpec/doc/grammar.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@

<statements> := <statement> <statement> ... <statement>
<statement> := <equality> | <conditional>
<equality> := <lhs> = <rhs>
<lhs> := <constant>
<rhs> := <expression> | <number>
<constant> := <constant-name> | <constant-array-name> | <number>
<constant-array-name> := <consant-name> ( <number> )
<expression> := <operand> <operator> <operand>
<operator> := + | - | * | / | div | mod
<operand> := <constant> | <parenthetical-expression>
<parenthetical-expression> := ( <expression> )
<IF-Then-Else-Conditional> := If <condition> Then [<statements> | <If-Then-Else-Conditional> [Else [<statements> | <If-Then-Else-Conditional>] Endif
<condition> := <literal> | ( <condition> <logical-connective> <condition> )
<logical-connective> := and | or
<literal> := <atomic-formula> | <negated-atomic-formula>
<negated-atomic-formula> := not <atomic-formula>
<atomic-formula> := ( <constant> <comparison-operator> <constant> ) | ( <constant> )
<comparison-operator> := < | <= | > | >=

STATEMENTS : STATEMENT
| STATEMENT STATEMENTS

STATEMENT : EQUALITY
| IF_THEN_ELSE

EQUALITY : CONSTANT EQUALS EXPRESSION
| CONSTANT EQUALS STRING

EXPRESSION : CONSTANT
| NUMBER
| EXPRESSION PLUS EXPRESSION
| EXPRESSION MINUS EXPRESSION
| EXPRESSION TIMES EXPRESSION
| EXPRESSION DIVIDE EXPRESSION
| EXPRESSION MOD EXPRESSION
| EXPRESSION DIV EXPRESSION
| LPAREN EXPRESSION RPAREN


IF_THEN_ELSE : IF CONDITION THEN STATEMENTS ENDIF
| IF CONDITION THEN STATEMENTS ELSE STATEMENTS ENDIF


CONDITION : LITERAL
| CONDITION AND CONDITION
| CONDITION OR CONDITION
| LPAREN CONDITION AND CONDITION RPAREN
| LPAREN CONDITION OR CONDITION RPAREN


LITERAL : ATOMIC_FORMULA
| NOT ATOMIC_FORMULA

ATOMIC_FORMULA : LPAREN CONSTANT RPAREN
| LPAREN CONSTANT COMPARISON_OPERATOR NUMBER RPAREN
| LPAREN CONSTANT COMPARISON_OPERATOR STRING RPAREN
| LPAREN CONSTANT COMPARISON_OPERATOR CONSTANT RPAREN
170 changes: 42 additions & 128 deletions RuleSpec/rules/national_insurance.rs
Original file line number Diff line number Diff line change
@@ -1,139 +1,53 @@
if (employment_status = employee)
national_insurance_class = class1
if (employee_category = "A")
if ((weekly_income < 162) or (monthly_income < 702))
national_insurance_employee_rate[1] = 0
national_insurance_employer_rate = 0
else if ((weekly_income > 162 and weekly_income <= 892) or
(monthly_income > 702 and monthly_income <= 3163))
national_insurance_employee_rate[2] = 12%
national_insurance_employer_rate = 13.8%
else if ((weekly_income > 892) or (monthly_income > 3863))
national_insurance_employee_rate[3] = 2%
national_insurance_employer_rate = 13.8%
else if (employee_category = "B") # married women or widows
if ((weekly_income < 162) or (monthly_income < 702))
national_insurance_employee_rate[1] = 0
national_insurance_employer_rate = 0
else if ((weekly_income > 162 and weekly_income <= 892) or
(monthly_income > 702 and monthly_income <= 3163))
national_insurance_employee_rate[2] = 5.85%
national_insurance_employer_rate = 13.8%
else if ((weekly_income > 892) or (monthly_income > 3863))
national_insurance_employee_rate[3] = 2%
national_insurance_employer_rate = 13.8%
else if (employee_category = "C") # employees over state pension age
if ((weekly_income < 162) or (monthly_income < 702))
national_insurance_employee_rate[1] = undefined
national_insurance_employee_rate[2] = undefined
national_insurance_employee_rate[3] = undefined
if ((weekly_income > 162) or (monthly_income > 702)
national_insurance_employer_rate = 13.8%
else if (employee_category = "J") # who can defer or are paying elsewhere
if ((weekly_income < 162) or (monthly_income < 702))
national_insurance_employee_rate[1] = 0
national_insurance_employer_rate = 0
else if ((weekly_income > 162 and weekly_income <= 892) or (monthly_income > 702 and monthly_income <= 3163))
national_insurance_employee_rate[2] = 12%
national_insurance_employer_rate = 13.8%
else if ((weekly_income > 892) or (monthly_income > 3863))
national_insurance_employee_rate[3] = 2%
national_insurance_employer_rate = 13.8%
else if (employee_category = "H") # apprentice under age of 25
if ((weekly_income < 162) or (monthly_income < 702))
national_insurance_employee_rate[1] = 0
national_insurance_employer_rate = 0
else if ((weekly_income > 162 and weekly_income <= 892) or (monthly_income > 702 and monthly_income <= 3163))
national_insurance_employee_rate[2] = 2%
national_insurance_employer_rate = 0
else if ((weekly_income > 892) or (monthly_income > 3863))
national_insurance_employee_rate[3] = 2%
national_insurance_employer_rate = 13.8%
else if (employee_category = "M") # employees under the age of 21
if ((weekly_income < 162) or (monthly_income < 702))
national_insurance_employee_rate[1] = 0
national_insurance_employer_rate = 0
else if ((weekly_income > 162 and weekly_income <= 892) or (monthly_income > 702 and monthly_income <= 3163))
national_insurance_employee_rate[2] = 12%
national_insurance_employer_rate = 0
else if ((weekly_income > 892) or (monthly_income > 3863))
national_insurance_employee_rate[3] = 2%
national_insurance_employer_rate = 13.8%
else if (employee_category = "Z")�� # employees under the age of 21 who can defer national insurance as they are paying it elsewhere
if ((weekly_income < 162) or (monthly_income < 702))
national_insurance_employee_rate[1] = 0
national_insurance_employer_rate = 0
else if ((weekly_income > 162 and weekly_income <= 892) or (monthly_income > 702 and monthly_income <= 3163))
national_insurance_employee_rate[2] = 2%
national_insurance_employer_rate = 0
else if ((weekly_income > 892) or (monthly_income > 3863))
national_insurance_employee_rate[3] = 2%
national_insurance_employer_rate = 13.8%
else if (employee_category = "X") # employees who do not have to pay, e.g., under 16 years of age
national_insurance_employee_rate[1] = 0
national_insurance_employee_rate[2] = 0
national_insurance_employee_rate[3] = 0
national_insurance_employer_rate = 0

if (employment_status == employee) and (R1 <> 'undefined') then
if (weekly_income < 162) then national_insurance_employee_amt = 0 endif
if (weekly_income > 162) and (weekly_income <= 892) then national_insurance_employee_amt = (weekly_income - 162) * R2 endif
if (weekly_income > 892) then national_insurance_employee_amt = (( 892 - 162 ) * R2) + ( weekly_income - 892 ) * R3 endif
endif

if (employment_status = employee) and (national_insurance_employee_rate[1] <> undefined)
if ((weekly_income < 162) or (monthly_income < 702))
national_insurance_employee_amt = 0
else if ((weekly_income > 162 and weekly_income <= 892) or (monthly_income > 702 and monthly_income <= 3163))
national_insurance_employee_amt = (weekly_income - 162) *
national_insurance_employee_rate[2]
national_insurance_employer_amt = (weekly_income - 162) *
national_insurance_employer_rate[2]
else if ((weekly_income > 892) or (monthly_income > 3863))
national_insurance_employee_amt = ( 892 *
national_insurance_employee_rate[2]) + ( weekly_income - 892 ) *
national_insurance_employee_rate[3]
national_insurance_employer_amt = ( 892 * national_insurance_employer_rate[2]) + ( weekly_income - 892 ) * national_insurance_employer_rate[3]
### fisherman
if (employement_status == 'self_employed') and (employment_title == 'Fisherman') then
WR1 = 3.5
endif


if (employement_status = self-employed)
### fisherman
if (employment_title = "Fisherman")
national_insurance_class = class2
national_insurance_self_employed_rate[1] = 3.5/week
###
### landlord
else if (employment_title = "Landlord")
if (annual_profit < 5965) #choice of class2/3 voluntary contribution
if (voluntary contribution_choice = class2)
### landlord
if (employment_status == 'self_employed') and (employment_title == 'Landlord') then
#choice of class2/3 voluntary contribution
if (annual_profit < 5965) then
if (voluntary_contribution_choice == class2) then
national_insurance_class = class2
national_insurance_self_employed_rate[1] = 2.95/week
else if (voluntary contribution_choice = class3)
WR1 = 2.95
else if (voluntary_contribution_choice == class3) then
national_insurance_class = class3
national_insurance_self_employed_rate[1] = 14.65/week
WR1 = 14.65
endif
endif
else
national_insurance_self_employed_rate[1] = 2.95/week
national_insurance_self_employed_rate[2] = 9%
national_insurance_self_employed_rate[3] = 2%
if (annual_profit < 8424)
WR1 = 2.95
R2 = 0.09
R3 = 0.02
if (annual_profit < 8424) then
national_insurance_class = class2
else
national_insurance_class = class4
###
else if (annual_profit < 6205)
national_insurance_self_employed_rate[1] = 0
national_insurance_self_employed_rate[2] = 0
else if (annual_profit > 6205) and (annual_profit < 8424)
endif
endif
endif

if (employment_status == 'self_employed') and (employment_title <> 'Landlord') and (employment_title <> 'Fisherman') then
if (annual_profit < 6205) then
R1 = 0
R2 = 0
else if (annual_profit < 8424) then
national_insurance_class = class2
national_insurance_self_employed_rate[1] = 2.95/week
national_insurance_self_employed_amt = national_insurance_self_employed_rate[1] * (annual_profit - 6205)
else if (annual_profit > 8424)
WR1 = 2.95
else if (annual_profit > 8424) then
national_insurance_class = class4
national_insurance_self_employed_rate[1] = 2.95/week
national_insurance_self_employed_rate[2] = 9%
national_insurance_self_employed_rate[3] = 2%

if (annual_profit < 56350)
national_insurance_self_employed_amt = national_insurance_self_employed_rate[1] * (annual_profit - 6205) + national_insurance_self_employed_rate[2] * (annual_profit - 8424)
else
national_insurance_self_employed_amt = national_insurance_self_employed_rate[1] * (annual_profit - 6205) + national_insurance_self_employed_rate[2] * (annual_profit - 8424) + national_insurance_self_employed_rate[3] * (annual_profit - 56350)


if (employment_status = director)
if (pay_method = annual)
weekly_pay = annual_pay / 52
WR1 = 2.95
R2 = 0.09
R3 = 0.02
endif
endif
endif
endif

150 changes: 0 additions & 150 deletions RuleSpec/rules/tax-calc.rs

This file was deleted.

Loading

0 comments on commit 4a6df2d

Please sign in to comment.