| |
|
|
- Page 1 - |
|
Julian Schmidt | Hi, I have me at times one small Parser probiert. Evtl. are this though another couple smaller ones Error drin.
Perhaps. can your me help. CompileMarkSeparationWindowTitle "Simple Parser"
Windowstyle 24
Window (%maxx-280)\2,(%maxy-210)\2 - 280,210
Var ed_calc&=Create("Edit",%hwnd,"5+(30*10)",10,10,width(%hwnd)-20,20)
Var lb_calc&=Create("ListBox", %hwnd, 0, 10, 40, width(%hwnd)-20, 100)
Var bt_calc&=Create("Button",%hwnd,"Ausrechnen",10,150,width(%hwnd)-20,25)
Declare calc$,pos_z1%
WhileNot iskey(27)
Waitinput 300
If getfocus(bt_calc&) or iskey(13)
ClearList lb_calc&
calc$=Translate$(Translate$(GetText$(ed_calc&),",",".")," ","")
Addstring(lb_calc&,calc$)
If (InStr(")",calc$)<>0) and (InStr("(",calc$)<>0)
while (InStr(")",calc$)<>0) and (InStr("(",calc$)<>0)
whileloop InStr(")",calc$),1,-1
pos_z1%=&loop
case (Mid$(calc$,pos_z1%,1)="(") : break
Endwhile
calc$=Init_Parse(Mid$(calc$,pos_z1%+1,InStr(")",calc$)-(pos_z1%+1)),Mid$(calc$,1,pos_z1%-1),Mid$(calc$,InStr(")",calc$)+1,Len(calc$)-InStr(")",calc$)))
Endwhile
Else
calc$=Translate$(Translate$(calc$,")",""),"(","")
Endif
Init_Parse(calc$)
Setfocus(ed_calc&)
Endif
EndWhile
Proc Init_Parse
Parameters math$, str1$, str2$
WhileNot iskey(27)
Case GetString$(lb_calc&,Getcount(lb_calc&)-1)<>str1$+math$+str2$ : Addstring(lb_calc&,str1$+math$+str2$)
If (InStr("^",math$)<>0)
Parse("^")
ElseIf (InStr("*",math$)<>0)
Parse("*")
ElseIf (InStr("+",math$)<>0)
Parse("+")
ElseIf (InStr("-",math$)<>0)
Parse("-")
ElseIf (InStr("\",math$)<>0) or (InStr("/",math$)<>0)
Parse("\")
Else
Settext ed_calc&,math$
break
EndIf
EndWhile
Return str1$+math$+str2$
EndProc
Proc Parse
Parameters op$
Declare pos_z2%, z1$,z2$
Var pos_op%=InStr(op$,math$)
Whileloop pos_op%-1,0,-1
If (Mid$(math$,&loop,1)="+") or (Mid$(math$,&loop,1)="-") or (Mid$(math$,&loop,1)="\") or (Mid$(math$,&loop,1)="/") or (Mid$(math$,&loop,1)="*") or (Mid$(math$,&loop,1)="^") or (trim$(Mid$(math$,&loop,1))="")
z1$=Mid$(math$,&loop+1,pos_op%-(&loop+1))
pos_z1%=&loop+1
break
Endif
EndWhile
Whileloop pos_op%+1,Len(math$)+1,1
If (Mid$(math$,&loop,1)="+") or (Mid$(math$,&loop,1)="-") or (Mid$(math$,&loop,1)="\") or (Mid$(math$,&loop,1)="/") or (Mid$(math$,&loop,1)="*") or (Mid$(math$,&loop,1)="^") or (trim$(Mid$(math$,&loop,1))="")
z2$=Mid$(math$,pos_op%+1,&loop-(pos_op%+1))
pos_z2%=&loop
break
Endif
EndWhile
If ((op$="\") and (z2$="0"))
math$="Division durch NULL!"
Else
Var erg$=Str$(If(op$="+",Val(z1$)+Val(z2$),If(op$="-",Val(z1$)-Val(z2$),If(op$="*",Val(z1$)*Val(z2$),If(op$="\",Val(z1$)/Val(z2$),If(op$="^",Val(z1$)^Val(z2$),"Error"))))))
math$=Mid$(math$,1,pos_z1%-1)+erg$+Mid$(math$,pos_z2%,(Len(math$)+1)-pos_z2%)
Settext ed_calc&,math$
Endif
ENDPROC
shining so far though To functions. only with the clinging!?
LG
Julian |
|
|
| |
|
|
|
| |
|
- Page 1 - |
|
Julian Schmidt | Jo, already seen. helps me but not really! i'm sooner on Fehlersuche and would gladly simply time some Cases of you listen where my Taschenrechner fail and evtl. a strategy to Solution the Problems.
Evtl. have I Yes even some Rechenregeln except eight let, which important wären!?? Evtl. too suggestions to that Programmier-Style, and How one gewisse items curtail could. |
|
|
| |
|
|
|
| gives Yes first only Klammer to point and point to Strich umzusetzen.
functions the because already fehlerfrei?
Tricky is sooner something like How -5 + -1 |
|
|
| |
|
|
|
Julian Schmidt | Klammer to Potenz to point to Strich functions already to that Großenteil. only there with designed Klammerabfolgen unfortunately yet Problems. about with "5+(30*(4/2)+10)". can it momentarily self with TraceModus not integrally understand. shining somehow with the shared inside the Klammer zusammenzuhängen. with time functions it integrally inspired!
How would you, The story with the negativen numbers concern? |
|
|
| |
|
|
|
| the depends very of it ex what very your Parser later time everything can should -
these information missing me z.B. .
i'd well How in xpse umgesetzt always a Parser produce the possible
flexibly is and even alike which Argumente of left to right parsen and dissolve
can. If you only bisl Matheformeln parsen want then can for the less-
omen simply Ausnahmen/ Erkennungen program. has of course nothing with
the Subject to do but You can also XProfan self use How z.B. in
XP-Script [...] (what but only To XP entire correctly functions). |
|
|
| |
|
|
|
Julian Schmidt | can you again konkret about To, How one on the best a flexiblen Parser building and to exception\recognition of/ one negativen number say? |
|
|
| |
|
|
|
| I wrote Yes already:
the depends very of it ex what very your Parser later time everything can should -
these information missing me z.B. . |
|
|
| |
|
|
|
Julian Schmidt | Konkret +,-,*,\,^ and the right Use with clinging, Minuszahlen. Evtl. root or. Wurzel_n and Logarithmus. |
|
|
| |
|
|
|
Julian Schmidt | How would you whom a Parser possible flexibly write? How program one best a "Minus-omen Ausnahmen/ Erkennungen"? |
|
|
| |
|
|
|
| Erstmal need couple Helferfunktionen.
Z.B. a function parse write The z.B. 2 Longs as Param sustain:
SpeicherAddr,BytesToParse
and the Number of geparsten Bytes returns.
If one Ansi-Parser go should then is BytesToParse is a 256 byte great
memory - simply Dim take. therein any Bytes on True The The function
parse bear should. Virtually.
byte BytesToParse,65=1,1,1,1,1
with which The Chars A;B;C;D;E meant wären -
means The function only such a long time ließt How these Chars appear.
is hold well the fastest there such function parse only aimed/ direct Speicherzugriffe
need in order to detect what go is.
quasi while byte(BytesToParse,byte(SpeicherAddr,pos++)) instead of 17 ifs and instrs -
means complete without Instr and herumsucherei.
If the first characters a Klammer is has The function z.B. too The Eigenheit,
The aufgehenden clinging To count and then The amount gelesener Bytes
zurückzuliefern if, The Number of clinging of 1 on 0 jumping - so can
simply The clinging then wegoperieren there always the first and the latest
characters clinging are if the first characters a Klammer is.
these then mostly 1 argument the one in a extra list take off
can for späteres further decompose.
make from:
1+(10*20)+5
Listboxeinträge:
add 1 add 10*20 add 5 and in a further Pass:
add 1 add tmp1,10 mul tmp1,20 add tmp1 add 5
quasi dissolve To it only yet Operationen gives The You in the list as "kann
I without further OP ausrechnen" markiert have.
After this take action can discretionary-complex and mehrzeilige Argumente-
chain dissolve. |
|
|
| |
|
|
|
Julian Schmidt | hmmmm...I must I sincere confess I have only railway-station understood.
How would you a Parser possible flexibly, under usage of (part)string-functions, write? |
|
|
| |
|
|
| |
|
- Page 2 - |
|
|
Andreas Koch | Hello Julian,
i'm of course not addressed been, faith but to know, what iF means, I very so a Parser time written have. The Grundüberlegung is, that one too The most complicated Formel in Bestandteile (substr) decompose can, The to the schema "Variable Operator Variable" aufgebaut are. example: "a+b" or "a*b" or "a^b" etc. a and b functions thereby as Placeholder and can self discretionary tricky his, really go tappt im dunkeln as complete new Formel treats. If one The Formel long and nice enough aufdröselt, sustain one sometime something, what calculate can and setting then backward one. i will try it in a example deutlicher To make:
25+8*(6-3*(15-3))
1. level: 25+a a=8*(6-3*(15-3)) 2. level: 8*b b=6-3*(15-3) 3. level: 6-c c=3*(15-3) 4. level: 3*d d=15-3
now can I d calculate and must only again backward bring into action: d=12 c=3*d=36 b=6-c=-30 a=8*b=-240 ans=25-240=-215
instead of a b c and d recommend itself naturally Listboxeinträge and no letters.
lovely Greeting
Andreas |
|
|
| |
|
|
|
Julian Schmidt | thanks, for your response. me was imho already clear what a Parser is, I have indeed already a self written (see Ausgangsbeitrag). me went it plenty More therefore, How one a Parser possible flexibly program circa quick further Rechengesetze How to that example Klammerregeln about. Binomische Formeln or further Rechenoperatoren How root or Logarithmus or the korekte discern of negativen numbers einzufügen IF having already dargelegt How it a Parser in the byte for byte geparst becomes write would. this was for my savour unfortunately something To tricky (or just for laymen unverständlich words)..... Greeting |
|
|
| |
|
|