Anonymous user
Arithmetic Evaluator/Go: Difference between revisions
→Operator precedence parser
(Alternative solution using library functions) |
imported>Katsumi |
||
(2 intermediate revisions by one other user not shown) | |||
Line 1:
__TOC__
=Operator precedence parser=
This is an operator precedence parser. The number format used in calculation can be changed with the line "type Number int".
<
import (
Line 230 ⟶ 228:
}
}
</syntaxhighlight>
Example
Line 245 ⟶ 243:
</pre>
== External links ==
* [
=Library=
Shown here is use of the package go/parser in the standard library. For the Go 1 release, there is a parser in the standard library, but not an evaluator. Evaluation is relatively easy though, once you have a parse tree.
Line 272 ⟶ 271:
"go", // a valid keyword, not valid in an expression.
"3@7", // error message is "illegal character."
"", // EOF seems a reasonable error message.
}
Line 287 ⟶ 285:
func parseAndEval(exp string) (int, error) {
if err != nil {
return 0, err
Line 346 ⟶ 343:
go: 1:1: expected operand, found 'go'
3@7: 1:2: illegal character U+0040 '@'
: 1:1: expected operand, found 'EOF'
</pre>
|