Help with program to test if an equation has matching parenthesis

I need some help with a program that lets the user type in an algebraic expression, ending w/a carriage return that contains these type of parenthesis (,[, and {. As the expression is being read in, the program evaluates each character. If at any point the expression is incorrectly bracketed, too many right or a mismatch between left and right. After the carriage return is typed, if it is correct, a message is displayed.

Here is what I have so far:

TITLE PAREN:BRACKET PGM
.MODEL SMALL
.STACK 100H
.DATA
ENTER_ DB 0DH,0AH,'ENTER AN ALGEBRAIC EXPRESSION: $'
ERROR DB 'TOO MANY RIGHT BRACKETS. BEGIN AGAIN! $'
CORRECT DB 0DH,0AH,'EXPRESSION IS CORRECT $'
RERUN DB 0DH,0AH,'TYPE Y IF YOU WANT TO CONTINUE: $'
MISMATCH DB 0DH,0AH,'BRACKET MISMATCH. BEGIN AGAIN! $'
.CODE
MAIN PROC
MOV AX,@DATA
MOV DS,AX
BEGIN:
LEA DX, ENTER_ ;DISPLAY MSG
MOV AH,9
INT 21H
XOR CX,CX

WHILE_:
;READ CHAR
MOV AH,1
INT 21H

CMP AL,0DH
JE END_WHILE

MOV DL,'(' ;CHECK IF ITS A L BRACKET THEN PUSH IT IF
CMP AL,DL ;IT IS
JNE CHECKFORRIGHT
MOV DL,'['
CMP AL,DL ;OTHERWISE DONT
JNE CHECKFORRIGHT
MOV DL,'['
CMP AL,DL
JNE CHECKFORRIGHT
PUSH AX
INC CX
CHECKFORRIGHT:
MOV DL,')' ;CHECK IF ITS A BRACKET

CMP AL,DL
JE RTBRACKET
MOV DL,']'

CMP AL,DL ;OTHERWISE DONT
JE RTBRACKET
MOV DL,'}'

CMP AL,DL
JE RTBRACKET
JMP NOTBRACKET
RTBRACKET:
POP BX
DEC CX
CMP BX,DX ;IF MATCH GO BACK TO WHILE
JE WHILE_

LEA DX,MISMATCH ;IF NOT, DISPLAY AN ERROR
MOV AH,9
INT 21H
JMP END_WHILE
NOTBRACKET:
JMP WHILE_
END_WHILE:
CMP CX,0 ;IF THE STACK IS EMPTY
JE ITSRIGHT

LEA DX,ERROR ;ELSE BRACKET ERROR
MOV AH,9
INT 21H
JMP RERUNLOOP

ITSRIGHT:
LEA DX,CORRECT
MOV AH,9
INT 21H

RERUNLOOP:
LEA DX,RERUN
MOV AH,9
INT 21H

MOV AH,1
INT 21H
CMP AL,'Y'
JE BEGIN
CMP AL,'y'
JE BEGIN
EXIT:
MOV AH,4CH
INT 21H
MAIN ENDP
END MAIN


any help would be greatly appreciated...thanks, tim

Comments

  • I have not see very well your code , and i dont realy understand your problem . I see that you have made the check with a stack , that seems good . also i see that you check first if stack is empty or not ...so the stack is well balance .... ????? so whats the problem ?





    : I need some help with a program that lets the user type in an algebraic expression, ending w/a carriage return that contains these type of parenthesis (,[, and {. As the expression is being read in, the program evaluates each character. If at any point the expression is incorrectly bracketed, too many right or a mismatch between left and right. After the carriage return is typed, if it is correct, a message is displayed.
    :
    : Here is what I have so far:
    :
    : TITLE PAREN:BRACKET PGM
    : .MODEL SMALL
    : .STACK 100H
    : .DATA
    : ENTER_ DB 0DH,0AH,'ENTER AN ALGEBRAIC EXPRESSION: $'
    : ERROR DB 'TOO MANY RIGHT BRACKETS. BEGIN AGAIN! $'
    : CORRECT DB 0DH,0AH,'EXPRESSION IS CORRECT $'
    : RERUN DB 0DH,0AH,'TYPE Y IF YOU WANT TO CONTINUE: $'
    : MISMATCH DB 0DH,0AH,'BRACKET MISMATCH. BEGIN AGAIN! $'
    : .CODE
    : MAIN PROC
    : MOV AX,@DATA
    : MOV DS,AX
    : BEGIN:
    : LEA DX, ENTER_ ;DISPLAY MSG
    : MOV AH,9
    : INT 21H
    : XOR CX,CX
    :
    : WHILE_:
    : ;READ CHAR
    : MOV AH,1
    : INT 21H
    :
    : CMP AL,0DH
    : JE END_WHILE
    :
    : MOV DL,'(' ;CHECK IF ITS A L BRACKET THEN PUSH IT IF
    : CMP AL,DL ;IT IS
    : JNE CHECKFORRIGHT
    : MOV DL,'['
    : CMP AL,DL ;OTHERWISE DONT
    : JNE CHECKFORRIGHT
    : MOV DL,'['
    : CMP AL,DL
    : JNE CHECKFORRIGHT
    : PUSH AX
    : INC CX
    : CHECKFORRIGHT:
    : MOV DL,')' ;CHECK IF ITS A BRACKET
    :
    : CMP AL,DL
    : JE RTBRACKET
    : MOV DL,']'
    :
    : CMP AL,DL ;OTHERWISE DONT
    : JE RTBRACKET
    : MOV DL,'}'
    :
    : CMP AL,DL
    : JE RTBRACKET
    : JMP NOTBRACKET
    : RTBRACKET:
    : POP BX
    : DEC CX
    : CMP BX,DX ;IF MATCH GO BACK TO WHILE
    : JE WHILE_
    :
    : LEA DX,MISMATCH ;IF NOT, DISPLAY AN ERROR
    : MOV AH,9
    : INT 21H
    : JMP END_WHILE
    : NOTBRACKET:
    : JMP WHILE_
    : END_WHILE:
    : CMP CX,0 ;IF THE STACK IS EMPTY
    : JE ITSRIGHT
    :
    : LEA DX,ERROR ;ELSE BRACKET ERROR
    : MOV AH,9
    : INT 21H
    : JMP RERUNLOOP
    :
    : ITSRIGHT:
    : LEA DX,CORRECT
    : MOV AH,9
    : INT 21H
    :
    : RERUNLOOP:
    : LEA DX,RERUN
    : MOV AH,9
    : INT 21H
    :
    : MOV AH,1
    : INT 21H
    : CMP AL,'Y'
    : JE BEGIN
    : CMP AL,'y'
    : JE BEGIN
    : EXIT:
    : MOV AH,4CH
    : INT 21H
    : MAIN ENDP
    : END MAIN
    :
    :
    : any help would be greatly appreciated...thanks, tim
    :

  • I have not see very well your code , and i dont realy understand your problem . I see that you have made the check with a stack , that seems good . also i see that you check first if stack is empty or not ...so the stack is well balance .... ????? so whats the problem ?





    : I need some help with a program that lets the user type in an algebraic expression, ending w/a carriage return that contains these type of parenthesis (,[, and {. As the expression is being read in, the program evaluates each character. If at any point the expression is incorrectly bracketed, too many right or a mismatch between left and right. After the carriage return is typed, if it is correct, a message is displayed.
    :
    : Here is what I have so far:
    :
    : TITLE PAREN:BRACKET PGM
    : .MODEL SMALL
    : .STACK 100H
    : .DATA
    : ENTER_ DB 0DH,0AH,'ENTER AN ALGEBRAIC EXPRESSION: $'
    : ERROR DB 'TOO MANY RIGHT BRACKETS. BEGIN AGAIN! $'
    : CORRECT DB 0DH,0AH,'EXPRESSION IS CORRECT $'
    : RERUN DB 0DH,0AH,'TYPE Y IF YOU WANT TO CONTINUE: $'
    : MISMATCH DB 0DH,0AH,'BRACKET MISMATCH. BEGIN AGAIN! $'
    : .CODE
    : MAIN PROC
    : MOV AX,@DATA
    : MOV DS,AX
    : BEGIN:
    : LEA DX, ENTER_ ;DISPLAY MSG
    : MOV AH,9
    : INT 21H
    : XOR CX,CX
    :
    : WHILE_:
    : ;READ CHAR
    : MOV AH,1
    : INT 21H
    :
    : CMP AL,0DH
    : JE END_WHILE
    :
    : MOV DL,'(' ;CHECK IF ITS A L BRACKET THEN PUSH IT IF
    : CMP AL,DL ;IT IS
    : JNE CHECKFORRIGHT
    : MOV DL,'['
    : CMP AL,DL ;OTHERWISE DONT
    : JNE CHECKFORRIGHT
    : MOV DL,'['
    : CMP AL,DL
    : JNE CHECKFORRIGHT
    : PUSH AX
    : INC CX
    : CHECKFORRIGHT:
    : MOV DL,')' ;CHECK IF ITS A BRACKET
    :
    : CMP AL,DL
    : JE RTBRACKET
    : MOV DL,']'
    :
    : CMP AL,DL ;OTHERWISE DONT
    : JE RTBRACKET
    : MOV DL,'}'
    :
    : CMP AL,DL
    : JE RTBRACKET
    : JMP NOTBRACKET
    : RTBRACKET:
    : POP BX
    : DEC CX
    : CMP BX,DX ;IF MATCH GO BACK TO WHILE
    : JE WHILE_
    :
    : LEA DX,MISMATCH ;IF NOT, DISPLAY AN ERROR
    : MOV AH,9
    : INT 21H
    : JMP END_WHILE
    : NOTBRACKET:
    : JMP WHILE_
    : END_WHILE:
    : CMP CX,0 ;IF THE STACK IS EMPTY
    : JE ITSRIGHT
    :
    : LEA DX,ERROR ;ELSE BRACKET ERROR
    : MOV AH,9
    : INT 21H
    : JMP RERUNLOOP
    :
    : ITSRIGHT:
    : LEA DX,CORRECT
    : MOV AH,9
    : INT 21H
    :
    : RERUNLOOP:
    : LEA DX,RERUN
    : MOV AH,9
    : INT 21H
    :
    : MOV AH,1
    : INT 21H
    : CMP AL,'Y'
    : JE BEGIN
    : CMP AL,'y'
    : JE BEGIN
    : EXIT:
    : MOV AH,4CH
    : INT 21H
    : MAIN ENDP
    : END MAIN
    :
    :
    : any help would be greatly appreciated...thanks, tim
    :

  • Hello, I didn't completely anilize your code & don't have a real fix.
    But what about this idea:

    set flag if {[( is pressed
    clear flag if }]) is pressed
    don't allow {[( pressed if associated flag is set = error message?

    Flag can be a byte for each. 0=clear 1=set
    or bits in a byte or word 8-16 flag bits.

    Pseudo code:
    get key
    if key = {[()]}
    jmp to associated key area#.
    process ok keys here

    area1:
    check if flag bit is set or clear for associated key
    jmp error if key flag incorrectly set for key pressed
    process key here
    go back for more keys
    area2:
    process a different key here, same as above

    Bitdog

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories