========================= Calculator with variables [TOC] ========================= Using the calculator ==================== Here an example for using variables ---- CODE(file=session07/ulm-calc/scripts/example6) ---------------------------- a=2*(1+2) b=a*4 + a b*a -------------------------------------------------------------------------------- Writing this in a file `example6` it gets evaluated line by line when you pass it a parameter to `ulmcalc-step3.ast`: ---- SHELL(path=session07/ulm-calc/scripts) ------------------------------------ ulmcalc-step3.ast example6 -------------------------------------------------------------------------------- Alternatively you can run `ulmcalc-step3.ast` without using a file. In that case type in the statements line by line and terminate the input with CTRL-D. Video tutorial ============== ---- VIDEO ------------------------------ https://www.youtube.com/embed/ffnYCxgEFjo ----------------------------------------- ... here the changes in the lexer, parser and code generator: - __scanner.cpp__ in `/home/numerik/pub/ulm-calc/astl-calc-step3/`, - __parser.ypp__ in `/home/numerik/pub/ulm-calc/astl-calc-step3/` and - most relevant __eval_step3.ast__ in `/home/numerik/pub/ulm-calc/lib/`: Even without understanding the ASTL language developed by __Andreas F. Borchert__ you see, it just describes these pre-order and post-order rules for certain node types that were mentioned in the video. Lexical elements ================ The lexer no longer treats a newline as the end of input. Because we want to have multiple lines with expressions. Tokens can be identifiers, decimal constants, operators and a eol (end of line) token: - Identifiers begin with a letter, i.e. 'A' to 'Z' and 'a' to 'z', an underscore '_', or a dot '.' and are optionally continued with a sequence of more letters, underscores, dots , or decimal digits '0' to '9'. - Decimal constants are described by the regular expression `0|[1-9][0-0]*` - The characters '+', '-', '*', '/' and '%' are operators. - The newline '\n' generates a eol (end of line) token. Spaces get consumed and newline is treated as the end of an input. For example, this file ---- CODE(file=session07/ulm-calc/scripts/lex-example) ------------------------- 11foo11*/% -=+dummy1.a% -------------------------------------------------------------------------------- contains the following tokens: ---- SHELL(path=session07/ulm-calc/scripts) ------------------------------------ ulmcalc-step3-lexer lex-example -------------------------------------------------------------------------------- Grammar ======= You can read the grammar as follows: - The calculator accepts a list of statements. Each of this statements has to end with a newline (eol). ---- LATEX ------------------------------------------------------------------- \begin{array}{lcl} \langle\text{calc}\rangle & \to & \langle\text{statement-list}\rangle \\ \langle\text{statement-list}\rangle & \to & \langle\text{statement}\rangle \quad \text{eol}\\ & \to & \langle\text{statement-list}\rangle \quad \langle\text{statement}\rangle \quad \text{eol}\\ \end{array} ------------------------------------------------------------------------------ - Statements can be expressions or assignments. ---- LATEX ------------------------------------------------------------------- \begin{array}{lcl} \langle\text{statement}\rangle & \to & \langle\text{expression}\rangle \\ & \to & \langle\text{assignment}\rangle \\ \langle\text{expression}\rangle & \to & \langle\text{exp}\rangle \\ \langle\text{assignment}\rangle & \to & \langle\text{identifier}\rangle \quad\text{"="}\quad \langle\text{exp}\rangle \\ \end{array} ------------------------------------------------------------------------------ - And basically the grammar for $\langle\text{exp}\rangle$ was just extended for using $\langle\text{identifiers}\rangle$ like an $\langle\text{integer}\rangle$: ---- LATEX ------------------------------------------------------------------- \begin{array}{lcl} \langle\text{exp}\rangle & \to & \langle\text{term}\rangle \\ & \to & \langle\text{exp}\rangle \quad \text{'+'} \quad \langle\text{term}\rangle \\ & \to & \langle\text{exp}\rangle \quad \text{'-'} \quad \langle\text{term}\rangle \\ \langle\text{term}\rangle & \to & \langle\text{factor}\rangle \\ & \to & \langle\text{term}\rangle \quad\text{'*'}\quad \langle\text{factor}\rangle \\ & \to & \langle\text{term}\rangle \quad\text{'/'}\quad \langle\text{factor}\rangle \\ & \to & \langle\text{term}\rangle \quad\text{'%'}\quad \langle\text{factor}\rangle \\ \langle\text{factor}\rangle & \to & \langle\text{primary}\rangle \\ & \to & \langle\text{unary-minus}\rangle \\ \langle\text{unary-minus}\rangle & \to & \text{'-'} \quad \langle\text{primary}\rangle \\ \langle\text{pimary}\rangle & \to & \langle\text{integer}\rangle \\ & \to & \langle\text{identifier}\rangle \\ & \to & \text{'('} \quad \langle\text{exp}\rangle \quad \text{')'}\\ \langle\text{integer}\rangle & \to & \text{decimal-constant} \\ \langle\text{identifier}\rangle & \to & \text{ident} \\ \end{array} ------------------------------------------------------------------------------ For the initial example on this page you get the following syntax tree ---- SHELL(path=session07/ulm-calc/scripts,hide) ------------------------------- ulmcalc-step3-jstree.ast -o tree-example6.html tree-example6 example6 -------------------------------------------------------------------------------- ---- RAW ------------------------------------ session07/ulm-calc/scripts/tree-example6.html --------------------------------------------- Semantic error detected in the code generator ============================================= We now have finally the possibility to construct an example where an error can not be detected before the code generation: ---- CODE(file=session07/ulm-calc/scripts/example7) ---------------------------- b = a + 1 -------------------------------------------------------------------------------- We are using an undefined variable and get this *semantic error*: ---- SHELL(path=session07/ulm-calc/scripts) ------------------------------------ ulmcalc-step3.ast example7 -------------------------------------------------------------------------------- This gets detected when the code generator tries to evaluate the tree generated by the parser: ---- SHELL(path=session07/ulm-calc/scripts,hide) ------------------------------- ulmcalc-step3-jstree.ast -o tree-example7.html tree-example7 example7 -------------------------------------------------------------------------------- ---- RAW ------------------------------------ session07/ulm-calc/scripts/tree-example7.html --------------------------------------------- More about lexer, parser and code generator =========================================== If you look at the bottom of this page you find this link __document source__. That is the source code from which this website was generated using `doctool`. Like the `ulmcalc`, `ulmas` etc. it has a lexer, parser and code generator. And also, whenever you type some commands in the shell like ---- SHELL --------------------------------------------------------------------- ls -l -------------------------------------------------------------------------------- the same components are in play. Tokens are `ls` and `-l`. The syntax tree will have a root node for `ls` with a child node `-l`. The code generator uses the root node as command and the sub nodes as arguments to this command. So you can say that the generated code is the execution of the syntax tree. So what kind of error do you get from ---- SHELL --------------------------------------------------------------------- ls-l -------------------------------------------------------------------------------- or ---- SHELL --------------------------------------------------------------------- cd.. -------------------------------------------------------------------------------- Both, the `ls-l` and `cd..` will generate just one token. The syntax tree will just contain one node `ls-l` and `cd..` respectively. Running this gives an error. So this is a semantic error. :links: scanner.cpp -> file:session07/ulm-calc/astl-calc-step3/scanner.cpp parser.ypp -> file:session07/ulm-calc/astl-calc-step3/parser.ypp eval_step3.ast -> file:session07/ulm-calc/lib/eval_step3.ast Andreas F. Borchert -> https://www.uni-ulm.de/mawi/mawi-numerik/institut/mitarbeiter/dr-andreas-f-borchert/ document source -> http://www.mathematik.uni-ulm.de/numerik/hpc/ss20/hpc0/session07/page04.doc.txt :navigate: up -> doc:index back -> doc:session07/page03