- If
- Posted by p4nt0mine@yahoo.com on March 9th, 2007
I wonder how the old compilers dealt with nested if-structure
(i)
if A then
if B1 then
C1
else
C2
else
D
and
(ii)
if A then
if B1 then
if C1 then
D
Could you tell me how the old compilers do with these two esp when the
nested if gets larger ?
Thanks
- Posted by Randy Howard on March 9th, 2007
On Thu, 8 Mar 2007 20:01:52 -0600, p4nt0mine@yahoo.com wrote
(in article <1173405712.130004.187800@h3g2000cwc.googlegroups. com>):
Which old compiler(s)? How old? Why do you seem to imply they all
did/do it the same way?
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw
- Posted by Pascal Bourguignon on March 9th, 2007
p4nt0mine@yahoo.com writes:
This kind of stuff is dealt with a stack.
The only difficulty being the "dangling else" problem. You can google it.
The way it is dealt with depends on the kind of parser. A simple
example is the LL(1) recursive descend kind:
(defun parse-if (input)
;; if-statement ::= 'IF' condition 'THEN' statements [ 'ELSE' statements ] 'END'
(accept input "IF") ; skip over the IF token.
(let ((condition (parse-condition input)))
(accept input "THEN")
(let ((then-statements (parse-statements input)))
(if (string= "ELSE" (next-token input))
(progn
(accept "ELSE")
(let ((else-statements (parse-statements input)))
(prog1 (build-parse-node 'if-else condition then-statements else-statements)
(accept "END"))
(prog1 (build-parse-node 'if condition then-statements)
(accept "END"))))))))
(defun parse-statements (input)
(loop while (member (next-token input) (first-set 'statement))
collect (parse-statement input)))
(defun parse-statement (input)
(let ((token (next-token input)))
(cond
((string= "IF" token) (parse-if input))
;; ...
(t (error "Invalid token ~A" token)))))
So when you call parse-statements, with an IF statement as input, it
calls parse-statement, which calls parse-if, which recursively calls
parse-statements, which calls parse-statement, which calls parse-if,
and so on while you have embedded IFs.
--
__Pascal Bourguignon__ http://www.informatimago.com/
Un chat errant
se soulage
dans le jardin d'hiver
Shiki
- Posted by robertwessel2@yahoo.com on March 9th, 2007
On Mar 8, 8:01 pm, p4nt0m...@yahoo.com wrote:
They dealt with nested ifs in whatever fashion the specification for
the language they were compiling said to do (modulo bugs, of course).
Did some early languages have what we would now consider very odd
nesting rules (eg. Cobol), or no nested ifs at all (early Fortran)?
Sure - but others (Algol, for example) were very similar to what's now
common.

