Path: utzoo!mnetor!uunet!husc6!bbn!rochester!PT.CS.CMU.EDU!sei!sei.cmu.edu!firth From: firth@sei.cmu.edu (Robert Firth) Newsgroups: comp.lang.pascal Subject: Re: Pascal Enumerated I/O Message-ID: <4294@aw.sei.cmu.edu> Date: 22 Feb 88 14:24:48 GMT References: <11903@brl-adm.ARPA> <673@cresswell.quintus.UUCP> <1077@csuna.UUCP> Sender: netnews@sei.cmu.edu Reply-To: firth@bd.sei.cmu.edu.UUCP (Robert Firth) Organization: Carnegie-Mellon University, SEI, Pgh, Pa Lines: 40 In article <1077@csuna.UUCP> abcscnuk@csuna.UUCP (Naoto Kimura) writes: >Ok, then go and implement a compiler that will allow input of enumerated types. >Output I can see how it could be done, but input is another story. The >compiler would have to have something like yacc or lex built into it. The Ada language requires both input and output of enumerated types. It is not really that hard. Consider type Day = (Sunday, Monday, Tuesday, ...); You build a string table in the order of the type values; simply index this table by the binary ordinal to get the display form. For input, you can use the same table if the number of values is small, searching it linearly; otherwise, build a true lookup table with the text values in alphabetical order, and search by binary chop. Or you can get really fancy, and use a hash table. It so happens I wrote this part of an Ada runtime support package, and it was less than a page of code for both translation procedures. To answer the usual silly questions (a) the language is not case sensitive, so you force to uniform case on both input and output. (b) otherwise, you emit, and recognise, exactly the names declared by the user, and no others (c) all such input can be read by a simple LR(1) scanner based on an FSM; the apparatus of 'lex' and 'yacc' (even supposing one would ever want to use them for anything) is not needed. (d) you read according to the lexis of an 'identifier'; you're required to skip leading white space, and of course you need a one-char lookahead or putback. (e) the error behaviour in Ada is to raise DATA_ERROR if there isn't an identifier to be read, or if the identifier read isn't a value of the type.