Skip to content

An Arithmetic Parser Demo using Recursive Descent in Lua

License

Notifications You must be signed in to change notification settings

lxsmnsyc/ArithmeticParserDemo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 

Repository files navigation

ArithmeticParserDemo

An Arithmetic Parser Demo using Recursive Descent in Lua

Features

  • Right-associative addition, subtraction, multiplication and addition.
  • Left-associative exponentiation
  • 'e' notation e.g 1e10
  • Unary
  • Parser error reporting (prints the position of the character error)

Grammar

This is in ANTLR4 format:

expr
  :   sum
  ;
  
sum
  :   prod (('+' | '-')? prod)* 
  ;
  
prod   
  :   pow (('*' | '/')? pow)*
  ;
  
pow
  :   value ('^' pow)*
  ;
  
value:  
  :   digits 
  |   ('(' expr ')')?
  ;
  
digits
  : ('-')? [0-9]* (('.')? [0-9]*)? (('e' | 'E')? [0-9]*)?
  ;