Calculator - Discussion #3
Replies: 3 comments 1 reply
-
|
hi |
Beta Was this translation helpful? Give feedback.
-
|
i have read these |
Beta Was this translation helpful? Give feedback.
-
|
@kr8457 As is explained in those five pages of Dragon Book, there will be three parts (or layers) in our project namely Lexical Analyzer, Syntax Analyzer and Calculator. The brief overview is given below, your task is to make a Syntax Analyzer. Lexical Analyzer #5Lexical Analyzer is proposed above, it will be made by me. Syntax AnalyzerThe token stream from lexical analyzer is passed as argument to Syntax analyzer, that converts a flat list to tree-like structure in accordance with operator-precedence (which is also passed in as dict, see Operator precedent). The analyzer should convert the list in binary tree-like structure (use your best intuition), it should not solve them. Operator precedentWhy not support custom operators/symbols? We can use a dict like this, and assume that whatever comes first in operators: dict = {
"^": lambda x, y: x ** y,
"%": lambda x, y: x % y,
"/": lambda x, y: x / y,
"*": lambda x, y: x * y,
"-": lambda x, y: x - y,
"+": lambda x, y: x + y,
}Tip Changed in version 3.7: Dictionary order is guaranteed to be insertion order. This behavior was an implementation detail of CPython from 3.6. CalculatorThe last layer will take the binary tree-like structure from Syntax analyzer and calculate the result by passing the arguments in the Callable. It should also handle Exceptions from those Callables. This layer will be simple, because all hard work will already be done by previous two layers. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Recapitulation of
CalculatorThe project is proposed in Issue #1. The further discussion on how to implement it will be done here. In the face of ambiguity, refuse the temptation to guess. And just ask.
Beta Was this translation helpful? Give feedback.
All reactions