3.2. Parser Module
3.2.1. Lexer Class
- class aggregate.parser.UnderwritingLexer[source]
Implements the Lexer for the agg language.
- static preprocess(program)[source]
Separate preprocessor step, allowing it to be called separately. Preprocessing involves six steps:
Remove // comments, through end of line
Remove n in [ ] (vectors) that appear from using
f'{np.linspace(...)}'Backslash (line continuation) mapped to space
nt is replaced with space, supporting the tabbed indented Portfolio layout
Split on newlines
- Parameters:
program
- Returns:
3.2.2. Parser Class
- class aggregate.parser.UnderwritingParser(safe_lookup_function, debug=False)[source]
Implements the Parser for the agg language.
Here are testers for the math expressions:
from aggregate import build for t in ['-123', '-2%', '45%', '1e-3%', 'inf', '-inf', 'exp(1)', 'exp(1/2)', 'exp(-1)', '-1/8', 'exp(10)/exp(3**2/2)', '2**10', '50/exp(.3**2/2)', '1/exp(1.9**2 / 2)']: a = build(t) print(a.name) assert float(a.name) == eval(t.replace('%', '/100').replace('exp', 'np.exp').replace('inf', 'np.inf'))
To test on the test_suite:
df = build.run_test_suite() assert len(df.query('error != 0')) == 0
3.2.3. Remaining Functions
3.2.3.1. Aggregate Lexer and Parser.
To debug in a repl use the following code:
from aggregate.parser import UnderwritingLexer, UnderwritingParser
lexer = UnderwritingLexer() parser = UnderwritingParser(lambda x: x, debug=True)
- while True:
- try:
text = input(“>> “) if not text:
continue
- except (EOFError, KeyboardInterrupt):
break
- try:
- if text == ‘x’:
break
tokens = list(lexer.tokenize(text)) print(“Tokens:”) for tok in tokens:
print(f” {tok.type:<10} {tok.value!r}”)
result = parser.parse(iter(tokens)) print(“Parsed:”) print(result)
- except Exception as e:
print(“Error:”, e)
- aggregate.parser.grammar(add_to_doc=False, save_to_fn='')[source]
Write the grammar at the top of the file as a docstring
To work with multi-rules enter them on one line, like so:
@_('builtin_agg PLUS expr', 'builtin_agg MINUS expr')
- Parameters:
add_to_doc – add the grammar to the docstring
save_to_fn – save the grammar to a file