pathterminuspages/projects/aboutcontactabout me

Simple Calculator - Generating parser

ComCalc #3 :: 31-08-2018

Contents
 -Simple Calculator - Preface
 -Simple Calculator - Syntax
 @Simple Calculator - Generating parser
 -Simple Calculator - The application

Alternatively we can build the whole parser using Grammar2Slr. Firstly we need the grammar written as something the generator can understand. We use the syntax from last chapter, and we get

prec "minus" 1 prec "plus" 1 prec "times" 2 prec "divide" 2 prec "power" 3 group "operator" {"minus","plus","divide"} group "operand" {"int","float","id"} group "parentheses" {"lpar","rpar"} token -cap "int", "float", "id" as "[0-9]+", "[0-9]*.[0-9]+", "[a-zA-Z]+" token "plus", "minus", "times", "divide", "power" as "\\+", "-", "\\*", "/", "\\^" token "lpar", "rpar", "comma" as "(", ")", "," !token "\n","\r","\t"," " assoc "right" : "power" assoc "left" : "minus","plus","divide","times" prod Exp -> Exp "plus" Exp | Exp "minus" Exp prod Exp -> Exp "times" Exp prod Exp -> Exp "divide" Exp prod Exp -> Exp "power" Exp prod Exp -> Call prod Exp -> Atom prod Exp -> "minus" Exp prod Call -> "id" "lpar" Args "rpar" prod Args -> Arg Args' | prod Args' -> "comma" Arg Args' | prod Arg -> Exp prod Atom -> "lpar" Exp "rpar" prod Atom -> "float" prod Atom -> "int"

We run that through the generator, and we get a parser in which we change the function addToken2tree and the array production_fun to this.

CommentsGuest Name:Comment: