update
This commit is contained in:
parent
1f1ad49427
commit
6f050a029e
2496 changed files with 37609 additions and 3031 deletions
|
|
@ -15,7 +15,7 @@ j=0; do forever; j=j+1; if j>L then leave; _=substr(x,j,1); _2=getX()
|
|||
z=z _ $; iterate
|
||||
end
|
||||
if _=='+' | _=="-" then do; p_=word(z,words(z)) /*last Z token*/
|
||||
if p_=='(' then z=z 0 /handle unary ±*/
|
||||
if p_=='(' then z=z 0 /*handle unary±*/
|
||||
z=z _ $; iterate
|
||||
end
|
||||
lets=0; sigs=0; #=_
|
||||
|
|
@ -27,7 +27,7 @@ j=0; do forever; j=j+1; if j>L then leave; _=substr(x,j,1); _2=getX()
|
|||
end /*exp*/
|
||||
if pos(_,nchars)==0 then leave
|
||||
lets=lets+datatype(_,'M') /*keep track of # of exponents. */
|
||||
#=# || translate(_,'EEEEE','eDdQq') /*keep buildingthe num.*/
|
||||
#=# || translate(_,'EEEEE','eDdQq') /*keep building the num*/
|
||||
end /*j*/
|
||||
j=j-1
|
||||
if \datatype(#,'N') then call serr 'invalid number: ' #
|
||||
|
|
|
|||
35
Task/Arithmetic-evaluation/Racket/arithmetic-evaluation.rkt
Normal file
35
Task/Arithmetic-evaluation/Racket/arithmetic-evaluation.rkt
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#lang racket
|
||||
|
||||
(require parser-tools/yacc parser-tools/lex
|
||||
(prefix-in ~ parser-tools/lex-sre))
|
||||
|
||||
(define-tokens value-tokens (NUM))
|
||||
(define-empty-tokens op-tokens (OPEN CLOSE + - * / EOF NEG))
|
||||
|
||||
(define lex
|
||||
(lexer [(eof) 'EOF]
|
||||
[whitespace (lex input-port)]
|
||||
[(~or "+" "-" "*" "/") (string->symbol lexeme)]
|
||||
["(" 'OPEN]
|
||||
[")" 'CLOSE]
|
||||
[(~: (~+ numeric) (~? #\. (~* numeric)))
|
||||
(token-NUM (string->number lexeme))]))
|
||||
|
||||
(define parse
|
||||
(parser [start E] [end EOF]
|
||||
[tokens value-tokens op-tokens]
|
||||
[error void]
|
||||
[precs (left - +) (left * /) (left NEG)]
|
||||
[grammar (E [(NUM) $1]
|
||||
[(E + E) (+ $1 $3)]
|
||||
[(E - E) (- $1 $3)]
|
||||
[(E * E) (* $1 $3)]
|
||||
[(E / E) (/ $1 $3)]
|
||||
[(- E) (prec NEG) (- $2)]
|
||||
[(OPEN E CLOSE) $2])]))
|
||||
|
||||
(define (calc str)
|
||||
(define i (open-input-string str))
|
||||
(displayln (parse (λ() (lex i)))))
|
||||
|
||||
(calc "(1 + 2 * 3) - (1+2)*-3")
|
||||
Loading…
Add table
Add a link
Reference in a new issue